1 22 package org.jboss.test.aop.proxy; 23 24 import java.io.Externalizable ; 25 import java.io.InputStream ; 26 import java.io.Serializable ; 27 import java.rmi.MarshalledObject ; 28 29 import org.jboss.aop.AspectManager; 30 import org.jboss.aop.ClassInstanceAdvisor; 31 import org.jboss.aop.InstanceDomain; 32 import org.jboss.aop.advice.AdviceBinding; 33 import org.jboss.aop.advice.AdviceFactory; 34 import org.jboss.aop.advice.AspectDefinition; 35 import org.jboss.aop.advice.GenericAspectFactory; 36 import org.jboss.aop.advice.InterceptorFactory; 37 import org.jboss.aop.advice.Scope; 38 import org.jboss.aop.introduction.InterfaceIntroduction; 39 import org.jboss.aop.pointcut.PointcutExpression; 40 import org.jboss.aop.proxy.ClassProxyFactory; 41 import org.jboss.aop.proxy.Proxy; 42 import org.jboss.aop.proxy.ProxyFactory; 43 import org.jboss.aop.proxy.ProxyMixin; 44 import org.jboss.aop.proxy.container.AspectManaged; 45 import org.jboss.aop.proxy.container.ClassProxyContainer; 46 import org.jboss.aop.proxy.container.ContainerProxyCacheKey; 47 import org.jboss.aop.proxy.container.ContainerProxyFactory; 48 import org.jboss.aop.proxy.container.Delegate; 49 import org.jboss.test.aop.AOPTestWithSetup; 50 51 import junit.framework.Test; 52 import junit.framework.TestSuite; 53 import junit.textui.TestRunner; 54 55 59 public class ProxyTestCase extends AOPTestWithSetup 60 { 61 62 public static void main(String [] args) 63 { 64 TestRunner.run(suite()); 65 } 66 67 public static Test suite() 68 { 69 TestSuite suite = new TestSuite("ProxyTester"); 70 suite.addTestSuite(ProxyTestCase.class); 71 return suite; 72 } 73 74 public ProxyTestCase(String name) 75 { 76 super(name); 77 } 78 79 public void testProxy() throws Exception 80 { 81 Class [] mixIntfs = {MixinInterface.class}; 82 ProxyMixin mixin = new ProxyMixin(new Mixin(), mixIntfs); 83 ProxyMixin[] mixins = {mixin}; 84 Class [] intfs = {SomeInterface.class}; 85 ClassInstanceAdvisor advisor = new ClassInstanceAdvisor(); 86 advisor.insertInterceptor(new EchoInterceptor()); 87 88 97 98 Proxy proxy = ProxyFactory.createInterfaceProxy(Thread.currentThread().getContextClassLoader(), intfs, mixins, advisor); 99 100 MixinInterface mi = (MixinInterface) proxy; 101 assertEquals(mi.hello("mixin"), "mixin"); 102 SomeInterface si = (SomeInterface) proxy; 103 assertEquals(si.helloWorld(), "echoed"); 104 105 MarshalledObject mo = new MarshalledObject (proxy); 106 proxy = (Proxy) mo.get(); 107 mi = (MixinInterface) proxy; 108 assertEquals(mi.hello("mixin"), "mixin"); 109 si = (SomeInterface) proxy; 110 assertEquals(si.helloWorld(), "echoed"); 111 } 112 113 public void testClassProxy() throws Exception 114 { 115 Class [] mixIntfs = {MixinInterface.class}; 116 ProxyMixin mixin = new ProxyMixin(new Mixin(), mixIntfs); 117 ProxyMixin[] mixins = {mixin}; 118 ClassInstanceAdvisor advisor = new ClassInstanceAdvisor(); 119 advisor.insertInterceptor(new EchoInterceptor()); 120 POJO proxy = (POJO) ClassProxyFactory.newInstance(POJO.class, mixins, advisor); 121 122 129 130 131 MixinInterface mi = (MixinInterface) proxy; 132 assertEquals(mi.hello("mixin"), "mixin"); 133 134 MarshalledObject mo = new MarshalledObject (proxy); 135 proxy = (POJO) mo.get(); 136 mi = (MixinInterface) proxy; 137 assertEquals(mi.hello("mixin"), "mixin"); 138 assertEquals(proxy.helloWorld(), "echoed"); 139 } 140 141 public void testContainerProxyCacheKey() throws Exception 142 { 143 ContainerProxyCacheKey key1 = new ContainerProxyCacheKey(this.getClass(), new Class [] {Serializable .class, InputStream .class, Externalizable .class}, null); 144 ContainerProxyCacheKey key2 = new ContainerProxyCacheKey(this.getClass(), new Class [] {Serializable .class, Externalizable .class}, null); 145 ContainerProxyCacheKey key3 = new ContainerProxyCacheKey(this.getClass(), new Class [] {Externalizable .class, InputStream .class, Serializable .class}, null); 146 147 assertFalse(key1.equals(key2)); 148 assertTrue(key1.equals(key3)); 149 150 assertFalse(key1.hashCode() == key2.hashCode()); 151 assertTrue(key1.hashCode() == key3.hashCode()); 152 } 153 154 public void testContainerProxy() throws Exception 155 { 156 InstanceDomain domain = new InstanceDomain(AspectManager.instance(), false); 157 158 159 InterfaceIntroduction intro = new InterfaceIntroduction("intro", "*", null); 160 String [] intfs = {MixinInterface.class.getName()}; 161 InterfaceIntroduction.Mixin mixin = new InterfaceIntroduction.Mixin(Mixin.class.getName(), intfs, null, false); 162 intro.getMixins().add(mixin); 163 domain.addInterfaceIntroduction(intro); 164 165 166 AspectDefinition def = new AspectDefinition("aspect", Scope.PER_VM, new GenericAspectFactory(EchoInterceptor.class.getName(), null)); 167 domain.addAspectDefinition(def); 168 AdviceFactory advice = new AdviceFactory(def, "invoke"); 169 domain.addInterceptorFactory(advice.getName(), advice); 170 { 172 PointcutExpression pointcut = new PointcutExpression("pointcut", "execution(java.lang.String " + POJO.class.getName() + "->helloWorld(..))"); 173 domain.addPointcut(pointcut); 174 InterceptorFactory[] interceptors = {advice}; 175 AdviceBinding binding = new AdviceBinding("pojo-binding", pointcut, null, null, interceptors); 176 domain.addBinding(binding); 177 } 178 179 { 180 PointcutExpression pointcut = new PointcutExpression("mixin-pointcut", "execution(java.lang.String $instanceof{" + MixinInterface.class.getName() + "}->intercepted(..))"); 181 domain.addPointcut(pointcut); 182 InterceptorFactory[] interceptors = {advice}; 183 AdviceBinding binding = new AdviceBinding("mixin-binding", pointcut, null, null, interceptors); 184 domain.addBinding(binding); 185 } 186 187 188 195 196 Class proxyClass = ContainerProxyFactory.getProxyClass(POJO.class, domain); 197 ClassProxyContainer container = new ClassProxyContainer("test", domain); 198 domain.setAdvisor(container); 199 container.setClass(proxyClass); 200 container.initializeClassContainer(); 201 POJO proxy = (POJO) proxyClass.newInstance(); 202 AspectManaged cp = (AspectManaged)proxy; 203 cp.setAdvisor(container); 204 Delegate delegate = (Delegate)cp; 205 delegate.setDelegate(new POJO()); 206 207 MixinInterface mi = (MixinInterface) proxy; 208 System.out.println("--- mixin"); 209 assertEquals(mi.hello("mixin"), "mixin"); 210 System.out.println("--- hw"); 211 assertEquals("echoed", proxy.helloWorld()); 212 System.out.println("--- icptd"); 213 assertEquals("echoed", mi.intercepted("error")); 214 215 } 216 } 217 | Popular Tags |