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