KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dynaop > ProxyTypeBuilderTest


1 package dynaop;
2
3 import java.io.Externalizable JavaDoc;
4 import java.io.Serializable JavaDoc;
5 import java.lang.reflect.Method JavaDoc;
6 import java.util.ArrayList JavaDoc;
7 import java.util.Arrays JavaDoc;
8 import java.util.Collection JavaDoc;
9 import java.util.Collections JavaDoc;
10 import java.util.HashSet JavaDoc;
11 import java.util.List JavaDoc;
12 import java.util.Map JavaDoc;
13 import java.util.Set JavaDoc;
14
15 import dynaop.Interceptor;
16 import dynaop.Pointcuts;
17 import dynaop.util.ArrayObject;
18 import dynaop.util.Classes;
19 import dynaop.util.Closure;
20 import junit.framework.TestCase;
21
22 /**
23  *
24  *
25  * @author Bob Lee (crazybob@crazybob.org)
26  */

27 public class ProxyTypeBuilderTest extends TestCase {
28
29     Object JavaDoc wrap(Object JavaDoc[] array) {
30         return Arrays.asList(array);
31     }
32     
33     public void testAddObjectClass() {
34         ProxyTypeBuilder builder = new ProxyTypeBuilder(Object JavaDoc.class);
35         builder.addInterface(Object JavaDoc.class);
36         builder.addInterfaces(new Class JavaDoc[] { Object JavaDoc.class });
37         assertEquals(0, builder.getInterfaces().size());
38     }
39     
40     public void testToString() {
41         ProxyTypeBuilder builder = new ProxyTypeBuilder(Object JavaDoc.class);
42
43         builder.toString();
44         builder.createProxyType().toString();
45
46         builder.addInterface(Foo.class);
47         builder.addInterface(List JavaDoc.class);
48         
49         builder.toString();
50         builder.createProxyType().toString();
51
52         builder.addInterceptorFactory(
53             new Aspects.SingletonInterceptorFactory(new A()));
54         
55         builder.toString();
56         builder.createProxyType().toString();
57
58         builder.toString();
59         builder.createProxyType().toString();
60     }
61     
62     public void testCreateProxyType() throws Throwable JavaDoc {
63         ProxyTypeBuilder builder = new ProxyTypeBuilder(Object JavaDoc.class);
64
65         builder.addInterface(Foo.class);
66         builder.addInterface(List JavaDoc.class);
67         
68         A a = new A();
69         
70         builder.addInterceptorFactory(
71                 new Aspects.SingletonInterceptorFactory(a));
72         
73         ProxyType proxyType = builder.createProxyType();
74         
75         assertEquals(wrap(new Class JavaDoc[] { Foo.class, List JavaDoc.class }),
76             wrap(proxyType.getInterfaces()));
77         
78         Method JavaDoc sizeMethod = List JavaDoc.class.getMethod("size", null);
79     }
80     
81     public void testCreation() {
82         ProxyTypeBuilder builder = new ProxyTypeBuilder(Object JavaDoc.class);
83         
84         Foo foo = new Foo() {
85             public void bar() {}
86         };
87         builder = new ProxyTypeBuilder(foo.getClass());
88         assertEquals(Collections.singletonList(Foo.class),
89                 builder.getInterfaces());
90         assertEquals(foo.getClass(),
91                 builder.getProxiedClass());
92     }
93     
94     public void testMethodClosure() throws Throwable JavaDoc {
95         final Set JavaDoc visited = new HashSet JavaDoc();
96
97         Foo foo = new Foo() {
98             public void bar() {}
99         };
100         ProxyTypeBuilder builder = new ProxyTypeBuilder(foo.getClass());
101
102         Closure closure =
103             new Closure() {
104                 public void execute(Object JavaDoc method) {
105                     visited.add(method);
106                 }
107             };
108         builder.execute(closure);
109         
110         Method JavaDoc barMethod = Foo.class.getMethod("bar", null);
111         
112         Set JavaDoc expected = new HashSet JavaDoc();
113         expected.add(barMethod);
114         expected.add(foo.getClass().getMethod("bar", null));
115         expected.addAll(Arrays.asList(Classes.OBJECT_METHODS));
116         
117         assertEquals(expected, visited);
118     }
119     
120     public void testMethodClosureExecutesOncePerMethod() {
121         final Set JavaDoc visited = new HashSet JavaDoc();
122
123         ProxyTypeBuilder builder = new ProxyTypeBuilder(Object JavaDoc.class);
124         builder.addInterface(Foo.class);
125         builder.addInterface(Bar.class);
126         
127         builder.execute(new Closure() {
128             public void execute(Object JavaDoc method) {
129                 if (visited.contains(method))
130                     fail();
131                 visited.add(method);
132             }
133         });
134     }
135
136     public void testIsEmpty() {
137         ProxyTypeBuilder builder = new ProxyTypeBuilder(Object JavaDoc.class);
138         assertTrue(builder.isEmpty());
139         builder.addInterceptorFactory(
140             new Aspects.SingletonInterceptorFactory(new A()));
141         assertFalse(builder.isEmpty());
142     }
143     
144     public void testAddInterfaces() {
145         List JavaDoc interfaces = new ArrayList JavaDoc();
146         interfaces.add(List JavaDoc.class);
147         interfaces.add(Set JavaDoc.class);
148         interfaces.add(Map JavaDoc.class);
149
150         ProxyTypeBuilder builder = new ProxyTypeBuilder(Object JavaDoc.class);
151
152         builder.addInterfaces(interfaces);
153
154         assertEquals(interfaces, builder.getInterfaces());
155
156         builder.addInterfaces(new Class JavaDoc[] { Serializable JavaDoc.class,
157             Externalizable JavaDoc.class });
158         builder.addInterface(Collection JavaDoc.class);
159
160         interfaces.add(Serializable JavaDoc.class);
161         interfaces.add(Externalizable JavaDoc.class);
162         interfaces.add(Collection JavaDoc.class);
163         
164         assertEquals(interfaces, builder.getInterfaces());
165     }
166     
167     public void SingletonInterceptorFactory() throws Throwable JavaDoc {
168         Method JavaDoc m1 = Classes.EQUALS_METHOD;
169         Method JavaDoc m2 = Classes.TOSTRING_METHOD;
170         Method JavaDoc m3 = Classes.HASHCODE_METHOD;
171         
172         ProxyTypeBuilder builder = new ProxyTypeBuilder(Object JavaDoc.class);
173         
174         A a = new A();
175         B b = new B();
176         C c = new C();
177         
178         builder.addInterceptorFactory(m1,
179             new Aspects.SingletonInterceptorFactory(a));
180         builder.addInterceptorFactory(m2,
181             new Aspects.SingletonInterceptorFactory(b));
182         
183         /**
184         assertEquals(Collections.singletonList(a),
185             builder.getInterceptors(m1));
186         assertEquals(Collections.singletonList(b),
187             builder.getInterceptors(m2));
188         assertNull(builder.getInterceptors(m3));
189         */

190         
191         builder.addInterceptorFactory(
192             new Aspects.SingletonInterceptorFactory(c));
193         
194         /*
195         assertEquals(Arrays.asList(new Object[] { a, c }),
196             builder.getInterceptors(m1));
197         assertEquals(Arrays.asList(new Object[] { b, c }),
198             builder.getInterceptors(m2));
199         assertEquals(Collections.singletonList(c),
200             builder.getInterceptors(m3));
201         */

202         
203         Method JavaDoc barMethod = Foo.class.getMethod("bar", null);
204         
205         builder.addInterface(Foo.class);
206         builder.addInterceptorFactory(Pointcuts.singleton(barMethod),
207                 new Aspects.SingletonInterceptorFactory(a));
208         
209         /*
210         assertEquals(Arrays.asList(new Object[] { a, c }),
211             builder.getInterceptors(m1));
212         assertEquals(Collections.singletonList(a),
213             builder.getInterceptors(barMethod));
214         */

215     }
216     
217     static class A implements Interceptor {
218         public Object JavaDoc intercept(Invocation invocation) throws Throwable JavaDoc {
219             return null;
220         }
221     }
222     
223     static class B implements Interceptor {
224         public Object JavaDoc intercept(Invocation invocation) throws Throwable JavaDoc {
225             return null;
226         }
227     }
228     
229     static class C implements Interceptor {
230         public Object JavaDoc intercept(Invocation invocation) throws Throwable JavaDoc {
231             return null;
232         }
233     }
234     
235     public interface Foo {
236         void bar();
237     }
238     
239     public interface Bar extends Foo {
240     }
241     
242     public static class BarImpl implements Bar {
243         public void bar() {}
244     }
245     
246     ArrayObject wrap(Object JavaDoc array) {
247         return new ArrayObject(array);
248     }
249 }
250
Popular Tags