View Javadoc

1   package org.apache.maven.doxia.module.apt;
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.IOException;
23  import java.io.Reader;
24  import java.io.StringWriter;
25  import java.io.Writer;
26  import java.util.Iterator;
27  
28  import org.apache.maven.doxia.parser.AbstractParserTest;
29  import org.apache.maven.doxia.parser.Parser;
30  import org.apache.maven.doxia.parser.ParseException;
31  
32  import org.apache.maven.doxia.sink.Sink;
33  import org.apache.maven.doxia.sink.SinkEventAttributeSet;
34  import org.apache.maven.doxia.sink.SinkEventElement;
35  import org.apache.maven.doxia.sink.SinkEventTestingSink;
36  
37  import org.codehaus.plexus.util.IOUtil;
38  
39  /**
40   * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton</a>
41   * @version $Id: AptParserTest.java 1465234 2013-04-06 12:21:13Z rfscholte $
42   */
43  public class AptParserTest
44      extends AbstractParserTest
45  {
46  
47      private AptParser parser;
48  
49      @Override
50      protected void setUp()
51          throws Exception
52      {
53          super.setUp();
54  
55          parser = (AptParser) lookup( Parser.ROLE, "apt" );
56      }
57  
58      /** {@inheritDoc} */
59      protected Parser createParser()
60      {
61          return parser;
62      }
63  
64      protected String parseFileToAptSink( String file )
65          throws ParseException
66      {
67          StringWriter output = null;
68          Reader reader = null;
69          try
70          {
71              output = new StringWriter();
72              reader = getTestReader( file );
73  
74              Sink sink = new AptSink( output );
75              createParser().parse( reader, sink );
76          }
77          finally
78          {
79              IOUtil.close( output );
80              IOUtil.close( reader );
81          }
82  
83          return output.toString();
84      }
85  
86      /** @throws Exception  */
87      public void testLineBreak()
88          throws Exception
89      {
90          String linebreak = parseFileToAptSink( "test/linebreak" );
91  
92          assertTrue( linebreak.indexOf( "Line\\" + EOL + "break." ) != -1 );
93      }
94  
95      /** @throws Exception  */
96      public void testSnippetMacro()
97          throws Exception
98      {
99          String macro = parseFileToAptSink( "test/macro" );
100 
101         assertTrue( macro.indexOf( "<modelVersion\\>4.0.0\\</modelVersion\\>" ) != -1 );
102     }
103 
104     /** @throws Exception  */
105     public void testCommentsBeforeTitle()
106         throws Exception
107     {
108         String comments = parseFileToAptSink( "test/comments" );
109 
110         assertEquals( 0, comments.indexOf( "~~ comments before title" + EOL + "~~ like a license header, for example"
111             + EOL + " -----" + EOL + " Test DOXIA-379" ) );
112     }
113 
114     /** @throws Exception  */
115     public void testSnippet()
116         throws Exception
117     {
118         // DOXIA-259
119 
120         Reader reader = null;
121         SinkEventTestingSink sink = new SinkEventTestingSink();
122 
123         try
124         {
125             reader = getTestReader( "test/snippet" );
126 
127             createParser().parse( reader, sink );
128         }
129         finally
130         {
131             IOUtil.close( reader );
132         }
133 
134         Iterator<SinkEventElement> it = sink.getEventList().iterator();
135 
136         assertEquals( it, "head", "head_", "body", "list", "listItem", "text", "verbatim", "text", "verbatim_",
137                       "paragraph", "text", "paragraph_", "listItem_", "listItem", "text", "verbatim", "text",
138                       "verbatim_", "paragraph", "text", "paragraph_", "listItem_", "list_", "body_" );
139 
140         assertFalse( it.hasNext() );
141     }
142 
143 
144     /** @throws Exception  */
145     public void testSnippetTrailingSpace()
146         throws Exception
147     {
148         // DOXIA-425
149         String text = "%{snippet|id=myid|file=pom.xml}  " + EOL;
150 
151         SinkEventTestingSink sink = new SinkEventTestingSink();
152 
153         parser.parse( text, sink );
154 
155         Iterator<SinkEventElement> it = sink.getEventList().iterator();
156 
157         assertEquals( it, "head", "head_", "body", "verbatim", "text", "verbatim_", "body_" );
158 
159         assertFalse( it.hasNext() );
160     }
161 
162     /** @throws Exception  */
163     public void testTocMacro()
164         throws Exception
165     {
166         String toc = parseFileToAptSink( "test/toc" );
167 
168         // No section, only subsection 1 and 2
169         assertTrue( toc.indexOf( "* {{{SubSection_1.1}SubSection 1.1}}" ) != -1 );
170         assertTrue( toc.indexOf( "* {{{SubSection_1.1.2.1.1}SubSection 1.1.2.1.1}}" ) == -1 );
171     }
172 
173     /**
174      * Parses the test document test.apt and re-emits
175      * it into parser/test.apt.
176      *
177      * @throws java.io.IOException if the test file cannot be read.
178      * @throws org.apache.maven.doxia.parser.ParseException if the test file cannot be parsed.
179      */
180     public void testTestDocument()
181         throws IOException, ParseException
182     {
183         Writer writer = null;
184         Reader reader = null;
185         try
186         {
187             writer = getTestWriter( "test" );
188             reader = getTestReader( "test" );
189 
190             Sink sink = new AptSink( writer );
191 
192             createParser().parse( reader, sink );
193         }
194         finally
195         {
196             IOUtil.close( writer );
197             IOUtil.close( reader );
198         }
199     }
200 
201     /** @throws Exception  */
202     public void testBoxedVerbatim()
203         throws Exception
204     {
205         String text = "+--" + EOL + "boxed verbatim" + EOL + "+--" + EOL
206                 + "---" + EOL + "un-boxed verbatim" + EOL + "---" + EOL;
207 
208         SinkEventTestingSink sink = new SinkEventTestingSink();
209 
210         parser.parse( text, sink );
211 
212         Iterator<SinkEventElement> it = sink.getEventList().iterator();
213 
214         assertEquals( it, "head", "head_", "body" );
215         assertEquals( it.next(), "verbatim", SinkEventAttributeSet.BOXED );
216         assertEquals( it, "text", "verbatim_" );
217 
218         assertEquals( it.next(), "verbatim", new Object[] { null } );
219         assertEquals( it, "text", "verbatim_", "body_" );
220 
221         assertFalse( it.hasNext() );
222     }
223 
224     /** @throws Exception  */
225     public void testMultiLinesInTableCells()
226         throws Exception
227     {
228         String text = "*----------*--------------+----------------:" + EOL +
229                 " cell 1, | cell 1,2       | cell 1,3" + EOL +
230                 " 1       |                | " + EOL +
231                 "*----------*--------------+----------------:" + EOL +
232                 " cell 2,1 | cell 2,       | cell 2,3" + EOL +
233                 "          | 2             |" + EOL +
234                 "*----------*--------------+----------------:" + EOL +
235                 " cell 3,1 | cell 3,2      | cell 3," + EOL +
236                 "          |               | 3" + EOL +
237                 "*----------*--------------+----------------:" + EOL;
238 
239         SinkEventTestingSink sink = new SinkEventTestingSink();
240 
241         parser.parse( text, sink );
242 
243         Iterator<SinkEventElement> it = sink.getEventList().iterator();
244 
245         assertEquals( it, "head", "head_", "body", "table", "tableRows", "tableRow", "tableCell" );
246         assertEquals( it.next(), "text", "cell 1, 1" );
247 
248         assertEquals( it, "tableCell_", "tableCell" );
249         assertEquals( it.next(), "text", "cell 1,2" );
250 
251         assertEquals( it, "tableCell_", "tableCell" );
252         assertEquals( it.next(), "text", "cell 1,3" );
253 
254         assertEquals( it, "tableCell_", "tableRow_", "tableRow", "tableCell" );
255         assertEquals( it.next(), "text", "cell 2,1" );
256 
257         assertEquals( it, "tableCell_", "tableCell" );
258         assertEquals( it.next(), "text", "cell 2, 2" );
259 
260         assertEquals( it, "tableCell_", "tableCell" );
261         assertEquals( it.next(), "text", "cell 2,3" );
262         
263         assertEquals( it, "tableCell_", "tableRow_", "tableRow", "tableCell" );
264         assertEquals( it.next(), "text", "cell 3,1" );
265 
266         assertEquals( it, "tableCell_", "tableCell" );
267         assertEquals( it.next(), "text", "cell 3,2" );
268 
269         assertEquals( it, "tableCell_", "tableCell" );
270         assertEquals( it.next(), "text", "cell 3, 3" );
271 
272         assertEquals( it, "tableCell_", "tableRow_", "tableRows_", "table_", "body_" );
273 
274         assertFalse( it.hasNext() );
275     }
276 
277     /** @throws Exception  */
278     public void testLineBreakInTableCells()
279         throws Exception
280     {
281         String text = "*----------*--------------+----------------:" + EOL +
282                 " cell 1,\\ | cell 1,2       | cell 1,3" + EOL +
283                 " 1       |                | " + EOL +
284                 "*----------*--------------+----------------:" + EOL +
285                 " cell 2,1 | cell 2,\\     | cell 2,3" + EOL +
286                 "          | 2             |" + EOL +
287                 "*----------*--------------+----------------:" + EOL +
288                 " cell 3,1 | cell 3,2      | cell 3,\\" + EOL +
289                 "          |               | 3" + EOL +
290                 "*----------*--------------+----------------:" + EOL;
291 
292         SinkEventTestingSink sink = new SinkEventTestingSink();
293 
294         parser.parse( text, sink );
295 
296         Iterator<SinkEventElement> it = sink.getEventList().iterator();
297 
298         assertEquals( it, "head", "head_", "body", "table", "tableRows", "tableRow", "tableCell" );
299         assertEquals( it.next(), "text", "cell 1,\u00A0" );
300 
301         assertEquals( it.next().getName(), "lineBreak" );
302         assertEquals( it.next(), "text", "1" );
303 
304         assertEquals( it, "tableCell_", "tableCell" );
305         assertEquals( it.next(), "text", "cell 1,2" );
306 
307         assertEquals( it, "tableCell_", "tableCell" );
308         assertEquals( it.next(), "text", "cell 1,3" );
309 
310         assertEquals( it, "tableCell_", "tableRow_", "tableRow", "tableCell" );
311         assertEquals( it.next(), "text", "cell 2,1" );
312 
313         assertEquals( it, "tableCell_", "tableCell" );
314         assertEquals( it.next(), "text", "cell 2,\u00A0" );
315 
316         assertEquals( it.next().getName(), "lineBreak" );
317         assertEquals( it.next(), "text", "2" );
318 
319         assertEquals( it, "tableCell_", "tableCell" );
320         assertEquals( it.next(), "text", "cell 2,3" );
321 
322         assertEquals( it, "tableCell_", "tableRow_", "tableRow", "tableCell" );
323         assertEquals( it.next(), "text", "cell 3,1" );
324 
325         assertEquals( it, "tableCell_", "tableCell" );
326         assertEquals( it.next(), "text", "cell 3,2" );
327 
328         assertEquals( it, "tableCell_", "tableCell" );
329         assertEquals( it.next(), "text", "cell 3,\u00A0" );
330 
331         assertEquals( it.next().getName(), "lineBreak" );
332         assertEquals( it.next(), "text", "3" );
333 
334         assertEquals( it, "tableCell_", "tableRow_", "tableRows_", "table_", "body_" );
335 
336         assertFalse( it.hasNext() );
337     }
338 
339     /** @throws Exception  */
340     public void testDOXIA38()
341         throws Exception
342     {
343         String text =
344                 "*----------*--------------*---------------*" + EOL +
345                 "| Centered |   Centered   |   Centered    |" + EOL +
346                 "*----------*--------------+---------------:" + EOL +
347                 "| Centered | Left-aligned | Right-aligned |" + EOL +
348                 "*----------*--------------+---------------:";
349 
350         SinkEventTestingSink sink = new SinkEventTestingSink();
351 
352         parser.parse( text, sink );
353 
354         Iterator<SinkEventElement> it = sink.getEventList().iterator();
355 
356         assertEquals( it, "head", "head_", "body", "table", "tableRows", "tableRow" );
357         assertAttributeEquals( it.next(), "tableCell", SinkEventAttributeSet.ALIGN, "center" );
358         assertEquals( it.next(), "text", "Centered" );
359         assertEquals( it.next().getName(), "tableCell_" );
360         
361         assertAttributeEquals( it.next(), "tableCell", SinkEventAttributeSet.ALIGN, "center" );
362         assertEquals( it.next(), "text", "Centered" );
363         assertEquals( it.next().getName(), "tableCell_" );
364         
365         assertAttributeEquals( it.next(), "tableCell", SinkEventAttributeSet.ALIGN, "center" );
366         assertEquals( it.next(), "text", "Centered" );
367         assertEquals( it, "tableCell_", "tableRow_", "tableRow" );
368         
369         assertAttributeEquals( it.next(), "tableCell", SinkEventAttributeSet.ALIGN, "center" );
370         assertEquals( it.next(), "text", "Centered" );
371         assertEquals( it.next().getName(), "tableCell_" );
372         
373         assertAttributeEquals( it.next(), "tableCell", SinkEventAttributeSet.ALIGN, "left" );
374         assertEquals( it.next(), "text", "Left-aligned" );
375         assertEquals( it.next().getName(), "tableCell_" );
376         
377         assertAttributeEquals( it.next(), "tableCell", SinkEventAttributeSet.ALIGN, "right" );
378         assertEquals( it.next(), "text", "Right-aligned" );
379         assertEquals( it, "tableCell_", "tableRow_", "tableRows_", "table_", "body_" );
380 
381         assertFalse( it.hasNext() );
382     }
383 
384     /** @throws Exception  */
385     public void testSpecialCharactersInTables()
386         throws Exception
387     {
388         // DOXIA-323, DOXIA-433
389         String text =
390                 "  \\~ \\= \\- \\+ \\* \\[ \\] \\< \\> \\{ \\} \\\\ \\u2713" + EOL
391                 + EOL
392                 + "*--------------------------------------------------+---------------+" + EOL
393                 + "| \\~ \\= \\- \\+ \\* \\[ \\] \\< \\> \\{ \\} \\\\ \\u2713 | special chars |" + EOL
394                 + "*--------------------------------------------------+---------------+";
395 
396         SinkEventTestingSink sink = new SinkEventTestingSink();
397         parser.parse( text, sink );
398 
399         Iterator<SinkEventElement> it = sink.getEventList().iterator();
400 
401         assertEquals( it, "head", "head_", "body", "paragraph" );
402         assertEquals( it.next(), "text", "~ = - + * [ ] < > { } \\ \u2713" );
403 
404         assertEquals( it, "paragraph_", "table", "tableRows", "tableRow", "tableCell" );
405         assertEquals( it.next(), "text", "~ = - + * [ ] < > { } \\ \u2713" );
406 
407         assertEquals( it, "tableCell_", "tableCell", "text", "tableCell_", "tableRow_", "tableRows_", "table_", "body_" );
408 
409         assertFalse( it.hasNext() );
410     }
411 
412     /** @throws Exception  */
413     public void testSpacesAndBracketsInAnchors()
414         throws Exception
415     {
416         final String text = "  {Anchor with spaces (and brackets)}" + EOL
417             + "  Link to {{Anchor with spaces (and brackets)}}" + EOL
418             + "  {{{http://fake.api#method(with, args)}method(with, args)}}" + EOL;
419 
420         final SinkEventTestingSink sink = new SinkEventTestingSink();
421 
422         parser.parse( text, sink );
423 
424         Iterator<SinkEventElement> it = sink.getEventList().iterator();
425 
426         assertEquals( it, "head", "head_", "body", "paragraph" );
427         assertEquals( it.next(), "anchor", "Anchor_with_spaces_and_brackets" );
428 
429         assertEquals( it.next(), "text", "Anchor with spaces (and brackets)" );
430 
431         assertEquals( it, "anchor_", "text" );
432         assertEquals( it.next(), "link", "#Anchor_with_spaces_and_brackets" );
433 
434         assertEquals( it.next(), "text", "Anchor with spaces (and brackets)" );
435 
436         assertEquals( it, "link_", "text" );
437         assertEquals( it.next(), "link", "http://fake.api#method(with, args)" );
438 
439         assertEquals( it.next(), "text", "method(with, args)" );
440 
441         assertEquals( it, "link_", "paragraph_", "body_" );
442 
443         assertFalse( it.hasNext() );
444     }
445 
446     /** @throws Exception  */
447     public void testSectionTitleAnchors()
448         throws Exception
449     {
450         // DOXIA-420
451         String text = "Enhancements to the APT format" + EOL + EOL
452             + "{Title with anchor}" + EOL;
453 
454         SinkEventTestingSink sink = new SinkEventTestingSink();
455 
456         parser.parse( text, sink );
457 
458         Iterator<SinkEventElement> it = sink.getEventList().iterator();
459 
460         assertEquals( it, "head", "head_", "body", "section1", "sectionTitle1", "text", "sectionTitle1_", "section1_",
461                       "section1", "sectionTitle1", "anchor", "text", "anchor_", "sectionTitle1_", "section1_", "body_" );
462 
463         assertFalse( it.hasNext() );
464     }
465     
466     /**
467      * @throws Exception
468      */
469     public void testTableHeaders() throws Exception
470     {
471         // DOXIA-404
472         String text = "*-----------+-----------+" + EOL + 
473         		"|| Header 1 || Header 2 |" + EOL +
474         		"*-----------+-----------+" + EOL +
475         		"  Cell 1    | Cell 2    |" + EOL +
476         		"*-----------+-----------+" + EOL +
477         		"  Cell 3    | Cell 4    |" + EOL +
478         		"*-----------+-----------+" + EOL;
479         
480         SinkEventTestingSink sink = new SinkEventTestingSink();
481 
482         parser.parse( text, sink );
483 
484         Iterator<SinkEventElement> it = sink.getEventList().iterator();
485 
486         assertEquals( it, "head", "head_", "body", "table", "tableRows" );
487         assertEquals( it, "tableRow", "tableHeaderCell", "text", "tableHeaderCell_", "tableHeaderCell", "text",
488                       "tableHeaderCell_", "tableRow_" );
489         assertEquals( it, "tableRow", "tableCell", "text", "tableCell_", "tableCell", "text", "tableCell_", "tableRow_" );
490         assertEquals( it, "tableRow", "tableCell", "text", "tableCell_", "tableCell", "text", "tableCell_", "tableRow_" );
491         assertEquals( it, "tableRows_", "table_", "body_" );
492 
493         assertFalse( it.hasNext() );
494     }
495     
496     public void testEscapedPipeInTableCell() throws Exception
497     {
498         // DOXIA-479
499         String text="*---+---+" + EOL + 
500         		"| cell \\| pipe | next cell " + EOL + 
501         		"*---+---+" + EOL;
502         
503         SinkEventTestingSink sink = new SinkEventTestingSink();
504 
505         parser.parse( text, sink );
506 
507         Iterator<SinkEventElement> it = sink.getEventList().iterator();
508         assertEquals( it, "head", "head_", "body", "table", "tableRows", "tableRow", "tableCell" );
509         assertEquals( it.next(), "text", "cell | pipe" );
510         assertEquals( it, "tableCell_", "tableCell" );
511         assertEquals( it.next(), "text", "next cell" );
512         assertEquals( it, "tableCell_", "tableRow_", "tableRows_", "table_", "body_" );
513         assertFalse( it.hasNext() );
514     }
515 
516     public void testLiteralAnchor()
517         throws Exception
518     {
519         // DOXIA-397
520         String text =
521             "{{{../apidocs/groovyx/net/http/ParserRegistry.html##parseText(org.apache.http.HttpResponse)}ParserRegistry}}";
522 
523         SinkEventTestingSink sink = new SinkEventTestingSink();
524 
525         parser.parse( text, sink );
526 
527         Iterator<SinkEventElement> it = sink.getEventList().iterator();
528         assertEquals( it, "head", "head_", "body", "section1", "sectionTitle1" );
529         assertEquals( it.next(), "link",
530                       "../apidocs/groovyx/net/http/ParserRegistry.html#parseText(org.apache.http.HttpResponse)" );
531         assertEquals( it.next(), "text", "ParserRegistry" );
532         assertEquals( it, "link_", "sectionTitle1_", "section1_", "body_" );
533         assertFalse( it.hasNext() );
534     }
535 
536     /** {@inheritDoc} */
537     protected String outputExtension()
538     {
539         return "apt";
540     }
541 }