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
34 /**
35 * Provide some common convenience methods to test Doxia modules (parsers and sinks).
36 *
37 * @version $Id: AbstractModuleTest.java 1410898 2012-11-18 15:33:17Z hboutemy $
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 /**
106 * Returns an XML FileWriter to write to a file with the given name
107 * in the test target output directory.
108 *
109 * @param baseName The name of the target file.
110 * @param extension The file extension of the file to write.
111 * @return An XML FileWriter.
112 * @throws IOException If the FileWriter could not be generated.
113 * @see WriterFactory#newXmlWriter(File)
114 */
115 protected Writer getXmlTestWriter( String baseName, String extension )
116 throws IOException
117 {
118 File outputFile = getTestWriterFile( baseName, extension );
119
120 if ( !outputFile.getParentFile().exists() )
121 {
122 outputFile.getParentFile().mkdirs();
123 }
124
125 return WriterFactory.newXmlWriter( outputFile );
126 }
127
128 /**
129 * Returns a FileWriter to write to a file with the given name
130 * in the test target output directory.
131 *
132 * @param baseName The name of the target file.
133 * @return A FileWriter.
134 * @throws IOException If the FileWriter could not be generated.
135 * @see #getTestWriter(String, String)
136 */
137 protected Writer getTestWriter( String baseName )
138 throws IOException
139 {
140 return getTestWriter( baseName, outputExtension() );
141 }
142
143 protected File getTestWriterFile( String baseName )
144 {
145 return getTestWriterFile( baseName, outputExtension() );
146 }
147
148 /**
149 * Returns an InputStreamReader to read a resource from a file
150 * in the test target output directory.
151 *
152 * @param baseName The name of the resource file to read.
153 * @param extension The file extension of the resource file to read.
154 * @return An InputStreamReader.
155 */
156 protected Reader getTestReader( String baseName, String extension )
157 {
158 InputStream is =
159 Thread.currentThread().getContextClassLoader().getResourceAsStream(
160 baseName + "." + extension );
161
162 assertNotNull( "Could not find resource: " + baseName + "." + extension, is );
163
164 InputStreamReader reader = new InputStreamReader( is );
165
166 return reader;
167 }
168
169 /**
170 * Returns an InputStreamReader to read a resource from a file
171 * in the test target output directory.
172 *
173 * @param baseName The name of the resource file to read.
174 * @return An InputStreamReader.
175 * @see #getTestReader(String, String)
176 */
177 protected Reader getTestReader( String baseName )
178 {
179 return getTestReader( baseName, outputExtension() );
180 }
181
182
183 // ----------------------------------------------------------------------
184 // Utility methods
185 // ----------------------------------------------------------------------
186
187 /**
188 * Returns the common base directory.
189 *
190 * @return The common base directory as a File.
191 */
192 protected File getBasedirFile()
193 {
194 return new File( getBasedir() );
195 }
196
197 /**
198 * Returns the base directory where all test output will go.
199 *
200 * @return The test output directory.
201 */
202 protected final String outputBaseDir()
203 {
204 return "target/test-output/";
205 }
206
207
208 // ----------------------------------------------------------------------
209 // Abstract methods the individual ModuleTests must provide
210 // ----------------------------------------------------------------------
211
212 /**
213 * Determines the default file extension for the current module.
214 *
215 * @return The default file extension.
216 */
217 protected abstract String outputExtension();
218
219 /**
220 * Returns the directory where test output will go.
221 * Should be relative to outputBaseDir().
222 *
223 * @return The test output directory, relative to outputBaseDir().
224 */
225 protected abstract String getOutputDir();
226 }