KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > aop > proxy > ProxyTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

22 package org.jboss.test.aop.proxy;
23
24 import java.io.Externalizable JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.Serializable JavaDoc;
27 import java.rmi.MarshalledObject JavaDoc;
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 /**
56  * @author <a HREF="mailto:bill@jboss.org">Bill Burke</a>
57  * @version $Revision: 45977 $
58  */

59 public class ProxyTestCase extends AOPTestWithSetup
60 {
61
62    public static void main(String JavaDoc[] 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 JavaDoc name)
75    {
76       super(name);
77    }
78
79    public void testProxy() throws Exception JavaDoc
80    {
81       Class JavaDoc[] mixIntfs = {MixinInterface.class};
82       ProxyMixin mixin = new ProxyMixin(new Mixin(), mixIntfs);
83       ProxyMixin[] mixins = {mixin};
84       Class JavaDoc[] intfs = {SomeInterface.class};
85       ClassInstanceAdvisor advisor = new ClassInstanceAdvisor();
86       advisor.insertInterceptor(new EchoInterceptor());
87
88       /*
89       CtClass clazz = ProxyFactory.createProxyCtClass(Thread.currentThread().getContextClassLoader(), mixins, intfs);
90       FileOutputStream fo = new FileOutputStream(clazz.getName() + ".class");
91       DataOutputStream dos = new DataOutputStream(fo);
92       clazz.toBytecode(dos);
93       dos.close();
94
95       System.out.println("**************");
96       */

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 JavaDoc mo = new MarshalledObject JavaDoc(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 JavaDoc
114    {
115       Class JavaDoc[] 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       /*
123       CtClass clazz = ClassProxyFactory.createProxyCtClass(mixins, POJO.class);
124       FileOutputStream fo = new FileOutputStream(clazz.getName() + ".class");
125       DataOutputStream dos = new DataOutputStream(fo);
126       clazz.toBytecode(dos);
127       dos.close();
128       */

129
130
131       MixinInterface mi = (MixinInterface) proxy;
132       assertEquals(mi.hello("mixin"), "mixin");
133
134       MarshalledObject JavaDoc mo = new MarshalledObject JavaDoc(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 JavaDoc
142    {
143       ContainerProxyCacheKey key1 = new ContainerProxyCacheKey(this.getClass(), new Class JavaDoc[] {Serializable JavaDoc.class, InputStream JavaDoc.class, Externalizable JavaDoc.class}, null);
144       ContainerProxyCacheKey key2 = new ContainerProxyCacheKey(this.getClass(), new Class JavaDoc[] {Serializable JavaDoc.class, Externalizable JavaDoc.class}, null);
145       ContainerProxyCacheKey key3 = new ContainerProxyCacheKey(this.getClass(), new Class JavaDoc[] {Externalizable JavaDoc.class, InputStream JavaDoc.class, Serializable JavaDoc.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 JavaDoc
155    {
156       InstanceDomain domain = new InstanceDomain(AspectManager.instance(), false);
157
158       
159       InterfaceIntroduction intro = new InterfaceIntroduction("intro", "*", null);
160       String JavaDoc[] 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       //PointcutExpression pointcut = new PointcutExpression("pointcut", "execution(java.lang.String $instanceof{" + POJO.class.getName() + "}->helloWorld(..))");
171
{
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       /*
189       CtClass clazz = ContainerProxyFactory.createProxyCtClass(ContainerProxyFactory.getIntroductions(POJO.class, container), POJO.class);
190       FileOutputStream fo = new FileOutputStream(clazz.getName() + ".class");
191       DataOutputStream dos = new DataOutputStream(fo);
192       clazz.toBytecode(dos);
193       dos.close();
194       */

195
196       Class JavaDoc 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