1 package org.apache.velocity.test;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.io.BufferedWriter;
23 import java.io.FileOutputStream;
24 import java.io.OutputStreamWriter;
25 import java.io.Writer;
26
27 import junit.framework.Test;
28 import junit.framework.TestSuite;
29
30 import org.apache.velocity.Template;
31 import org.apache.velocity.VelocityContext;
32 import org.apache.velocity.app.VelocityEngine;
33 import org.apache.velocity.runtime.RuntimeConstants;
34 import org.apache.velocity.test.misc.TestLogChute;
35
36
37
38
39
40
41
42
43 public class MultiLoaderTestCase extends BaseTestCase
44 {
45
46
47
48 private static final String TMPL_FILE_EXT = "vm";
49
50
51
52
53 private static final String CMP_FILE_EXT = "cmp";
54
55
56
57
58 private static final String RESULT_FILE_EXT = "res";
59
60
61
62
63 private static final String RESULTS_DIR = TEST_RESULT_DIR + "/multiloader";
64
65
66
67
68
69 private final static String FILE_RESOURCE_LOADER_PATH = TEST_COMPARE_DIR + "/multiloader";
70
71
72
73
74 private static final String COMPARE_DIR = TEST_COMPARE_DIR + "/multiloader/compare";
75
76 VelocityEngine engine;
77
78
79
80
81 public MultiLoaderTestCase(String name)
82 {
83 super(name);
84 }
85
86 public void setUp()
87 throws Exception
88 {
89 engine = new VelocityEngine();
90
91 assureResultsDirectoryExists(RESULTS_DIR);
92
93
94
95
96
97 engine.setProperty(RuntimeConstants.RESOURCE_LOADER, "file");
98
99 engine.setProperty(
100 RuntimeConstants.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);
101
102 engine.addProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
103
104 engine.addProperty(RuntimeConstants.RESOURCE_LOADER, "jar");
105
106
107
108
109
110 engine.setProperty(
111 "classpath." + RuntimeConstants.RESOURCE_LOADER + ".class",
112 "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
113
114 engine.setProperty(
115 "classpath." + RuntimeConstants.RESOURCE_LOADER + ".cache", "false");
116
117 engine.setProperty(
118 "classpath." + RuntimeConstants.RESOURCE_LOADER + ".modificationCheckInterval",
119 "2");
120
121
122
123
124
125 engine.setProperty(
126 "jar." + RuntimeConstants.RESOURCE_LOADER + ".class",
127 "org.apache.velocity.runtime.resource.loader.JarResourceLoader");
128
129 engine.setProperty( "jar." + RuntimeConstants.RESOURCE_LOADER + ".path",
130 "jar:file:" + FILE_RESOURCE_LOADER_PATH + "/test2.jar" );
131
132
133 engine.setProperty(
134 RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, TestLogChute.class.getName());
135
136 engine.init();
137 }
138
139 public static Test suite ()
140 {
141 return new TestSuite(MultiLoaderTestCase.class);
142 }
143
144
145
146
147 public void testMultiLoader ()
148 throws Exception
149 {
150
151
152
153 assureResultsDirectoryExists(RESULTS_DIR);
154
155
156
157
158 Template template1 = engine.getTemplate(
159 getFileName(null, "path1", TMPL_FILE_EXT));
160
161
162
163
164 Template template2 = engine.getTemplate("template/test1." + TMPL_FILE_EXT);
165
166
167
168
169 Template template3 = engine.getTemplate("template/test2." + TMPL_FILE_EXT);
170
171
172
173
174
175 FileOutputStream fos1 =
176 new FileOutputStream (
177 getFileName(RESULTS_DIR, "path1", RESULT_FILE_EXT));
178
179 FileOutputStream fos2 =
180 new FileOutputStream (
181 getFileName(RESULTS_DIR, "test2", RESULT_FILE_EXT));
182
183 FileOutputStream fos3 =
184 new FileOutputStream (
185 getFileName(RESULTS_DIR, "test3", RESULT_FILE_EXT));
186
187 Writer writer1 = new BufferedWriter(new OutputStreamWriter(fos1));
188 Writer writer2 = new BufferedWriter(new OutputStreamWriter(fos2));
189 Writer writer3 = new BufferedWriter(new OutputStreamWriter(fos3));
190
191
192
193
194
195 VelocityContext context = new VelocityContext();
196
197 template1.merge(context, writer1);
198 writer1.flush();
199 writer1.close();
200
201 template2.merge(context, writer2);
202 writer2.flush();
203 writer2.close();
204
205 template3.merge(context, writer3);
206 writer3.flush();
207 writer3.close();
208
209 if (!isMatch(RESULTS_DIR,COMPARE_DIR,"path1",RESULT_FILE_EXT,CMP_FILE_EXT))
210 {
211 fail("Output incorrect for FileResourceLoader test.");
212 }
213
214 if (!isMatch(RESULTS_DIR,COMPARE_DIR,"test2",RESULT_FILE_EXT,CMP_FILE_EXT) )
215 {
216 fail("Output incorrect for ClasspathResourceLoader test.");
217 }
218
219 if( !isMatch(RESULTS_DIR,COMPARE_DIR,"test3",RESULT_FILE_EXT,CMP_FILE_EXT))
220 {
221 fail("Output incorrect for JarResourceLoader test.");
222 }
223 }
224 }