KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > framework > autoproxy > AdvisorAutoProxyCreatorTests


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.autoproxy;
18
19 import java.io.IOException JavaDoc;
20
21 import javax.servlet.ServletException JavaDoc;
22
23 import junit.framework.TestCase;
24
25 import org.springframework.aop.framework.Advised;
26 import org.springframework.aop.framework.Lockable;
27 import org.springframework.aop.framework.MethodCounter;
28 import org.springframework.aop.interceptor.NopInterceptor;
29 import org.springframework.aop.support.AopUtils;
30 import org.springframework.aop.target.CommonsPoolTargetSource;
31 import org.springframework.aop.target.LazyInitTargetSource;
32 import org.springframework.aop.target.PrototypeTargetSource;
33 import org.springframework.aop.target.ThreadLocalTargetSource;
34 import org.springframework.beans.ITestBean;
35 import org.springframework.beans.factory.BeanFactory;
36 import org.springframework.context.support.ClassPathXmlApplicationContext;
37 import org.springframework.transaction.CountingTxManager;
38
39 /**
40  * Tests for auto proxy creation by advisor recognition.
41  *
42  * @author Rod Johnson
43  */

44 public class AdvisorAutoProxyCreatorTests extends TestCase {
45
46     private static final String JavaDoc ADVISOR_APC_BEAN_NAME = "aapc";
47
48     private static final String JavaDoc TXMANAGER_BEAN_NAME = "txManager";
49
50     /**
51      * Return a bean factory with attributes and EnterpriseServices configured.
52      */

53     protected BeanFactory getBeanFactory() throws IOException JavaDoc {
54         return new ClassPathXmlApplicationContext("/org/springframework/aop/framework/autoproxy/advisorAutoProxyCreator.xml");
55     }
56
57     public void testDefaultExclusionPrefix() throws Exception JavaDoc {
58         DefaultAdvisorAutoProxyCreator aapc = (DefaultAdvisorAutoProxyCreator) getBeanFactory().getBean(ADVISOR_APC_BEAN_NAME);
59         assertEquals(ADVISOR_APC_BEAN_NAME + DefaultAdvisorAutoProxyCreator.SEPARATOR, aapc.getAdvisorBeanNamePrefix());
60         assertFalse(aapc.isUsePrefix());
61     }
62
63     /**
64      * If no pointcuts match (no attrs) there should be proxying.
65      */

66     public void testNoProxy() throws Exception JavaDoc {
67         BeanFactory bf = getBeanFactory();
68         Object JavaDoc o = bf.getBean("noSetters");
69         assertFalse(AopUtils.isAopProxy(o));
70     }
71
72     public void testTxIsProxied() throws Exception JavaDoc {
73         BeanFactory bf = getBeanFactory();
74         ITestBean test = (ITestBean) bf.getBean("test");
75         assertTrue(AopUtils.isAopProxy(test));
76     }
77
78     public void testRegexpApplied() throws Exception JavaDoc {
79         BeanFactory bf = getBeanFactory();
80         ITestBean test = (ITestBean) bf.getBean("test");
81         MethodCounter counter = (MethodCounter) bf.getBean("countingAdvice");
82         assertEquals(0, counter.getCalls());
83         test.getName();
84         assertEquals(1, counter.getCalls());
85     }
86
87     /**
88      * Check that we can provide a common interceptor that will
89      * appear in the chain before "specific" interceptors,
90      * which are sourced from matching advisors
91      */

92     public void testCommonInterceptorAndAdvisor() throws Exception JavaDoc {
93         BeanFactory bf = new ClassPathXmlApplicationContext("/org/springframework/aop/framework/autoproxy/advisorAutoProxyCreatorWithCommonInterceptors.xml");
94         ITestBean test1 = (ITestBean) bf.getBean("test1");
95         assertTrue(AopUtils.isAopProxy(test1));
96
97         Lockable lockable1 = (Lockable) test1;
98         NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor");
99         assertEquals(0, nop.getCount());
100
101         ITestBean test2 = (ITestBean) bf.getBean("test2");
102         Lockable lockable2 = (Lockable) test2;
103         
104         // Locking should be independent; nop is shared
105
assertFalse(lockable1.locked());
106         assertFalse(lockable2.locked());
107         // equals 2 calls on shared nop, because it's first
108
// and sees calls against the Lockable interface introduced
109
// by the specific advisor
110
assertEquals(2, nop.getCount());
111         lockable1.lock();
112         assertTrue(lockable1.locked());
113         assertFalse(lockable2.locked());
114         assertEquals(5, nop.getCount());
115     }
116
117     /**
118      * We have custom TargetSourceCreators but there's no match, and
119      * hence no proxying, for this bean
120      */

121     public void testCustomTargetSourceNoMatch() throws Exception JavaDoc {
122         BeanFactory bf = new ClassPathXmlApplicationContext("/org/springframework/aop/framework/autoproxy/customTargetSource.xml");
123         ITestBean test = (ITestBean) bf.getBean("test");
124         assertFalse(AopUtils.isAopProxy(test));
125         assertEquals("Rod", test.getName());
126         assertEquals("Kerry", test.getSpouse().getName());
127     }
128
129     public void testCustomPoolingTargetSource() throws Exception JavaDoc {
130         BeanFactory bf = new ClassPathXmlApplicationContext("/org/springframework/aop/framework/autoproxy/customTargetSource.xml");
131         ITestBean test = (ITestBean) bf.getBean("poolingTest");
132         assertTrue(AopUtils.isAopProxy(test));
133         Advised advised = (Advised) test;
134         assertTrue(advised.getTargetSource() instanceof CommonsPoolTargetSource);
135         assertEquals("Rod", test.getName());
136         // Check that references survived pooling
137
assertEquals("Kerry", test.getSpouse().getName());
138     }
139
140     public void testCustomPrototypeTargetSource() throws Exception JavaDoc {
141         CountingTestBean.count = 0;
142         BeanFactory bf = new ClassPathXmlApplicationContext("/org/springframework/aop/framework/autoproxy/customTargetSource.xml");
143         ITestBean test = (ITestBean) bf.getBean("prototypeTest");
144         assertTrue(AopUtils.isAopProxy(test));
145         Advised advised = (Advised) test;
146         assertTrue(advised.getTargetSource() instanceof PrototypeTargetSource);
147         assertEquals("Rod", test.getName());
148         // Check that references survived prototype creation
149
assertEquals("Kerry", test.getSpouse().getName());
150         assertEquals("Only 2 CountingTestBeans instantiated", 2, CountingTestBean.count);
151         CountingTestBean.count = 0;
152     }
153
154     public void testLazyInitTargetSource() throws Exception JavaDoc {
155         CountingTestBean.count = 0;
156         BeanFactory bf = new ClassPathXmlApplicationContext("/org/springframework/aop/framework/autoproxy/customTargetSource.xml");
157         ITestBean test = (ITestBean) bf.getBean("lazyInitTest");
158         assertTrue(AopUtils.isAopProxy(test));
159         Advised advised = (Advised) test;
160         assertTrue(advised.getTargetSource() instanceof LazyInitTargetSource);
161         assertEquals("No CountingTestBean instantiated yet", 0, CountingTestBean.count);
162         assertEquals("Rod", test.getName());
163         assertEquals("Kerry", test.getSpouse().getName());
164         assertEquals("Only 1 CountingTestBean instantiated", 1, CountingTestBean.count);
165         CountingTestBean.count = 0;
166     }
167
168     public void testQuickTargetSourceCreator() throws Exception JavaDoc {
169         ClassPathXmlApplicationContext bf =
170                 new ClassPathXmlApplicationContext("/org/springframework/aop/framework/autoproxy/quickTargetSource.xml");
171         ITestBean test = (ITestBean) bf.getBean("test");
172         assertFalse(AopUtils.isAopProxy(test));
173         assertEquals("Rod", test.getName());
174         // Check that references survived pooling
175
assertEquals("Kerry", test.getSpouse().getName());
176     
177         // Now test the pooled one
178
test = (ITestBean) bf.getBean(":test");
179         assertTrue(AopUtils.isAopProxy(test));
180         Advised advised = (Advised) test;
181         assertTrue(advised.getTargetSource() instanceof CommonsPoolTargetSource);
182         assertEquals("Rod", test.getName());
183         // Check that references survived pooling
184
assertEquals("Kerry", test.getSpouse().getName());
185         
186         // Now test the ThreadLocal one
187
test = (ITestBean) bf.getBean("%test");
188         assertTrue(AopUtils.isAopProxy(test));
189         advised = (Advised) test;
190         assertTrue(advised.getTargetSource() instanceof ThreadLocalTargetSource);
191         assertEquals("Rod", test.getName());
192         // Check that references survived pooling
193
assertEquals("Kerry", test.getSpouse().getName());
194         
195         // Now test the Prototype TargetSource
196
test = (ITestBean) bf.getBean("!test");
197         assertTrue(AopUtils.isAopProxy(test));
198         advised = (Advised) test;
199         assertTrue(advised.getTargetSource() instanceof PrototypeTargetSource);
200         assertEquals("Rod", test.getName());
201         // Check that references survived pooling
202
assertEquals("Kerry", test.getSpouse().getName());
203
204
205         ITestBean test2 = (ITestBean) bf.getBean("!test");
206         assertFalse("Prototypes cannot be the same object", test == test2);
207         assertEquals("Rod", test2.getName());
208         assertEquals("Kerry", test2.getSpouse().getName());
209         bf.close();
210     }
211     
212     /*
213     public void testIntroductionIsProxied() throws Exception {
214         BeanFactory bf = getBeanFactory();
215         Object modifiable = bf.getBean("modifiable1");
216         // We can tell it's a CGLIB proxy by looking at the class name
217         System.out.println(modifiable.getClass().getName());
218         assertFalse(modifiable.getClass().getName().equals(ModifiableTestBean.class.getName()));
219     }
220     */

221
222     public void testTransactionAttributeOnMethod() throws Exception JavaDoc {
223         BeanFactory bf = getBeanFactory();
224         ITestBean test = (ITestBean) bf.getBean("test");
225
226         CountingTxManager txMan = (CountingTxManager) bf.getBean(TXMANAGER_BEAN_NAME);
227         OrderedTxCheckAdvisor txc = (OrderedTxCheckAdvisor) bf.getBean("orderedBeforeTransaction");
228         assertEquals(0, txc.getCountingBeforeAdvice().getCalls());
229
230         assertEquals(0, txMan.commits);
231         assertEquals("Initial value was correct", 4, test.getAge());
232         int newAge = 5;
233         test.setAge(newAge);
234         assertEquals(1, txc.getCountingBeforeAdvice().getCalls());
235
236         assertEquals("New value set correctly", newAge, test.getAge());
237         assertEquals("Transaction counts match", 1, txMan.commits);
238     }
239
240     /**
241      * Should not roll back on servlet exception.
242      */

243     public void testRollbackRulesOnMethodCauseRollback() throws Exception JavaDoc {
244         BeanFactory bf = getBeanFactory();
245         Rollback rb = (Rollback) bf.getBean("rollback");
246
247         CountingTxManager txMan = (CountingTxManager) bf.getBean(TXMANAGER_BEAN_NAME);
248         OrderedTxCheckAdvisor txc = (OrderedTxCheckAdvisor) bf.getBean("orderedBeforeTransaction");
249         assertEquals(0, txc.getCountingBeforeAdvice().getCalls());
250
251         assertEquals(0, txMan.commits);
252         rb.echoException(null);
253         // Fires only on setters
254
assertEquals(0, txc.getCountingBeforeAdvice().getCalls());
255         assertEquals("Transaction counts match", 1, txMan.commits);
256
257         assertEquals(0, txMan.rollbacks);
258         Exception JavaDoc ex = new Exception JavaDoc();
259         try {
260             rb.echoException(ex);
261         }
262         catch (Exception JavaDoc actual) {
263             assertEquals(ex, actual);
264         }
265         assertEquals("Transaction counts match", 1, txMan.rollbacks);
266     }
267
268     public void testRollbackRulesOnMethodPreventRollback() throws Exception JavaDoc {
269         BeanFactory bf = getBeanFactory();
270         Rollback rb = (Rollback) bf.getBean("rollback");
271
272         CountingTxManager txMan = (CountingTxManager) bf.getBean(TXMANAGER_BEAN_NAME);
273
274         assertEquals(0, txMan.commits);
275         // Should NOT roll back on ServletException
276
try {
277             rb.echoException(new ServletException JavaDoc());
278         }
279         catch (ServletException JavaDoc ex) {
280
281         }
282         assertEquals("Transaction counts match", 1, txMan.commits);
283     }
284
285     public void testProgrammaticRollback() throws Exception JavaDoc {
286         BeanFactory bf = getBeanFactory();
287
288         assertTrue(bf.getBean(TXMANAGER_BEAN_NAME) instanceof CountingTxManager);
289         CountingTxManager txMan = (CountingTxManager) bf.getBean(TXMANAGER_BEAN_NAME);
290
291         Rollback rb = (Rollback) bf.getBean("rollback");
292         assertEquals(0, txMan.commits);
293         rb.rollbackOnly(false);
294         assertEquals("Transaction counts match", 1, txMan.commits);
295         assertEquals(0, txMan.rollbacks);
296         // Will cause rollback only
297
rb.rollbackOnly(true);
298         assertEquals(1, txMan.rollbacks);
299     }
300
301     
302     /**
303      * Tests an introduction pointcut. This is a prototype, so that it can add
304      * a Modifiable mixin. Tests that the autoproxy infrastructure can create
305      * advised objects with independent interceptor instances.
306      * The Modifiable behaviour of each instance of TestBean should be distinct.
307      */

308     /*
309     public void testIntroductionViaPrototype() throws Exception {
310         BeanFactory bf = getBeanFactory();
311
312         Object o = bf.getBean("modifiable1");
313         ITestBean modifiable1 = (ITestBean) bf.getBean("modifiable1");
314         ITestBean modifiable2 = (ITestBean) bf.getBean("modifiable2");
315         
316         Advised pc = (Advised) modifiable1;
317         System.err.println(pc.toProxyConfigString());
318         
319         // For convenience only
320         Modifiable mod1 = (Modifiable) modifiable1;
321         Modifiable mod2 = (Modifiable) modifiable2;
322         
323         assertFalse(mod1.isModified());
324         assertFalse(mod2.isModified());
325         
326         int newAge = 33;
327         modifiable1.setAge(newAge);
328         assertTrue(mod1.isModified());
329         // Changes to one shouldn't have affected the other
330         assertFalse("Instances of prototype introduction pointcut don't seem distinct", mod2.isModified());
331         mod1.acceptChanges();
332         assertFalse(mod1.isModified());
333         assertEquals(modifiable1.getAge(), newAge);
334         assertFalse(mod1.isModified());
335     }
336     */

337
338 }
339
Popular Tags