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