1 package dynaop; 2 3 import java.lang.reflect.Method ; 4 5 import dynaop.util.Classes; 6 import junit.framework.TestCase; 7 8 13 public class ProxyInvocationHandlerTest extends TestCase { 14 15 public void testGetInterceptors() throws Throwable { 16 ProxyTypeBuilder builder = new ProxyTypeBuilder(Object .class); 17 18 Method barMethod = Foo.class.getMethod("bar", null); 19 builder.addInterceptorFactory(barMethod, 20 new Aspects.SingletonInterceptorFactory(new A())); 21 builder.addInterceptorFactory(barMethod, 22 new Aspects.SingletonInterceptorFactory(new B())); 23 24 builder.addInterface(Foo.class); 25 builder.addInterceptorFactory(new Class [] { Foo.class }, 26 new Aspects.MixinInterceptorFactory(FooImpl.class, null)); 27 28 ProxyType type = builder.createProxyType(); 29 Object target = new Object (); 30 31 DynamicProxyInvocationHandler handler = 32 new DynamicProxyInvocationHandler(type, target); 33 34 Interceptor[] i = handler.getInterceptors(barMethod); 35 assertEquals(3, i.length); 36 assertTrue(i[0] instanceof A); 37 assertTrue(i[1] instanceof B); 38 assertTrue(i[2] instanceof Aspects.MixinInterceptor); 39 40 i = 41 handler.getInterceptors(Classes.EQUALS_METHOD); 42 assertEquals(0, i.length); 43 } 44 45 public void testInvoke() throws Throwable { 46 ProxyTypeBuilder builder = new ProxyTypeBuilder(Object .class); 47 48 final Object target = new Object (); 49 final Method barMethod = Foo.class.getMethod("bar", null); 50 final boolean[] invoked = new boolean[1]; 51 52 builder.addInterface(Foo.class); 53 54 builder.addInterceptorFactory(new Aspects.SingletonInterceptorFactory( 55 new Interceptor() { 56 public Object intercept(Invocation i) throws Throwable { 57 invoked[0] = true; 58 assertSame(target, i.getProxy().getProxyContext().unwrap()); 59 assertNull(i.getArguments()); 60 assertTrue(i.getProxy() instanceof Foo); 61 return null; 62 } 63 } 64 )); 65 66 ProxyType type = builder.createProxyType(); 67 DynamicProxyCreator creator = new DynamicProxyCreator(type); 68 DynamicProxyInvocationHandler handler = 69 new DynamicProxyInvocationHandler(type, target); 70 71 Proxy proxy = creator.createProxy(handler); 72 handler.setProxy(proxy); 73 74 ((Foo) proxy).bar(); 75 76 assertTrue(invoked[0]); 77 } 78 79 interface Foo { 80 public void bar(); 81 } 82 83 public static class FooImpl implements Foo { 84 public void bar() {} 85 } 86 87 public static class A implements Interceptor { 88 public Object intercept(Invocation invocation) throws Throwable { 89 return null; 90 } 91 } 92 93 public static class B implements Interceptor { 94 public Object intercept(Invocation invocation) throws Throwable { 95 return null; 96 } 97 } 98 } 99 | Popular Tags |