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.StringWriter;
23 import java.io.Writer;
24 import org.apache.velocity.VelocityContext;
25 import org.apache.velocity.app.VelocityEngine;
26 import org.apache.velocity.app.event.EventCartridge;
27 import org.apache.velocity.app.event.MethodExceptionEventHandler;
28 import org.apache.velocity.app.event.ReferenceInsertionEventHandler;
29 import org.apache.velocity.context.Context;
30 import org.apache.velocity.exception.MethodInvocationException;
31 import org.apache.velocity.runtime.RuntimeConstants;
32 import org.apache.velocity.runtime.RuntimeServices;
33 import org.apache.velocity.util.ContextAware;
34 import org.apache.velocity.util.RuntimeServicesAware;
35 import org.apache.velocity.test.misc.TestLogChute;
36
37
38
39
40
41
42
43
44 public class EventHandlingTestCase extends BaseTestCase
45 {
46 private static String NO_REFERENCE_VALUE = "<no reference value>";
47 private static String REFERENCE_VALUE = "<reference value>";
48
49 public EventHandlingTestCase(String name)
50 {
51 super(name);
52 }
53
54 public void testManualEventHandlers()
55 throws Exception
56 {
57 TestEventCartridge te = new TestEventCartridge();
58
59
60
61
62
63
64
65 EventCartridge ec = new EventCartridge();
66 ec.addEventHandler(te);
67 ec.attachToContext(context);
68
69
70
71
72
73 doTestReferenceInsertionEventHandler1();
74 doTestReferenceInsertionEventHandler2();
75 doTestMethodExceptionEventHandler1();
76 doTestMethodExceptionEventHandler2();
77 }
78
79
80
81
82 public void testConfigurationEventHandlers()
83 throws Exception
84 {
85 engine.setProperty(RuntimeConstants.EVENTHANDLER_METHODEXCEPTION, TestEventCartridge.class.getName());
86 engine.setProperty(RuntimeConstants.EVENTHANDLER_REFERENCEINSERTION, TestEventCartridge.class.getName());
87
88 doTestReferenceInsertionEventHandler1();
89 doTestReferenceInsertionEventHandler2();
90 doTestMethodExceptionEventHandler1();
91 doTestMethodExceptionEventHandler2();
92 }
93
94
95
96
97 private void doTestReferenceInsertionEventHandler1()
98 throws Exception
99 {
100 VelocityContext outer = context;
101 context = new VelocityContext(context);
102 context.put("name", "Velocity");
103
104
105
106
107 String expected = REFERENCE_VALUE + REFERENCE_VALUE + REFERENCE_VALUE;
108 assertEvalEquals(expected, "$name$name$name");
109
110 context = outer;
111 }
112
113 private void doTestReferenceInsertionEventHandler2()
114 throws Exception
115 {
116 VelocityContext outer = context;
117 context = new VelocityContext(context);
118 context.put("name", "Velocity");
119
120
121
122
123
124 assertEvalEquals(NO_REFERENCE_VALUE, "$floobie");
125
126 context = outer;
127 }
128
129 private void doTestMethodExceptionEventHandler1()
130 throws Exception
131 {
132 VelocityContext outer = context;
133 context = new VelocityContext(context);
134
135
136
137
138
139
140
141
142
143
144
145 context.put("allow_exception",Boolean.TRUE);
146 context.put("this", this );
147
148 evaluate(" $this.throwException()");
149
150 context = outer;
151 }
152
153 private void doTestMethodExceptionEventHandler2()
154 throws Exception
155 {
156 VelocityContext outer = context;
157 context = new VelocityContext(context);
158 context.put("this", this );
159
160
161
162
163
164
165 assertEvalException("$this.throwException()", MethodInvocationException.class);
166
167 context = outer;
168 }
169
170
171
172
173
174 public void throwException()
175 throws Exception
176 {
177 throw new Exception("Hello from throwException()");
178 }
179
180
181
182 public static class TestEventCartridge
183 implements ReferenceInsertionEventHandler,
184 MethodExceptionEventHandler,
185 RuntimeServicesAware,ContextAware
186 {
187 private RuntimeServices rs;
188
189
190
191
192 public void setRuntimeServices( RuntimeServices rs )
193 {
194
195 if (this.rs == null)
196 this.rs = rs;
197
198 else
199 fail("initialize called more than once.");
200 }
201
202
203
204
205 public Object referenceInsert( String reference, Object value )
206 {
207
208 if (rs == null)
209 fail ("Event handler not initialized!");
210
211
212
213
214
215
216 String s = null;
217
218 if( value != null )
219 {
220 s = REFERENCE_VALUE;
221 }
222 else
223 {
224
225
226
227
228 if ( reference.equals("$floobie") )
229 {
230 s = NO_REFERENCE_VALUE;
231 }
232 }
233 return s;
234 }
235
236
237
238
239 public Object methodException( Class claz, String method, Exception e )
240 {
241
242 if (rs == null)
243 fail ("Event handler not initialized!");
244
245
246 if (context != null)
247 {
248 boolean exceptionSwitch = context.containsKey("allow_exception");
249
250 if( exceptionSwitch && method.equals("throwException"))
251 {
252 return "handler";
253 }
254 else
255 throw new RuntimeException(e);
256
257 } else
258
259 throw new RuntimeException(e);
260 }
261
262 Context context;
263
264
265 public void setContext(Context context)
266 {
267 this.context = context;
268 }
269 }
270 }