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