1 package dynaop; 2 3 import java.io.Externalizable ; 4 import java.io.Serializable ; 5 import java.lang.reflect.Method ; 6 import java.util.ArrayList ; 7 import java.util.Arrays ; 8 import java.util.Collection ; 9 import java.util.Collections ; 10 import java.util.HashSet ; 11 import java.util.List ; 12 import java.util.Map ; 13 import java.util.Set ; 14 15 import dynaop.Interceptor; 16 import dynaop.Pointcuts; 17 import dynaop.util.ArrayObject; 18 import dynaop.util.Classes; 19 import dynaop.util.Closure; 20 import junit.framework.TestCase; 21 22 27 public class ProxyTypeBuilderTest extends TestCase { 28 29 Object wrap(Object [] array) { 30 return Arrays.asList(array); 31 } 32 33 public void testAddObjectClass() { 34 ProxyTypeBuilder builder = new ProxyTypeBuilder(Object .class); 35 builder.addInterface(Object .class); 36 builder.addInterfaces(new Class [] { Object .class }); 37 assertEquals(0, builder.getInterfaces().size()); 38 } 39 40 public void testToString() { 41 ProxyTypeBuilder builder = new ProxyTypeBuilder(Object .class); 42 43 builder.toString(); 44 builder.createProxyType().toString(); 45 46 builder.addInterface(Foo.class); 47 builder.addInterface(List .class); 48 49 builder.toString(); 50 builder.createProxyType().toString(); 51 52 builder.addInterceptorFactory( 53 new Aspects.SingletonInterceptorFactory(new A())); 54 55 builder.toString(); 56 builder.createProxyType().toString(); 57 58 builder.toString(); 59 builder.createProxyType().toString(); 60 } 61 62 public void testCreateProxyType() throws Throwable { 63 ProxyTypeBuilder builder = new ProxyTypeBuilder(Object .class); 64 65 builder.addInterface(Foo.class); 66 builder.addInterface(List .class); 67 68 A a = new A(); 69 70 builder.addInterceptorFactory( 71 new Aspects.SingletonInterceptorFactory(a)); 72 73 ProxyType proxyType = builder.createProxyType(); 74 75 assertEquals(wrap(new Class [] { Foo.class, List .class }), 76 wrap(proxyType.getInterfaces())); 77 78 Method sizeMethod = List .class.getMethod("size", null); 79 } 80 81 public void testCreation() { 82 ProxyTypeBuilder builder = new ProxyTypeBuilder(Object .class); 83 84 Foo foo = new Foo() { 85 public void bar() {} 86 }; 87 builder = new ProxyTypeBuilder(foo.getClass()); 88 assertEquals(Collections.singletonList(Foo.class), 89 builder.getInterfaces()); 90 assertEquals(foo.getClass(), 91 builder.getProxiedClass()); 92 } 93 94 public void testMethodClosure() throws Throwable { 95 final Set visited = new HashSet (); 96 97 Foo foo = new Foo() { 98 public void bar() {} 99 }; 100 ProxyTypeBuilder builder = new ProxyTypeBuilder(foo.getClass()); 101 102 Closure closure = 103 new Closure() { 104 public void execute(Object method) { 105 visited.add(method); 106 } 107 }; 108 builder.execute(closure); 109 110 Method barMethod = Foo.class.getMethod("bar", null); 111 112 Set expected = new HashSet (); 113 expected.add(barMethod); 114 expected.add(foo.getClass().getMethod("bar", null)); 115 expected.addAll(Arrays.asList(Classes.OBJECT_METHODS)); 116 117 assertEquals(expected, visited); 118 } 119 120 public void testMethodClosureExecutesOncePerMethod() { 121 final Set visited = new HashSet (); 122 123 ProxyTypeBuilder builder = new ProxyTypeBuilder(Object .class); 124 builder.addInterface(Foo.class); 125 builder.addInterface(Bar.class); 126 127 builder.execute(new Closure() { 128 public void execute(Object method) { 129 if (visited.contains(method)) 130 fail(); 131 visited.add(method); 132 } 133 }); 134 } 135 136 public void testIsEmpty() { 137 ProxyTypeBuilder builder = new ProxyTypeBuilder(Object .class); 138 assertTrue(builder.isEmpty()); 139 builder.addInterceptorFactory( 140 new Aspects.SingletonInterceptorFactory(new A())); 141 assertFalse(builder.isEmpty()); 142 } 143 144 public void testAddInterfaces() { 145 List interfaces = new ArrayList (); 146 interfaces.add(List .class); 147 interfaces.add(Set .class); 148 interfaces.add(Map .class); 149 150 ProxyTypeBuilder builder = new ProxyTypeBuilder(Object .class); 151 152 builder.addInterfaces(interfaces); 153 154 assertEquals(interfaces, builder.getInterfaces()); 155 156 builder.addInterfaces(new Class [] { Serializable .class, 157 Externalizable .class }); 158 builder.addInterface(Collection .class); 159 160 interfaces.add(Serializable .class); 161 interfaces.add(Externalizable .class); 162 interfaces.add(Collection .class); 163 164 assertEquals(interfaces, builder.getInterfaces()); 165 } 166 167 public void SingletonInterceptorFactory() throws Throwable { 168 Method m1 = Classes.EQUALS_METHOD; 169 Method m2 = Classes.TOSTRING_METHOD; 170 Method m3 = Classes.HASHCODE_METHOD; 171 172 ProxyTypeBuilder builder = new ProxyTypeBuilder(Object .class); 173 174 A a = new A(); 175 B b = new B(); 176 C c = new C(); 177 178 builder.addInterceptorFactory(m1, 179 new Aspects.SingletonInterceptorFactory(a)); 180 builder.addInterceptorFactory(m2, 181 new Aspects.SingletonInterceptorFactory(b)); 182 183 190 191 builder.addInterceptorFactory( 192 new Aspects.SingletonInterceptorFactory(c)); 193 194 202 203 Method barMethod = Foo.class.getMethod("bar", null); 204 205 builder.addInterface(Foo.class); 206 builder.addInterceptorFactory(Pointcuts.singleton(barMethod), 207 new Aspects.SingletonInterceptorFactory(a)); 208 209 215 } 216 217 static class A implements Interceptor { 218 public Object intercept(Invocation invocation) throws Throwable { 219 return null; 220 } 221 } 222 223 static class B implements Interceptor { 224 public Object intercept(Invocation invocation) throws Throwable { 225 return null; 226 } 227 } 228 229 static class C implements Interceptor { 230 public Object intercept(Invocation invocation) throws Throwable { 231 return null; 232 } 233 } 234 235 public interface Foo { 236 void bar(); 237 } 238 239 public interface Bar extends Foo { 240 } 241 242 public static class BarImpl implements Bar { 243 public void bar() {} 244 } 245 246 ArrayObject wrap(Object array) { 247 return new ArrayObject(array); 248 } 249 } 250 | Popular Tags |