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.codehaus.plexus.util.IOUtil;
29  import org.junit.Assert;
30  
31  import java.io.IOException;
32  import java.io.Reader;
33  import java.io.Writer;
34  import java.util.Iterator;
35  
36  /**
37   * Test the parsing of sample input files.
38   * <br/>
39   * <b>Note</b>: you have to provide a sample "test." + outputExtension()
40   * file in the test resources directory if you extend this class.
41   *
42   * @version $Id: AbstractParserTest.java 1726411 2016-01-23 16:34:09Z hboutemy $
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      public final void testParser()
74          throws IOException, ParseException
75      {
76          WellformednessCheckingSink sink = new WellformednessCheckingSink();
77  
78          Reader reader = null;
79          try
80          {
81              reader = getTestReader( "test", outputExtension() );
82  
83              createParser().parse( reader, sink );
84  
85              assertTrue( "Parser output not well-formed, last offending element: "
86                  + sink.getOffender(), sink.isWellformed() );
87          }
88          finally
89          {
90              IOUtil.close( reader );
91          }
92      }
93  
94       /**
95       * Parse a test document '"test." + outputExtension()'
96       * with parser from {@link #createParser()}, and output to a text file,
97       * using the {@link org.apache.maven.doxia.sink.impl.TextSink TextSink}.
98       *
99       * @throws IOException if the test document cannot be read.
100      * @throws ParseException if the test document cannot be parsed.
101      */
102     public final void testDocument()
103         throws IOException, ParseException
104     {
105         Writer writer = null;
106         Reader reader = null;
107 
108         try
109         {
110             writer = getTestWriter( "test", "txt" );
111 
112             reader = getTestReader( "test", outputExtension() );
113 
114             Sink sink = new TextSink( writer );
115 
116             createParser().parse( reader, sink );
117         }
118         finally
119         {
120             IOUtil.close( reader );
121             IOUtil.close( writer );
122         }
123     }
124 
125     protected void assertEquals( SinkEventElement element, String name, Object... args )
126     {
127         assertEquals( "Name of element doesn't match", name, element.getName() );
128         Assert.assertArrayEquals( "Arguments don't match",  args, element.getArgs() );
129     }
130 
131     protected void assertAttributeEquals( SinkEventElement element, String name, String attr, String value )
132     {
133         assertEquals( name, element.getName() );
134         SinkEventAttributeSet atts = (SinkEventAttributeSet) element.getArgs()[0];
135         assertEquals( value, atts.getAttribute( attr ) );
136     }
137 
138     protected void assertEquals( Iterator<SinkEventElement> it, String... names )
139     {
140         StringBuilder expected = new StringBuilder();
141         StringBuilder actual = new StringBuilder();
142 
143         for ( String name : names )
144         {
145             expected.append( name ).append( '\n' );
146             if ( it.hasNext() )
147             {
148                 actual.append( it.next().getName() ).append( '\n' );
149             }
150         }
151         assertEquals( expected.toString(), actual.toString() );
152     }
153 
154 
155 }