KickJava   Java API By Example, From Geeks To Geeks.

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


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.Properties 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 import org.apache.velocity.test.provider.TestProvider;
32 import org.apache.velocity.util.StringUtils;
33 import org.apache.velocity.runtime.VelocimacroFactory;
34
35 import junit.framework.TestCase;
36
37 /**
38  * Tests if the VM template-locality is working.
39  *
40  * @author <a HREF="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
41  * @author <a HREF="mailto:dlr@collab.net">Daniel Rall</a>
42  * @version $Id: InlineScopeVMTestCase.java,v 1.11.10.1 2004/03/03 23:23:04 geirm Exp $
43  */

44 public class InlineScopeVMTestCase extends BaseTestCase implements TemplateTestBase
45 {
46     /**
47      * The name of this test case.
48      */

49     private static final String JavaDoc TEST_CASE_NAME = "InlineScopeVMTestCase";
50
51     InlineScopeVMTestCase()
52     {
53         super(TEST_CASE_NAME);
54
55         try
56         {
57             /*
58              * do our properties locally, and just override the ones we want
59              * changed
60              */

61
62             Velocity.setProperty(
63                 Velocity.VM_PERM_ALLOW_INLINE_REPLACE_GLOBAL, "true");
64             
65             Velocity.setProperty(
66                 Velocity.VM_PERM_INLINE_LOCAL, "true");
67
68             Velocity.setProperty(
69                 Velocity.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);
70             
71             Velocity.init();
72         }
73         catch (Exception JavaDoc e)
74         {
75             System.err.println("Cannot setup " + TEST_CASE_NAME);
76             System.exit(1);
77         }
78     }
79
80     public static junit.framework.Test suite ()
81     {
82         return new InlineScopeVMTestCase();
83     }
84
85     /**
86      * Runs the test.
87      */

88     public void runTest ()
89     {
90         try
91         {
92             assureResultsDirectoryExists(RESULT_DIR);
93             
94             /*
95              * Get the template and the output. Do them backwards.
96              * vm_test2 uses a local VM and vm_test1 doesn't
97              */

98
99             Template template2 = RuntimeSingleton.getTemplate(
100                 getFileName(null, "vm_test2", TMPL_FILE_EXT));
101             
102             Template template1 = RuntimeSingleton.getTemplate(
103                 getFileName(null, "vm_test1", TMPL_FILE_EXT));
104            
105             FileOutputStream JavaDoc fos1 =
106                 new FileOutputStream JavaDoc (
107                     getFileName(RESULT_DIR, "vm_test1", RESULT_FILE_EXT));
108
109             FileOutputStream JavaDoc fos2 =
110                 new FileOutputStream JavaDoc (
111                     getFileName(RESULT_DIR, "vm_test2", 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 both
118              */

119
120             VelocityContext context = new VelocityContext();
121
122             template1.merge(context, writer1);
123             writer1.flush();
124             writer1.close();
125             
126             template2.merge(context, writer2);
127             writer2.flush();
128             writer2.close();
129
130             if (!isMatch(RESULT_DIR,COMPARE_DIR,"vm_test1",
131                     RESULT_FILE_EXT,CMP_FILE_EXT) ||
132                 !isMatch(RESULT_DIR,COMPARE_DIR,"vm_test2",
133                     RESULT_FILE_EXT,CMP_FILE_EXT))
134             {
135                 fail("Output incorrect.");
136             }
137         }
138         catch (Exception JavaDoc e)
139         {
140             fail(e.getMessage());
141         }
142     }
143 }
144
Popular Tags