01: package org.mockejb.interceptor.test;
02:
03: import java.lang.reflect.*;
04: import org.mockejb.interceptor.*;
05:
06: import junit.framework.TestCase;
07:
08: /**
09: *
10: * @author Alexander Ananiev
11: */
12: public class InterceptorInvokerTest extends TestCase {
13:
14: static final String TEST_CONTEXT = "testContext";
15:
16: private AspectSystem aspectSystem = AspectSystemFactory
17: .getAspectSystem();
18:
19: private Object target;
20: private Method ifaceMethod;
21: private Method targetMethod;
22:
23: private InterceptorInvoker invoker;
24:
25: protected void setUp() throws Exception {
26:
27: target = new TestImpl();
28: ifaceMethod = TestIface.class.getMethod("echo",
29: new Class[] { String.class });
30: targetMethod = TestImpl.class.getMethod("echo",
31: new Class[] { String.class });
32:
33: invoker = new InterceptorInvoker();
34: }
35:
36: public void testInvoke() throws Throwable {
37:
38: InterceptorInvoker invoker = new InterceptorInvoker();
39: Object retVal = invoker.invoke(null, ifaceMethod, target,
40: targetMethod, new Object[] { "test" });
41: assertEquals("test", retVal);
42:
43: TestInterceptor interceptor = new TestInterceptor();
44:
45: aspectSystem.getAspectList().clear();
46:
47: aspectSystem.add(new ClassPointcut(TestIface.class),
48: interceptor);
49: retVal = invoker.invoke(null, ifaceMethod, target,
50: targetMethod, new Object[] { "test" });
51: assertEquals("test", retVal);
52: assertTrue(interceptor.wasInvoked());
53:
54: // test target object pointcut
55: aspectSystem.getAspectList().clear();
56: interceptor = new TestInterceptor();
57: aspectSystem
58: .add(new ClassPointcut(TestImpl.class), interceptor);
59: retVal = invoker.invoke(null, ifaceMethod, target,
60: targetMethod, new Object[] { "test" });
61: assertEquals("test", retVal);
62: assertTrue(interceptor.wasInvoked());
63:
64: }
65:
66: public void testContext() throws Throwable {
67:
68: invoker.setContext(TEST_CONTEXT, "test");
69:
70: aspectSystem.getAspectList().clear();
71: TestInterceptor interceptor = new TestInterceptor();
72: aspectSystem.add(new ClassPointcut(TestIface.class),
73: interceptor);
74:
75: Object retVal = invoker.invoke(null, ifaceMethod, target,
76: targetMethod, new Object[] { "test" });
77:
78: assertEquals("test", interceptor.getContext());
79:
80: }
81:
82: }
|