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