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.TextSink;
27  import org.codehaus.plexus.util.IOUtil;
28  
29  import java.io.IOException;
30  import java.io.Reader;
31  import java.io.Writer;
32  
33  /**
34   * Test the parsing of sample input files.
35   * <br/>
36   * <b>Note</b>: you have to provide a sample "test." + outputExtension()
37   * file in the test resources directory if you extend this class.
38   *
39   * @version $Id: AbstractParserTest.java 735474 2009-01-18 15:33:20Z vsiveton $
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          Reader reader = null;
76          try
77          {
78              reader = getTestReader( "test", outputExtension() );
79  
80              createParser().parse( reader, sink );
81  
82              assertTrue( "Parser output not well-formed, last offending element: "
83                  + sink.getOffender(), sink.isWellformed() );
84          }
85          finally
86          {
87              IOUtil.close( reader );
88          }
89      }
90  
91       /**
92       * Parse a test document '"test." + outputExtension()'
93       * with parser from {@link #createParser()}, and output to a text file,
94       * using the {@link org.apache.maven.doxia.sink.TextSink TextSink}.
95       *
96       * @throws IOException if the test document cannot be read.
97       * @throws ParseException if the test document cannot be parsed.
98       */
99      public final void testDocument()
100         throws IOException, ParseException
101     {
102         Writer writer = null;
103         Reader reader = null;
104 
105         try
106         {
107             writer = getTestWriter( "test", "txt" );
108 
109             reader = getTestReader( "test", outputExtension() );
110 
111             Sink sink = new TextSink( writer );
112 
113             createParser().parse( reader, sink );
114         }
115         finally
116         {
117             IOUtil.close( reader );
118             IOUtil.close( writer );
119         }
120     }
121 
122 
123 }