1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one 3 * or more contributor license agreements. See the NOTICE file 4 * distributed with this work for additional information 5 * regarding copyright ownership. The ASF licenses this file 6 * to you under the Apache License, Version 2.0 (the 7 * "License"); you may not use this file except in compliance 8 * with the License. You may obtain a copy of the License at 9 * 10 * http://www.apache.org/licenses/LICENSE-2.0 11 * 12 * Unless required by applicable law or agreed to in writing, 13 * software distributed under the License is distributed on an 14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 15 * KIND, either express or implied. See the License for the 16 * specific language governing permissions and limitations 17 * under the License. 18 */ 19 package org.apache.maven.doxia; 20 21 import java.io.File; 22 import java.io.IOException; 23 import java.io.InputStream; 24 import java.io.InputStreamReader; 25 import java.io.Reader; 26 import java.io.Writer; 27 import java.nio.charset.StandardCharsets; 28 29 import org.apache.maven.doxia.markup.Markup; 30 import org.codehaus.plexus.testing.PlexusTest; 31 import org.codehaus.plexus.util.WriterFactory; 32 33 import static org.codehaus.plexus.testing.PlexusExtension.getBasedir; 34 import static org.junit.jupiter.api.Assertions.assertNotNull; 35 36 /** 37 * Provide some common convenience methods to test Doxia modules (parsers and sinks). 38 * 39 * @since 1.0 40 */ 41 @PlexusTest 42 public abstract class AbstractModuleTest 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 // Safety 53 System.setProperty("line.separator", "\n"); 54 } 55 56 // ---------------------------------------------------------------------- 57 // Methods for creating test reader and writer 58 // ---------------------------------------------------------------------- 59 60 /** 61 * Returns a FileWriter to write to a file with the given name 62 * in the test target output directory. 63 * 64 * @param baseName The name of the target file. 65 * @param extension The file extension of the file to write. 66 * @return A FileWriter. 67 * @throws IOException If the FileWriter could not be generated. 68 * @see WriterFactory#newWriter(File, String) 69 */ 70 protected Writer getTestWriter(String baseName, String extension) throws IOException { 71 File outputFile = getTestWriterFile(baseName, extension); 72 73 if (!outputFile.getParentFile().exists()) { 74 outputFile.getParentFile().mkdirs(); 75 } 76 77 return WriterFactory.newWriter(outputFile, "UTF-8"); 78 } 79 80 protected File getTestWriterFile(String baseName, String extension) { 81 File outputDirectory = new File(getBasedirFile(), outputBaseDir() + getOutputDir()); 82 return new File(outputDirectory, baseName + '.' + extension); 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) throws IOException { 95 return getXmlTestWriter(baseName, outputExtension()); 96 } 97 98 protected static String normalizeLineEnds(String s) { 99 if (s != null) { 100 return s.replaceAll("\r\n", "\n").replaceAll("\r", "\n"); 101 } else { 102 return null; 103 } 104 } 105 106 /** 107 * Returns an XML FileWriter to write to a file with the given name 108 * in the test target output directory. 109 * 110 * @param baseName The name of the target file. 111 * @param extension The file extension of the file to write. 112 * @return An XML FileWriter. 113 * @throws IOException If the FileWriter could not be generated. 114 * @see WriterFactory#newXmlWriter(File) 115 */ 116 protected Writer getXmlTestWriter(String baseName, String extension) throws IOException { 117 File outputFile = getTestWriterFile(baseName, extension); 118 119 if (!outputFile.getParentFile().exists()) { 120 outputFile.getParentFile().mkdirs(); 121 } 122 123 return WriterFactory.newXmlWriter(outputFile); 124 } 125 126 /** 127 * Returns a FileWriter to write to a file with the given name 128 * in the test target output directory. 129 * 130 * @param baseName The name of the target file. 131 * @return A FileWriter. 132 * @throws IOException If the FileWriter could not be generated. 133 * @see #getTestWriter(String, String) 134 */ 135 protected Writer getTestWriter(String baseName) throws IOException { 136 return getTestWriter(baseName, outputExtension()); 137 } 138 139 protected File getTestWriterFile(String baseName) { 140 return getTestWriterFile(baseName, outputExtension()); 141 } 142 143 /** 144 * Returns an InputStreamReader to read a resource from a file 145 * in the test target output directory. 146 * 147 * @param baseName The name of the resource file to read. 148 * @param extension The file extension of the resource file to read. 149 * @return An InputStreamReader. 150 */ 151 protected Reader getTestReader(String baseName, String extension) { 152 InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(baseName + "." + extension); 153 154 assertNotNull(is, "Could not find resource: " + baseName + "." + extension); 155 156 return new InputStreamReader(is, StandardCharsets.UTF_8); 157 } 158 159 /** 160 * Returns an InputStreamReader to read a resource from a file 161 * in the test target output directory. 162 * 163 * @param baseName The name of the resource file to read. 164 * @return An InputStreamReader. 165 * @see #getTestReader(String, String) 166 */ 167 protected Reader getTestReader(String baseName) { 168 return getTestReader(baseName, outputExtension()); 169 } 170 171 // ---------------------------------------------------------------------- 172 // Utility methods 173 // ---------------------------------------------------------------------- 174 175 /** 176 * Returns the common base directory. 177 * 178 * @return The common base directory as a File. 179 */ 180 protected File getBasedirFile() { 181 return new File(getBasedir()); 182 } 183 184 /** 185 * Returns the base directory where all test output will go. 186 * 187 * @return The test output directory. 188 */ 189 protected final String outputBaseDir() { 190 return "target/test-output/"; 191 } 192 193 // ---------------------------------------------------------------------- 194 // Abstract methods the individual ModuleTests must provide 195 // ---------------------------------------------------------------------- 196 197 /** 198 * Determines the default file extension for the current module. 199 * 200 * @return The default file extension. 201 */ 202 protected abstract String outputExtension(); 203 204 /** 205 * Returns the directory where test output will go. 206 * Should be relative to outputBaseDir(). 207 * 208 * @return The test output directory, relative to outputBaseDir(). 209 */ 210 protected abstract String getOutputDir(); 211 }