1 package dynaop; 2 3 import java.io.ByteArrayInputStream ; 4 import java.io.ByteArrayOutputStream ; 5 import java.io.IOException ; 6 import java.io.ObjectInputStream ; 7 import java.io.ObjectOutputStream ; 8 import java.io.Serializable ; 9 import java.lang.reflect.Method ; 10 import java.util.ArrayList ; 11 import java.util.Arrays ; 12 13 import dynaop.util.Classes; 14 15 import junit.framework.TestCase; 16 17 22 public class ProxyFactoryTest extends TestCase { 23 24 public void testMultipleClassProxyCreations() throws Throwable { 25 Aspects aspects = new Aspects(); 26 27 ClassPointcut foos = Pointcuts.instancesOf(Foo.class); 28 aspects.mixin(foos, BarImpl.class, null); 29 aspects.interceptor(foos, Pointcuts.ALL_METHODS, A.class, null); 30 31 ProxyFactory factory = ProxyFactory.getInstance(aspects); 32 33 FooImpl foo = (FooImpl) factory.extend(FooImpl.class); 34 foo = (FooImpl) factory.extend(FooImpl.class); 35 foo = (FooImpl) factory.extend(FooImpl.class); 36 } 37 38 public void testCyclicClassProxySerialization() throws Throwable { 39 Aspects aspects = new Aspects(); 40 aspects.interceptor(Pointcuts.ALL_CLASSES, 41 Pointcuts.ALL_METHODS, new A()); 42 ProxyFactory f = ProxyFactory.getInstance(aspects); 43 44 Bob b = (Bob) f.extend(Bob.class); 46 b.setBob(b); 47 48 Handle h = (Handle) serializeAndDeserialize( 49 ((Proxy) b).getProxyContext().getHandle()); 50 b = (Bob) h.getProxy(); 51 } 52 53 public void testClassProxySerialization() throws Throwable { 54 Aspects aspects = new Aspects(); 55 56 ClassPointcut foos = Pointcuts.instancesOf(Foo.class); 57 aspects.mixin(foos, BarImpl.class, null); 58 aspects.mixin(foos, ArrayList .class, null); 59 aspects.interceptor(foos, Pointcuts.ALL_METHODS, new A()); 60 aspects.interceptor(foos, Pointcuts.ALL_METHODS, new A()); 61 62 ProxyFactory factory = ProxyFactory.getInstance(aspects); 63 64 FooImpl foo = (FooImpl) factory.extend(FooImpl.class); 65 foo.foo(); 66 foo.fooCalled(); 67 68 assertTrue(foo instanceof Proxy); 69 70 ((Bar) foo).bar(); 71 assertTrue(foo.fooCalled()); 72 assertTrue(foo.fooCalled()); 73 74 Handle h = ((Proxy) foo).getProxyContext().getHandle(); 75 h = (Handle) serializeAndDeserialize(h); 76 foo = (FooImpl) h.getProxy(); 77 78 assertTrue(foo.fooCalled()); 79 80 Bar bar = (Bar) foo; 81 assertTrue(bar.barCalled()); 82 } 83 84 Object serializeAndDeserialize(Object o) throws Exception { 85 ByteArrayOutputStream bout = new ByteArrayOutputStream (); 86 new ObjectOutputStream (bout).writeObject(o); 87 return new ObjectInputStream ( 88 new ByteArrayInputStream (bout.toByteArray())).readObject(); 89 } 90 91 public void testCglibProxyWithNoAspects() throws Throwable { 92 Aspects aspects = new Aspects(); 93 94 ProxyFactory factory = new ProxyFactory(aspects); 95 96 FooImpl foo = (FooImpl) factory.extend(FooImpl.class); 97 assertEquals(FooImpl.class, foo.getClass()); 98 } 99 100 public void testCglibProxyWithNullAspects() throws Throwable { 101 ProxyFactory factory = ProxyFactory.getInstance(new Aspects()); 102 103 FooImpl foo = (FooImpl) factory.extend(FooImpl.class); 104 assertEquals(FooImpl.class, foo.getClass()); 105 } 106 107 public void testCglibProxyInterface() throws Throwable { 108 Aspects aspects = new Aspects(); 109 110 ClassPointcut foos = Pointcuts.instancesOf(Foo.class); 111 aspects.mixin(foos, BarImpl.class, null); 112 113 ProxyFactory factory = new ProxyFactory(aspects); 114 115 FooImpl foo = (FooImpl) factory.extend(FooImpl.class); 116 ((Proxy) foo).getProxyContext().getInterceptors( 117 Bar.class.getMethod("bar", null)); 118 } 119 120 public void testClassProxy() throws Throwable { 121 Aspects aspects = new Aspects(); 122 123 ClassPointcut foos = Pointcuts.instancesOf(Foo.class); 124 aspects.mixin(foos, BarImpl.class, null); 125 aspects.mixin(foos, ArrayList .class, null); 126 aspects.interceptor(foos, Pointcuts.ALL_METHODS, new A()); 127 aspects.interceptor(foos, Pointcuts.ALL_METHODS, new A()); 128 129 ProxyFactory factory = new ProxyFactory(aspects); 130 131 FooImpl foo = (FooImpl) factory.extend(FooImpl.class); 132 foo.foo(); 133 assertTrue(foo.fooCalled()); 134 135 Bar bar = (Bar) foo; 136 bar.bar(); 137 assertTrue(bar.barCalled()); 138 139 } 141 142 public void testClassProxySelfCall() throws Throwable { 143 Aspects aspects = new Aspects(); 144 145 Counter c = new Counter(); 146 aspects.interceptor(Pointcuts.ALL_CLASSES, Pointcuts.ALL_METHODS, c); 147 148 ProxyFactory factory = ProxyFactory.getInstance(aspects); 149 150 Bob b = (Bob) factory.extend(Bob.class); 151 assertEquals(0, c.getCount()); 152 b.callSelf(); 153 assertEquals(2, c.getCount()); 154 } 155 156 public void testSerialization() throws Throwable { 157 Aspects aspects = new Aspects(); 158 159 ClassPointcut fooSet = Pointcuts.singleton(FooImpl.class); 160 aspects.mixin(fooSet, BarImpl.class, null); 161 aspects.mixin(fooSet, ArrayList .class, null); 162 aspects.interceptor(fooSet, Pointcuts.ALL_METHODS, new A()); 163 aspects.interceptor(fooSet, Pointcuts.ALL_METHODS, new A()); 164 165 ProxyFactory factory = ProxyFactory.getInstance(aspects); 166 Foo foo = (Foo) factory.wrap(new FooImpl()); 167 168 foo.foo(); 169 ((Bar) foo).bar(); 170 171 ByteArrayOutputStream bout = new ByteArrayOutputStream (); 172 new ObjectOutputStream (bout).writeObject(foo); 173 foo = (Foo) new ObjectInputStream ( 174 new ByteArrayInputStream (bout.toByteArray())).readObject(); 175 176 foo.foo(); 177 assertTrue(foo.fooCalled()); 178 assertTrue(((Bar) foo).barCalled()); 179 } 180 181 public void testProxyInterface() throws Throwable { 182 ClassPointcut fooSet = Pointcuts.singleton(FooImpl.class); 183 Aspects aspects = new Aspects(); 184 aspects.mixin(fooSet, BarImpl.class, null); 185 aspects.interceptor(fooSet, Pointcuts.ALL_METHODS, new A()); 186 187 ProxyFactory factory = ProxyFactory.getInstance(aspects); 188 189 Method fooMethod = Foo.class.getMethod("foo", null); 190 191 Proxy proxy = (Proxy) factory.wrap(new FooImpl()); 192 193 proxy.getProxyContext().getInterceptors(fooMethod); 194 195 ProxyInvocationHandler handler = 196 (ProxyInvocationHandler) 197 java.lang.reflect.Proxy.getInvocationHandler(proxy); 198 199 assertNotNull(handler.interceptors.get(fooMethod)); 200 201 proxy = (Proxy) factory.wrap(new FooImpl()); 202 proxy.getProxyContext().getInterceptors(fooMethod); 203 handler = (ProxyInvocationHandler) 204 java.lang.reflect.Proxy.getInvocationHandler(proxy); 205 assertNotNull(handler.interceptors.get(fooMethod)); 206 207 ProxyType type = proxy.getProxyContext().getProxyType(); 208 assertEquals( 209 Arrays.asList(new Class [] { Foo.class, Bar.class }), 210 Arrays.asList(type.getInterfaces()) 211 ); 212 } 213 214 void testProxyType(ProxyType type) throws Throwable { 215 InterceptorFactory[] interceptors = 216 type.getInterceptorFactories(Classes.EQUALS_METHOD); 217 assertTrue(interceptors.length == 1); 218 assertTrue(interceptors[0].create(null) instanceof A); 219 } 220 221 public void testGetCglibProxyCreator() throws Throwable { 222 ProxyFactory factory = ProxyFactory.getInstance(new Aspects()); 223 224 assertNull(factory.classProxyCreators.get(FooImpl.class)); 225 assertNull(factory.classProxyCreators.get(FooImpl.class)); 226 227 ClassPointcut fooSet = Pointcuts.singleton(FooImpl.class); 228 Aspects aspects = new Aspects(); 229 aspects.mixin(fooSet, BarImpl.class, null); 230 aspects.interceptor(fooSet, Pointcuts.ALL_METHODS, new A()); 231 232 factory = new ProxyFactory(aspects); 233 assertSame(factory.classProxyCreators.get(FooImpl.class), 234 factory.classProxyCreators.get(FooImpl.class)); 235 236 ProxyType type = 237 ((ClassProxyCreator) factory.classProxyCreators.get(FooImpl.class)). 238 getProxyType(); 239 testProxyType(type); 240 } 241 242 243 public void testGetDynamicProxyCreatorForTargetClass() throws Throwable { 244 ProxyFactory factory = ProxyFactory.getInstance(new Aspects()); 245 assertNull(factory.dynamicProxyCreators.get(FooImpl.class)); 246 assertNull(factory.dynamicProxyCreators.get(FooImpl.class)); 247 248 ClassPointcut fooSet = Pointcuts.singleton(FooImpl.class); 249 Aspects aspects = new Aspects(); 250 aspects.mixin(fooSet, BarImpl.class, null); 251 aspects.interceptor(fooSet, Pointcuts.ALL_METHODS, new A()); 252 factory = new ProxyFactory(aspects); 253 assertSame(factory.dynamicProxyCreators.get(FooImpl.class), 254 factory.dynamicProxyCreators.get(FooImpl.class)); 255 256 ProxyType type = 257 ((DynamicProxyCreator) factory.dynamicProxyCreators.get( 258 FooImpl.class)).getProxyType(); 259 testProxyType(type); 260 } 261 262 public void testCreateProxy() { 263 Object target = new FooImpl(); 264 265 ProxyFactory factory = ProxyFactory.getInstance(new Aspects()); 266 assertSame(target, factory.wrap(target)); 267 268 Aspects aspects = new Aspects(); 269 ClassPointcut fooSet = Pointcuts.singleton(FooImpl.class); 270 aspects.mixin(fooSet, BarImpl.class, null); 271 aspects.interceptor(fooSet, Pointcuts.ALL_METHODS, new A()); 272 factory = new ProxyFactory(aspects); 273 274 Object proxy = factory.wrap(target); 275 276 Foo foo = (Foo) proxy; 277 Bar bar = (Bar) proxy; 278 279 foo.foo(); 280 bar.bar(); 281 282 assertTrue(foo.fooCalled()); 283 assertTrue(bar.barCalled()); 284 } 285 286 public static class A implements Interceptor, Serializable { 287 public Object intercept(Invocation invocation) throws Throwable { 288 return invocation.proceed(); 289 } 290 } 291 292 public static class Counter implements Interceptor { 293 294 int count = 0; 295 296 public Object intercept(Invocation invocation) throws Throwable { 297 count++; 298 return invocation.proceed(); 299 } 300 301 public int getCount() { 302 return this.count; 303 } 304 } 305 306 public interface Foo { 307 boolean fooCalled(); 308 void foo(); 309 } 310 311 public static class FooImpl implements Foo, Serializable { 312 boolean called; 313 public boolean fooCalled() { return called; } 314 public void foo() { called = true; } 315 } 316 317 public interface Bar { 318 boolean barCalled(); 319 void bar(); 320 void test(Object proxy, Object target); 321 } 322 323 public static class BarImpl implements Bar, Serializable , 324 ProxyAware { 325 326 transient Proxy proxy; 327 boolean called; 328 329 public boolean barCalled() { 330 return called; 331 } 332 public void bar() { called = true; } 333 334 public void test(Object proxy, Object target) { 335 assertSame(proxy, this.proxy); 336 assertSame(target, this.proxy.getProxyContext().unwrap()); 337 } 338 339 public void setProxy(Proxy proxy) { 340 this.proxy = proxy; 341 } 342 343 private void writeObject(ObjectOutputStream out) throws IOException { 344 out.defaultWriteObject(); 345 out.writeObject(((Proxy) this.proxy).getProxyContext().getHandle()); 346 } 347 348 private void readObject(ObjectInputStream in) throws IOException , 349 ClassNotFoundException { 350 in.defaultReadObject(); 351 this.proxy = ((Handle) in.readObject()).getProxy(); 352 } 353 } 354 355 public static class Bob implements Serializable { 356 357 Bob bob; 358 359 public Bob getBob() { 360 return bob; 361 } 362 363 public void setBob(Bob bob) { 364 this.bob = bob; 365 } 366 367 public void callSelf() { 368 doSomething(); 369 } 370 371 public void doSomething() { 372 } 374 375 private void writeObject(ObjectOutputStream out) throws IOException { 376 out.writeObject(((Proxy) bob).getProxyContext().getHandle()); 377 } 378 379 private void readObject(ObjectInputStream in) throws IOException , 380 ClassNotFoundException { 381 this.bob = (Bob) ((Handle) in.readObject()).getProxy(); 382 } 383 } 384 } 385
| Popular Tags
|