KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dynaop > AspectsTest


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

13 public class AspectsTest extends TestCase {
14
15     public void testCreateProxyTypeForTargetClass() throws Throwable JavaDoc {
16         Aspects aspects = new Aspects();
17
18         assertNull(aspects.createProxyType(FooImpl.class,
19                 new ProxyTypeBuilder(FooImpl.class)));
20         
21         ClassPointcut fooSet = Pointcuts.singleton(FooImpl.class);
22         aspects.mixin(fooSet, BarImpl.class, null);
23         aspects.interceptor(fooSet, Pointcuts.ALL_METHODS, A.class, null);
24         
25         ProxyType type = aspects.createProxyType(FooImpl.class,
26                 new ProxyTypeBuilder(FooImpl.class));
27         testProxyType(type);
28     }
29
30     void testProxyType(ProxyType type) throws Throwable JavaDoc {
31         //assertTrue(type.getMixinFactory(Bar.class.getMethod("bar", null)).
32
// getInstance(null, null) instanceof Bar);
33
InterceptorFactory[] factories =
34             type.getInterceptorFactories(Classes.EQUALS_METHOD);
35         assertTrue(factories.length == 1);
36         assertTrue(factories[0].create(null) instanceof A);
37     }
38
39     public static class A implements Interceptor, Serializable JavaDoc {
40         public Object JavaDoc intercept(Invocation invocation) throws Throwable JavaDoc {
41             return invocation.proceed();
42         }
43     }
44     
45     public interface Foo {
46         boolean fooCalled();
47         void foo();
48     }
49     
50     public static class FooImpl implements Foo, Serializable JavaDoc {
51         boolean called;
52         public boolean fooCalled() { return called; }
53         public void foo() { called = true; }
54     }
55
56     public interface Bar {
57         boolean barCalled();
58         void bar();
59         void test(Object JavaDoc proxy, Object JavaDoc target);
60     }
61     
62     public static class BarImpl implements Bar, Serializable JavaDoc {
63         
64         Object JavaDoc proxy;
65         Object JavaDoc target;
66         
67         public BarImpl(Object JavaDoc proxy, Object JavaDoc target) {
68             this.proxy = proxy;
69             this.target = target;
70         }
71         
72         boolean called;
73         public boolean barCalled() { return called; }
74         public void bar() { called = true; }
75         public void test(Object JavaDoc proxy, Object JavaDoc target) {
76             assertSame(proxy, this.proxy);
77             assertSame(target, this.target);
78         }
79     }
80 }
81
Popular Tags