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 712574 2008-11-09 22:16:42Z 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 outputDirectory = 75 new File( getBasedirFile(), outputBaseDir() + getOutputDir() ); 76 77 if ( !outputDirectory.exists() ) 78 { 79 outputDirectory.mkdirs(); 80 } 81 82 return WriterFactory.newWriter( new File( outputDirectory, baseName + "." + extension ), "UTF-8" ); 83 } 84 85 /** 86 * Returns an XML FileWriter to write to a file with the given name 87 * in the test target output directory. 88 * 89 * @param baseName The name of the target file. 90 * @return An XML FileWriter. 91 * @throws IOException If the FileWriter could not be generated. 92 * @see #getXmlTestWriter(String, String) 93 */ 94 protected Writer getXmlTestWriter( String baseName ) 95 throws IOException 96 { 97 return getXmlTestWriter( baseName, outputExtension() ); 98 } 99 100 /** 101 * Returns an XML FileWriter to write to a file with the given name 102 * in the test target output directory. 103 * 104 * @param baseName The name of the target file. 105 * @param extension The file extension of the file to write. 106 * @return An XML FileWriter. 107 * @throws IOException If the FileWriter could not be generated. 108 * @see WriterFactory#newXmlWriter(File) 109 */ 110 protected Writer getXmlTestWriter( String baseName, String extension ) 111 throws IOException 112 { 113 File outputDirectory = 114 new File( getBasedirFile(), outputBaseDir() + getOutputDir() ); 115 116 if ( !outputDirectory.exists() ) 117 { 118 outputDirectory.mkdirs(); 119 } 120 121 return WriterFactory.newXmlWriter( 122 new File( outputDirectory, baseName + "." + extension ) ); 123 } 124 125 /** 126 * Returns a FileWriter to write to a file with the given name 127 * in the test target output directory. 128 * 129 * @param baseName The name of the target file. 130 * @return A FileWriter. 131 * @throws IOException If the FileWriter could not be generated. 132 * @see #getTestWriter(String, String) 133 */ 134 protected Writer getTestWriter( String baseName ) 135 throws IOException 136 { 137 return getTestWriter( baseName, outputExtension() ); 138 } 139 140 /** 141 * Returns an InputStreamReader to read a resource from a file 142 * in the test target output directory. 143 * 144 * @param baseName The name of the resource file to read. 145 * @param extension The file extension of the resource file to read. 146 * @return An InputStreamReader. 147 */ 148 protected Reader getTestReader( String baseName, String extension ) 149 { 150 InputStream is = 151 Thread.currentThread().getContextClassLoader().getResourceAsStream( 152 baseName + "." + extension ); 153 154 assertNotNull( "Could not find resource: " 155 + baseName + "." + extension, is ); 156 157 InputStreamReader reader = new InputStreamReader( is ); 158 159 return reader; 160 } 161 162 /** 163 * Returns an InputStreamReader to read a resource from a file 164 * in the test target output directory. 165 * 166 * @param baseName The name of the resource file to read. 167 * @return An InputStreamReader. 168 * @see #getTestReader(String, String) 169 */ 170 protected Reader getTestReader( String baseName ) 171 { 172 return getTestReader( baseName, outputExtension() ); 173 } 174 175 176 // ---------------------------------------------------------------------- 177 // Utility methods 178 // ---------------------------------------------------------------------- 179 180 /** 181 * Returns the common base directory. 182 * 183 * @return The common base directory as a File. 184 */ 185 protected File getBasedirFile() 186 { 187 return new File( getBasedir() ); 188 } 189 190 /** 191 * Returns the base directory where all test output will go. 192 * 193 * @return The test output directory. 194 */ 195 protected final String outputBaseDir() 196 { 197 return "target/test-output/"; 198 } 199 200 201 // ---------------------------------------------------------------------- 202 // Abstract methods the individual ModuleTests must provide 203 // ---------------------------------------------------------------------- 204 205 /** 206 * Determines the default file extension for the current module. 207 * 208 * @return The default file extension. 209 */ 210 protected abstract String outputExtension(); 211 212 /** 213 * Returns the directory where test output will go. 214 * Should be relative to outputBaseDir(). 215 * 216 * @return The test output directory, relative to outputBaseDir(). 217 */ 218 protected abstract String getOutputDir(); 219 }