1 package org.apache.maven.doxia.module; 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 java.io.IOException; 23 import java.io.StringReader; 24 import java.io.StringWriter; 25 import java.io.Writer; 26 27 import org.apache.maven.doxia.AbstractModuleTest; 28 29 import org.apache.maven.doxia.logging.PlexusLoggerWrapper; 30 import org.apache.maven.doxia.parser.ParseException; 31 import org.apache.maven.doxia.parser.Parser; 32 33 import org.apache.maven.doxia.sink.Sink; 34 import org.apache.maven.doxia.sink.impl.SinkTestDocument; 35 import org.apache.maven.doxia.sink.impl.TextSink; 36 import org.codehaus.plexus.DefaultPlexusContainer; 37 38 /** 39 * If a module provides both Parser and Sink, this class 40 * can be used to check that chaining them together 41 * results in the identity transformation, ie the model is still the same 42 * after being piped through a Parser and the corresponding Sink. 43 */ 44 public abstract class AbstractIdentityTest 45 extends AbstractModuleTest 46 { 47 /** Expected Identity String */ 48 private String expected; 49 50 /** 51 * Set to true if the identity transformation should actually be asserted, 52 * by default only the expected and actual results are written to a file, but not compared. 53 */ 54 private boolean assertIdentity; 55 56 /** 57 * Create a new instance of the parser to test. 58 * 59 * @return the parser to test. 60 */ 61 protected abstract Parser createParser(); 62 63 /** 64 * Return a new instance of the sink that is being tested. 65 * 66 * @param writer The writer for the sink. 67 * @return A new sink. 68 */ 69 protected abstract Sink createSink( Writer writer ); 70 71 /** 72 * Pipes a full model generated by {@link SinkTestDocument} through 73 * a Sink (generated by {@link #createSink(Writer)}) and a Parser 74 * (generated by {@link #createParser()}) and checks if the result 75 * is the same as the original model. By default, this doesn't actually 76 * assert anything (use {@link #assertIdentity(boolean)} in the setUp() 77 * of an implementation to switch on the test), but the two generated 78 * output files, expected.txt and actual.txt, can be compared for differences. 79 * 80 * @throws IOException if there's a problem reading/writing a test file. 81 * @throws ParseException if a model cannot be parsed. 82 */ 83 public void testIdentity() 84 throws IOException, ParseException 85 { 86 // generate the expected model 87 StringWriter writer = new StringWriter(); 88 Sink sink = new TextSink( writer ); 89 SinkTestDocument.generate( sink ); 90 sink.close(); 91 expected = writer.toString(); 92 93 // write to file for comparison 94 try ( Writer fileWriter = getTestWriter( "expected" ) ) 95 { 96 fileWriter.write( expected ); 97 } 98 // generate the actual model 99 writer = new StringWriter(); 100 sink = createSink( writer ); 101 SinkTestDocument.generate( sink ); 102 sink.close(); 103 StringReader reader = new StringReader( writer.toString() ); 104 105 writer = new StringWriter(); 106 sink = new TextSink( writer ); 107 Parser parser = createParser(); 108 parser.enableLogging( new PlexusLoggerWrapper( ( ( DefaultPlexusContainer )getContainer() ).getLogger() ) ); 109 parser.parse( reader, sink ); 110 String actual = writer.toString(); 111 112 // write to file for comparison 113 try( Writer fileWriter = getTestWriter( "actual" ) ) 114 { 115 fileWriter.write( actual ); 116 } 117 118 // Disabled by default, it's unlikely that all our modules 119 // will pass this test any time soon, but the generated 120 // output files can still be compared. 121 122 if ( assertIdentity ) 123 { 124 // TODO: make this work for at least apt and xdoc modules? 125 assertEquals( "Identity test failed! See results in " + getTestWriterFile( "actual" ).getParent(), 126 getExpected(), actual ); 127 } 128 } 129 130 /** {@inheritDoc} */ 131 protected String getOutputDir() 132 { 133 return "identity/"; 134 } 135 136 /** 137 * The output files generated by this class are text files, 138 * independent of the kind of module being tested. 139 * 140 * @return The String "txt". 141 */ 142 protected String outputExtension() 143 { 144 return "txt"; 145 } 146 147 /** 148 * Set to true if the identity transformation should actually be asserted, 149 * by default only the expected and actual results are written to a file, but not compared. 150 * This should be called during setUp(). 151 * 152 * @param doAssert True to actually execute the test. 153 */ 154 protected void assertIdentity( boolean doAssert ) 155 { 156 this.assertIdentity = doAssert; 157 } 158 159 /** 160 * @return the expected identity string 161 */ 162 protected String getExpected() 163 { 164 return expected; 165 } 166 }