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.markup.HtmlMarkup;
26  import org.apache.maven.doxia.sink.Sink;
27  import org.apache.maven.doxia.sink.impl.AbstractSinkTest;
28  import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet;
29  
30  /**
31   * @author Jason van Zyl
32   * @version $Id: XhtmlSinkTest.java 1726411 2016-01-23 16:34:09Z hboutemy $
33   * @since 1.0
34   */
35  public class XhtmlSinkTest
36      extends AbstractSinkTest
37  {
38      /** {@inheritDoc} */
39      protected String outputExtension()
40      {
41          return "xhtml";
42      }
43  
44      /** {@inheritDoc} */
45      protected Sink createSink( Writer writer )
46      {
47          return new XhtmlSink( writer, "UTF-8" );
48      }
49  
50      /** {@inheritDoc} */
51      protected boolean isXmlSink()
52      {
53          return true;
54      }
55  
56      /**
57       * Test link generation.
58       *
59       * @throws java.lang.Exception if any.
60       */
61      public void testLinks()
62          throws Exception
63      {
64          XhtmlSink sink = null;
65          Writer writer =  new StringWriter();
66          try
67          {
68              sink = (XhtmlSink) createSink( writer );
69              sink.link( "http:/www.xdoc.com" );
70              sink.link_();
71              sink.link( "./index.html#anchor" );
72              sink.link_();
73              sink.link( "../index.html#anchor" );
74              sink.link_();
75              sink.link( "index.html" );
76              sink.link_();
77          }
78          finally
79          {
80              if ( sink != null )
81              {
82                  sink.close();
83              }
84          }
85  
86          String actual = writer.toString();
87          assertTrue( actual.indexOf( "<a class=\"externalLink\" href=\"http:/www.xdoc.com\"></a>" ) != -1 );
88          assertTrue( actual.indexOf( "<a href=\"./index.html#anchor\"></a>" ) != -1 );
89          assertTrue( actual.indexOf( "<a href=\"../index.html#anchor\"></a>" ) != -1 );
90          assertTrue( actual.indexOf( "<a href=\"index.html\"></a>" ) != -1 );
91      }
92  
93      /** {@inheritDoc} */
94      protected String getTitleBlock( String title )
95      {
96          return "<title>" + title + "</title>";
97      }
98  
99      /** {@inheritDoc} */
100     protected String getAuthorBlock( String author )
101     {
102         return author;
103     }
104 
105     /** {@inheritDoc} */
106     protected String getDateBlock( String date )
107     {
108         return date;
109     }
110 
111     /** {@inheritDoc} */
112     protected String getHeadBlock()
113     {
114         return "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" +
115                 "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n<head>\n<title></title>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\"/></head>";
116     }
117 
118     /** {@inheritDoc} */
119     protected String getBodyBlock()
120     {
121         return "<body></body></html>";
122     }
123 
124     /** {@inheritDoc} */
125     protected String getSectionTitleBlock( String title )
126     {
127         return title;
128     }
129 
130     /** {@inheritDoc} */
131     protected String getSection1Block( String title )
132     {
133         return "<div class=\"section\">\n<h2>" + title + "</h2></div>";
134     }
135 
136     /** {@inheritDoc} */
137     protected String getSection2Block( String title )
138     {
139         return "<div class=\"section\">\n<h3>" + title + "</h3></div>";
140     }
141 
142     /** {@inheritDoc} */
143     protected String getSection3Block( String title )
144     {
145         return "<div class=\"section\">\n<h4>" + title + "</h4></div>";
146     }
147 
148     /** {@inheritDoc} */
149     protected String getSection4Block( String title )
150     {
151         return "<div class=\"section\">\n<h5>" + title + "</h5></div>";
152     }
153 
154     /** {@inheritDoc} */
155     protected String getSection5Block( String title )
156     {
157         return "<div class=\"section\">\n<h6>" + title + "</h6></div>";
158     }
159 
160     /** {@inheritDoc} */
161     protected String getListBlock( String item )
162     {
163         return "<ul>\n<li>" + item + "</li></ul>";
164     }
165 
166     /** {@inheritDoc} */
167     protected String getNumberedListBlock( String item )
168     {
169         return "<ol style=\"list-style-type: lower-roman\">\n<li>" + item + "</li></ol>";
170     }
171 
172     /** {@inheritDoc} */
173     protected String getDefinitionListBlock( String definum, String definition )
174     {
175         return "<dl>\n<dt>" + definum + "</dt>\n<dd>" + definition + "</dd></dl>";
176     }
177 
178     /** {@inheritDoc} */
179     protected String getFigureBlock( String source, String caption )
180     {
181         String figureBlock = "<img src=\"" + source + "\"";
182         if( caption != null )
183         {
184             figureBlock += " alt=\"" + caption + "\"";
185         }
186         else //@todo fix DOXIA-361
187         {
188             figureBlock += " alt=\"\"";
189         }
190         figureBlock += " />";
191         return figureBlock;
192     }
193 
194     /** {@inheritDoc} */
195     protected String getTableBlock( String cell, String caption )
196     {
197         return "<table border=\"0\" class=\"bodyTable\">"
198             + "<caption>Table caption</caption><tr class=\"a\">\n<td>cell</td></tr>"
199             + "</table>";
200     }
201 
202     // Disable testTable until the order of attributes issue is clarified
203     // TODO: remove
204     /** {@inheritDoc} */
205     public void testTable()
206     {
207         assertEquals( "Dummy!", "", "" );
208     }
209 
210     /** {@inheritDoc} */
211     protected String getParagraphBlock( String text )
212     {
213         return "<p>" + text + "</p>";
214     }
215 
216     /** {@inheritDoc} */
217     protected String getVerbatimBlock( String text )
218     {
219         return "<div class=\"source\">\n<pre>" + text + "</pre></div>";
220     }
221 
222     /** {@inheritDoc} */
223     protected String getHorizontalRuleBlock()
224     {
225         return "<hr />";
226     }
227 
228     /** {@inheritDoc} */
229     protected String getPageBreakBlock()
230     {
231         return "<!-- PB -->";
232     }
233 
234     /** {@inheritDoc} */
235     protected String getAnchorBlock( String anchor )
236     {
237         return "<a name=\"" + anchor + "\">" + anchor + "</a>";
238     }
239 
240     /** {@inheritDoc} */
241     protected String getLinkBlock( String link, String text )
242     {
243         return "<a href=\"" + link + "\">" + text + "</a>";
244     }
245 
246     /** {@inheritDoc} */
247     protected String getItalicBlock( String text )
248     {
249         return "<i>" + text + "</i>";
250     }
251 
252     /** {@inheritDoc} */
253     protected String getBoldBlock( String text )
254     {
255         return "<b>" + text + "</b>";
256     }
257 
258     /** {@inheritDoc} */
259     protected String getMonospacedBlock( String text )
260     {
261         return "<tt>" + text + "</tt>";
262     }
263 
264     /** {@inheritDoc} */
265     protected String getLineBreakBlock()
266     {
267         return "<br />";
268     }
269 
270     /** {@inheritDoc} */
271     protected String getNonBreakingSpaceBlock()
272     {
273         return "&#160;";
274     }
275 
276     /** {@inheritDoc} */
277     protected String getTextBlock( String text )
278     {
279         // TODO: need to be able to retreive those from outside the sink
280         return "~,_=,_-,_+,_*,_[,_],_&lt;,_&gt;,_{,_},_\\";
281     }
282 
283     /** {@inheritDoc} */
284     protected String getRawTextBlock( String text )
285     {
286         return text;
287     }
288 
289     /**
290      * Test entities is section titles and paragraphs.
291      */
292     public void testEntities()
293     {
294         XhtmlSink sink = null;
295         Writer writer =  new StringWriter();
296 
297         try
298         {
299             sink = new XhtmlSink( writer );
300             sink.section( Sink.SECTION_LEVEL_1, null );
301             sink.sectionTitle( Sink.SECTION_LEVEL_1, null );
302             sink.text( "&", null );
303             sink.sectionTitle_( Sink.SECTION_LEVEL_1 );
304             sink.paragraph( null );
305             sink.text( "&", null );
306             sink.paragraph_();
307             sink.section_( Sink.SECTION_LEVEL_1 );
308         }
309         finally
310         {
311             sink.close();
312         }
313 
314         assertEquals( "<div class=\"section\">\n<h2>&amp;</h2>\n<p>&amp;</p></div>", writer.toString() );
315     }
316 
317     /**
318      * Test head events.
319      */
320     public void testHead()
321     {
322         XhtmlSink sink = null;
323         Writer writer =  new StringWriter();
324 
325         try
326         {
327             sink = new XhtmlSink( writer );
328             sink.head();
329             sink.title();
330             sink.text( "Title" );
331             sink.title_();
332             sink.comment( "A comment" );
333             sink.author();
334             // note: this is really illegal, there should be no un-resolved entities emitted into text()
335             sink.text( "&#x123;&" );
336             sink.author_();
337             SinkEventAttributeSet atts = new SinkEventAttributeSet( 1 );
338             atts.addAttribute( "href", "http://maven.apache.org/" );
339             sink.unknown( "base", new Object[] {new Integer( HtmlMarkup.TAG_TYPE_SIMPLE )}, atts );
340             sink.head_();
341         }
342         finally
343         {
344             sink.close();
345         }
346 
347         String expected =
348             "<head>\n<title>Title</title><!--A comment--><meta name=\"author\" content=\"&#x123;&amp;\" />"
349                 + "<base href=\"http://maven.apache.org/\" /></head>";
350         String actual = writer.toString();
351         assertTrue( actual, actual.indexOf( expected ) != -1 );
352     }
353 
354     /** {@inheritDoc} */
355     protected String getCommentBlock( String text )
356     {
357         return "<!--" + toXmlComment( text ) + "-->";
358     }
359 }