1 package dynaop; 2 3 import java.lang.reflect.Method ; 4 import java.util.ArrayList ; 5 import java.util.List ; 6 7 import dynaop.util.Classes; 8 9 import junit.framework.TestCase; 10 11 16 public class DispatchInterceptorTest extends TestCase { 17 18 public void testMethodPointcut() throws Exception { 19 MethodPointcut mp = DispatchInterceptor.methodPointcut(A.class); 20 assertFalse(mp.picks(Classes.EQUALS_METHOD)); 21 assertFalse(mp.picks(ArrayList .class.getMethod("iterator", null))); 22 assertTrue(mp.picks(ArrayList .class.getMethod("size", null))); 23 } 24 25 public void testMultipleInterceptors() throws Throwable { 26 InvocationImpl invocation = new InvocationImpl(); 27 invocation.setInterceptors(new Interceptor[] { new A(), new B() }); 28 invocation.setTarget(new ArrayList ()); 29 invocation.setMethod(List .class.getMethod("size", null)); 30 invocation.setProxy( 31 new MockProxy() { 32 public ProxyContext getProxyContext() { 33 return new MockProxyContext() { 34 public boolean isClassProxy() { 35 return false; 36 } 37 }; 38 } 39 } 40 ); 41 invocation.proceed(); 42 } 43 44 public void testObjectMethods() { 45 Object foo = new Object () { 46 public int hashCode() { 47 return 5; 48 } 49 public String toString() { 50 return "test"; 51 } 52 public boolean equals(Object o) { 53 return true; 54 } 55 }; 56 57 ProxyTypeBuilder builder = new ProxyTypeBuilder(Object .class); 58 Aspects aspects = new Aspects(); 59 aspects.interceptor(Pointcuts.ALL_CLASSES, Pointcuts.ALL_METHODS, 60 TestDispatchInterceptor.class, null); 61 62 ProxyFactory factory = 63 ProxyFactory.getInstance(aspects); 64 foo = factory.wrap(foo); 65 66 assertEquals(5, foo.hashCode()); 67 assertEquals("test", foo.toString()); 68 assertTrue(foo.equals(null)); 69 } 70 71 public void testDispatch() { 72 Aspects aspects = new Aspects(); 73 aspects.interceptor(Pointcuts.ALL_CLASSES, Pointcuts.ALL_METHODS, 74 TestDispatchInterceptor.class, null); 75 aspects.interfaces(Pointcuts.ALL_CLASSES, new Class [] { List .class }); 76 77 ProxyFactory factory = 78 ProxyFactory.getInstance(aspects); 79 List list = (List ) factory.wrap(new ArrayList ()); 80 81 assertEquals(5, list.size()); 82 } 83 84 public void testIsInterceptMethod() throws Throwable { 85 Method method = List .class.getMethod("size", null); 86 Method intercept = Interceptor.class.getMethod("intercept", 87 new Class [] { Invocation.class }); 88 DispatchInterceptor interceptor = new DispatchInterceptor() {}; 89 assertFalse(DispatchInterceptor.isInterceptMethod(method)); 90 assertTrue(DispatchInterceptor.isInterceptMethod(intercept)); 91 } 92 93 public void testCacheNull() throws Throwable { 94 DispatchInterceptor interceptor = new DispatchInterceptor() {}; 95 Method method = List .class.getMethod("size", null); 96 interceptor.getDispatchMethod(method); 97 assertNull(interceptor.cache.get(method)); 98 } 99 100 public void testCacheMethod() throws Throwable { 101 DispatchInterceptor interceptor = new DispatchInterceptor() { 102 public void size() throws Throwable { 103 proceed(); 104 } 105 }; 106 107 Method method = List .class.getMethod("size", null); 108 interceptor.getDispatchMethod(method); 109 assertNotNull(interceptor.cache.get(method)); 110 } 111 112 public void testSetInvocation() { 113 Invocation i = new InvocationImpl(); 114 DispatchInterceptor interceptor = new DispatchInterceptor() {}; 115 interceptor.setInvocation(i); 116 assertSame(i, interceptor.getInvocation()); 117 } 118 119 public void testProceed() throws Throwable { 120 final boolean[] proceedCalled = new boolean[1]; 121 Invocation i = new InvocationImpl() { 122 public Object proceed() { 123 proceedCalled[0] = true; 124 return null; 125 } 126 }; 127 128 DispatchInterceptor interceptor = new DispatchInterceptor() {}; 129 interceptor.setInvocation(i); 130 interceptor.proceed(); 131 assertTrue(proceedCalled[0]); 132 } 133 134 public static class TestDispatchInterceptor extends DispatchInterceptor { 135 public int size() { 136 return 5; 137 } 138 } 139 140 public static class A extends DispatchInterceptor { 141 public int size() throws Throwable { 142 return ((Integer ) proceed()).intValue(); 143 } 144 } 145 146 public static class B extends DispatchInterceptor { 147 public int size() throws Throwable { 148 return ((Integer ) proceed()).intValue(); 149 } 150 } 151 } 152
| Popular Tags
|