View Javadoc
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.parser.ParseException;
30  import org.apache.maven.doxia.parser.Parser;
31  
32  import org.apache.maven.doxia.sink.Sink;
33  import org.apache.maven.doxia.sink.impl.SinkTestDocument;
34  import org.apache.maven.doxia.sink.impl.TextSink;
35  import org.junit.jupiter.api.Test;
36  
37  import static org.junit.jupiter.api.Assertions.assertEquals;
38  
39  /**
40   * If a module provides both Parser and Sink, this class
41   * can be used to check that chaining them together
42   * results in the identity transformation, ie the model is still the same
43   * after being piped through a Parser and the corresponding Sink.
44   */
45  public abstract class AbstractIdentityTest
46      extends AbstractModuleTest
47  {
48      /** Expected Identity String */
49      private String expected;
50  
51      /**
52       * Set to true if the identity transformation should actually be asserted,
53       * by default only the expected and actual results are written to a file, but not compared.
54       */
55      private boolean assertIdentity;
56  
57      /**
58       * Create a new instance of the parser to test.
59       *
60       * @return the parser to test.
61       */
62      protected abstract Parser createParser();
63  
64      /**
65       * Return a new instance of the sink that is being tested.
66       *
67       * @param writer The writer for the sink.
68       * @return A new sink.
69       */
70      protected abstract Sink createSink( Writer writer );
71  
72      /**
73       * Pipes a full model generated by {@link SinkTestDocument} through
74       * a Sink (generated by {@link #createSink(Writer)}) and a Parser
75       * (generated by {@link #createParser()}) and checks if the result
76       * is the same as the original model. By default, this doesn't actually
77       * assert anything (use {@link #assertIdentity(boolean)} in the setUp()
78       * of an implementation to switch on the test), but the two generated
79       * output files, expected.txt and actual.txt, can be compared for differences.
80       *
81       * @throws IOException if there's a problem reading/writing a test file.
82       * @throws ParseException if a model cannot be parsed.
83       */
84      @Test
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          try ( Writer fileWriter = getTestWriter( "expected" ) )
97          {
98              fileWriter.write( expected );
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.parse( reader, sink );
111         String actual = writer.toString();
112 
113         // write to file for comparison
114         try( Writer fileWriter = getTestWriter( "actual" ) )
115         {
116             fileWriter.write( actual );
117         }
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( getExpected(), actual,
127                           "Identity test failed! See results in "
128                                   + getTestWriterFile( "actual" ).getParent() );
129         }
130     }
131 
132     /** {@inheritDoc} */
133     protected String getOutputDir()
134     {
135         return "identity/";
136     }
137 
138     /**
139      * The output files generated by this class are text files,
140      * independent of the kind of module being tested.
141      *
142      * @return The String "txt".
143      */
144     protected String outputExtension()
145     {
146         return "txt";
147     }
148 
149     /**
150      * Set to true if the identity transformation should actually be asserted,
151      * by default only the expected and actual results are written to a file, but not compared.
152      * This should be called during setUp().
153      *
154      * @param doAssert True to actually execute the test.
155      */
156     protected void assertIdentity( boolean doAssert )
157     {
158         this.assertIdentity = doAssert;
159     }
160 
161     /**
162      * @return the expected identity string
163      */
164     protected String getExpected()
165     {
166         return expected;
167     }
168 }