1 22 package org.jboss.test.proxyfactory; 23 24 import java.util.HashSet ; 25 import java.util.Set ; 26 27 import org.jboss.aop.metadata.SimpleMetaData; 28 import org.jboss.aop.proxy.container.AOPProxyFactory; 29 import org.jboss.aop.proxy.container.AOPProxyFactoryMixin; 30 import org.jboss.aop.proxy.container.AOPProxyFactoryParameters; 31 import org.jboss.aop.proxy.container.GeneratedAOPProxyFactory; 32 import org.jboss.test.AbstractTestCaseWithSetup; 33 import org.jboss.test.AbstractTestDelegate; 34 35 41 public abstract class AbstractProxyTest extends AbstractTestCaseWithSetup 42 { 43 44 protected AOPProxyFactory proxyFactory; 45 46 53 public static AbstractTestDelegate getDelegate(Class clazz) throws Exception 54 { 55 return new AbstractProxyTestDelegate(clazz); 56 } 57 58 63 public AbstractProxyTest(String name) 64 { 65 super(name); 66 } 67 68 protected void setUp() throws Exception 69 { 70 super.setUp(); 71 configureLogging(); 72 proxyFactory = new GeneratedAOPProxyFactory(); 73 } 74 75 82 protected Object createProxy(Object target) throws Exception 83 { 84 AOPProxyFactoryParameters params = new AOPProxyFactoryParameters(); 85 params.setProxiedClass(target.getClass()); 86 params.setTarget(target); 87 return proxyFactory.createAdvisedProxy(params); 88 } 89 90 98 protected Object assertCreateProxy(Object target, Class expected) throws Exception 99 { 100 Object proxy = createProxy(target); 101 assertNotNull(proxy); 102 assertTrue("Proxy " + proxy + " should implement " + expected.getName() + " interfaces=" + getInterfaces(proxy), expected.isInstance(proxy)); 103 return proxy; 104 } 105 106 114 protected Object createProxy(Object target, Class [] interfaces) throws Exception 115 { 116 AOPProxyFactoryParameters params = new AOPProxyFactoryParameters(); 117 params.setProxiedClass(target.getClass()); 118 params.setInterfaces(interfaces); 119 params.setTarget(target); 120 return proxyFactory.createAdvisedProxy(params); 121 } 122 123 131 protected Object createProxy(Object target, AOPProxyFactoryMixin[] mixins) throws Exception 132 { 133 AOPProxyFactoryParameters params = new AOPProxyFactoryParameters(); 134 params.setProxiedClass(target.getClass()); 135 params.setMixins(mixins); 136 params.setTarget(target); 137 return proxyFactory.createAdvisedProxy(params); 138 } 139 140 149 protected Object createProxy(Object target, Class [] interfaces, AOPProxyFactoryMixin[] mixins) throws Exception 150 { 151 AOPProxyFactoryParameters params = new AOPProxyFactoryParameters(); 152 params.setProxiedClass(target.getClass()); 153 params.setInterfaces(interfaces); 154 params.setMixins(mixins); 155 params.setTarget(target); 156 return proxyFactory.createAdvisedProxy(params); 157 } 158 167 protected Object assertCreateProxy(Object target, AOPProxyFactoryMixin[] mixins, Class expected) throws Exception 168 { 169 Object proxy = createProxy(target, mixins); 170 assertNotNull(proxy); 171 assertTrue("Proxy " + proxy + " should implement " + expected.getName() + " interfaces=" + getInterfaces(proxy), expected.isInstance(proxy)); 172 return proxy; 173 } 174 175 185 protected Object assertCreateProxy(Object target, Class [] interfaces, AOPProxyFactoryMixin[] mixins, Class [] expected) throws Exception 186 { 187 Object proxy = createProxy(target, interfaces, mixins); 188 assertNotNull(proxy); 189 for (int i = 0 ; i < expected.length ; i++) 190 { 191 assertTrue("Proxy " + proxy + " should implement " + expected[i].getName() + " interfaces=" + getInterfaces(proxy), expected[i].isInstance(proxy)); 192 } 193 return proxy; 194 } 195 196 205 protected Object assertCreateProxy(Object target, Class [] interfaces, Class expected) throws Exception 206 { 207 Object proxy = createProxy(target, interfaces); 208 assertNotNull(proxy); 209 assertTrue("Proxy " + proxy + " should implement " + expected.getName() + " interfaces=" + getInterfaces(proxy), expected.isInstance(proxy)); 210 return proxy; 211 } 212 213 222 protected Object createProxy(Object target, Class [] interfaces, SimpleMetaData metaData) throws Exception 223 { 224 AOPProxyFactoryParameters params = new AOPProxyFactoryParameters(); 225 params.setProxiedClass(target.getClass()); 226 params.setInterfaces(interfaces); 227 params.setSimpleMetaData(metaData); 228 params.setTarget(target); 229 return proxyFactory.createAdvisedProxy(params); 230 } 231 232 242 protected Object assertCreateProxy(Object target, Class [] interfaces, SimpleMetaData metaData, Class expected) throws Exception 243 { 244 Object proxy = createProxy(target, interfaces, metaData); 245 assertNotNull(proxy); 246 assertTrue("Proxy " + proxy + " should implement " + expected.getName() + " interfaces=" + getInterfaces(proxy), expected.isInstance(proxy)); 247 return proxy; 248 } 249 250 258 protected Object createHollowProxy(Class [] interfaces, SimpleMetaData metaData) throws Exception 259 { 260 AOPProxyFactoryParameters params = new AOPProxyFactoryParameters(); 261 params.setInterfaces(interfaces); 262 params.setSimpleMetaData(metaData); 263 return proxyFactory.createAdvisedProxy(params); 264 } 265 266 274 protected Object createHollowProxy(AOPProxyFactoryMixin[] mixins, SimpleMetaData metaData) throws Exception 275 { 276 AOPProxyFactoryParameters params = new AOPProxyFactoryParameters(); 277 params.setMixins(mixins); 278 params.setSimpleMetaData(metaData); 279 return proxyFactory.createAdvisedProxy(params); 280 } 281 282 290 protected Object createHollowProxy(Class [] interfaces, AOPProxyFactoryMixin[] mixins, SimpleMetaData metaData) throws Exception 291 { 292 AOPProxyFactoryParameters params = new AOPProxyFactoryParameters(); 293 params.setInterfaces(interfaces); 294 params.setMixins(mixins); 295 params.setSimpleMetaData(metaData); 296 return proxyFactory.createAdvisedProxy(params); 297 } 298 299 308 protected Object assertCreateHollowProxy(Class [] interfaces, SimpleMetaData metaData, Class expected) throws Exception 309 { 310 Object proxy = createHollowProxy(interfaces, metaData); 311 assertNotNull(proxy); 312 assertTrue("Proxy " + proxy + " should implement " + expected.getName() + " interfaces=" + getInterfaces(proxy), expected.isInstance(proxy)); 313 return proxy; 314 } 315 316 325 protected Object assertCreateHollowProxy(AOPProxyFactoryMixin[] mixins, SimpleMetaData metaData, Class expected) throws Exception 326 { 327 Object proxy = createHollowProxy(mixins, metaData); 328 assertNotNull(proxy); 329 assertTrue("Proxy " + proxy + " should implement " + expected.getName() + " interfaces=" + getInterfaces(proxy), expected.isInstance(proxy)); 330 return proxy; 331 } 332 333 343 protected Object assertCreateHollowProxy(Class [] interfaces, AOPProxyFactoryMixin[] mixins, SimpleMetaData metaData, Class [] expected) throws Exception 344 { 345 Object proxy = createHollowProxy(interfaces, mixins, metaData); 346 assertNotNull(proxy); 347 for (int i = 0 ; i < expected.length ; i++) 348 { 349 assertTrue("Proxy " + proxy + " should implement " + expected[i].getName() + " interfaces=" + getInterfaces(proxy), expected[i].isInstance(proxy)); 350 } 351 return proxy; 352 } 353 359 protected Set getInterfaces(Object object) 360 { 361 Set interfaces = new HashSet (); 362 addInterfaces(interfaces, object.getClass()); 363 return interfaces; 364 } 365 366 372 protected void addInterfaces(Set interfaces, Class clazz) 373 { 374 Class [] intfs = clazz.getInterfaces(); 375 for (int i = 0; i < intfs.length; ++i) 376 interfaces.add(intfs[i]); 377 Class superClass = clazz.getSuperclass(); 378 if (superClass != null) 379 addInterfaces(interfaces, superClass); 380 } 381 382 387 protected AbstractProxyTestDelegate getMCDelegate() 388 { 389 return (AbstractProxyTestDelegate) getDelegate(); 390 } 391 392 } 393 | Popular Tags |