KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileOutputStream JavaDoc;
21 import java.io.OutputStreamWriter JavaDoc;
22 import java.io.Writer JavaDoc;
23
24 import java.util.Vector JavaDoc;
25
26 import org.apache.velocity.VelocityContext;
27
28 import org.apache.velocity.Template;
29 import org.apache.velocity.app.Velocity;
30 import org.apache.velocity.runtime.RuntimeSingleton;
31
32 /**
33  * Tests if we are context safe : can we switch objects in the context
34  * and re-merge the template safely.
35  *
36  * NOTE:
37  * This class should not extend RuntimeTestCase because this test
38  * is run from the VelocityTestSuite which in effect a runtime
39  * test suite and the test suite initializes the Runtime. Extending
40  * RuntimeTestCase causes the Runtime to be initialized twice.
41  *
42  * @author <a HREF="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
43  * @version $Id: ContextSafetyTestCase.java,v 1.10.4.1 2004/03/03 23:23:04 geirm Exp $
44  */

45 public class ContextSafetyTestCase extends BaseTestCase implements TemplateTestBase
46 {
47     public ContextSafetyTestCase()
48     {
49         super("ContextSafetyTestCase");
50         
51         try
52         {
53             Velocity.setProperty(
54                 Velocity.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);
55             
56              Velocity.init();
57         }
58         catch (Exception JavaDoc e)
59         {
60             System.err.println("Cannot setup ContextSafetyTestCase!");
61             e.printStackTrace();
62             System.exit(1);
63         }
64     }
65
66     public static junit.framework.Test suite()
67     {
68         return new ContextSafetyTestCase();
69     }
70
71     /**
72      * Runs the test.
73      */

74     public void runTest ()
75     {
76         /*
77          * make a Vector and String array because
78          * they are treated differently in Foreach()
79          */

80         Vector JavaDoc v = new Vector JavaDoc();
81         
82         v.addElement( new String JavaDoc("vector hello 1") );
83         v.addElement( new String JavaDoc("vector hello 2") );
84         v.addElement( new String JavaDoc("vector hello 3") );
85         
86         String JavaDoc strArray[] = new String JavaDoc[3];
87         
88         strArray[0] = "array hello 1";
89         strArray[1] = "array hello 2";
90         strArray[2] = "array hello 3";
91        
92         VelocityContext context = new VelocityContext();
93        
94         try
95         {
96             assureResultsDirectoryExists(RESULT_DIR);
97             
98             /*
99              * get the template and the output
100              */

101
102             Template template = RuntimeSingleton.getTemplate(
103                 getFileName(null, "context_safety", TMPL_FILE_EXT));
104
105             FileOutputStream JavaDoc fos1 =
106                 new FileOutputStream JavaDoc (
107                     getFileName(RESULT_DIR, "context_safety1", RESULT_FILE_EXT));
108
109             FileOutputStream JavaDoc fos2 =
110                 new FileOutputStream JavaDoc (
111                     getFileName(RESULT_DIR, "context_safety2", RESULT_FILE_EXT));
112
113             Writer JavaDoc writer1 = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(fos1));
114             Writer JavaDoc writer2 = new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(fos2));
115             
116             /*
117              * put the Vector into the context, and merge
118              */

119
120             context.put("vector", v);
121             template.merge(context, writer1);
122             writer1.flush();
123             writer1.close();
124             
125             /*
126              * now put the string array into the context, and merge
127              */

128
129             context.put("vector", strArray);
130             template.merge(context, writer2);
131             writer2.flush();
132             writer2.close();
133
134             if (!isMatch(RESULT_DIR,COMPARE_DIR,"context_safety1",
135                     RESULT_FILE_EXT,CMP_FILE_EXT) ||
136                 !isMatch(RESULT_DIR,COMPARE_DIR,"context_safety2",
137                     RESULT_FILE_EXT,CMP_FILE_EXT))
138             {
139                 fail("Output incorrect.");
140             }
141         }
142         catch (Exception JavaDoc e)
143         {
144             fail(e.getMessage());
145         }
146     }
147 }
148
Popular Tags