View Javadoc
1   package org.apache.maven.doxia.sink;
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.File;
23  import java.io.FileWriter;
24  import java.io.InputStream;
25  import java.io.InputStreamReader;
26  import java.io.Reader;
27  import java.io.Writer;
28  
29  import org.apache.maven.doxia.parser.Parser;
30  import org.codehaus.plexus.PlexusTestCase;
31  
32  /**
33   * @author <a href="mailto:jason@maven.org">Jason van Zyl</a>
34   * @version $Id: AbstractSinkTestCase.java 747735 2009-02-25 10:43:09Z ltheussl $
35   * @since 1.0
36   */
37  public abstract class AbstractSinkTestCase
38      extends PlexusTestCase
39  {
40      private Writer testWriter;
41  
42      // ---------------------------------------------------------------------
43      // Test case
44      // ----------------------------------------------------------------------
45  
46      /**
47       * Parses the test apt document (obtained via {@link #getTestReader()}) with the Parser returned
48       * by {@link #createParser()} into the Sink returned by {@link #createSink()}.
49       *
50       * @throws java.lang.Exception if anything goes wrong.
51       */
52      public void testApt()
53          throws Exception
54      {
55          Parser parser = createParser();
56  
57          parser.parse( getTestReader(), createSink() );
58      }
59  
60      // ----------------------------------------------------------------------
61      // Abstract methods the individual SinkTests must provide
62      // ----------------------------------------------------------------------
63  
64      /**
65       * Return the default extension of files created by the test Sink.
66       *
67       * @return the extension of files created by the test Sink.
68       * @see #createSink()
69       */
70      protected abstract String outputExtension();
71  
72      /**
73       * Return a Parser for testing.
74       *
75       * @return a test Parser.
76       */
77      protected abstract Parser createParser();
78  
79      /**
80       * Return a Sink for testing.
81       *
82       * @return a test Sink.
83       * @throws java.lang.Exception if the Sink cannot be constructed.
84       */
85      protected abstract Sink createSink()
86          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()
101         throws Exception
102     {
103         if ( testWriter == null )
104         {
105             File outputDirectory = new File( getBasedirFile(), "target/output" );
106 
107             if ( !outputDirectory.exists() )
108             {
109                 outputDirectory.mkdirs();
110             }
111 
112             testWriter = new FileWriter( new File( outputDirectory, "test." + outputExtension() ) );
113         }
114 
115         return testWriter;
116     }
117 
118     /**
119      * Returns a Reader that gives access to a common test apt file.
120      *
121      * @return a Reader to access the test apt resource file.
122      * @throws java.lang.Exception if the Reader cannot be constructed.
123      */
124     protected Reader getTestReader()
125         throws Exception
126     {
127         InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream( "test.apt" );
128 
129         InputStreamReader reader = new InputStreamReader( is );
130 
131         return reader;
132     }
133 
134     // ----------------------------------------------------------------------
135     // Utility methods
136     // ----------------------------------------------------------------------
137 
138     /**
139      * Return the current base diretory as a File.
140      *
141      * @return the current base diretory as a File.
142      */
143     public File getBasedirFile()
144     {
145         return new File( getBasedir() );
146     }
147 }