001package org.apache.maven.doxia;
002
003/*
004 * Licensed to the Apache Software Foundation (ASF) under one
005 * or more contributor license agreements.  See the NOTICE file
006 * distributed with this work for additional information
007 * regarding copyright ownership.  The ASF licenses this file
008 * to you under the Apache License, Version 2.0 (the
009 * "License"); you may not use this file except in compliance
010 * with the License.  You may obtain a copy of the License at
011 *
012 *   http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing,
015 * software distributed under the License is distributed on an
016 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017 * KIND, either express or implied.  See the License for the
018 * specific language governing permissions and limitations
019 * under the License.
020 */
021
022import org.apache.maven.doxia.markup.Markup;
023
024import org.codehaus.plexus.PlexusTestCase;
025import org.codehaus.plexus.util.WriterFactory;
026
027import java.io.File;
028import java.io.IOException;
029import java.io.InputStream;
030import java.io.InputStreamReader;
031import java.io.Reader;
032import java.io.Writer;
033
034/**
035 * Provide some common convenience methods to test Doxia modules (parsers and sinks).
036 *
037 * @version $Id$
038 * @since 1.0
039 */
040public abstract class AbstractModuleTest
041    extends PlexusTestCase
042    implements Markup
043{
044    /**
045     * Set the system properties:
046     * <ul>
047     * <li><code>line.separator</code> to <code>\n</code> (Unix) to prevent
048     * failure on windows.</li>
049     * </ul>
050     */
051    static
052    {
053        // Safety
054        System.setProperty( "line.separator", "\n" );
055    }
056
057    // ----------------------------------------------------------------------
058    // Methods for creating test reader and writer
059    // ----------------------------------------------------------------------
060
061    /**
062     * Returns a FileWriter to write to a file with the given name
063     * in the test target output directory.
064     *
065     * @param baseName The name of the target file.
066     * @param extension The file extension of the file to write.
067     * @return A FileWriter.
068     * @throws IOException If the FileWriter could not be generated.
069     * @see WriterFactory#newWriter(File, String)
070     */
071    protected Writer getTestWriter( String baseName, String extension )
072        throws IOException
073    {
074        File outputFile = getTestWriterFile( baseName, extension );
075
076        if ( !outputFile.getParentFile().exists() )
077        {
078            outputFile.getParentFile().mkdirs();
079        }
080
081        return WriterFactory.newWriter( outputFile, "UTF-8" );
082    }
083
084    protected File getTestWriterFile( String baseName, String extension )
085    {
086        File outputDirectory = new File( getBasedirFile(), outputBaseDir() + getOutputDir() );
087        return new File( outputDirectory, baseName + '.' + extension );
088    }
089
090    /**
091     * Returns an XML FileWriter to write to a file with the given name
092     * in the test target output directory.
093     *
094     * @param baseName The name of the target file.
095     * @return An XML FileWriter.
096     * @throws IOException If the FileWriter could not be generated.
097     * @see #getXmlTestWriter(String, String)
098     */
099    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        InputStreamReader reader = new InputStreamReader( is );
177
178        return reader;
179    }
180
181    /**
182     * Returns an InputStreamReader to read a resource from a file
183     * in the test target output directory.
184     *
185     * @param baseName The name of the resource file to read.
186     * @return An InputStreamReader.
187     * @see #getTestReader(String, String)
188     */
189    protected Reader getTestReader( String baseName )
190    {
191        return getTestReader( baseName, outputExtension() );
192    }
193
194
195    // ----------------------------------------------------------------------
196    // Utility methods
197    // ----------------------------------------------------------------------
198
199    /**
200     * Returns the common base directory.
201     *
202     * @return The common base directory as a File.
203     */
204    protected File getBasedirFile()
205    {
206        return new File( getBasedir() );
207    }
208
209    /**
210     * Returns the base directory where all test output will go.
211     *
212     * @return The test output directory.
213     */
214    protected final String outputBaseDir()
215    {
216        return "target/test-output/";
217    }
218
219
220    // ----------------------------------------------------------------------
221    // Abstract methods the individual ModuleTests must provide
222    // ----------------------------------------------------------------------
223
224    /**
225     * Determines the default file extension for the current module.
226     *
227     * @return The default file extension.
228     */
229    protected abstract String outputExtension();
230
231    /**
232     * Returns the directory where test output will go.
233     * Should be relative to outputBaseDir().
234     *
235     * @return The test output directory, relative to outputBaseDir().
236     */
237    protected abstract String getOutputDir();
238}