KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > framework > autoproxy > metadata > AbstractMetadataAutoProxyTests


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.metadata;
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.support.AopUtils;
27 import org.springframework.aop.target.AbstractPoolingTargetSource;
28 import org.springframework.aop.target.PrototypeTargetSource;
29 import org.springframework.aop.target.ThreadLocalTargetSource;
30 import org.springframework.beans.ITestBean;
31 import org.springframework.beans.TestBean;
32 import org.springframework.beans.factory.BeanFactory;
33 import org.springframework.transaction.CountingTxManager;
34
35 /**
36  * Abstract tests for auto-proxying. Subclasses must load the appropriate
37  * bean factory defining the necessary beans and their transaction attributes.
38  *
39  * <p>See the enterpriseServices.xml file for definitions of beans.
40  *
41  * @author Rod Johnson
42  */

43 public abstract class AbstractMetadataAutoProxyTests extends TestCase {
44     
45     private static final String JavaDoc TXMANAGER_BEAN_NAME = "es.txManager";
46
47     /**
48      * Return a bean factory with attributes and EnterpriseServices configured.
49      */

50     protected abstract BeanFactory getBeanFactory() throws IOException JavaDoc;
51
52     /**
53      * If no pointcuts match (no atts) there should be proxying.
54      */

55     public void testNoProxy() throws Exception JavaDoc {
56         BeanFactory bf = getBeanFactory();
57         TestBean tb = (TestBean) bf.getBean("noProxy");
58         // We can tell it's not a CGLIB proxy by looking at the class name
59
assertTrue(tb.getClass().getName().equals(TestBean.class.getName()));
60     }
61     
62     public void testTxIsProxied() throws Exception JavaDoc {
63         BeanFactory bf = getBeanFactory();
64         TxClass txClass = (TxClass) bf.getBean("txClass");
65         assertTrue(AopUtils.isAopProxy(txClass));
66     }
67     
68     public void testIntroductionIsProxied() throws Exception JavaDoc {
69         BeanFactory bf = getBeanFactory();
70         Object JavaDoc modifiable = bf.getBean("modifiable1");
71         assertTrue(AopUtils.isAopProxy(modifiable));
72     }
73
74     /**
75      * @see junit.framework.TestCase#setUp()
76      */

77     public void testDefaultTransactionAttributeOnMethod() throws Exception JavaDoc {
78         BeanFactory bf = getBeanFactory();
79         TxClass txClass = (TxClass) bf.getBean("txClass");
80         
81         CountingTxManager txMan = (CountingTxManager) bf.getBean(TXMANAGER_BEAN_NAME);
82         
83         assertEquals(0, txMan.commits);
84         int count = txClass.defaultTxAttribute();
85         assertEquals("Return value was correct", 1, count);
86         assertEquals("Transaction counts match", 1, txMan.commits);
87     }
88     
89     
90     /**
91      * Should not roll back on servlet exception
92      * @throws Exception
93      */

94     public void testRollbackRulesOnMethodCauseRollback() throws Exception JavaDoc {
95         BeanFactory bf = getBeanFactory();
96         TxClass txClass = (TxClass) bf.getBean("txClass");
97         assertTrue(AopUtils.isAopProxy(txClass));
98     
99         CountingTxManager txMan = (CountingTxManager) bf.getBean(TXMANAGER_BEAN_NAME);
100     
101         assertEquals(0, txMan.commits);
102         txClass.echoException(null);
103         assertEquals("Transaction counts match", 1, txMan.commits);
104         
105         assertEquals(0, txMan.rollbacks);
106         Exception JavaDoc ex = new Exception JavaDoc();
107         try {
108             txClass.echoException(ex);
109         }
110         catch (Exception JavaDoc actual) {
111             assertEquals(ex, actual);
112         }
113         assertEquals("Transaction counts match", 1, txMan.rollbacks);
114     }
115     
116     public void testRollbackRulesOnMethodPreventRollback() throws Exception JavaDoc {
117         BeanFactory bf = getBeanFactory();
118         TxClass txClass = (TxClass) bf.getBean("txClass");
119
120         CountingTxManager txMan = (CountingTxManager) bf.getBean(TXMANAGER_BEAN_NAME);
121
122         assertEquals(0, txMan.commits);
123         // Should NOT roll back on ServletException
124
try {
125             txClass.echoException(new ServletException JavaDoc());
126         }
127         catch (ServletException JavaDoc ex) {
128             
129         }
130         assertEquals("Transaction counts match", 1, txMan.commits);
131     }
132     
133     public void testProgrammaticRollback() throws Exception JavaDoc {
134         BeanFactory bf = getBeanFactory();
135
136         CountingTxManager txMan = (CountingTxManager) bf.getBean(TXMANAGER_BEAN_NAME);
137         
138         TxClassWithClassAttribute txClass = (TxClassWithClassAttribute) bf.getBean("txClassWithClassAttribute");
139         // No interface, so must be a CGLIB proxy
140
assertTrue(AopUtils.isCglibProxy(txClass));
141         assertEquals(0, txMan.commits);
142         txClass.rollbackOnly(false);
143         assertEquals("Transaction counts match", 1, txMan.commits);
144         assertEquals(0, txMan.rollbacks);
145         txClass.rollbackOnly(true);
146         assertEquals(1, txMan.rollbacks);
147     }
148
149
150     public void testTransactionAttributeForMethodInheritedFromClass() throws Exception JavaDoc {
151         BeanFactory bf = getBeanFactory();
152         TxClassWithClassAttribute txClass = (TxClassWithClassAttribute) bf.getBean("txClassWithClassAttribute");
153     
154         CountingTxManager txMan = (CountingTxManager) bf.getBean(TXMANAGER_BEAN_NAME);
155     
156         assertEquals(0, txMan.commits);
157         int ret = txClass.inheritClassTxAttribute(25);
158         assertEquals("Return value was correct", 25, ret);
159         assertEquals("Transaction counts match when inherited from class", 1, txMan.commits);
160
161         // Check method attribute override class att
162
try {
163              txClass.echoException(new Exception JavaDoc());
164          }
165          catch (Exception JavaDoc ex) {
166         
167          }
168          assertEquals("Transaction rollbacks correct", 1, txMan.rollbacks);
169     }
170     
171     /**
172      * Plain old class with no special attributes.
173      */

174     public void testNoAutoProxying() throws Exception JavaDoc {
175         BeanFactory bf = getBeanFactory();
176         ITestBean test = (ITestBean) bf.getBean("rawTest");
177         assertFalse(AopUtils.isAopProxy(test));
178     }
179     
180     public void testAutoPrototype() throws Exception JavaDoc {
181         BeanFactory bf = getBeanFactory();
182         ITestBean test = (ITestBean) bf.getBean("protoTest");
183         assertTrue(AopUtils.isAopProxy(test));
184         Advised advised = (Advised) test;
185         assertTrue(advised.getTargetSource() instanceof PrototypeTargetSource );
186         ITestBean test2 = (ITestBean) bf.getBean("protoTest");
187         assertFalse(test == test2);
188     }
189     
190     public void testAutoThreadLocal() throws Exception JavaDoc {
191         final BeanFactory bf = getBeanFactory();
192         final ITestBean test = (ITestBean) bf.getBean("threadLocalTest");
193         assertTrue(AopUtils.isAopProxy(test));
194         Advised advised = (Advised) test;
195         assertTrue(advised.getTargetSource() instanceof ThreadLocalTargetSource );
196         String JavaDoc nameForMainThread = "tom";
197         test.setName(nameForMainThread);
198         assertEquals(nameForMainThread, test.getName());
199         // Check that in another thread we don't see that value back
200
Runnable JavaDoc r = new Runnable JavaDoc() {
201             public void run() {
202                 //System.err.println("RUN INNER CLASS");
203
ITestBean myTest = (ITestBean) bf.getBean("threadLocalTest");
204                 assertNull(myTest.getName());
205                 String JavaDoc myName = "Fred";
206                 myTest.setName(myName);
207                 assertEquals(myName, myTest.getName());
208                 //assertEquals(myName, test.getName());
209
}
210         };
211         Thread JavaDoc t = new Thread JavaDoc(r);
212         t.start();
213         t.join();
214         // Inner class didn't change outer thread's value
215
assertEquals(nameForMainThread, test.getName());
216     }
217     
218     /**
219      * Test that the pooling TargetSourceCreator works.
220      */

221     public void testAutoPooling() throws Exception JavaDoc {
222         BeanFactory bf = getBeanFactory();
223         TxClassWithClassAttribute txClass = (TxClassWithClassAttribute) bf.getBean("txClassWithClassAttribute");
224         Advised advised = (Advised) txClass;
225         assertTrue(advised.getTargetSource() instanceof AbstractPoolingTargetSource );
226         AbstractPoolingTargetSource ts = (AbstractPoolingTargetSource) advised.getTargetSource();
227         // Check pool size is that specified in the attribute
228
assertEquals(10, ts.getMaxSize());
229         // Check the object works
230
txClass.rollbackOnly(false);
231     }
232
233     
234     /**
235      * Tests an introduction pointcut. This is a prototype, so that it can add
236      * a Modifiable mixin. Tests that the autoproxy infrastructure can create
237      * advised objects with independent interceptor instances.
238      * The Modifiable behaviour of each instance of TestBean should be distinct.
239      */

240     public void testIntroductionViaPrototype() throws Exception JavaDoc {
241         BeanFactory bf = getBeanFactory();
242
243         ITestBean modifiable1 = (ITestBean) bf.getBean("modifiable1");
244         ITestBean modifiable2 = (ITestBean) bf.getBean("modifiable2");
245         
246         //Advised pc = (Advised) modifiable1;
247

248         // For convenience only
249
Modifiable mod1 = (Modifiable) modifiable1;
250         Modifiable mod2 = (Modifiable) modifiable2;
251         
252         assertFalse(mod1.isModified());
253         assertFalse(mod2.isModified());
254         
255         int newAge = 33;
256         modifiable1.setAge(newAge);
257         assertTrue(mod1.isModified());
258         // Changes to one shouldn't have affected the other
259
assertFalse("Instances of prototype introduction don't seem distinct", mod2.isModified());
260         mod1.acceptChanges();
261         assertFalse(mod1.isModified());
262         assertEquals(modifiable1.getAge(), newAge);
263         assertFalse(mod1.isModified());
264     }
265     
266 }
267
Popular Tags