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