1 package test.crispy;
2
3 import java.lang.reflect.Method;
4 import java.util.Properties;
5 import java.util.Vector;
6
7 import junit.framework.TestCase;
8 import net.sf.crispy.Interceptor;
9 import net.sf.crispy.InterceptorContext;
10 import net.sf.crispy.InterceptorHandler;
11 import net.sf.crispy.InvocationException;
12 import net.sf.crispy.Modifier;
13 import net.sf.crispy.impl.ModifierChain;
14 import net.sf.crispy.impl.StaticLocalObjectProxy;
15 import net.sf.crispy.interceptor.StopWatchInterceptor;
16 import net.sf.crispy.proxy.DynamicJdkProxy;
17 import net.sf.crispy.proxy.DynamicProxy;
18 import net.sf.crispy.proxy.DynamicProxyFactory;
19 import net.sf.crispy.proxy.InvocationHandler;
20 import net.sf.crispy.proxy.ProxyDecorator;
21 import net.sf.crispy.util.Invoker;
22 import test.crispy.example.interceptor.TestInterceptor1;
23 import test.crispy.example.interceptor.TestInterceptor2;
24 import test.crispy.example.modify.CalculatorModifierAfter;
25 import test.crispy.example.modify.CalculatorModifierBefore;
26 import test.crispy.example.service.Calculator;
27 import test.crispy.example.service.CalculatorImpl;
28 import test.crispy.example.service.EchoImpl;
29
30 public class InvocationHandlerTest extends TestCase {
31
32 public void testInterceptorCanNotModifyReturnValue() throws Exception {
33 InterceptorHandler lvInterceptorHandler = new InterceptorHandler();
34 lvInterceptorHandler.addInterceptor(new Interceptor() {
35 public void beforeNewInstance(Class pvInterfaceClass) { }
36 public void afterNewInstance(Class pvInterfaceClass, Object pvProxyObject) { }
37 public void onError(Throwable pvThrowable) { }
38 public void afterMethodInvocation(InterceptorContext pvInterceptorContext) {
39 pvInterceptorContext.setResult(new Integer(-2));
40 }
41
42 public void beforeMethodInvocation(InterceptorContext pvInterceptorContext) {
43 pvInterceptorContext.setResult(new Integer(-3));
44 }
45
46 });
47
48 Object lvResult = createInvocationHandlerAndInvoke(lvInterceptorHandler);
49 assertNotNull(lvResult);
50 Integer lvResultInteger = (Integer) lvResult;
51 assertEquals(lvResultInteger, new Integer(2));
52 }
53
54 public void testInterceptorCanNotModifyMethod() throws Exception {
55 InterceptorHandler lvInterceptorHandler = new InterceptorHandler();
56 lvInterceptorHandler.addInterceptor(new Interceptor() {
57 public void beforeNewInstance(Class pvInterfaceClass) { }
58 public void afterNewInstance(Class pvInterfaceClass, Object pvProxyObject) { }
59 public void onError(Throwable pvThrowable) { }
60 public void afterMethodInvocation(InterceptorContext pvInterceptorContext) {
61 Vector lvParams = new Vector(2);
62 lvParams.add(new Integer(1));
63 lvParams.add(new Integer(0));
64 Method lvMethod = null;
65 try {
66 lvMethod = Invoker.findMethod(CalculatorImpl.class, "subtract", lvParams);
67 } catch (Exception e) {
68 e.printStackTrace();
69 }
70 pvInterceptorContext.setMethod(lvMethod);
71 }
72
73 public void beforeMethodInvocation(InterceptorContext pvInterceptorContext) {
74 pvInterceptorContext.setResult(new Integer(-3));
75 }
76
77 });
78
79 Object lvResult = createInvocationHandlerAndInvoke(lvInterceptorHandler);
80 assertNotNull(lvResult);
81 Integer lvResultInteger = (Integer) lvResult;
82 assertEquals(lvResultInteger, new Integer(2));
83 }
84
85 public void testInterceptorCanNotModifyArgs() throws Exception {
86 InterceptorHandler lvInterceptorHandler = new InterceptorHandler();
87 lvInterceptorHandler.addInterceptor(new Interceptor() {
88 public void beforeNewInstance(Class pvInterfaceClass) { }
89 public void afterNewInstance(Class pvInterfaceClass, Object pvProxyObject) { }
90 public void onError(Throwable pvThrowable) { }
91 public void afterMethodInvocation(InterceptorContext pvInterceptorContext) {
92 pvInterceptorContext.setArgs(new Integer[]{ new Integer (1) });
93 }
94
95 public void beforeMethodInvocation(InterceptorContext pvInterceptorContext) {
96 pvInterceptorContext.setResult(new Integer(-3));
97 }
98
99 });
100
101 Object lvResult = createInvocationHandlerAndInvoke(lvInterceptorHandler);
102 assertNotNull(lvResult);
103 Integer lvResultInteger = (Integer) lvResult;
104 assertEquals(lvResultInteger, new Integer(2));
105 }
106
107 public void testCallOrder() throws Exception {
108 InterceptorHandler lvInterceptorHandler = new InterceptorHandler();
109 ModifierChain lvChain = new ModifierChain();
110 lvChain.addModifier(new CalculatorModifierBefore());
111 lvChain.addModifier(new CalculatorModifierAfter());
112 lvInterceptorHandler.setModifier(lvChain);
113
114
115 lvInterceptorHandler.addInterceptor(new Interceptor() {
116
117 public void beforeNewInstance(Class pvInterfaceClass) { }
118 public void afterNewInstance(Class pvInterfaceClass, Object pvProxyObject) { }
119 public void onError(Throwable pvThrowable) { }
120
121 public void beforeMethodInvocation(InterceptorContext pvInterceptorContext) {
122 Object lvArgs[] = pvInterceptorContext.getArgs();
123 Integer lvArgsInt[] = (Integer[]) lvArgs;
124 if (lvArgsInt[0] != CalculatorModifierBefore.NEW_ADD_VALUE) {
125 throw new InvocationException("Expected Value: " + CalculatorModifierBefore.NEW_ADD_VALUE
126 + " and the value is:" + lvArgsInt[0]);
127 }
128 }
129
130 public void afterMethodInvocation(InterceptorContext pvInterceptorContext) {
131 Integer lvResult = (Integer) pvInterceptorContext.getResult();
132 if (!(lvResult.equals(CalculatorModifierAfter.NEW_ADD_VALUE))) {
133 throw new InvocationException("Expected Value: " + CalculatorModifierAfter.NEW_ADD_VALUE
134 + " and the value is:" + lvResult);
135 }
136 }
137
138 });
139
140 Object lvResult = createInvocationHandlerAndInvoke(lvInterceptorHandler);
141 assertNotNull(lvResult);
142 }
143
144
145 public void testInterruptInvocationByInterceptor() throws Exception {
146 InterceptorHandler lvInterceptorHandler = new InterceptorHandler();
147 lvInterceptorHandler.addInterceptor(new Interceptor() {
148
149 public void beforeNewInstance(Class pvInterfaceClass) { }
150 public void afterNewInstance(Class pvInterfaceClass, Object pvProxyObject) { }
151 public void onError(Throwable pvThrowable) { }
152 public void afterMethodInvocation(InterceptorContext pvInterceptorContext) {}
153
154 public void beforeMethodInvocation(InterceptorContext pvInterceptorContext) {
155 pvInterceptorContext.setInterruptInvocation(true);
156 pvInterceptorContext.setResult(new Integer(99));
157 }
158
159
160 });
161
162 Object lvResult = createInvocationHandlerAndInvoke(lvInterceptorHandler);
163 assertNotNull(lvResult);
164 assertEquals(lvResult, new Integer(99));
165 }
166
167 public void testInterruptInvocationByModifier() throws Exception {
168 InterceptorHandler lvInterceptorHandler = new InterceptorHandler();
169 lvInterceptorHandler.setModifier(new Modifier() {
170
171 public InterceptorContext modifyBeforeInvocation(InterceptorContext pvInterceptorContext) {
172 pvInterceptorContext.setInterruptInvocation(true);
173 pvInterceptorContext.setResult(new Integer(99));
174 return pvInterceptorContext;
175 }
176
177 public InterceptorContext modifyAfterInvocation(InterceptorContext pvInterceptorContext) {
178 return pvInterceptorContext;
179 }
180
181 });
182
183 Object lvResult = createInvocationHandlerAndInvoke(lvInterceptorHandler);
184 assertNotNull(lvResult);
185 assertEquals(lvResult, new Integer(99));
186 }
187
188 public void testInterruptInvocationByModifierWithThrowException() throws Exception {
189 InterceptorHandler lvInterceptorHandler = new InterceptorHandler();
190 lvInterceptorHandler.setModifier(new Modifier() {
191
192 public InterceptorContext modifyBeforeInvocation(InterceptorContext pvInterceptorContext) {
193 pvInterceptorContext.setInterruptInvocation(true);
194 pvInterceptorContext.setResult(new InvocationException("This is a JUnit test Exception!"));
195 return pvInterceptorContext;
196 }
197
198 public InterceptorContext modifyAfterInvocation(InterceptorContext pvInterceptorContext) {
199 return pvInterceptorContext;
200 }
201
202 });
203
204 Object lvResult = createInvocationHandlerAndInvoke(lvInterceptorHandler);
205 assertNotNull(lvResult);
206 assertTrue(lvResult instanceof InvocationException);
207 }
208
209 public void testRemoveAllInterceptors() throws Exception {
210 InterceptorHandler lvInterceptorHandler = new InterceptorHandler();
211 lvInterceptorHandler.addInterceptor(new StopWatchInterceptor());
212 assertEquals(lvInterceptorHandler.getInterceptorSize(), 1);
213 lvInterceptorHandler.removeAllInterceptors();
214 assertEquals(lvInterceptorHandler.getInterceptorSize(), 0);
215 }
216
217
218
219
220 private Object createInvocationHandlerAndInvoke (InterceptorHandler pvInterceptorHandler) throws Exception {
221 ProxyDecorator lvProxyDecorator = new ProxyDecorator(new StaticLocalObjectProxy(), pvInterceptorHandler);
222 Properties lvProperties = new Properties();
223 lvProperties.put(Calculator.class.getName(), CalculatorImpl.class.getName());
224 lvProxyDecorator.setProperties(lvProperties);
225
226 Object lvProxyObj = lvProxyDecorator.newInstance(Calculator.class);
227 Object lvArgs[] = new Object [] {new Integer(1), new Integer(1)};
228 Vector lvParams = new Vector(2);
229 lvParams.add(lvArgs[0]);
230 lvParams.add(lvArgs[1]);
231 Method lvMethod = Invoker.findMethod(CalculatorImpl.class, "add", lvParams);
232 DynamicProxy lvDynamicProxy = DynamicProxyFactory.getDefaultDynamicProxy();
233 lvDynamicProxy.setProxyObject(lvProxyObj);
234
235 try {
236 Object lvResult = InvocationHandler.doInvoke(lvDynamicProxy, null, lvMethod, lvArgs, pvInterceptorHandler);
237 return lvResult;
238 } catch (Throwable e) {
239 return e;
240 }
241 }
242
243
244
245 public void testInvalidDoInvoke() throws Exception {
246 DynamicProxy lvProxy = new DynamicJdkProxy();
247 EchoImpl lvEcho = new EchoImpl();
248 lvProxy.setProxyClass(lvEcho.getClass());
249 lvProxy.setProxyObject(lvEcho);
250 String lvEchoString = "Hello Crispy";
251 try {
252 InvocationHandler.doInvoke(lvProxy, lvEcho,
253 lvEcho.getClass().getMethod("echo", new Class [] {String.class}),
254 new Object[] {lvEchoString}, null);
255 fail("NullPointerException is thrown. It was no InterceptorHandler set.");
256 } catch (SecurityException e) {
257 assertTrue(false);
258 } catch (NoSuchMethodException e) {
259 assertTrue(false);
260 } catch (Throwable e) {
261 assertTrue(true);
262 }
263 }
264
265 public void testInvalidDoInvoke2() throws Exception {
266 DynamicProxy lvProxy = new DynamicJdkProxy();
267 EchoImpl lvEcho = new EchoImpl();
268 lvProxy.setProxyClass(lvEcho.getClass());
269 lvProxy.setProxyObject(lvEcho);
270 String lvEchoString = "Hello Crispy";
271 String lvEchoStringAfter = null;
272 try {
273 lvEchoStringAfter = (String) InvocationHandler.doInvoke(lvProxy, lvEcho,
274 lvEcho.getClass().getMethod("echo", new Class [] {String.class}),
275 new Object[] {lvEchoString});
276 } catch (Throwable e) {
277 fail("No Exception thrown: " + e);
278 }
279 assertNotNull(lvEchoStringAfter);
280 assertEquals(lvEchoString, lvEchoStringAfter);
281 }
282
283 public void testInvokeWithError() throws Exception {
284 DynamicProxy lvProxy = new DynamicJdkProxy();
285 EchoImpl lvEcho = new EchoImpl();
286 lvProxy.setProxyClass(lvEcho.getClass());
287 lvProxy.setProxyObject(lvEcho);
288 try {
289 InvocationHandler.doInvoke(lvProxy, lvEcho,
290 lvEcho.getClass().getMethod("throwError", new Class [] {String.class}),
291 new Object[] { "Test-Error" });
292 fail("Must thrownan Error.");
293 } catch (Throwable e) {
294 assertNotNull(e);
295 assertTrue(e instanceof Error);
296 assertEquals("Test-Error", e.getMessage());
297 }
298
299 }
300
301 public void testInvalidDoInvokeWithElseObjectEqualsNull() throws Exception {
302 DynamicProxy lvProxy = new DynamicJdkProxy();
303 EchoImpl lvEcho = new EchoImpl();
304 lvProxy.setProxyClass(lvEcho.getClass());
305 lvProxy.setProxyObject(lvEcho);
306 String lvEchoString = "Hello Crispy";
307 String lvEchoStringAfter = null;
308 try {
309 lvEchoStringAfter = (String) InvocationHandler.doInvoke(lvProxy, lvEcho,
310 lvEcho.getClass().getMethod("echo", new Class [] {String.class}),
311 new Object[] {lvEchoString}, (Object) null);
312 } catch (Throwable e) {
313 fail("No Exception thrown: " + e);
314 }
315 assertNotNull(lvEchoStringAfter);
316 assertEquals(lvEchoString, lvEchoStringAfter);
317 }
318
319 public void testDoInvoke() throws Exception {
320 DynamicProxy lvProxy = new DynamicJdkProxy();
321 EchoImpl lvEcho = new EchoImpl();
322 lvProxy.setProxyClass(lvEcho.getClass());
323 lvProxy.setProxyObject(lvEcho);
324 String lvEchoString = "Hello Crispy";
325 Object lvResult = null;
326 try {
327 lvResult = InvocationHandler.doInvoke(lvProxy, lvEcho,
328 lvEcho.getClass().getMethod("echo", new Class [] {String.class}),
329 new Object[] {lvEchoString}, new InterceptorHandler());
330 } catch (SecurityException e) {
331 e.printStackTrace();
332 } catch (NoSuchMethodException e) {
333 e.printStackTrace();
334 } catch (Throwable e) {
335 e.printStackTrace();
336 }
337 assertEquals(lvEchoString, lvResult);
338 }
339
340 public void testOrderOfInterceptors() throws Exception {
341 InterceptorHandler lvHandler = new InterceptorHandler();
342 lvHandler.addInterceptor(new TestInterceptor1());
343 lvHandler.addInterceptor(new TestInterceptor2());
344
345 Object lvResult = null;
346 try {
347 lvResult = InvocationHandler.doInvoke(new DynamicJdkProxy(), new EchoImpl(),
348 EchoImpl.class.getMethod("echo", new Class [] {String.class}),
349 new Object[] {"TestEchoString"}, lvHandler);
350
351 } catch (Throwable e) {
352 e.printStackTrace();
353 assertTrue(false);
354 }
355
356 assertNotNull(lvResult);
357
358 assertEquals(TestInterceptor2.RESULT, lvResult);
359 }
360
361 public void testOrderOfInterceptors2() throws Exception {
362 InterceptorHandler lvHandler = new InterceptorHandler();
363 lvHandler.addInterceptor(new TestInterceptor2());
364 lvHandler.addInterceptor(new TestInterceptor1());
365
366 Object lvResult = null;
367 try {
368 lvResult = InvocationHandler.doInvoke(new DynamicJdkProxy(), new EchoImpl(),
369 EchoImpl.class.getMethod("echo", new Class [] {String.class}),
370 new Object[] {"TestEchoString"}, lvHandler);
371
372 } catch (Throwable e) {
373 e.printStackTrace();
374 assertTrue(false);
375 }
376
377 assertNotNull(lvResult);
378
379 assertEquals(TestInterceptor1.RESULT, lvResult);
380 }
381
382 public void testOrderOfInterceptors3() throws Exception {
383 InterceptorHandler lvHandler = new InterceptorHandler();
384 lvHandler.addInterceptor(new TestInterceptor2(false));
385 lvHandler.addInterceptor(new TestInterceptor1());
386
387 Object lvResult = null;
388 try {
389 lvResult = InvocationHandler.doInvoke(new DynamicJdkProxy(), new EchoImpl(),
390 EchoImpl.class.getMethod("echo", new Class [] {String.class}),
391 new Object[] {"TestEchoString"}, lvHandler);
392
393 } catch (Throwable e) {
394 e.printStackTrace();
395 assertTrue(false);
396 }
397
398 assertNotNull(lvResult);
399
400 assertEquals(TestInterceptor1.RESULT, lvResult);
401 }
402
403 public void testOrderOfInterceptors4() throws Exception {
404 InterceptorHandler lvHandler = new InterceptorHandler();
405 lvHandler.addInterceptor(new TestInterceptor2());
406 lvHandler.addInterceptor(new TestInterceptor1(false));
407
408 Object lvResult = null;
409 try {
410 lvResult = InvocationHandler.doInvoke(new DynamicJdkProxy(), new EchoImpl(),
411 EchoImpl.class.getMethod("echo", new Class [] {String.class}),
412 new Object[] {"TestEchoString"}, lvHandler);
413
414 } catch (Throwable e) {
415 e.printStackTrace();
416 assertTrue(false);
417 }
418
419 assertNotNull(lvResult);
420
421 assertEquals(TestInterceptor1.RESULT, lvResult);
422 }
423
424 public void testOrderOfInterceptors5() throws Exception {
425 InterceptorHandler lvHandler = new InterceptorHandler();
426 lvHandler.addInterceptor(new TestInterceptor2(false));
427 lvHandler.addInterceptor(new TestInterceptor1(false));
428
429 Object lvResult = null;
430 String lvTestEchoString = "TestEchoString";
431 try {
432 DynamicProxy lvProxy = new DynamicJdkProxy();
433 EchoImpl lvEcho = new EchoImpl();
434 lvProxy.setProxyClass(lvEcho.getClass());
435 lvProxy.setProxyObject(lvEcho);
436
437 Method lvMethod = EchoImpl.class.getMethod("echo", new Class [] {String.class});
438 lvResult = InvocationHandler.doInvoke(lvProxy, new EchoImpl(), lvMethod, new Object[] {lvTestEchoString}, lvHandler);
439 } catch (Throwable e) {
440 e.printStackTrace();
441 assertTrue(false);
442 }
443
444 assertNotNull(lvResult);
445
446 assertEquals(lvTestEchoString, lvResult);
447 }
448
449
450
451
452 }