KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > velocity > test > TemplateTestCase


1 package org.apache.velocity.test;
2
3 /*
4  * Copyright 2001,2004 The Apache Software Foundation.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18
19 import java.io.BufferedWriter JavaDoc;
20 import java.io.File JavaDoc;
21 import java.io.FileOutputStream JavaDoc;
22 import java.io.OutputStreamWriter JavaDoc;
23 import java.io.Writer JavaDoc;
24
25 import java.util.ArrayList JavaDoc;
26 import java.util.Hashtable JavaDoc;
27 import java.util.HashMap JavaDoc;
28 import java.util.Vector JavaDoc;
29
30 import org.apache.velocity.VelocityContext;
31 import org.apache.velocity.Template;
32 import org.apache.velocity.runtime.RuntimeSingleton;
33 import org.apache.velocity.test.provider.TestProvider;
34 import org.apache.velocity.test.provider.BoolObj;
35 import org.apache.velocity.util.StringUtils;
36
37 import org.apache.velocity.app.FieldMethodizer;
38
39 import junit.framework.TestCase;
40
41 /**
42  * Easily add test cases which evaluate templates and check their output.
43  *
44  * NOTE:
45  * This class DOES NOT extend RuntimeTestCase because the TemplateTestSuite
46  * already initializes the Velocity runtime and adds the template
47  * test cases. Having this class extend RuntimeTestCase causes the
48  * Runtime to be initialized twice which is not good. I only discovered
49  * this after a couple hours of wondering why all the properties
50  * being setup were ending up as Vectors. At first I thought it
51  * was a problem with the Configuration class, but the Runtime
52  * was being initialized twice: so the first time the property
53  * is seen it's stored as a String, the second time it's seen
54  * the Configuration class makes a Vector with both Strings.
55  * As a result all the getBoolean(property) calls were failing because
56  * the Configurations class was trying to create a Boolean from
57  * a Vector which doesn't really work that well. I have learned
58  * my lesson and now have to add some code to make sure the
59  * Runtime isn't initialized more then once :-)
60  *
61  * @author <a HREF="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
62  * @author <a HREF="mailto:jvanzyl@apache.org">Jason van Zyl</a>
63  * @author <a HREF="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
64  * @author <a HREF="mailto:jon@latchkey.com">Jon S. Stevens</a>
65  * @version $Id: TemplateTestCase.java,v 1.34.8.1 2004/03/03 23:23:04 geirm Exp $
66  */

67 public class TemplateTestCase extends BaseTestCase implements TemplateTestBase
68 {
69     /**
70      * The base file name of the template and comparison file (i.e. array for
71      * array.vm and array.cmp).
72      */

73     protected String JavaDoc baseFileName;
74
75     private TestProvider provider;
76     private ArrayList JavaDoc al;
77     private Hashtable JavaDoc h;
78     private VelocityContext context;
79     private VelocityContext context1;
80     private VelocityContext context2;
81     private Vector JavaDoc vec;
82
83     /**
84      * Creates a new instance.
85      *
86      * @param baseFileName The base name of the template and comparison file to
87      * use (i.e. array for array.vm and array.cmp).
88      */

89     public TemplateTestCase (String JavaDoc baseFileName)
90     {
91         super(getTestCaseName(baseFileName));
92         this.baseFileName = baseFileName;
93     }
94
95     public static junit.framework.Test suite()
96     {
97         return new TemplateTestSuite();
98     }
99
100     /**
101      * Sets up the test.
102      */

103     protected void setUp ()
104     {
105         provider = new TestProvider();
106         al = provider.getCustomers();
107         h = new Hashtable JavaDoc();
108
109         h.put("Bar", "this is from a hashtable!");
110         h.put("Foo", "this is from a hashtable too!");
111
112         /*
113          * lets set up a vector of objects to test late introspection. See ASTMethod.java
114          */

115
116         vec = new Vector JavaDoc();
117
118         vec.addElement(new String JavaDoc("string1"));
119         vec.addElement(new String JavaDoc("string2"));
120
121         /*
122          * set up 3 chained contexts, and add our data
123          * throught the 3 of them.
124          */

125
126         context2 = new VelocityContext();
127         context1 = new VelocityContext( context2 );
128         context = new VelocityContext( context1 );
129
130         context.put("provider", provider);
131         context1.put("name", "jason");
132         context2.put("providers", provider.getCustomers2());
133         context.put("list", al);
134         context1.put("hashtable", h);
135         context2.put("hashmap", new HashMap JavaDoc());
136         context2.put("search", provider.getSearch());
137         context.put("relatedSearches", provider.getRelSearches());
138         context1.put("searchResults", provider.getRelSearches());
139         context2.put("stringarray", provider.getArray());
140         context.put("vector", vec );
141         context.put("mystring", new String JavaDoc());
142         context.put("runtime", new FieldMethodizer( "org.apache.velocity.runtime.RuntimeSingleton" ));
143         context.put("fmprov", new FieldMethodizer( provider ));
144         context.put("Floog", "floogie woogie");
145         context.put("boolobj", new BoolObj() );
146
147         /*
148          * we want to make sure we test all types of iterative objects
149          * in #foreach()
150          */

151
152         Object JavaDoc[] oarr = { "a","b","c","d" } ;
153        int intarr[] = { 10, 20, 30, 40, 50 };
154
155         context.put( "collection", vec );
156         context2.put("iterator", vec.iterator());
157         context1.put("map", h );
158         context.put("obarr", oarr );
159         context.put("enumerator", vec.elements());
160         context.put("intarr", intarr );
161     }
162
163     /**
164      * Runs the test.
165      */

166     public void runTest ()
167     {
168         try
169         {
170             Template template = RuntimeSingleton.getTemplate
171                 (getFileName(null, baseFileName, TMPL_FILE_EXT));
172             
173             assureResultsDirectoryExists(RESULT_DIR);
174
175             /* get the file to write to */
176             FileOutputStream JavaDoc fos =
177                 new FileOutputStream JavaDoc (getFileName(
178                     RESULT_DIR, baseFileName, RESULT_FILE_EXT));
179
180             Writer JavaDoc writer = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(fos));
181
182             /* process the template */
183             template.merge( context, writer);
184
185             /* close the file */
186             writer.flush();
187             writer.close();
188             
189             if (!isMatch(RESULT_DIR,COMPARE_DIR,baseFileName,
190                     RESULT_FILE_EXT,CMP_FILE_EXT))
191             {
192                 fail("Processed template did not match expected output");
193             }
194         }
195         catch (Exception JavaDoc e)
196         {
197             System.out.println("EXCEPTION : " + e );
198
199             fail(e.getMessage());
200         }
201     }
202 }
203
Popular Tags