KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tirsen > nanning > AspectInstanceTest


1 /*
2  * Nanning Aspects
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package com.tirsen.nanning;
8
9 import junit.framework.TestCase;
10
11 import java.lang.reflect.Method JavaDoc;
12 import java.lang.reflect.Proxy JavaDoc;
13
14 /**
15  * TODO document AspectClassTest
16  *
17  * <!-- $Id: AspectInstanceTest.java,v 1.13 2003/06/09 17:40:42 tirsen Exp $ -->
18  *
19  * @author $Author: tirsen $
20  * @version $Revision: 1.13 $
21  */

22 public class AspectInstanceTest extends TestCase {
23     private Method JavaDoc callMethod;
24
25     protected void setUp() throws Exception JavaDoc {
26         super.setUp();
27
28         callMethod = Interface.class.getMethod("call", null);
29     }
30
31     public void testEmptyAspectInstance() {
32         AspectInstance instance = new AspectInstance();
33         Object JavaDoc proxy = instance.getProxy();
34         // test some of the methods from java.lang.Object
35
assertNotNull(proxy);
36         assertNotNull(proxy.toString());
37         assertSame(proxy, instance.getProxy());
38     }
39
40     public static interface Interface {
41         void call();
42     }
43
44     public static class Target implements Interface {
45         public void call() {
46         }
47     }
48
49     public void testAspectInstanceWithOneMixin() {
50         AspectInstance instance = new AspectInstance();
51         instance.addMixin(new MixinInstance(Interface.class, new Target()));
52         Object JavaDoc proxy = instance.getProxy();
53         assertTrue(proxy instanceof Interface);
54         Interface intf = (Interface) proxy;
55         intf.call();
56         assertNotNull(proxy.toString());
57     }
58
59     boolean wasCalled = false;
60
61     public void testInterceptor() throws NoSuchMethodException JavaDoc {
62         AspectInstance instance = new AspectInstance();
63
64         final Target target = new Target();
65         MixinInstance mixin = new MixinInstance(Interface.class, target);
66         instance.addMixin(mixin);
67         final Interface intf = (Interface) instance.getProxy();
68
69         mixin.addInterceptor(callMethod, new MethodInterceptor() {
70             public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc {
71                 wasCalled = true;
72                 assertEquals(callMethod, invocation.getMethod());
73                 assertSame(target, invocation.getTarget());
74                 assertSame(intf, invocation.getProxy());
75                 return invocation.invokeNext();
76             }
77         });
78
79         intf.call();
80         assertTrue(wasCalled);
81     }
82
83     public void testConstructors() {
84         AspectInstance aspectInstance = new AspectInstance();
85         assertNull(aspectInstance.getClassIdentifier());
86         assertNull(aspectInstance.getAspectFactory());
87
88         AspectFactory aspectFactory = new NullAspectFactory();
89         aspectInstance = new AspectInstance(aspectFactory, Interface.class);
90         assertSame(aspectFactory, aspectInstance.getAspectFactory());
91         assertSame(Interface.class, aspectInstance.getClassIdentifier());
92     }
93
94     public void testChangeTarget() {
95         AspectInstance instance = new AspectInstance();
96         MixinInstance mixin = new MixinInstance(Interface.class, new Target());
97         instance.addMixin(mixin);
98         Target target = new Target();
99         mixin.setTarget(target);
100         assertSame(target, mixin.getTarget());
101     }
102
103     public void testInvocationOnTarget() {
104         AspectInstance instance = new AspectInstance();
105         MixinInstance mixin = new MixinInstance(Interface.class, null);
106         instance.addMixin(mixin);
107
108         final Interface proxy = (Interface) instance.getProxy();
109         mixin.setTarget(new Target() {
110             public void call() {
111                 wasCalled = true;
112                 assertSame(proxy, Aspects.getThis());
113             }
114         });
115
116         assertFalse(wasCalled);
117         proxy.call();
118         assertTrue(wasCalled);
119     }
120
121     public void testChangeTargetDuringInterception() {
122         AspectInstance instance = new AspectInstance();
123         MixinInstance mixin = new MixinInstance(Interface.class, new Target());
124         instance.addMixin(mixin);
125
126         final Target target = new Target();
127         mixin.setTarget(target);
128         mixin.addInterceptor(new MethodInterceptor() {
129             public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc {
130                 invocation.setTarget(target);
131                 return invocation.invokeNext();
132             }
133         });
134         assertSame(target, mixin.getTarget());
135     }
136
137     public static interface EmptyInterface {
138     }
139
140     public void testClassIdentifierAsInterfaceInProxy() {
141         AspectInstance instance = new AspectInstance(EmptyInterface.class);
142         assertTrue(instance.getProxy() instanceof EmptyInterface);
143     }
144
145     public void testGetInterceptors() {
146         AspectInstance instance = new AspectInstance();
147         MixinInstance mixin = new MixinInstance(Interface.class, new Target());
148         MethodInterceptor interceptor = new NOPInterceptor();
149         mixin.addInterceptor(interceptor);
150         instance.addMixin(mixin);
151
152         assertTrue(instance.getAllInterceptors().contains(interceptor));
153
154         MixinInstance mixin2 = new MixinInstance(EmptyInterface.class, null);
155         MethodInterceptor interceptor2 = new NOPInterceptor();
156         mixin2.addInterceptor(interceptor2);
157         assertTrue(instance.getInterceptors(Interface.class).contains(interceptor));
158         assertFalse(instance.getInterceptors(Interface.class).contains(interceptor2));
159     }
160
161     public void testGetMixin() {
162         AspectInstance instance = new AspectInstance();
163         MixinInstance mixin = new MixinInstance(Interface.class, new Target());
164         instance.addMixin(mixin);
165         assertSame(mixin, instance.getMixinForInterface(Interface.class));
166         assertTrue(instance.hasMixinForInterface(Interface.class));
167     }
168
169     public static interface Base {
170     }
171
172     public static interface Sub extends Base {
173     }
174
175     public void testGetMixinWithInheritance() {
176         AspectInstance instance = new AspectInstance();
177         MixinInstance mixin = new MixinInstance(Sub.class, null);
178         instance.addMixin(mixin);
179         assertSame(mixin, instance.getMixinForInterface(Base.class));
180     }
181
182 // public void testEqualsAndHashCode() {
183
// AspectInstance instance = new AspectInstance();
184
// AspectInstance instance2 = new AspectInstance();
185
// assertEqualsAndHashCodeEquals(instance, instance2);
186
//
187
// NullAspectFactory aspectFactory = new NullAspectFactory();
188
// instance = new AspectInstance(aspectFactory, Interface.class);
189
// instance2 = new AspectInstance(aspectFactory, Interface.class);
190
// assertEqualsAndHashCodeEquals(instance, instance2);
191
//
192
// MixinInstance mixin = new MixinInstance(Interface.class, null);
193
// MixinInstance mixin2 = new MixinInstance(Interface.class, null);
194
// assertEqualsAndHashCodeEquals(mixin, mixin2);
195
// instance.addMixin(mixin);
196
// instance2.addMixin(mixin2);
197
// assertEqualsAndHashCodeEquals(instance, instance2);
198
// }
199

200 // private void assertEqualsAndHashCodeEquals(Object o1, Object o2) {
201
// assertEquals(o1, o2);
202
// assertEquals(o1.hashCode(), o2.hashCode());
203
// }
204

205
206     public static class BlahongaException extends RuntimeException JavaDoc {
207     }
208
209     public static class BlahongaError extends Error JavaDoc {
210     }
211
212     public void testThrowsCorrectExceptions() {
213         AspectInstance instance = new AspectInstance();
214         MixinInstance mixin = new MixinInstance(Interface.class, null);
215         instance.addMixin(mixin);
216
217         Interface proxy = (Interface) instance.getProxy();
218
219         mixin.setTarget(new Interface() {
220             public void call() {
221                 throw new BlahongaException();
222             }
223         });
224
225         try {
226             proxy.call();
227             fail();
228         } catch (BlahongaException shouldHappen) {
229         } catch (Exception JavaDoc e) {
230             fail();
231         }
232
233         mixin.setTarget(new Interface() {
234             public void call() {
235                 throw new BlahongaError();
236             }
237         });
238
239         try {
240             proxy.call();
241             fail();
242         } catch (BlahongaError shouldHappen) {
243         } catch (Exception JavaDoc e) {
244             fail();
245         }
246     }
247
248     public static class BlahongaCheckedException extends RuntimeException JavaDoc {
249     }
250
251     public static interface InterfaceWithException {
252         void call() throws BlahongaCheckedException;
253     }
254
255     public void testThrowCheckedException() {
256         AspectInstance instance = new AspectInstance();
257         MixinInstance mixin = new MixinInstance(InterfaceWithException.class, null);
258         instance.addMixin(mixin);
259
260         InterfaceWithException proxy = (InterfaceWithException) instance.getProxy();
261
262         mixin.setTarget(new InterfaceWithException() {
263             public void call() throws BlahongaCheckedException {
264                 throw new BlahongaCheckedException();
265             }
266         });
267
268         try {
269             proxy.call();
270             fail();
271         } catch (BlahongaCheckedException e) {
272         } catch (Throwable JavaDoc e) {
273             fail();
274         }
275     }
276
277
278     public void testGetRealClass() {
279         assertSame(Intf.class,
280                 Aspects.getRealClass(Proxy.getProxyClass(AspectInstanceTest.class.getClassLoader(), new Class JavaDoc[]{Intf.class})));
281     }
282
283     public void testAddInterceptor() {
284         MethodInterceptor interceptor = new MethodInterceptor() {
285                 public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc {
286                     return null;
287                 }
288             };
289         AspectInstance instance = new AspectInstance();
290         instance.addMixin(new MixinInstance(Interface.class, new Target()));
291         instance.addInterceptor(interceptor);
292         assertTrue(instance.getInterceptorsForMethod(callMethod).contains(interceptor));
293     }
294
295
296
297     public void testSideAspectAndAspectsOnProxy() throws IllegalAccessException JavaDoc, InstantiationException JavaDoc, NoSuchMethodException JavaDoc {
298         AspectInstance aspectInstance = new AspectInstance();
299         MixinInstance mixinInstance = new MixinInstance();
300         mixinInstance.setInterfaceClass(Intf.class);
301         mixinInstance.addInterceptor(new MockInterceptor());
302         mixinInstance.addInterceptor(new NullInterceptor());
303         mixinInstance.setTarget(new IntfImpl());
304         aspectInstance.addMixin(mixinInstance);
305         MixinInstance sideMixinInstance = new MixinInstance();
306         sideMixinInstance.setInterfaceClass(TestMixin.class);
307         sideMixinInstance.addInterceptor(new NullInterceptor());
308         sideMixinInstance.addInterceptor(new MockInterceptor());
309         sideMixinInstance.setTarget(new TestMixinImpl());
310         aspectInstance.addMixin(sideMixinInstance);
311
312         Object JavaDoc bigMomma = aspectInstance.getProxy();
313
314         assertEquals(4, Aspects.getInterceptors(bigMomma).size());
315
316         verifySideAspect(bigMomma);
317     }
318
319     public static void verifySideAspect(Object JavaDoc bigMomma) throws NoSuchMethodException JavaDoc {
320         IntfImpl target = (IntfImpl) Aspects.getTarget(bigMomma, Intf.class);
321         target.expectThis(bigMomma);
322         MockInterceptor classInterceptor = (MockInterceptor) (Aspects.getInterceptors(bigMomma, Intf.class.getMethods()[0])[0]);
323         classInterceptor.expectAtIndex(0);
324         classInterceptor.expectNumberOfInterceptors(2);
325         classInterceptor.expectCalledTimes(1);
326         classInterceptor.expectProxy(bigMomma);
327         classInterceptor.expectMethod(Intf.class.getMethod("call", null));
328         classInterceptor.expectTarget(target);
329
330         TestMixinImpl sideTarget = (TestMixinImpl) Aspects.getTarget(bigMomma, TestMixin.class);
331         MockInterceptor sideInterceptor = (MockInterceptor) (Aspects.getInterceptors(bigMomma, TestMixin.class.getMethods()[0])[1]);
332         sideInterceptor.expectAtIndex(1);
333         sideInterceptor.expectNumberOfInterceptors(2);
334         sideInterceptor.expectCalledTimes(1);
335         sideInterceptor.expectProxy(bigMomma);
336         sideInterceptor.expectMethod(TestMixin.class.getMethod("mixinCall", null));
337         sideInterceptor.expectTarget(sideTarget);
338
339         // this calls the class-target and the class-interceptor
340
((Intf) bigMomma).call();
341         // this calls the side-target, the class-interceptor and the side-interceptor
342
classInterceptor.expectTarget(null);
343         classInterceptor.expectMethod(null);
344         classInterceptor.expectNumberOfInterceptors(2);
345         ((TestMixin) bigMomma).mixinCall();
346
347         classInterceptor.verify();
348         target.verify();
349         sideInterceptor.verify();
350         sideTarget.verify();
351     }
352
353     public void testNoInterceptors() throws IllegalAccessException JavaDoc, InstantiationException JavaDoc {
354         AspectInstance aspectInstance = new AspectInstance();
355         MixinInstance mixinInstance = new MixinInstance();
356         mixinInstance.setInterfaceClass(Intf.class);
357         mixinInstance.setTarget(new IntfImpl());
358         aspectInstance.addMixin(mixinInstance);
359         Intf intf = (Intf) aspectInstance.getProxy();
360
361         IntfImpl impl = (IntfImpl) Aspects.getTarget(intf, Intf.class);
362
363         intf.call();
364         impl.verify();
365     }
366
367     public static class ImplWithEquals extends IntfImpl {
368         String JavaDoc state;
369
370         public ImplWithEquals(String JavaDoc state) {
371             this.state = state;
372         }
373
374         public boolean equals(Object JavaDoc o) {
375             if (this == o) return true;
376             if (!(o instanceof ImplWithEquals)) return false;
377
378             final ImplWithEquals implWithEquals = (ImplWithEquals) o;
379
380             if (!state.equals(implWithEquals.state)) return false;
381
382             return true;
383         }
384
385         public int hashCode() {
386             return state.hashCode();
387         }
388     }
389
390     private static class NOPInterceptor implements MethodInterceptor {
391         public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc {
392             return invocation.invokeNext();
393         }
394     }
395
396     private static class NullAspectFactory implements AspectFactory {
397         public Object JavaDoc newInstance(Class JavaDoc classIdentifier) {
398             return null;
399         }
400
401         public void reinitialize(AspectInstance aspectInstance) {
402         }
403
404     }
405
406 // public void testCallsOnJavaLangObject() {
407
// AspectInstance aspectInstance1 = new AspectInstance();
408
// aspectInstance1.addMixin(new MixinInstance(Intf.class, new ImplWithEquals("state")));
409
// AspectInstance aspectInstance2 = new AspectInstance();
410
// aspectInstance2.addMixin(new MixinInstance(Intf.class, new ImplWithEquals("state")));
411
// assertEquals(aspectInstance1.getProxy(), aspectInstance2.getProxy());
412
// }
413

414 }
415
Popular Tags