KickJava   Java API By Example, From Geeks To Geeks.

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


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 com.tirsen.nanning.definition.AspectClass;
10 import com.tirsen.nanning.definition.AspectDefinition;
11 import junit.framework.TestCase;
12
13 /**
14  * TODO document AspectClassTest
15  *
16  * <!-- $Id: AspectClassTest.java,v 1.13 2003/05/11 13:40:52 tirsen Exp $ -->
17  *
18  * @author $Author: tirsen $
19  * @version $Revision: 1.13 $
20  */

21 public class AspectClassTest extends TestCase {
22     public static class BlahongaException extends RuntimeException JavaDoc {
23     }
24
25     public void testThrowsCorrectExceptions() {
26         AspectClass aspectClass = new AspectClass();
27         aspectClass.setInterface(Intf.class);
28         aspectClass.addInterceptor(MockInterceptor.class);
29         aspectClass.addInterceptor(MockInterceptor.class);
30         aspectClass.setTarget(IntfImpl.class);
31
32         Intf proxy = (Intf) aspectClass.newInstance();
33
34         Aspects.setTarget(proxy, Intf.class, new IntfImpl() {
35             public void call() {
36                 throw new BlahongaException();
37             }
38         });
39
40         try {
41             proxy.call();
42             fail();
43         } catch (BlahongaException shouldHappen) {
44         } catch (Exception JavaDoc e) {
45             fail();
46         }
47     }
48
49     public void testSideAspectAndAspectsOnProxy() throws IllegalAccessException JavaDoc, InstantiationException JavaDoc, NoSuchMethodException JavaDoc {
50         AspectClass aspectClass = new AspectClass();
51         aspectClass.setInterface(Intf.class);
52         aspectClass.addInterceptor(MockInterceptor.class);
53         aspectClass.addInterceptor(NullInterceptor.class);
54         aspectClass.setTarget(IntfImpl.class);
55         AspectDefinition aspectDefinition = new AspectDefinition();
56         aspectDefinition.setInterface(TestMixin.class);
57         aspectDefinition.addInterceptor(NullInterceptor.class);
58         aspectDefinition.addInterceptor(MockInterceptor.class);
59         aspectDefinition.setTarget(TestMixinImpl.class);
60         aspectClass.addAspect(aspectDefinition);
61
62         Object JavaDoc bigMomma = aspectClass.newInstance();
63
64         assertEquals(4, Aspects.getInterceptors(bigMomma).size());
65
66         verifySideAspect(bigMomma);
67     }
68
69     public static void verifySideAspect(Object JavaDoc bigMomma) throws NoSuchMethodException JavaDoc {
70         IntfImpl target = (IntfImpl) Aspects.getTarget(bigMomma, Intf.class);
71         target.expectThis(bigMomma);
72         MockInterceptor classInterceptor =
73                 (MockInterceptor) (Aspects.getInterceptors(bigMomma, Intf.class.getMethod("call", null))[0]);
74         classInterceptor.expectAtIndex(0);
75         classInterceptor.expectNumberOfInterceptors(2);
76         classInterceptor.expectCalledTimes(1);
77         classInterceptor.expectProxy(bigMomma);
78         classInterceptor.expectMethod(Intf.class.getMethod("call", null));
79         classInterceptor.expectTarget(target);
80
81         TestMixinImpl sideTarget = (TestMixinImpl) Aspects.getTarget(bigMomma, TestMixin.class);
82         MockInterceptor sideInterceptor =
83                 (MockInterceptor) (Aspects.getInterceptors(bigMomma, TestMixin.class.getMethod("mixinCall", null))[1]);
84         sideInterceptor.expectAtIndex(1);
85         sideInterceptor.expectNumberOfInterceptors(2);
86         sideInterceptor.expectCalledTimes(1);
87         sideInterceptor.expectProxy(bigMomma);
88         sideInterceptor.expectMethod(TestMixin.class.getMethod("mixinCall", null));
89         sideInterceptor.expectTarget(sideTarget);
90
91         // this calls the class-target and the class-interceptor
92
((Intf) bigMomma).call();
93         // this calls the side-target, the class-interceptor and the side-interceptor
94
classInterceptor.expectTarget(null);
95         classInterceptor.expectMethod(null);
96         classInterceptor.expectNumberOfInterceptors(2);
97         ((TestMixin) bigMomma).mixinCall();
98
99         classInterceptor.verify();
100         target.verify();
101         sideInterceptor.verify();
102         sideTarget.verify();
103     }
104
105     public void testNoAspects() throws IllegalAccessException JavaDoc, InstantiationException JavaDoc {
106         AspectClass aspectClass = new AspectClass();
107         aspectClass.setInterface(Intf.class);
108         aspectClass.setTarget(IntfImpl.class);
109         Intf intf = (Intf) aspectClass.newInstance();
110
111         IntfImpl impl = (IntfImpl) Aspects.getTarget(intf, Intf.class);
112
113         intf.call();
114         impl.verify();
115     }
116 }
117
Popular Tags