KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dynaop > InvocationTest


1 package dynaop;
2
3 import dynaop.util.Classes;
4 import junit.framework.TestCase;
5
6 /**
7  *
8  *
9  * @author Bob Lee (crazybob@crazybob.org)
10  */

11 public class InvocationTest extends TestCase {
12
13     public void testToString() {
14         InvocationImpl i = new InvocationImpl();
15
16         i.setMethod(Classes.EQUALS_METHOD);
17         i.setProxy(new MockProxy());
18         
19         i.toString();
20         
21         i.setArguments(new Object JavaDoc[1]);
22         i.setTarget(new Object JavaDoc());
23         i.setInterceptors(new Interceptor[0]);
24         
25         i.toString();
26     }
27     
28     public void testWithNoMixin() throws Throwable JavaDoc {
29         try {
30             InvocationImpl i = new InvocationImpl();
31         
32             i.setArguments(new Object JavaDoc[] { null, null });
33             i.setMethod(A.class.getMethod("a",
34                 new Class JavaDoc[] { String JavaDoc.class, Object JavaDoc.class }));
35
36             i.proceed();
37             
38             fail();
39         }
40         catch (NullPointerException JavaDoc e) {
41         }
42     }
43     
44     public void testWithInterceptors() throws Throwable JavaDoc {
45         TestInterceptor[] interceptors = new TestInterceptor[] {
46             new TestInterceptor(),
47             new TestInterceptor(),
48             new TestInterceptor()
49         };
50         
51         A target = new A();
52         String JavaDoc foo = "foo";
53         Object JavaDoc bar = new Object JavaDoc();
54         
55         InvocationImpl i = new InvocationImpl();
56         
57         i.setInterceptors(interceptors);
58         i.setTarget(target);
59         i.setArguments(new Object JavaDoc[] { foo, bar });
60         i.setMethod(A.class.getMethod("a",
61             new Class JavaDoc[] { String JavaDoc.class, Object JavaDoc.class }));
62         i.setProxy(new MockProxy());
63         
64         assertEquals("result", i.proceed());
65         
66         interceptors[0].assertCalled();
67         interceptors[1].assertCalled();
68         interceptors[2].assertCalled();
69         
70         target.assertCalled();
71         target.test(foo, bar);
72     }
73     
74     public void testMultipleInvocations() throws Throwable JavaDoc {
75         TestInterceptor[] interceptors = new TestInterceptor[] {
76             new TestInterceptor(),
77             new TestInterceptor(),
78             new TestInterceptor()
79         };
80         
81         A target = new A();
82         
83         InvocationImpl i = new InvocationImpl();
84         
85         i.setInterceptors(interceptors);
86         i.setTarget(target);
87         i.setArguments(new Object JavaDoc[] { "foo", "bar" });
88         i.setMethod(A.class.getMethod("a",
89             new Class JavaDoc[] { String JavaDoc.class, Object JavaDoc.class }));
90         i.setProxy(new MockProxy());
91         
92         i.proceed();
93         i.proceed();
94         i.proceed();
95         
96         for (int index = 0; index < interceptors.length; index++)
97             assertEquals(3, interceptors[index].calls);
98         assertEquals(3, target.calls);
99     }
100
101     public static class A {
102         
103         String JavaDoc foo;
104         Object JavaDoc bar;
105         boolean called;
106         int calls;
107         
108         public String JavaDoc a(String JavaDoc foo, Object JavaDoc bar) {
109             this.called = true;
110             this.foo = foo;
111             this.bar = bar;
112             this.calls++;
113             return "result";
114         }
115         
116         void test(String JavaDoc foo, Object JavaDoc bar) {
117             assertEquals(foo, this.foo);
118             assertEquals(bar, this.bar);
119         }
120
121         void assertCalled() {
122             assertTrue(this.called);
123         }
124     }
125     
126     
127     static class TestInterceptor implements Interceptor {
128         
129         boolean called;
130         int calls = 0;
131         
132         public Object JavaDoc intercept(Invocation i) throws Throwable JavaDoc {
133             this.called = true;
134             calls++;
135             return i.proceed();
136         }
137
138         void assertCalled() {
139             assertTrue(this.called);
140         }
141     }
142 }
143
Popular Tags