View Javadoc

1   package org.apache.maven.doxia.module.xhtml;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *   http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.StringWriter;
23  import java.io.Writer;
24  
25  import org.apache.maven.doxia.sink.AbstractSinkTest;
26  import org.apache.maven.doxia.sink.Sink;
27  
28  /**
29   * @author Jason van Zyl
30   * @version $Id: XhtmlSinkTest.java 926772 2010-03-23 20:52:37Z ltheussl $
31   * @since 1.0
32   */
33  public class XhtmlSinkTest
34      extends AbstractSinkTest
35  {
36      /** {@inheritDoc} */
37      protected String outputExtension()
38      {
39          return "xhtml";
40      }
41  
42      /** {@inheritDoc} */
43      protected Sink createSink( Writer writer )
44      {
45          return new XhtmlSink( writer, "UTF-8" );
46      }
47  
48      /** {@inheritDoc} */
49      protected boolean isXmlSink()
50      {
51          return true;
52      }
53  
54      /**
55       * Test link generation.
56       *
57       * @throws java.lang.Exception if any.
58       */
59      public void testLinks()
60          throws Exception
61      {
62          XhtmlSink sink = null;
63          Writer writer =  new StringWriter();
64          try
65          {
66              sink = (XhtmlSink) createSink( writer );
67              sink.link( "http:/www.xdoc.com" );
68              sink.link_();
69              sink.link( "./index.html#anchor" );
70              sink.link_();
71              sink.link( "../index.html#anchor" );
72              sink.link_();
73              sink.link( "index.html" );
74              sink.link_();
75          }
76          finally
77          {
78              if ( sink != null )
79              {
80                  sink.close();
81              }
82          }
83  
84          String actual = writer.toString();
85          assertTrue( actual.indexOf( "<a class=\"externalLink\" href=\"http:/www.xdoc.com\"></a>" ) != -1 );
86          assertTrue( actual.indexOf( "<a href=\"./index.html#anchor\"></a>" ) != -1 );
87          assertTrue( actual.indexOf( "<a href=\"../index.html#anchor\"></a>" ) != -1 );
88          assertTrue( actual.indexOf( "<a href=\"index.html\"></a>" ) != -1 );
89      }
90  
91      /** {@inheritDoc} */
92      protected String getTitleBlock( String title )
93      {
94          return "<title>" + title + "</title>";
95      }
96  
97      /** {@inheritDoc} */
98      protected String getAuthorBlock( String author )
99      {
100         return author;
101     }
102 
103     /** {@inheritDoc} */
104     protected String getDateBlock( String date )
105     {
106         return date;
107     }
108 
109     /** {@inheritDoc} */
110     protected String getHeadBlock()
111     {
112         return "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" +
113                 "<html xmlns=\"http://www.w3.org/1999/xhtml\"><head><title></title><meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/></head>";
114     }
115 
116     /** {@inheritDoc} */
117     protected String getBodyBlock()
118     {
119         return "<body></body></html>";
120     }
121 
122     /** {@inheritDoc} */
123     protected String getSectionTitleBlock( String title )
124     {
125         return title;
126     }
127 
128     /** {@inheritDoc} */
129     protected String getSection1Block( String title )
130     {
131         return "<div class=\"section\"><h2>" + title + "</h2></div>";
132     }
133 
134     /** {@inheritDoc} */
135     protected String getSection2Block( String title )
136     {
137         return "<div class=\"section\"><h3>" + title + "</h3></div>";
138     }
139 
140     /** {@inheritDoc} */
141     protected String getSection3Block( String title )
142     {
143         return "<div class=\"section\"><h4>" + title + "</h4></div>";
144     }
145 
146     /** {@inheritDoc} */
147     protected String getSection4Block( String title )
148     {
149         return "<div class=\"section\"><h5>" + title + "</h5></div>";
150     }
151 
152     /** {@inheritDoc} */
153     protected String getSection5Block( String title )
154     {
155         return "<div class=\"section\"><h6>" + title + "</h6></div>";
156     }
157 
158     /** {@inheritDoc} */
159     protected String getListBlock( String item )
160     {
161         return "<ul><li>" + item + "</li></ul>";
162     }
163 
164     /** {@inheritDoc} */
165     protected String getNumberedListBlock( String item )
166     {
167         return "<ol style=\"list-style-type: lower-roman\"><li>" + item + "</li></ol>";
168     }
169 
170     /** {@inheritDoc} */
171     protected String getDefinitionListBlock( String definum, String definition )
172     {
173         return "<dl><dt>" + definum + "</dt><dd>" + definition + "</dd></dl>";
174     }
175 
176     /** {@inheritDoc} */
177     protected String getFigureBlock( String source, String caption )
178     {
179         return "<img src=\"" + source + "\" alt=\"" + caption + "\" />";
180     }
181 
182     /** {@inheritDoc} */
183     protected String getTableBlock( String cell, String caption )
184     {
185         return "<table border=\"0\" class=\"bodyTable\">"
186             + "<caption>Table caption</caption><tr class=\"a\"><td>cell</td></tr>"
187             + "</table>";
188     }
189 
190     // Disable testTable until the order of attributes issue is clarified
191     // TODO: remove
192     /** {@inheritDoc} */
193     public void testTable()
194     {
195         assertEquals( "Dummy!", "", "" );
196     }
197 
198     /** {@inheritDoc} */
199     protected String getParagraphBlock( String text )
200     {
201         return "<p>" + text + "</p>";
202     }
203 
204     /** {@inheritDoc} */
205     protected String getVerbatimBlock( String text )
206     {
207         return "<div class=\"source\"><pre>" + text + "</pre></div>";
208     }
209 
210     /** {@inheritDoc} */
211     protected String getHorizontalRuleBlock()
212     {
213         return "<hr />";
214     }
215 
216     /** {@inheritDoc} */
217     protected String getPageBreakBlock()
218     {
219         return "<!-- PB -->";
220     }
221 
222     /** {@inheritDoc} */
223     protected String getAnchorBlock( String anchor )
224     {
225         return "<a name=\"" + anchor + "\">" + anchor + "</a>";
226     }
227 
228     /** {@inheritDoc} */
229     protected String getLinkBlock( String link, String text )
230     {
231         return "<a href=\"" + link + "\">" + text + "</a>";
232     }
233 
234     /** {@inheritDoc} */
235     protected String getItalicBlock( String text )
236     {
237         return "<i>" + text + "</i>";
238     }
239 
240     /** {@inheritDoc} */
241     protected String getBoldBlock( String text )
242     {
243         return "<b>" + text + "</b>";
244     }
245 
246     /** {@inheritDoc} */
247     protected String getMonospacedBlock( String text )
248     {
249         return "<tt>" + text + "</tt>";
250     }
251 
252     /** {@inheritDoc} */
253     protected String getLineBreakBlock()
254     {
255         return "<br />";
256     }
257 
258     /** {@inheritDoc} */
259     protected String getNonBreakingSpaceBlock()
260     {
261         return "&#160;";
262     }
263 
264     /** {@inheritDoc} */
265     protected String getTextBlock( String text )
266     {
267         // TODO: need to be able to retreive those from outside the sink
268         return "~,_=,_-,_+,_*,_[,_],_&lt;,_&gt;,_{,_},_\\";
269     }
270 
271     /** {@inheritDoc} */
272     protected String getRawTextBlock( String text )
273     {
274         return text;
275     }
276 
277     /**
278      * Test entities is section titles and paragraphs.
279      */
280     public void testEntities()
281     {
282         XhtmlSink sink = null;
283         Writer writer =  new StringWriter();
284 
285         try
286         {
287             sink = new XhtmlSink( writer );
288             sink.section( Sink.SECTION_LEVEL_1, null );
289             sink.sectionTitle( Sink.SECTION_LEVEL_1, null );
290             sink.text( "&", null );
291             sink.sectionTitle_( Sink.SECTION_LEVEL_1 );
292             sink.paragraph( null );
293             sink.text( "&", null );
294             sink.paragraph_();
295             sink.section_( Sink.SECTION_LEVEL_1 );
296         }
297         finally
298         {
299             sink.close();
300         }
301 
302         assertEquals( "<div class=\"section\"><h2>&amp;</h2><p>&amp;</p></div>", writer.toString() );
303     }
304 
305     /**
306      * Test head events.
307      */
308     public void testHead()
309     {
310         XhtmlSink sink = null;
311         Writer writer =  new StringWriter();
312 
313         try
314         {
315             sink = new XhtmlSink( writer );
316             sink.head();
317             sink.title();
318             sink.text( "Title" );
319             sink.title_();
320             sink.comment( "A comment" );
321             sink.author();
322             // note: this is really illegal, there should be no un-resolved entities emitted into text()
323             sink.text( "&#x123;&" );
324             sink.author_();
325             sink.head_();
326         }
327         finally
328         {
329             sink.close();
330         }
331 
332         String exp =
333                 "<head><title>Title</title><!-- A comment --><meta name=\"author\" content=\"&#x123;&amp;\" /></head>";
334         assertTrue( writer.toString().indexOf( exp ) != -1 );
335     }
336 
337     /** {@inheritDoc} */
338     protected String getCommentBlock( String text )
339     {
340         return "<!-- Simple comment with - - - - -->";
341     }
342 }