KickJava   Java API By Example, From Geeks To Geeks.

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


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

24 public class InterceptorTest extends TestCase {
25     public void testInterceptor() throws IllegalAccessException JavaDoc, InstantiationException JavaDoc, NoSuchMethodException JavaDoc {
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 intf = (Intf) aspectClass.newInstance();
33         IntfImpl impl = (IntfImpl) Aspects.getTarget(intf, Intf.class);
34
35         Interceptor[] interceptors = Aspects.getInterceptors(intf, Intf.class);
36         MockInterceptor interceptor = (MockInterceptor) interceptors[0];
37         MockInterceptor interceptor2 = (MockInterceptor) interceptors[1];
38
39         interceptor.expectTarget(impl);
40         interceptor.expectProxy(intf);
41         interceptor.expectMethod(Intf.class.getMethod("call", null));
42         interceptor2.expectTarget(impl);
43         interceptor2.expectProxy(intf);
44         interceptor2.expectMethod(Intf.class.getMethod("call", null));
45
46         intf.call();
47         impl.verify();
48         interceptor.verify();
49         interceptor2.verify();
50     }
51
52     public static interface ErrorIntf {
53         void call() throws Exception JavaDoc;
54     }
55
56     public static class StatelessInterceptorImpl implements MethodInterceptor, SingletonInterceptor {
57         public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc {
58             return invocation.invokeNext();
59         }
60     }
61
62     public void testStatelessInterceptor() throws NoSuchMethodException JavaDoc {
63         AspectClass aspectClass = new AspectClass();
64         aspectClass.setInterface(Intf.class);
65         aspectClass.addInterceptor(new InterceptorDefinition(StatelessInterceptorImpl.class));
66         aspectClass.addInterceptor(new InterceptorDefinition(MockInterceptor.class));
67         aspectClass.setTarget(IntfImpl.class);
68
69         Object JavaDoc proxy = aspectClass.newInstance();
70         Interceptor singletonInterceptor = Aspects.getInterceptors(proxy, Intf.class.getMethod("call", null))[0];
71         Interceptor interceptor = Aspects.getInterceptors(proxy, Intf.class.getMethod("call", null))[1];
72         assertTrue(singletonInterceptor instanceof SingletonInterceptor);
73
74         Object JavaDoc proxy2 = aspectClass.newInstance();
75         Interceptor singletonInterceptor2 = Aspects.getInterceptors(proxy2, Intf.class.getMethod("call", null))[0];
76         Interceptor interceptor2 = Aspects.getInterceptors(proxy2, Intf.class.getMethod("call", null))[1];
77         assertSame("singleton interceptor instantiated twice", singletonInterceptor, singletonInterceptor2);
78         assertNotSame("ordinary interceptor not instantiated twice", interceptor, interceptor2);
79     }
80
81     public static class TestFilterMethodsInterceptor implements FilterMethodsInterceptor {
82         public boolean interceptsMethod(Method JavaDoc method) {
83             return method.getName().equals("interceptThis");
84         }
85
86         public Object JavaDoc invoke(Invocation invocation) throws Throwable JavaDoc {
87             if (!invocation.getMethod().getName().equals("interceptThis")) {
88                 fail("should not intercept " + invocation.getMethod());
89             }
90             return invocation.invokeNext();
91         }
92     }
93
94     public static interface TestFilterIntf {
95         void interceptThis();
96
97         void dontInterceptThis();
98     }
99
100     public static class TestFilterImpl implements TestFilterIntf {
101         public void interceptThis() {
102         }
103
104         public void dontInterceptThis() {
105         }
106     }
107
108     public void testFilterMethods() {
109         AspectClass aspectClass = new AspectClass();
110         aspectClass.setInterface(TestFilterIntf.class);
111         aspectClass.addInterceptor(TestFilterMethodsInterceptor.class);
112         aspectClass.setTarget(TestFilterImpl.class);
113         TestFilterIntf instance = (TestFilterIntf) aspectClass.newInstance();
114         instance.interceptThis();
115         instance.dontInterceptThis();
116     }
117 }
118
Popular Tags