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.File;
24 import java.io.FileOutputStream;
25 import java.io.OutputStreamWriter;
26 import java.io.Writer;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import junit.framework.TestSuite;
31
32 import org.apache.velocity.Template;
33 import org.apache.velocity.VelocityContext;
34 import org.apache.velocity.app.Velocity;
35 import org.apache.velocity.app.VelocityEngine;
36 import org.apache.velocity.runtime.RuntimeConstants;
37 import org.apache.velocity.test.misc.TestLogChute;
38
39
40
41
42
43
44 public class VMLibraryTestCase extends BaseTestCase
45 {
46
47
48
49 private VelocityEngine ve1 = new VelocityEngine();
50
51
52
53
54 private VelocityEngine ve2 = new VelocityEngine();
55
56 private static final String RESULT_DIR = TEST_RESULT_DIR + "/macrolibs";
57
58 private static final String COMPARE_DIR = TEST_COMPARE_DIR + "/macrolibs/compare";
59
60 public VMLibraryTestCase(String name)
61 {
62 super(name);
63 }
64
65 public void setUp()
66 throws Exception
67 {
68
69
70
71 ve1.setProperty( Velocity.VM_PERM_INLINE_LOCAL, Boolean.TRUE);
72 ve1.setProperty("velocimacro.permissions.allow.inline.to.replace.global",
73 Boolean.FALSE);
74
75
76
77 ve1.setProperty("file.resource.loader.cache", Boolean.TRUE);
78
79 ve1.setProperty(
80 Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, TestLogChute.class.getName());
81
82 ve1.setProperty(RuntimeConstants.RESOURCE_LOADER, "file");
83 ve1.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
84 TEST_COMPARE_DIR + "/macrolibs");
85 ve1.init();
86
87
88
89
90 ve2.setProperty( Velocity.VM_PERM_INLINE_LOCAL, Boolean.FALSE);
91 ve2.setProperty("velocimacro.permissions.allow.inline.to.replace.global",
92 Boolean.TRUE);
93
94
95
96 ve2.setProperty("file.resource.loader.cache", Boolean.FALSE);
97
98 ve2.setProperty(
99 Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, TestLogChute.class.getName());
100
101 ve2.setProperty(RuntimeConstants.RESOURCE_LOADER, "file");
102 ve2.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH,
103 TEST_COMPARE_DIR + "/macrolibs");
104 ve2.init();
105 }
106
107 public static junit.framework.Test suite()
108 {
109 return new TestSuite(VMLibraryTestCase.class);
110 }
111
112
113
114
115 public void testVelociMacroLibWithLocalNamespace()
116 throws Exception
117 {
118 assureResultsDirectoryExists(RESULT_DIR);
119
120
121
122 File file = new File(getFileName(
123 RESULT_DIR, "vm_library_local", RESULT_FILE_EXT));
124 if (file.exists())
125 {
126 file.delete();
127 }
128
129
130
131
132 FileOutputStream fos = new FileOutputStream (getFileName(
133 RESULT_DIR, "vm_library_local", RESULT_FILE_EXT), true);
134
135 List templateList = new ArrayList();
136 VelocityContext context = new VelocityContext();
137 Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
138
139 templateList.add("vm_library1.vm");
140
141 Template template = ve1.getTemplate("vm_library_local.vm");
142 template.merge(context, writer, templateList);
143
144
145
146
147
148 templateList.remove(0);
149 templateList.add("vm_library2.vm");
150 template = ve1.getTemplate("vm_library_local.vm");
151 template.merge(context, writer, templateList);
152
153
154
155
156 Template t1 = ve1.getTemplate("vm_library_local.vm");
157 Template t2 = ve1.getTemplate("vm_library_local.vm");
158
159 assertEquals("Both templates refer to the same object", t1, t2);
160
161
162
163
164 template = ve1.getTemplate("vm_library_local.vm");
165 template.merge(context, writer);
166
167
168
169
170 writer.flush();
171 writer.close();
172
173 if (!isMatch(RESULT_DIR, COMPARE_DIR, "vm_library_local",
174 RESULT_FILE_EXT,CMP_FILE_EXT))
175 {
176 fail("Processed template did not match expected output");
177 }
178 }
179
180
181
182
183 public void testVelociMacroLibWithGlobalNamespace()
184 throws Exception
185 {
186 assureResultsDirectoryExists(RESULT_DIR);
187
188
189
190 File file = new File(getFileName(
191 RESULT_DIR, "vm_library_global", RESULT_FILE_EXT));
192 if (file.exists())
193 {
194 file.delete();
195 }
196
197
198
199
200 FileOutputStream fos = new FileOutputStream (getFileName(
201 RESULT_DIR, "vm_library_global", RESULT_FILE_EXT), true);
202
203 List templateList = new ArrayList();
204 VelocityContext context = new VelocityContext();
205 Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
206
207 templateList.add("vm_library1.vm");
208
209 Template template = ve1.getTemplate("vm_library_global.vm");
210 template.merge(context, writer, templateList);
211
212
213
214
215
216 templateList.remove(0);
217 templateList.add("vm_library2.vm");
218 template = ve1.getTemplate("vm_library_global.vm");
219 template.merge(context, writer, templateList);
220
221
222
223
224 Template t1 = ve2.getTemplate("vm_library_global.vm");
225 Template t2 = ve2.getTemplate("vm_library_global.vm");
226
227 assertNotSame("Defferent objects", t1, t2);
228
229
230
231
232 writer.flush();
233 writer.close();
234
235 if (!isMatch(RESULT_DIR, COMPARE_DIR, "vm_library_global",
236 RESULT_FILE_EXT,CMP_FILE_EXT))
237 {
238 fail("Processed template did not match expected output");
239 }
240 }
241
242
243
244
245 public void testVelociMacroLibWithDuplicateDefinitions()
246 throws Exception
247 {
248 assureResultsDirectoryExists(RESULT_DIR);
249
250
251
252 File file = new File(getFileName(
253 RESULT_DIR, "vm_library_duplicate", RESULT_FILE_EXT));
254 if (file.exists())
255 {
256 file.delete();
257 }
258
259
260
261
262 FileOutputStream fos = new FileOutputStream (getFileName(
263 RESULT_DIR, "vm_library_duplicate", RESULT_FILE_EXT), true);
264
265 List templateList = new ArrayList();
266 VelocityContext context = new VelocityContext();
267 Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
268
269 templateList.add("vm_library1.vm");
270 templateList.add("vm_library2.vm");
271
272 Template template = ve1.getTemplate("vm_library.vm");
273 template.merge(context, writer, templateList);
274
275
276
277
278 writer.flush();
279 writer.close();
280
281 if (!isMatch(RESULT_DIR, COMPARE_DIR, "vm_library_duplicate",
282 RESULT_FILE_EXT,CMP_FILE_EXT))
283 {
284 fail("Processed template did not match expected output");
285 }
286 }
287
288
289
290
291
292
293
294 public void testMacrosWithNoDefinition()
295 throws Exception
296 {
297 assureResultsDirectoryExists(RESULT_DIR);
298
299 FileOutputStream fos = new FileOutputStream (getFileName(
300 RESULT_DIR, "vm_library", RESULT_FILE_EXT));
301
302 VelocityContext context = new VelocityContext();
303 Writer writer = new BufferedWriter(new OutputStreamWriter(fos));
304
305 Template template = ve1.getTemplate("vm_library.vm");
306 template.merge(context, writer, null);
307
308
309
310
311 writer.flush();
312 writer.close();
313
314
315
316
317 if (!isMatch(RESULT_DIR, COMPARE_DIR, "vm_library",
318 RESULT_FILE_EXT,CMP_FILE_EXT))
319 {
320 fail("Processed template did not match expected output");
321 }
322 }
323
324
325 }