KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > framework > CglibProxyTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.aop.framework;
18
19 import net.sf.cglib.core.CodeGenerationException;
20 import org.aopalliance.aop.AspectException;
21 import org.aopalliance.intercept.MethodInterceptor;
22
23 import org.springframework.aop.interceptor.NopInterceptor;
24 import org.springframework.aop.support.AopUtils;
25 import org.springframework.beans.ITestBean;
26 import org.springframework.beans.TestBean;
27 import org.springframework.context.ApplicationContext;
28 import org.springframework.context.ApplicationContextException;
29 import org.springframework.context.support.ClassPathXmlApplicationContext;
30
31 /**
32  * Additional and overridden tests for the CGLIB proxy.
33  *
34  * @author Rod Johnson
35  * @author Juergen Hoeller
36  * @author Rob Harrop
37  */

38 public class CglibProxyTests extends AbstractAopProxyTests {
39
40     protected Object JavaDoc createProxy(AdvisedSupport as) {
41         as.setProxyTargetClass(true);
42         Object JavaDoc proxy = as.createAopProxy().getProxy();
43         assertTrue(AopUtils.isCglibProxy(proxy));
44         return proxy;
45     }
46
47     protected AopProxy createAopProxy(AdvisedSupport as) {
48         as.setProxyTargetClass(true);
49         return new Cglib2AopProxy(as);
50     }
51
52     protected boolean requiresTarget() {
53         return true;
54     }
55
56     public void testNullConfig() {
57         try {
58             AopProxy aop = new Cglib2AopProxy(null);
59             aop.getProxy();
60             fail("Shouldn't allow null interceptors");
61         }
62         catch (AopConfigException ex) {
63             // Ok
64
}
65     }
66
67     public void testNoTarget() {
68         AdvisedSupport pc = new AdvisedSupport(new Class JavaDoc[]{ITestBean.class});
69         pc.addAdvice(new NopInterceptor());
70         try {
71             AopProxy aop = createAopProxy(pc);
72             aop.getProxy();
73             fail("Shouldn't allow no target with CGLIB proxy");
74         }
75         catch (AopConfigException ex) {
76             // Ok
77
}
78     }
79
80     public void testProtectedMethodInvocation() throws Throwable JavaDoc {
81         ProtectedMethodTestBean bean = new ProtectedMethodTestBean();
82         mockTargetSource.setTarget(bean);
83
84         AdvisedSupport as = new AdvisedSupport(new Class JavaDoc[]{});
85         as.setTargetSource(mockTargetSource);
86         as.addAdvice(new NopInterceptor());
87         AopProxy aop = new Cglib2AopProxy(as);
88
89         Object JavaDoc proxy = aop.getProxy();
90
91         assertTrue("CGLIB proxy not generated", AopUtils.isCglibProxy(proxy));
92     }
93
94     public void testProxyCanBeClassNotInterface() throws Throwable JavaDoc {
95         TestBean raw = new TestBean();
96         raw.setAge(32);
97         mockTargetSource.setTarget(raw);
98         AdvisedSupport pc = new AdvisedSupport(new Class JavaDoc[]{});
99         pc.setTargetSource(mockTargetSource);
100         AopProxy aop = new Cglib2AopProxy(pc);
101
102         Object JavaDoc proxy = aop.getProxy();
103         assertTrue("Proxy is CGLIB enhanced", AopUtils.isCglibProxy(proxy));
104         assertTrue(proxy instanceof ITestBean);
105         assertTrue(proxy instanceof TestBean);
106         TestBean tb = (TestBean) proxy;
107         assertEquals("Correct age", 32, tb.getAge());
108     }
109
110     public void testCglibProxyingGivesMeaningfulExceptionIfAskedToProxyNonvisibleClass() {
111         class YouCantSeeThis {
112
113             void hidden() {
114             }
115         }
116         YouCantSeeThis mine = new YouCantSeeThis();
117         try {
118             ProxyFactory pf = new ProxyFactory(mine);
119             pf.getProxy();
120             fail("Shouldn't be able to proxy non-visible class with CGLIB");
121         }
122         catch (AspectException ex) {
123             // Check that stack trace is preserved
124
// FIX: CGLIB will throw an IllegalArgumentException when trying to
125
// create a proxy
126
// of a class where the constructor is not visible - Rob Harrop
127
assertTrue((ex.getCause() instanceof CodeGenerationException)
128                     || (ex.getCause() instanceof IllegalArgumentException JavaDoc));
129
130             // Check that error message is helpful
131

132             // TODO check why these methods fail with NPE on AOP Alliance code
133
//ex.printStackTrace();
134
//assertTrue(ex.getMessage().indexOf("final") != -1);
135
//assertTrue(ex.getMessage().indexOf("visible") != -1);
136
}
137     }
138
139     public void testMethodInvocationDuringConstructor() {
140         CglibTestBean bean = new CglibTestBean();
141         bean.setName("Rob Harrop");
142
143         AdvisedSupport as = new AdvisedSupport(new Class JavaDoc[]{});
144         as.setTarget(bean);
145         as.addAdvice(new NopInterceptor());
146         AopProxy aop = new Cglib2AopProxy(as);
147
148         CglibTestBean proxy = (CglibTestBean) aop.getProxy();
149
150         assertEquals("The name property has been overwritten by the constructor",
151                 "Rob Harrop", proxy.getName());
152     }
153
154     public void testUnadvisedProxyCreationWithCallDuringConstructor() throws Exception JavaDoc {
155         CglibTestBean target = new CglibTestBean();
156         target.setName("Rob Harrop");
157
158         AdvisedSupport pc = new AdvisedSupport(new Class JavaDoc[]{});
159         pc.setFrozen(true);
160         pc.setTarget(target);
161
162         Cglib2AopProxy aop = new Cglib2AopProxy(pc);
163
164         CglibTestBean proxy = (CglibTestBean) aop.getProxy();
165
166         assertNotNull("Proxy should not be null", proxy);
167         assertEquals("Constructor overrode the value of name", "Rob Harrop", proxy.getName());
168
169     }
170
171     public void testMultipleProxies() {
172
173         TestBean target = new TestBean();
174         target.setAge(20);
175         TestBean target2 = new TestBean();
176         target2.setAge(21);
177
178         ITestBean proxy1 = getAdvisedProxy(target);
179         ITestBean proxy2 = getAdvisedProxy(target2);
180         assertTrue(proxy1.getClass() == proxy2.getClass());
181         assertEquals(target.getAge(), proxy1.getAge());
182         assertEquals(target2.getAge(), proxy2.getAge());
183     }
184
185     private ITestBean getAdvisedProxy(TestBean target) {
186         ProxyFactory pf = new ProxyFactory(new Class JavaDoc[]{ITestBean.class});
187         pf.setProxyTargetClass(true);
188
189         MethodInterceptor static1 = new NopInterceptor();
190         pf.addAdvice(static1);
191
192         pf.setTarget(target);
193         pf.setFrozen(true);
194         pf.setExposeProxy(false);
195
196         return (ITestBean) pf.getProxy();
197     }
198
199     public void testWithNoArgConstructor() {
200         NoArgCtorTestBean target = new NoArgCtorTestBean("b", 1);
201         target.reset();
202
203         mockTargetSource.setTarget(target);
204         AdvisedSupport pc = new AdvisedSupport(new Class JavaDoc[]{});
205         pc.setTargetSource(mockTargetSource);
206         Cglib2AopProxy aop = new Cglib2AopProxy(pc);
207         aop.setConstructorArguments(new Object JavaDoc[]{"Rob Harrop", new Integer JavaDoc(22)},
208                 new Class JavaDoc[]{String JavaDoc.class, int.class});
209
210         NoArgCtorTestBean proxy = (NoArgCtorTestBean) aop.getProxy();
211         proxy = (NoArgCtorTestBean) aop.getProxy();
212
213         assertNotNull("Proxy should be null", proxy);
214     }
215
216     public void testProxyAProxy() {
217         ITestBean target = new TestBean();
218
219         mockTargetSource.setTarget(target);
220         AdvisedSupport as = new AdvisedSupport(new Class JavaDoc[]{});
221         as.setTargetSource(mockTargetSource);
222         as.addAdvice(new NopInterceptor());
223         Cglib2AopProxy cglib = new Cglib2AopProxy(as);
224
225         ITestBean proxy1 = (ITestBean) cglib.getProxy();
226
227         mockTargetSource.setTarget(proxy1);
228         as = new AdvisedSupport(new Class JavaDoc[]{});
229         as.setTargetSource(mockTargetSource);
230         as.addAdvice(new NopInterceptor());
231         cglib = new Cglib2AopProxy(as);
232
233         ITestBean proxy2 = (ITestBean) cglib.getProxy();
234     }
235
236     public void testExceptionHandling() {
237         ExceptionThrower bean = new ExceptionThrower();
238
239         mockTargetSource.setTarget(bean);
240
241         AdvisedSupport as = new AdvisedSupport(new Class JavaDoc[]{});
242         as.setTargetSource(mockTargetSource);
243         as.addAdvice(new NopInterceptor());
244         AopProxy aop = new Cglib2AopProxy(as);
245
246         ExceptionThrower proxy = (ExceptionThrower) aop.getProxy();
247
248         try {
249             proxy.doTest();
250         }
251         catch (Exception JavaDoc ex) {
252             assertTrue("Invalid exception class", ex instanceof ApplicationContextException);
253         }
254
255         assertTrue("Catch was not invoked", proxy.isCatchInvoked());
256         assertTrue("Finally was not invoked", proxy.isFinallyInvoked());
257     }
258
259     public void testWithDependencyChecking() {
260         ApplicationContext ctx =
261                 new ClassPathXmlApplicationContext("org/springframework/aop/framework/withDependencyChecking.xml");
262         ctx.getBean("testBean");
263     }
264
265
266     public static class ExceptionThrower {
267
268         private boolean catchInvoked;
269
270         private boolean finallyInvoked;
271
272         public boolean isCatchInvoked() {
273             return catchInvoked;
274         }
275
276         public boolean isFinallyInvoked() {
277             return finallyInvoked;
278         }
279
280         public void doTest() throws Exception JavaDoc {
281             try {
282                 throw new ApplicationContextException("foo");
283             }
284             catch (Exception JavaDoc ex) {
285                 catchInvoked = true;
286                 throw ex;
287             }
288             finally {
289                 finallyInvoked = true;
290             }
291         }
292     }
293
294 }
295
Popular Tags