1 22 package org.jboss.aop.microcontainer.junit; 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 String property = System.getProperty("jboss.mc.secure", "false"); 56 boolean enableSecurity = Boolean.valueOf(property).booleanValue(); 57 AbstractProxyTestDelegate delegate = new AbstractProxyTestDelegate(clazz); 58 delegate.enableSecurity = enableSecurity; 59 return delegate; 60 } 61 62 67 public AbstractProxyTest(String name) 68 { 69 super(name); 70 } 71 72 protected void setUp() throws Exception 73 { 74 super.setUp(); 75 configureLogging(); 76 proxyFactory = new GeneratedAOPProxyFactory(); 77 } 78 79 86 protected Object createProxy(Object target) throws Exception 87 { 88 AOPProxyFactoryParameters params = new AOPProxyFactoryParameters(); 89 params.setProxiedClass(target.getClass()); 90 params.setTarget(target); 91 return proxyFactory.createAdvisedProxy(params); 92 } 93 94 102 protected Object assertCreateProxy(Object target, Class expected) throws Exception 103 { 104 Object proxy = createProxy(target); 105 assertNotNull(proxy); 106 assertTrue("Proxy " + proxy + " should implement " + expected.getName() + " interfaces=" + getInterfaces(proxy), expected.isInstance(proxy)); 107 return proxy; 108 } 109 110 118 protected Object createProxy(Object target, Class [] interfaces) throws Exception 119 { 120 AOPProxyFactoryParameters params = new AOPProxyFactoryParameters(); 121 params.setProxiedClass(target.getClass()); 122 params.setInterfaces(interfaces); 123 params.setTarget(target); 124 return proxyFactory.createAdvisedProxy(params); 125 } 126 127 135 protected Object createProxy(Object target, AOPProxyFactoryMixin[] mixins) throws Exception 136 { 137 AOPProxyFactoryParameters params = new AOPProxyFactoryParameters(); 138 params.setProxiedClass(target.getClass()); 139 params.setMixins(mixins); 140 params.setTarget(target); 141 return proxyFactory.createAdvisedProxy(params); 142 } 143 144 153 protected Object createProxy(Object target, Class [] interfaces, AOPProxyFactoryMixin[] mixins) throws Exception 154 { 155 AOPProxyFactoryParameters params = new AOPProxyFactoryParameters(); 156 params.setProxiedClass(target.getClass()); 157 params.setInterfaces(interfaces); 158 params.setMixins(mixins); 159 params.setTarget(target); 160 return proxyFactory.createAdvisedProxy(params); 161 } 162 171 protected Object assertCreateProxy(Object target, AOPProxyFactoryMixin[] mixins, Class expected) throws Exception 172 { 173 Object proxy = createProxy(target, mixins); 174 assertNotNull(proxy); 175 assertTrue("Proxy " + proxy + " should implement " + expected.getName() + " interfaces=" + getInterfaces(proxy), expected.isInstance(proxy)); 176 return proxy; 177 } 178 179 189 protected Object assertCreateProxy(Object target, Class [] interfaces, AOPProxyFactoryMixin[] mixins, Class [] expected) throws Exception 190 { 191 Object proxy = createProxy(target, interfaces, mixins); 192 assertNotNull(proxy); 193 for (int i = 0 ; i < expected.length ; i++) 194 { 195 assertTrue("Proxy " + proxy + " should implement " + expected[i].getName() + " interfaces=" + getInterfaces(proxy), expected[i].isInstance(proxy)); 196 } 197 return proxy; 198 } 199 200 209 protected Object assertCreateProxy(Object target, Class [] interfaces, Class expected) throws Exception 210 { 211 Object proxy = createProxy(target, interfaces); 212 assertNotNull(proxy); 213 assertTrue("Proxy " + proxy + " should implement " + expected.getName() + " interfaces=" + getInterfaces(proxy), expected.isInstance(proxy)); 214 return proxy; 215 } 216 217 226 protected Object createProxy(Object target, Class [] interfaces, SimpleMetaData metaData) throws Exception 227 { 228 AOPProxyFactoryParameters params = new AOPProxyFactoryParameters(); 229 params.setProxiedClass(target.getClass()); 230 params.setInterfaces(interfaces); 231 params.setSimpleMetaData(metaData); 232 params.setTarget(target); 233 return proxyFactory.createAdvisedProxy(params); 234 } 235 236 246 protected Object assertCreateProxy(Object target, Class [] interfaces, SimpleMetaData metaData, Class expected) throws Exception 247 { 248 Object proxy = createProxy(target, interfaces, metaData); 249 assertNotNull(proxy); 250 assertTrue("Proxy " + proxy + " should implement " + expected.getName() + " interfaces=" + getInterfaces(proxy), expected.isInstance(proxy)); 251 return proxy; 252 } 253 254 262 protected Object createHollowProxy(Class [] interfaces, SimpleMetaData metaData) throws Exception 263 { 264 AOPProxyFactoryParameters params = new AOPProxyFactoryParameters(); 265 params.setInterfaces(interfaces); 266 params.setSimpleMetaData(metaData); 267 return proxyFactory.createAdvisedProxy(params); 268 } 269 270 278 protected Object createHollowProxy(AOPProxyFactoryMixin[] mixins, SimpleMetaData metaData) throws Exception 279 { 280 AOPProxyFactoryParameters params = new AOPProxyFactoryParameters(); 281 params.setMixins(mixins); 282 params.setSimpleMetaData(metaData); 283 return proxyFactory.createAdvisedProxy(params); 284 } 285 286 294 protected Object createHollowProxy(Class [] interfaces, AOPProxyFactoryMixin[] mixins, SimpleMetaData metaData) throws Exception 295 { 296 AOPProxyFactoryParameters params = new AOPProxyFactoryParameters(); 297 params.setInterfaces(interfaces); 298 params.setMixins(mixins); 299 params.setSimpleMetaData(metaData); 300 return proxyFactory.createAdvisedProxy(params); 301 } 302 303 312 protected Object assertCreateHollowProxy(Class [] interfaces, SimpleMetaData metaData, Class expected) throws Exception 313 { 314 Object proxy = createHollowProxy(interfaces, metaData); 315 assertNotNull(proxy); 316 assertTrue("Proxy " + proxy + " should implement " + expected.getName() + " interfaces=" + getInterfaces(proxy), expected.isInstance(proxy)); 317 return proxy; 318 } 319 320 329 protected Object assertCreateHollowProxy(AOPProxyFactoryMixin[] mixins, SimpleMetaData metaData, Class expected) throws Exception 330 { 331 Object proxy = createHollowProxy(mixins, metaData); 332 assertNotNull(proxy); 333 assertTrue("Proxy " + proxy + " should implement " + expected.getName() + " interfaces=" + getInterfaces(proxy), expected.isInstance(proxy)); 334 return proxy; 335 } 336 337 347 protected Object assertCreateHollowProxy(Class [] interfaces, AOPProxyFactoryMixin[] mixins, SimpleMetaData metaData, Class [] expected) throws Exception 348 { 349 Object proxy = createHollowProxy(interfaces, mixins, metaData); 350 assertNotNull(proxy); 351 for (int i = 0 ; i < expected.length ; i++) 352 { 353 assertTrue("Proxy " + proxy + " should implement " + expected[i].getName() + " interfaces=" + getInterfaces(proxy), expected[i].isInstance(proxy)); 354 } 355 return proxy; 356 } 357 363 protected Set getInterfaces(Object object) 364 { 365 Set <Class > interfaces = new HashSet <Class >(); 366 addInterfaces(interfaces, object.getClass()); 367 return interfaces; 368 } 369 370 376 protected void addInterfaces(Set <Class > interfaces, Class clazz) 377 { 378 Class [] intfs = clazz.getInterfaces(); 379 for (int i = 0; i < intfs.length; ++i) 380 interfaces.add(intfs[i]); 381 Class superClass = clazz.getSuperclass(); 382 if (superClass != null) 383 addInterfaces(interfaces, superClass); 384 } 385 386 391 protected AbstractProxyTestDelegate getMCDelegate() 392 { 393 return (AbstractProxyTestDelegate) getDelegate(); 394 } 395 396 } 397 | Popular Tags |