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.sink.impl; 20 21 import java.io.File; 22 import java.io.FileWriter; 23 import java.io.InputStream; 24 import java.io.InputStreamReader; 25 import java.io.Reader; 26 import java.io.Writer; 27 import java.util.Objects; 28 29 import org.apache.maven.doxia.parser.Parser; 30 import org.apache.maven.doxia.sink.Sink; 31 import org.codehaus.plexus.testing.PlexusTest; 32 import org.junit.jupiter.api.Test; 33 34 import static org.codehaus.plexus.testing.PlexusExtension.getBasedir; 35 36 /** 37 * @author <a href="mailto:jason@maven.org">Jason van Zyl</a> 38 * @since 1.0 39 */ 40 @PlexusTest 41 public abstract class AbstractSinkTestCase { 42 private Writer testWriter; 43 44 // --------------------------------------------------------------------- 45 // Test case 46 // ---------------------------------------------------------------------- 47 48 /** 49 * Parses the test apt document (obtained via {@link #getTestReader()}) with the Parser returned 50 * by {@link #createParser()} into the Sink returned by {@link #createSink()}. 51 * 52 * @throws java.lang.Exception if anything goes wrong. 53 */ 54 @Test 55 public void testApt() throws Exception { 56 Parser parser = createParser(); 57 58 parser.parse(getTestReader(), createSink()); 59 } 60 61 // ---------------------------------------------------------------------- 62 // Abstract methods the individual SinkTests must provide 63 // ---------------------------------------------------------------------- 64 65 /** 66 * Return the default extension of files created by the test Sink. 67 * 68 * @return the extension of files created by the test Sink. 69 * @see #createSink() 70 */ 71 protected abstract String outputExtension(); 72 73 /** 74 * Return a Parser for testing. 75 * 76 * @return a test Parser. 77 */ 78 protected abstract Parser createParser(); 79 80 /** 81 * Return a Sink for testing. 82 * 83 * @return a test Sink. 84 * @throws java.lang.Exception if the Sink cannot be constructed. 85 */ 86 protected abstract Sink createSink() throws Exception; 87 88 // ---------------------------------------------------------------------- 89 // Methods for creating the test reader and writer 90 // ---------------------------------------------------------------------- 91 92 /** 93 * Returns a Writer to write a test output result. The Writer writes to a File 94 * <code>"target/output/test. + extension"</code>, where extension is returned by 95 * {@link #outputExtension()}, in the current base directory. 96 * 97 * @return a Writer to write a test output result. 98 * @throws java.lang.Exception if the Writer cannot be constructed. 99 */ 100 protected Writer getTestWriter() throws Exception { 101 if (testWriter == null) { 102 File outputDirectory = new File(getBasedirFile(), "target/output"); 103 104 if (!outputDirectory.exists()) { 105 outputDirectory.mkdirs(); 106 } 107 108 testWriter = new FileWriter(new File(outputDirectory, "test." + outputExtension())); 109 } 110 111 return testWriter; 112 } 113 114 /** 115 * Returns a Reader that gives access to a common test apt file. 116 * 117 * @return a Reader to access the test apt resource file. 118 */ 119 protected Reader getTestReader() { 120 InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream("test.apt"); 121 122 return new InputStreamReader(Objects.requireNonNull(is)); 123 } 124 125 // ---------------------------------------------------------------------- 126 // Utility methods 127 // ---------------------------------------------------------------------- 128 129 /** 130 * Return the current base directory as a File. 131 * 132 * @return the current base directory as a File. 133 */ 134 public File getBasedirFile() { 135 return new File(getBasedir()); 136 } 137 }