01: package dynaop;
02:
03: import java.io.Serializable;
04:
05: import dynaop.util.Classes;
06: import junit.framework.TestCase;
07:
08: /**
09: *
10: *
11: * @author Bob Lee (crazybob@crazybob.org)
12: */
13: public class AspectsTest extends TestCase {
14:
15: public void testCreateProxyTypeForTargetClass() throws Throwable {
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,
24: null);
25:
26: ProxyType type = aspects.createProxyType(FooImpl.class,
27: new ProxyTypeBuilder(FooImpl.class));
28: testProxyType(type);
29: }
30:
31: void testProxyType(ProxyType type) throws Throwable {
32: //assertTrue(type.getMixinFactory(Bar.class.getMethod("bar", null)).
33: // getInstance(null, null) instanceof Bar);
34: InterceptorFactory[] factories = type
35: .getInterceptorFactories(Classes.EQUALS_METHOD);
36: assertTrue(factories.length == 1);
37: assertTrue(factories[0].create(null) instanceof A);
38: }
39:
40: public static class A implements Interceptor, Serializable {
41: public Object intercept(Invocation invocation) throws Throwable {
42: return invocation.proceed();
43: }
44: }
45:
46: public interface Foo {
47: boolean fooCalled();
48:
49: void foo();
50: }
51:
52: public static class FooImpl implements Foo, Serializable {
53: boolean called;
54:
55: public boolean fooCalled() {
56: return called;
57: }
58:
59: public void foo() {
60: called = true;
61: }
62: }
63:
64: public interface Bar {
65: boolean barCalled();
66:
67: void bar();
68:
69: void test(Object proxy, Object target);
70: }
71:
72: public static class BarImpl implements Bar, Serializable {
73:
74: Object proxy;
75: Object target;
76:
77: public BarImpl(Object proxy, Object target) {
78: this .proxy = proxy;
79: this .target = target;
80: }
81:
82: boolean called;
83:
84: public boolean barCalled() {
85: return called;
86: }
87:
88: public void bar() {
89: called = true;
90: }
91:
92: public void test(Object proxy, Object target) {
93: assertSame(proxy, this.proxy);
94: assertSame(target, this.target);
95: }
96: }
97: }
|