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
24 import junit.framework.Test;
25 import junit.framework.TestCase;
26 import junit.framework.TestSuite;
27
28 import org.apache.velocity.VelocityContext;
29 import org.apache.velocity.app.Velocity;
30 import org.apache.velocity.exception.MethodInvocationException;
31 import org.apache.velocity.test.misc.TestLogChute;
32
33
34
35
36
37
38
39 public class MethodInvocationExceptionTestCase extends TestCase
40 {
41 protected boolean DEBUG = false;
42
43
44
45
46
47 public MethodInvocationExceptionTestCase(String name)
48 {
49 super(name);
50 }
51
52 public void setUp()
53 throws Exception
54 {
55
56
57
58
59 Velocity.setProperty(
60 Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, TestLogChute.class.getName());
61
62 Velocity.init();
63 }
64
65 public static Test suite ()
66 {
67 return new TestSuite(MethodInvocationExceptionTestCase.class);
68 }
69
70 protected void log(String out)
71 {
72 Velocity.getLog().debug(out);
73 if (DEBUG)
74 {
75 System.out.println(out);
76 }
77 }
78
79
80
81
82
83
84
85
86
87 public void testNormalMethodInvocationException ()
88 throws Exception
89 {
90 String template = "$woogie.doException() boing!";
91
92 VelocityContext vc = new VelocityContext();
93
94 vc.put("woogie", this );
95
96 StringWriter w = new StringWriter();
97
98 try
99 {
100 Velocity.evaluate( vc, w, "test", template );
101 fail("No exception thrown");
102 }
103 catch( MethodInvocationException mie )
104 {
105 log("Caught MIE (good!) :" );
106 log(" reference = " + mie.getReferenceName() );
107 log(" method = " + mie.getMethodName() );
108
109 Throwable t = mie.getWrappedThrowable();
110 log(" throwable = " + t );
111
112 if( t instanceof Exception)
113 {
114 log(" exception = " + ( (Exception) t).getMessage() );
115 }
116 }
117 }
118
119
120 public void testGetterMethodInvocationException ()
121 throws Exception
122 {
123 VelocityContext vc = new VelocityContext();
124 vc.put("woogie", this );
125
126 StringWriter w = new StringWriter();
127
128
129
130
131
132
133 String template = "$woogie.foo boing!";
134
135 try
136 {
137 Velocity. evaluate( vc, w, "test", template );
138 fail("No exception thrown, second test.");
139 }
140 catch( MethodInvocationException mie )
141 {
142 log("Caught MIE (good!) :" );
143 log(" reference = " + mie.getReferenceName() );
144 log(" method = " + mie.getMethodName() );
145
146 Throwable t = mie.getWrappedThrowable();
147 log(" throwable = " + t );
148
149 if( t instanceof Exception)
150 {
151 log(" exception = " + ( (Exception) t).getMessage() );
152 }
153 }
154 }
155
156
157 public void testCapitalizedGetterMethodInvocationException ()
158 throws Exception
159 {
160 VelocityContext vc = new VelocityContext();
161 vc.put("woogie", this );
162
163 StringWriter w = new StringWriter();
164
165 String template = "$woogie.Foo boing!";
166
167 try
168 {
169 Velocity. evaluate( vc, w, "test", template );
170 fail("No exception thrown, third test.");
171 }
172 catch( MethodInvocationException mie )
173 {
174 log("Caught MIE (good!) :" );
175 log(" reference = " + mie.getReferenceName() );
176 log(" method = " + mie.getMethodName() );
177
178 Throwable t = mie.getWrappedThrowable();
179 log(" throwable = " + t );
180
181 if( t instanceof Exception)
182 {
183 log(" exception = " + ( (Exception) t).getMessage() );
184 }
185 }
186 }
187
188 public void testSetterMethodInvocationException ()
189 throws Exception
190 {
191 VelocityContext vc = new VelocityContext();
192 vc.put("woogie", this );
193
194 StringWriter w = new StringWriter();
195
196 String template = "#set($woogie.foo = 'lala') boing!";
197
198 try
199 {
200 Velocity. evaluate( vc, w, "test", template );
201 fail("No exception thrown, set test.");
202 }
203 catch( MethodInvocationException mie )
204 {
205 log("Caught MIE (good!) :" );
206 log(" reference = " + mie.getReferenceName() );
207 log(" method = " + mie.getMethodName() );
208
209 Throwable t = mie.getWrappedThrowable();
210 log(" throwable = " + t );
211
212 if( t instanceof Exception)
213 {
214 log(" exception = " + ( (Exception) t).getMessage() );
215 }
216 }
217 }
218
219
220
221
222
223
224
225
226 public void testMacroInvocationException ()
227 throws Exception
228 {
229 VelocityContext vc = new VelocityContext();
230 vc.put("woogie", this );
231
232 StringWriter w = new StringWriter();
233
234 String template = "#macro (macro1 $param) $param #end #macro1($woogie.getFoo())";
235
236 try
237 {
238 Velocity. evaluate( vc, w, "test", template );
239 fail("No exception thrown, macro invocation test.");
240 }
241 catch( MethodInvocationException mie )
242 {
243 log("Caught MIE (good!) :" );
244 log(" reference = " + mie.getReferenceName() );
245 log(" method = " + mie.getMethodName() );
246
247 Throwable t = mie.getWrappedThrowable();
248 log(" throwable = " + t );
249
250 if( t instanceof Exception)
251 {
252 log(" exception = " + ( (Exception) t).getMessage() );
253 }
254 }
255 catch( Exception e)
256 {
257 fail("Wrong exception thrown, test of exception within macro parameter");
258 }
259 }
260
261 public void doException()
262 throws Exception
263 {
264 throw new NullPointerException();
265 }
266
267 public void getFoo()
268 throws Exception
269 {
270 throw new Exception("Hello from getFoo()" );
271 }
272
273 public void setFoo( String foo )
274 throws Exception
275 {
276 throw new Exception("Hello from setFoo()");
277 }
278 }