View Javadoc
1   package org.apache.maven.doxia.parser;
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 org.apache.maven.doxia.AbstractModuleTest;
23  import org.apache.maven.doxia.sink.impl.SinkEventAttributeSet;
24  import org.apache.maven.doxia.sink.impl.SinkEventElement;
25  import org.apache.maven.doxia.sink.impl.TextSink;
26  import org.apache.maven.doxia.sink.impl.WellformednessCheckingSink;
27  import org.apache.maven.doxia.sink.Sink;
28  import org.junit.Assert;
29  
30  import java.io.IOException;
31  import java.io.Reader;
32  import java.io.Writer;
33  import java.util.Iterator;
34  
35  /**
36   * Test the parsing of sample input files.
37   * <br>
38   * <b>Note</b>: you have to provide a sample "test." + outputExtension()
39   * file in the test resources directory if you extend this class.
40   * @since 1.0
41   */
42  public abstract class AbstractParserTest
43      extends AbstractModuleTest
44  {
45      /**
46       * Create a new instance of the parser to test.
47       *
48       * @return the parser to test.
49       */
50      protected abstract Parser createParser();
51  
52      /**
53       * Returns the directory where all parser test output will go.
54       *
55       * @return The test output directory.
56       */
57      protected String getOutputDir()
58      {
59          return "parser/";
60      }
61  
62      /**
63       * Parse a test document '"test." + outputExtension()'
64       * with parser from {@link #createParser()}, and output to a new
65       * {@link WellformednessCheckingSink}. Asserts that output is well-formed.
66       *
67       * @throws IOException if the test document cannot be read.
68       * @throws ParseException if the test document cannot be parsed.
69       */
70      public final void testParser()
71          throws IOException, ParseException
72      {
73          WellformednessCheckingSink sink = new WellformednessCheckingSink();
74  
75          try ( Reader reader = getTestReader( "test", outputExtension() ) )
76          {
77              createParser().parse( reader, sink );
78  
79              assertTrue( "Parser output not well-formed, last offending element: " + sink.getOffender(),
80                      sink.isWellformed() );
81          }
82      }
83  
84       /**
85       * Parse a test document '"test." + outputExtension()'
86       * with parser from {@link #createParser()}, and output to a text file,
87       * using the {@link org.apache.maven.doxia.sink.impl.TextSink TextSink}.
88       *
89       * @throws IOException if the test document cannot be read.
90       * @throws ParseException if the test document cannot be parsed.
91       */
92      public final void testDocument()
93          throws IOException, ParseException
94      {
95          try ( Writer writer = getTestWriter( "test", "txt" );
96                Reader reader = getTestReader( "test", outputExtension() ) )
97          {
98              Sink sink = new TextSink( writer );
99              createParser().parse( reader, sink );
100         }
101     }
102 
103     protected void assertEquals( SinkEventElement element, String name, Object... args )
104     {
105         assertEquals( "Name of element doesn't match", name, element.getName() );
106         Assert.assertArrayEquals( "Arguments don't match",  args, element.getArgs() );
107     }
108 
109     protected void assertAttributeEquals( SinkEventElement element, String name, String attr, String value )
110     {
111         assertEquals( name, element.getName() );
112         SinkEventAttributeSet atts = (SinkEventAttributeSet) element.getArgs()[0];
113         assertEquals( value, atts.getAttribute( attr ) );
114     }
115 
116     protected void assertEquals( Iterator<SinkEventElement> it, String... names )
117     {
118         StringBuilder expected = new StringBuilder();
119         StringBuilder actual = new StringBuilder();
120 
121         for ( String name : names )
122         {
123             expected.append( name ).append( '\n' );
124         }
125 
126         while ( it.hasNext() )
127         {
128             actual.append( it.next().getName() ).append( '\n' );
129         }
130 
131         assertEquals( expected.toString(), actual.toString() );
132     }
133 
134     protected void assertStartsWith( Iterator<SinkEventElement> it, String... names )
135     {
136         StringBuilder expected = new StringBuilder();
137         StringBuilder actual = new StringBuilder();
138 
139         for ( String name : names )
140         {
141             expected.append( name ).append( '\n' );
142             if ( it.hasNext() )
143             {
144                 actual.append( it.next().getName() ).append( '\n' );
145             }
146         }
147         assertEquals( expected.toString(), actual.toString() );
148     }
149 }