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    /**
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}