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
29 import junit.framework.Test;
30 import junit.framework.TestSuite;
31
32 import org.apache.velocity.Template;
33 import org.apache.velocity.VelocityContext;
34 import org.apache.velocity.app.VelocityEngine;
35 import org.apache.velocity.exception.MethodInvocationException;
36 import org.apache.velocity.runtime.RuntimeConstants;
37 import org.apache.velocity.runtime.RuntimeServices;
38 import org.apache.velocity.runtime.log.LogChute;
39
40
41
42
43
44
45
46
47 public class FilteredEventHandlingTestCase extends BaseTestCase implements LogChute
48 {
49
50
51
52
53 private static final String TMPL_FILE_EXT = "vm";
54
55
56
57
58 private static final String CMP_FILE_EXT = "cmp";
59
60
61
62
63 private static final String RESULT_FILE_EXT = "res";
64
65
66
67
68
69 private final static String FILE_RESOURCE_LOADER_PATH = TEST_COMPARE_DIR + "/includeevent";
70
71
72
73
74 private static final String RESULTS_DIR = TEST_RESULT_DIR + "/includeevent";
75
76
77
78
79 private static final String COMPARE_DIR = TEST_COMPARE_DIR + "/includeevent/compare";
80
81
82 private String logString = null;
83
84
85
86
87 public FilteredEventHandlingTestCase(String name)
88 {
89 super(name);
90 }
91
92
93
94
95
96 public void init( RuntimeServices rs )
97 {
98
99 }
100
101 public static Test suite ()
102 {
103 return new TestSuite(FilteredEventHandlingTestCase.class);
104 }
105
106 public void testFilteredEventHandling() throws Exception
107 {
108 String handler1 = "org.apache.velocity.test.eventhandler.Handler1";
109 String handler2 = "org.apache.velocity.test.eventhandler.Handler2";
110 String sequence1 = handler1 + "," + handler2;
111 String sequence2 = handler2 + "," + handler1;
112
113 assureResultsDirectoryExists(RESULTS_DIR);
114
115
116
117
118 VelocityEngine ve = new VelocityEngine();
119 ve.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, this);
120 ve.setProperty(RuntimeConstants.EVENTHANDLER_METHODEXCEPTION, sequence1);
121 ve.setProperty(RuntimeConstants.EVENTHANDLER_NULLSET, sequence1);
122 ve.setProperty(RuntimeConstants.EVENTHANDLER_REFERENCEINSERTION, sequence1);
123 ve.setProperty(RuntimeConstants.EVENTHANDLER_INCLUDE, sequence1);
124 ve.addProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);
125 ve.init();
126
127 VelocityEngine ve2 = new VelocityEngine();
128 ve2.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM, this);
129 ve2.setProperty(RuntimeConstants.EVENTHANDLER_METHODEXCEPTION, sequence2);
130 ve2.setProperty(RuntimeConstants.EVENTHANDLER_NULLSET, sequence2);
131 ve2.setProperty(RuntimeConstants.EVENTHANDLER_REFERENCEINSERTION, sequence2);
132 ve2.setProperty(RuntimeConstants.EVENTHANDLER_INCLUDE, sequence2);
133 ve2.addProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, FILE_RESOURCE_LOADER_PATH);
134 ve2.init();
135
136 VelocityContext context;
137 StringWriter w;
138
139
140
141 context = new VelocityContext();
142 w = new StringWriter();
143 context.put("test","abc");
144 ve.evaluate( context, w, "test", "$test" );
145 if ( !w.toString().equals( "ABCABC" ))
146 {
147 fail( "Reference insertion test 1");
148 }
149
150 context = new VelocityContext();
151 w = new StringWriter();
152 context.put("test","abc");
153 ve2.evaluate( context, w, "test", "$test" );
154 if ( !w.toString().equals( "ABCabc" ))
155 {
156 fail( "Reference insertion test 2");
157 }
158
159
160
161
162 context = new VelocityContext();
163 w = new StringWriter();
164 context.put("test",new ArrayList());
165
166 try
167 {
168 ve.evaluate( context, w, "test", "$test.get(0)");
169 fail ( "Method exception event test 1" );
170 }
171 catch( MethodInvocationException mee )
172 {
173
174 }
175
176
177 context = new VelocityContext();
178 w = new StringWriter();
179 context.put("test",new ArrayList());
180
181 ve2.evaluate( context, w, "test", "$test.get(0)");
182
183
184
185 context = new VelocityContext();
186 w = new StringWriter();
187 logString = null;
188 ve.evaluate( context, w, "test", "#set($test1 = $test2)" );
189 if ( logString != null)
190 {
191 fail( "log null set test 1");
192 }
193
194
195 context = new VelocityContext();
196 w = new StringWriter();
197 logString = null;
198 ve2.evaluate( context, w, "test", "#set($test1 = $test2)" );
199 if ( logString != null)
200 {
201 fail( "log null set test 2");
202 }
203
204
205
206
207
208 Template template;
209 FileOutputStream fos;
210 Writer fwriter;
211
212 template = ve.getTemplate( getFileName(null, "test4", TMPL_FILE_EXT) );
213
214 fos = new FileOutputStream (
215 getFileName(RESULTS_DIR, "test4", RESULT_FILE_EXT));
216
217 fwriter = new BufferedWriter( new OutputStreamWriter(fos) );
218
219 context = new VelocityContext();
220 template.merge(context, fwriter);
221 fwriter.flush();
222 fwriter.close();
223
224 if (!isMatch(RESULTS_DIR, COMPARE_DIR, "test4", RESULT_FILE_EXT, CMP_FILE_EXT))
225 {
226 fail("Output incorrect.");
227 }
228
229
230 template = ve2.getTemplate( getFileName(null, "test5", TMPL_FILE_EXT) );
231
232 fos = new FileOutputStream (
233 getFileName(RESULTS_DIR, "test5", RESULT_FILE_EXT));
234
235 fwriter = new BufferedWriter( new OutputStreamWriter(fos) );
236
237 context = new VelocityContext();
238 template.merge(context, fwriter);
239 fwriter.flush();
240 fwriter.close();
241
242 if (!isMatch(RESULTS_DIR, COMPARE_DIR, "test5", RESULT_FILE_EXT, CMP_FILE_EXT))
243 {
244 fail("Output incorrect.");
245 }
246
247 }
248
249
250
251
252
253
254
255 public void log(int level, String message)
256 {
257 logString = message;
258 }
259
260 public void log(int level, String message, Throwable t)
261 {
262 logString = message;
263 }
264
265 public boolean isLevelEnabled(int level)
266 {
267 return true;
268 }
269
270 }