View Javadoc
1   package org.apache.maven.doxia;
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.markup.Markup;
23  
24  import org.codehaus.plexus.PlexusTestCase;
25  import org.codehaus.plexus.util.WriterFactory;
26  
27  import java.io.File;
28  import java.io.IOException;
29  import java.io.InputStream;
30  import java.io.InputStreamReader;
31  import java.io.Reader;
32  import java.io.Writer;
33  import java.nio.charset.StandardCharsets;
34  
35  /**
36   * Provide some common convenience methods to test Doxia modules (parsers and sinks).
37   *
38   * @since 1.0
39   */
40  public abstract class AbstractModuleTest
41      extends PlexusTestCase
42      implements Markup
43  {
44      /*
45       * Set the system properties:
46       * <ul>
47       * <li><code>line.separator</code> to <code>\n</code> (Unix) to prevent
48       * failure on windows.</li>
49       * </ul>
50       */
51      static
52      {
53          // Safety
54          System.setProperty( "line.separator", "\n" );
55      }
56  
57      // ----------------------------------------------------------------------
58      // Methods for creating test reader and writer
59      // ----------------------------------------------------------------------
60  
61      /**
62       * Returns a FileWriter to write to a file with the given name
63       * in the test target output directory.
64       *
65       * @param baseName The name of the target file.
66       * @param extension The file extension of the file to write.
67       * @return A FileWriter.
68       * @throws IOException If the FileWriter could not be generated.
69       * @see WriterFactory#newWriter(File, String)
70       */
71      protected Writer getTestWriter( String baseName, String extension )
72          throws IOException
73      {
74          File outputFile = getTestWriterFile( baseName, extension );
75  
76          if ( !outputFile.getParentFile().exists() )
77          {
78              outputFile.getParentFile().mkdirs();
79          }
80  
81          return WriterFactory.newWriter( outputFile, "UTF-8" );
82      }
83  
84      protected File getTestWriterFile( String baseName, String extension )
85      {
86          File outputDirectory = new File( getBasedirFile(), outputBaseDir() + getOutputDir() );
87          return new File( outputDirectory, baseName + '.' + extension );
88      }
89  
90      /**
91       * Returns an XML FileWriter to write to a file with the given name
92       * in the test target output directory.
93       *
94       * @param baseName The name of the target file.
95       * @return An XML FileWriter.
96       * @throws IOException If the FileWriter could not be generated.
97       * @see #getXmlTestWriter(String, String)
98       */
99      protected Writer getXmlTestWriter( String baseName )
100         throws IOException
101     {
102         return getXmlTestWriter( baseName, outputExtension() );
103     }
104 
105     protected static String normalizeLineEnds( String s )
106     {
107         if ( s != null )
108         {
109             return s.replaceAll( "\r\n", "\n" ).replaceAll( "\r", "\n" );
110         }
111         else
112         {
113             return null;
114         }
115     }
116 
117     /**
118      * Returns an XML FileWriter to write to a file with the given name
119      * in the test target output directory.
120      *
121      * @param baseName The name of the target file.
122      * @param extension The file extension of the file to write.
123      * @return An XML FileWriter.
124      * @throws IOException If the FileWriter could not be generated.
125      * @see WriterFactory#newXmlWriter(File)
126      */
127     protected Writer getXmlTestWriter( String baseName, String extension )
128         throws IOException
129     {
130         File outputFile = getTestWriterFile( baseName, extension );
131 
132         if ( !outputFile.getParentFile().exists() )
133         {
134             outputFile.getParentFile().mkdirs();
135         }
136 
137         return WriterFactory.newXmlWriter( outputFile );
138     }
139 
140     /**
141      * Returns a FileWriter to write to a file with the given name
142      * in the test target output directory.
143      *
144      * @param baseName The name of the target file.
145      * @return A FileWriter.
146      * @throws IOException If the FileWriter could not be generated.
147      * @see #getTestWriter(String, String)
148      */
149     protected Writer getTestWriter( String baseName )
150         throws IOException
151     {
152         return getTestWriter( baseName, outputExtension() );
153     }
154 
155     protected File getTestWriterFile( String baseName )
156     {
157         return getTestWriterFile( baseName, outputExtension() );
158     }
159 
160     /**
161      * Returns an InputStreamReader to read a resource from a file
162      * in the test target output directory.
163      *
164      * @param baseName The name of the resource file to read.
165      * @param extension The file extension of the resource file to read.
166      * @return An InputStreamReader.
167      */
168     protected Reader getTestReader( String baseName, String extension )
169     {
170         InputStream is =
171             Thread.currentThread().getContextClassLoader().getResourceAsStream(
172                 baseName + "." + extension );
173 
174         assertNotNull( "Could not find resource: " + baseName + "." + extension, is );
175 
176         return new InputStreamReader( is, StandardCharsets.UTF_8 );
177     }
178 
179     /**
180      * Returns an InputStreamReader to read a resource from a file
181      * in the test target output directory.
182      *
183      * @param baseName The name of the resource file to read.
184      * @return An InputStreamReader.
185      * @see #getTestReader(String, String)
186      */
187     protected Reader getTestReader( String baseName )
188     {
189         return getTestReader( baseName, outputExtension() );
190     }
191 
192 
193     // ----------------------------------------------------------------------
194     // Utility methods
195     // ----------------------------------------------------------------------
196 
197     /**
198      * Returns the common base directory.
199      *
200      * @return The common base directory as a File.
201      */
202     protected File getBasedirFile()
203     {
204         return new File( getBasedir() );
205     }
206 
207     /**
208      * Returns the base directory where all test output will go.
209      *
210      * @return The test output directory.
211      */
212     protected final String outputBaseDir()
213     {
214         return "target/test-output/";
215     }
216 
217 
218     // ----------------------------------------------------------------------
219     // Abstract methods the individual ModuleTests must provide
220     // ----------------------------------------------------------------------
221 
222     /**
223      * Determines the default file extension for the current module.
224      *
225      * @return The default file extension.
226      */
227     protected abstract String outputExtension();
228 
229     /**
230      * Returns the directory where test output will go.
231      * Should be relative to outputBaseDir().
232      *
233      * @return The test output directory, relative to outputBaseDir().
234      */
235     protected abstract String getOutputDir();
236 }