KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > freemarker > testcase > AbstractTestCase


1 /*
2  * Copyright (c) 2003 The Visigoth Software Society. All rights
3  * reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * 2. Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in
14  * the documentation and/or other materials provided with the
15  * distribution.
16  *
17  * 3. The end-user documentation included with the redistribution, if
18  * any, must include the following acknowledgement:
19  * "This product includes software developed by the
20  * Visigoth Software Society (http://www.visigoths.org/)."
21  * Alternately, this acknowledgement may appear in the software itself,
22  * if and wherever such third-party acknowledgements normally appear.
23  *
24  * 4. Neither the name "FreeMarker", "Visigoth", nor any of the names of the
25  * project contributors may be used to endorse or promote products derived
26  * from this software without prior written permission. For written
27  * permission, please contact visigoths@visigoths.org.
28  *
29  * 5. Products derived from this software may not be called "FreeMarker" or "Visigoth"
30  * nor may "FreeMarker" or "Visigoth" appear in their names
31  * without prior written permission of the Visigoth Software Society.
32  *
33  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
34  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
35  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
36  * DISCLAIMED. IN NO EVENT SHALL THE VISIGOTH SOFTWARE SOCIETY OR
37  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
38  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
39  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
40  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
41  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
42  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
43  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
44  * SUCH DAMAGE.
45  * ====================================================================
46  *
47  * This software consists of voluntary contributions made by many
48  * individuals on behalf of the Visigoth Software Society. For more
49  * information on the Visigoth Software Society, please see
50  * http://www.visigoths.org/
51  */

52
53 package freemarker.testcase;
54
55 import java.io.BufferedReader JavaDoc;
56 import java.io.File JavaDoc;
57 import java.io.FileOutputStream JavaDoc;
58 import java.io.IOException JavaDoc;
59 import java.io.InputStream JavaDoc;
60 import java.io.InputStreamReader JavaDoc;
61 import java.io.OutputStreamWriter JavaDoc;
62 import java.io.Reader JavaDoc;
63 import java.io.StringReader JavaDoc;
64 import java.io.StringWriter JavaDoc;
65
66 import freemarker.template.Configuration;
67 import freemarker.template.SimpleHash;
68 import freemarker.template.Template;
69 import freemarker.template.TemplateException;
70 import freemarker.template.utility.ClassUtil;
71
72 /**
73  * <p>Abstract class used to implement test cases for FreeMarker. This
74  * class is an abstract subclass of the <code>TestCase</code> class
75  * provided by <a HREF="http://junit.sourceforge.net/"
76  * target="_top">JUnit</a>. All FreeMarker testcases are subclassed off
77  * this class.
78  *
79  * <p>This class offers functionality to its subclasses to ease testing where
80  * a template is read in, and then written out with a specific data model.
81  * Passing the test means that the resulting page matches a given reference
82  * text. This class also supports the creation of those reference texts.<P>
83  *
84  * <p>To write a new test by subclassing this class, you'll want to do the
85  * following:
86  *
87  * <ul>
88  *
89  * <li> Create a template which your test case will use as input, and store
90  * it in the <tt>templates</tt> directory located immediately below
91  * this directory. Say it is called <tt>test-foo.html</tt>.</li>
92  *
93  * <li> Create a subclass of <tt>AbstractTestCase</tt>, and in the
94  * <tt>setUp</tt> method, call <tt>setUpFile("test-foo.html")</tt>.
95  * This will initialize a couple of protected instance variables:
96  * <tt>referenceText</tt> will contain the contents of
97  * file "reference/test-foo.html" and
98  * <tt>filename</tt> will contain the string "test-foo.html".
99  *
100  * <p>You can also set up a data model, as per usual JUnit practice.</li>
101  *
102  * <li> You may optionally override the runTest method in your subclass.</li>
103  *
104  * <li> In addition, if there is a difference, the actual output produced
105  * by the test will be written out in the current directory (wherever
106  * the test was called from), in this case, as "test-foo.html".
107  * This makes it possible to examine the test result and see what
108  * was actually output. It also makes it possible to copy that
109  * into the reference/ directory.</li>
110  *
111  * </ul>
112  *
113  * @version $Id: AbstractTestCase.java,v 1.32 2004/11/28 12:58:34 ddekany Exp $
114  */

115 public abstract class AbstractTestCase extends junit.framework.TestCase {
116
117     protected Configuration config;
118     private static final int TEST_BUFFER_SIZE = 1024;
119         
120     static {
121         try {
122             Class JavaDoc log4jbc = ClassUtil.forName("org.apache.log4j.BasicConfigurator");
123             log4jbc.getMethod("configure", null).invoke(null, null);
124         }
125         catch(Exception JavaDoc e) {
126             throw new RuntimeException JavaDoc("Failed to initialize test case class:" + e);
127         }
128     }
129     
130     protected String JavaDoc referenceText;
131     protected String JavaDoc filename;
132     protected SimpleHash root = new SimpleHash();
133
134     /**
135      * Creates new AbstractTestCase, with a filename from which to populate
136      * reference text and template text.
137      */

138     public AbstractTestCase( String JavaDoc aTestName ) {
139         super( aTestName );
140         try {
141             config = new Configuration();
142             config.setClassForTemplateLoading(AbstractTestCase.class, "/freemarker/testcase/template");
143             config.setDefaultEncoding("UTF-8");
144         }
145         catch(Exception JavaDoc e) {
146             throw new RuntimeException JavaDoc("Failed to initialize test case intance:" + e);
147         }
148     }
149     
150     public void runTest() throws TemplateException, IOException JavaDoc {
151         StringWriter JavaDoc sw = new StringWriter JavaDoc();
152         Template template = config.getTemplate(filename);
153         template.process(root, sw);
154         showTestResults( referenceText, sw.toString() );
155     }
156
157     /**
158      * Sets up the reference and template files to be used for the test.
159      * This would normally be called from the setUp() method in the JUnit
160      * framework.
161      *
162      * @param aFilename the filename to be used for retrieving the reference
163      * text and the template
164      */

165     protected void setUpFiles( String JavaDoc aFilename ) {
166         try {
167             referenceText = getReferenceText( aFilename );
168         } catch( IOException JavaDoc e ) {
169             referenceText = "";
170         }
171         filename = aFilename;
172     }
173
174     /**
175      * Reads text from a file. The file is obtained relative to the
176      * implementing class.
177      *
178      * @param aFilename the filename to read from
179      */

180     protected String JavaDoc getTextFromFile( String JavaDoc aFilename ) throws IOException JavaDoc {
181         Class JavaDoc thisClass = this.getClass();
182         InputStream JavaDoc cStream;
183         Reader JavaDoc cReader;
184         StringBuffer JavaDoc cBuffer = new StringBuffer JavaDoc();
185         char[] aBuffer = new char[ TEST_BUFFER_SIZE ];
186         int nLength;
187
188         cStream = thisClass.getResourceAsStream( aFilename );
189
190         if( cStream == null ) {
191             throw new IOException JavaDoc( "Could not find stream " + aFilename );
192         }
193         cReader = new InputStreamReader JavaDoc( cStream, "UTF-8" );
194 // cReader = new InputStreamReader( cStream, "ISO-8859-1" );
195

196         nLength = cReader.read( aBuffer );
197
198         while( nLength > 0 ) {
199             cBuffer.append( aBuffer, 0, nLength );
200             nLength = cReader.read( aBuffer );
201         }
202         return cBuffer.toString();
203     }
204
205     /**
206      * Gets the reference text for the implementing test class.
207      */

208     protected String JavaDoc getReferenceText( String JavaDoc aFilename ) throws IOException JavaDoc {
209
210         return getTextFromFile( "reference/" + aFilename );
211     }
212
213     /**
214      * Gets the template text for the implementing test class.
215      */

216     protected String JavaDoc getTemplateText( String JavaDoc aFilename ) throws IOException JavaDoc {
217
218         return getTextFromFile( "template/" + aFilename );
219     }
220
221     /**
222      * Performs the test on the output text to indicate whether the text is
223      * identical to the reference text.
224      */

225     protected void isTextIdentical( String JavaDoc aReference, String JavaDoc aOutput ) throws TestCaseException {
226         BufferedReader JavaDoc r1 = new BufferedReader JavaDoc(new StringReader JavaDoc(aReference));
227         BufferedReader JavaDoc r2 = new BufferedReader JavaDoc(new StringReader JavaDoc(aOutput));
228         int line = 0;
229         try
230         {
231             for(;;)
232             {
233                 String JavaDoc l1 = r1.readLine();
234                 String JavaDoc l2 = r2.readLine();
235                 if(l1 == null)
236                 {
237                     if(l2 == null)
238                     {
239                         return;
240                     }
241                     throw new TestCaseException("Output text is " + (aOutput.length() - aReference.length()) + " characters longer than reference text.");
242                 }
243                 if(l2 == null)
244                 {
245                     throw new TestCaseException("Output text is " + (aReference.length() - aOutput.length()) + " characters shorter than reference text.");
246                 }
247                 ++line;
248                 if(!l1.equals(l2)) {
249                     int l = Math.min(l1.length(), l2.length());
250                     for(int i = 0; i < l; ++i) {
251                         if(l1.charAt(i) != l2.charAt(i)) {
252                             throw new TestCaseException( "Difference encountered at line " +
253                                 line + ", character " + (i + 1) + "." );
254                         }
255                     }
256                     throw new TestCaseException( "Difference encountered at line " +
257                         line + ", character " + l + "." );
258                 }
259             }
260         }
261         catch(IOException JavaDoc e)
262         {
263             // Cannot happen
264
throw new Error JavaDoc();
265         }
266     }
267
268     /**
269      * Verify that the output of a test is identical to the reference text.
270      * If they are identical, say nothing (in the JUnit tradition). If
271      * they differ, output the first line number and character where they
272      * differ.
273      *
274      * @param aReference the reference text
275      * @param aOutput the output from the test
276      */

277     protected void showTestResults( String JavaDoc aReference, String JavaDoc aOutput ) {
278
279         try {
280             isTextIdentical( referenceText, aOutput );
281         } catch( TestCaseException error ) {
282             try {
283                 writeText( filename, aOutput );
284             } catch( IOException JavaDoc writeException ) {
285                 fail( writeException.getMessage() );
286             }
287             assertTrue( error.getMessage(), false );
288         }
289     }
290
291     /**
292      * Writes text to a given filename. Useful when we want to set up the
293      * reference file for future tests to compare against.
294      */

295     protected void writeText( String JavaDoc aFilename, String JavaDoc aText ) throws IOException JavaDoc {
296         FileOutputStream JavaDoc cStream = new FileOutputStream JavaDoc( aFilename );
297         OutputStreamWriter JavaDoc cWriter = new OutputStreamWriter JavaDoc( cStream, "UTF-8" );
298
299         cWriter.write( aText );
300         cWriter.flush();
301         cWriter.close();
302         cStream.close();
303     }
304
305     /**
306      * Retrieve the root path of the FreeMarker distribution from a
307      * properties file. This is used to set the root of the FreeMarker
308      * cache, which in turn is used to find the example source code.
309      */

310     protected String JavaDoc getTestcasePath() {
311         java.net.URL JavaDoc url = getClass().getResource("AbstractTestCase.class");
312         return new File JavaDoc(url.getFile()).getParent();
313     }
314
315 }
316
Popular Tags