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.StringWriter;
26 import java.io.Writer;
27 import java.util.ArrayList;
28 import java.util.List;
29
30 import junit.framework.Test;
31 import junit.framework.TestSuite;
32
33 import org.apache.velocity.Template;
34 import org.apache.velocity.VelocityContext;
35 import org.apache.velocity.app.VelocityEngine;
36 import org.apache.velocity.app.event.EventCartridge;
37 import org.apache.velocity.app.event.implement.EscapeHtmlReference;
38 import org.apache.velocity.app.event.implement.EscapeJavaScriptReference;
39 import org.apache.velocity.app.event.implement.EscapeReference;
40 import org.apache.velocity.app.event.implement.EscapeSqlReference;
41 import org.apache.velocity.app.event.implement.EscapeXmlReference;
42 import org.apache.velocity.app.event.implement.InvalidReferenceInfo;
43 import org.apache.velocity.app.event.implement.ReportInvalidReferences;
44 import org.apache.velocity.context.Context;
45 import org.apache.velocity.runtime.RuntimeConstants;
46
47
48
49
50
51
52
53 public class BuiltInEventHandlerTestCase extends BaseTestCase {
54
55 protected boolean DEBUG = false;
56
57
58
59
60 private static final String TMPL_FILE_EXT = "vm";
61
62
63
64
65 private static final String CMP_FILE_EXT = "cmp";
66
67
68
69
70 private static final String RESULT_FILE_EXT = "res";
71
72
73
74
75
76 private final static String FILE_RESOURCE_LOADER_PATH = TEST_COMPARE_DIR + "/includeevent";
77
78
79
80
81 private static final String RESULTS_DIR = TEST_RESULT_DIR + "/includeevent";
82
83
84
85
86 private static final String COMPARE_DIR = TEST_COMPARE_DIR + "/includeevent/compare";
87
88
89
90
91 public BuiltInEventHandlerTestCase(String name)
92 {
93 super(name);
94 }
95
96 public void setUp()
97 {
98 assureResultsDirectoryExists(RESULTS_DIR);
99 }
100
101 public static Test suite()
102 {
103 return new TestSuite(BuiltInEventHandlerTestCase.class);
104 }
105
106 protected void log(String out)
107 {
108 if (DEBUG)
109 {
110 System.out.println (out);
111 }
112 }
113
114
115
116
117
118 public void testReportInvalidReferences1() throws Exception
119 {
120 VelocityEngine ve = new VelocityEngine();
121 ReportInvalidReferences reporter = new ReportInvalidReferences();
122 ve.init();
123
124 VelocityContext context = new VelocityContext();
125 EventCartridge ec = new EventCartridge();
126 ec.addEventHandler(reporter);
127 ec.attachToContext(context);
128
129 context.put("a1","test");
130 context.put("b1","test");
131 Writer writer = new StringWriter();
132
133 ve.evaluate(context,writer,"test","$a1 $c1 $a1.length() $a1.foobar()");
134
135 List errors = reporter.getInvalidReferences();
136 assertEquals(2,errors.size());
137 assertEquals("$c1",((InvalidReferenceInfo) errors.get(0)).getInvalidReference());
138 assertEquals("$a1.foobar()",((InvalidReferenceInfo) errors.get(1)).getInvalidReference());
139
140 log("Caught invalid references (local configuration).");
141 }
142
143 public void testReportInvalidReferences2() throws Exception
144 {
145 VelocityEngine ve = new VelocityEngine();
146 ve.setProperty("eventhandler.invalidreference.exception","true");
147 ReportInvalidReferences reporter = new ReportInvalidReferences();
148 ve.init();
149
150 VelocityContext context = new VelocityContext();
151 EventCartridge ec = new EventCartridge();
152 ec.addEventHandler(reporter);
153 ec.attachToContext(context);
154
155 context.put("a1","test");
156 context.put("b1","test");
157 Writer writer = new StringWriter();
158
159 ve.evaluate(context,writer,"test","$a1 no problem");
160
161 try {
162 ve.evaluate(context,writer,"test","$a1 $c1 $a1.length() $a1.foobar()");
163 fail ("Expected exception.");
164 } catch (RuntimeException E) {}
165
166
167 log("Caught invalid references (global configuration).");
168
169 }
170
171
172
173
174
175 public void testEscapeHtml() throws Exception
176 {
177 EscapeReference esc = new EscapeHtmlReference();
178 assertEquals("test string&another<b>bold</b>test",esc.referenceInsert("","test string&another<b>bold</b>test"));
179 assertEquals("<">",esc.referenceInsert("","<\">"));
180 assertEquals("test string",esc.referenceInsert("","test string"));
181
182 log("Correctly escaped HTML");
183
184 }
185
186
187
188
189
190 public void testEscapeXml() throws Exception
191 {
192 EscapeReference esc = new EscapeXmlReference();
193 assertEquals("test string&another<b>bold</b>test",esc.referenceInsert("","test string&another<b>bold</b>test"));
194 assertEquals("<">",esc.referenceInsert("","<\">"));
195 assertEquals("'",esc.referenceInsert("","'"));
196 assertEquals("test string",esc.referenceInsert("","test string"));
197
198 log("Correctly escaped XML");
199
200 }
201
202
203
204
205
206 public void testEscapeSql() throws Exception
207 {
208 EscapeReference esc = new EscapeSqlReference();
209 assertEquals("Jimmy''s Pizza",esc.referenceInsert("","Jimmy's Pizza"));
210 assertEquals("test string",esc.referenceInsert("","test string"));
211
212 log("Correctly escaped SQL");
213
214 }
215
216
217
218
219
220 public void testEscapeJavaScript() throws Exception
221 {
222 EscapeReference esc = new EscapeJavaScriptReference();
223 assertEquals("Jimmy\\'s Pizza",esc.referenceInsert("","Jimmy's Pizza"));
224 assertEquals("test string",esc.referenceInsert("","test string"));
225
226
227 log("Correctly escaped Javascript");
228 }
229
230
231
232
233
234 public void testEscapeReferenceMatchAll() throws Exception
235 {
236 VelocityEngine ve = new VelocityEngine();
237 ve.setProperty(RuntimeConstants.EVENTHANDLER_REFERENCEINSERTION, "org.apache.velocity.app.event.implement.EscapeHtmlReference");
238 ve.init();
239
240 Context context;
241 Writer writer;
242
243
244 context = new VelocityContext();
245 writer = new StringWriter();
246 context.put("bold","<b>");
247 ve.evaluate(context,writer,"test","$bold test & test");
248 assertEquals("<b> test & test",writer.toString());
249
250
251 context = new VelocityContext();
252 writer = new StringWriter();
253 context.put("bold","<b>");
254 ve.evaluate(context,writer,"test","$bold.substring(0,1)");
255 assertEquals("<",writer.toString());
256
257 log("Escape matched all references (global configuration)");
258
259 }
260
261
262
263
264
265 public void testEscapeReferenceMatch() throws Exception
266 {
267
268 VelocityEngine ve = new VelocityEngine();
269 ve.setProperty(RuntimeConstants.EVENTHANDLER_REFERENCEINSERTION, "org.apache.velocity.app.event.implement.EscapeHtmlReference,org.apache.velocity.app.event.implement.EscapeJavaScriptReference");
270 ve.setProperty("eventhandler.escape.javascript.match", "/.*_js.*/");
271 ve.init();
272
273 Writer writer;
274
275
276 writer = new StringWriter();
277 ve.evaluate(newEscapeContext(),writer,"test","$test1");
278 assertEquals("Jimmy's <b>pizza</b>",writer.toString());
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300 log("Escape selected references (global configuration)");
301
302
303
304 }
305
306 private Context newEscapeContext()
307 {
308 Context context = new VelocityContext();
309 context.put("test1","Jimmy's <b>pizza</b>");
310 context.put("test1_js","Jimmy's <b>pizza</b>");
311 context.put("test1_js_test","Jimmy's <b>pizza</b>");
312 return context;
313 }
314
315 public void testPrintExceptionHandler() throws Exception
316 {
317 VelocityEngine ve1 = new VelocityEngine();
318 ve1.setProperty(RuntimeConstants.EVENTHANDLER_METHODEXCEPTION, "org.apache.velocity.app.event.implement.PrintExceptions");
319 ve1.init();
320
321 VelocityEngine ve2 = new VelocityEngine();
322 ve2.setProperty(RuntimeConstants.EVENTHANDLER_METHODEXCEPTION, "org.apache.velocity.app.event.implement.PrintExceptions");
323 ve2.setProperty("eventhandler.methodexception.message","true");
324 ve2.init();
325
326 VelocityEngine ve3 = new VelocityEngine();
327 ve3.setProperty(RuntimeConstants.EVENTHANDLER_METHODEXCEPTION, "org.apache.velocity.app.event.implement.PrintExceptions");
328 ve3.setProperty("eventhandler.methodexception.stacktrace","true");
329 ve3.init();
330
331 Context context;
332 StringWriter writer;
333
334 context = new VelocityContext();
335 context.put("list",new ArrayList());
336
337
338 writer = new StringWriter();
339 ve1.evaluate(context,writer,"test","$list.get(0)");
340 assertTrue(writer.toString().indexOf("IndexOutOfBoundsException") != -1);
341 assertTrue(writer.toString().indexOf("Index: 0, Size: 0") == -1);
342 assertTrue(writer.toString().indexOf("ArrayList") == -1);
343
344
345 writer = new StringWriter();
346 ve2.evaluate(context,writer,"test","$list.get(0)");
347 assertTrue(writer.toString().indexOf("IndexOutOfBoundsException") != -1);
348 assertTrue(writer.toString().indexOf("Index: 0, Size: 0") != -1);
349 assertTrue(writer.toString().indexOf("ArrayList") == -1);
350
351
352 writer = new StringWriter();
353 ve3.evaluate(context,writer,"test","$list.get(0)");
354 assertTrue(writer.toString().indexOf("IndexOutOfBoundsException") != -1);
355 assertTrue(writer.toString().indexOf("ArrayList") != -1);
356
357 log("PrintException handler successful.");
358
359 }
360
361 public void testIncludeNotFound() throws Exception
362 {
363 VelocityEngine ve = new VelocityEngine();
364 ve.setProperty(RuntimeConstants.EVENTHANDLER_INCLUDE, "org.apache.velocity.app.event.implement.IncludeNotFound");
365 ve.addProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);
366 ve.init();
367
368 Template template;
369 FileOutputStream fos;
370 Writer fwriter;
371 Context context;
372
373 template = ve.getTemplate( getFileName(null, "test6", TMPL_FILE_EXT) );
374
375 fos = new FileOutputStream (
376 getFileName(RESULTS_DIR, "test6", RESULT_FILE_EXT));
377
378 fwriter = new BufferedWriter( new OutputStreamWriter(fos) );
379
380 context = new VelocityContext();
381 template.merge(context, fwriter);
382 fwriter.flush();
383 fwriter.close();
384
385 if (!isMatch(RESULTS_DIR, COMPARE_DIR, "test6", RESULT_FILE_EXT, CMP_FILE_EXT))
386 {
387 fail("Output incorrect.");
388 }
389
390 log("IncludeNotFound handler successful.");
391
392 }
393
394 public void testIncludeRelativePath() throws Exception
395 {
396 VelocityEngine ve = new VelocityEngine();
397 ve.setProperty(RuntimeConstants.EVENTHANDLER_INCLUDE, "org.apache.velocity.app.event.implement.IncludeRelativePath");
398 ve.addProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);
399 ve.init();
400
401 Template template;
402 FileOutputStream fos;
403 Writer fwriter;
404 Context context;
405
406 template = ve.getTemplate( getFileName(null, "subdir/test2", TMPL_FILE_EXT) );
407
408 fos = new FileOutputStream (
409 getFileName(RESULTS_DIR, "test2", RESULT_FILE_EXT));
410
411 fwriter = new BufferedWriter( new OutputStreamWriter(fos) );
412
413 context = new VelocityContext();
414 template.merge(context, fwriter);
415 fwriter.flush();
416 fwriter.close();
417
418 if (!isMatch(RESULTS_DIR, COMPARE_DIR, "test2", RESULT_FILE_EXT, CMP_FILE_EXT))
419 {
420 fail("Output incorrect.");
421 }
422
423 log("IncludeRelativePath handler successful.");
424
425 }
426 }