KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > transaction > interceptor > AttributesTransactionAttributeSourceTests


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.transaction.interceptor;
18
19 import java.lang.reflect.Field JavaDoc;
20 import java.lang.reflect.Method JavaDoc;
21 import java.util.Map JavaDoc;
22
23 import javax.servlet.ServletException JavaDoc;
24
25 import junit.framework.TestCase;
26
27 import org.springframework.beans.ITestBean;
28 import org.springframework.beans.TestBean;
29 import org.springframework.metadata.Attributes;
30 import org.springframework.metadata.commons.CommonsAttributes;
31 import org.springframework.metadata.support.MapAttributes;
32 import org.springframework.transaction.TransactionDefinition;
33
34 /**
35  * @author Rod Johnson
36  */

37 public class AttributesTransactionAttributeSourceTests extends TestCase {
38
39     public void testNullOrEmpty() throws Exception JavaDoc {
40         Method JavaDoc method = ITestBean.class.getMethod("getAge", (Class JavaDoc[]) null);
41
42         MapAttributes mar = new MapAttributes();
43         mar.register(method, null);
44         AttributesTransactionAttributeSource atas = new AttributesTransactionAttributeSource(mar);
45         assertNull(atas.getTransactionAttribute(method, null));
46
47         mar.register(method, new Object JavaDoc[0]);
48         assertNull(atas.getTransactionAttribute(method, null));
49         // Try again in case of caching
50
assertNull(atas.getTransactionAttribute(method, null));
51     }
52
53     public void testSingleTransactionAttribute() throws Exception JavaDoc {
54         Method JavaDoc method = ITestBean.class.getMethod("getAge", (Class JavaDoc[]) null);
55
56         TransactionAttribute txAtt = new DefaultTransactionAttribute();
57
58         MapAttributes ma = new MapAttributes();
59         ma.register(method, new Object JavaDoc[]{txAtt});
60         AttributesTransactionAttributeSource atas = new AttributesTransactionAttributeSource(ma);
61         TransactionAttribute actual = atas.getTransactionAttribute(method, method.getDeclaringClass());
62         assertEquals(txAtt, actual);
63         // Check that the same attribute comes back if we ask twice
64
assertSame(txAtt, atas.getTransactionAttribute(method, method.getDeclaringClass()));
65     }
66
67
68     public void testTransactionAttributeAmongOthers() throws Exception JavaDoc {
69         Method JavaDoc method = TestBean.class.getMethod("getAge", (Class JavaDoc[]) null);
70
71         TransactionAttribute txAtt = new DefaultTransactionAttribute();
72
73         MapAttributes ma = new MapAttributes();
74         AttributesTransactionAttributeSource atas = new AttributesTransactionAttributeSource(ma);
75         ma.register(method, new Object JavaDoc[]{new Object JavaDoc(), "", txAtt, "er"});
76         TransactionAttribute actual = atas.getTransactionAttribute(method, method.getDeclaringClass());
77         assertEquals(txAtt, actual);
78         assertSame(txAtt, atas.getTransactionAttribute(method, method.getDeclaringClass()));
79     }
80
81     public void testOverloadedMethodsGetDistinctTransactionAttributes() throws Exception JavaDoc {
82         Method JavaDoc method1 = TestBeanWithOverloadedMethod.class.getMethod("getAge", (Class JavaDoc[]) null);
83         Method JavaDoc method2 = TestBeanWithOverloadedMethod.class.getMethod("getAge", new Class JavaDoc[]{int.class});
84
85         TransactionAttribute txAtt1 = new DefaultTransactionAttribute();
86         TransactionAttribute txAtt2 = new DefaultTransactionAttribute(TransactionDefinition.PROPAGATION_NEVER);
87
88         MapAttributes ma = new MapAttributes();
89         AttributesTransactionAttributeSource atas = new AttributesTransactionAttributeSource(ma);
90         ma.register(method1, new Object JavaDoc[]{new Object JavaDoc(), "", txAtt1, "er"});
91         ma.register(method2, new Object JavaDoc[]{txAtt2});
92
93         TransactionAttribute actual = atas.getTransactionAttribute(method1, TestBeanWithOverloadedMethod.class);
94         assertEquals(txAtt1, actual);
95         assertSame(txAtt1, atas.getTransactionAttribute(method1, method1.getDeclaringClass()));
96
97         TransactionAttribute actual2 = atas.getTransactionAttribute(method2, TestBeanWithOverloadedMethod.class);
98         assertEquals(txAtt2, actual2);
99         assertSame(txAtt2, atas.getTransactionAttribute(method2, method2.getDeclaringClass()));
100     }
101
102     /**
103      * Test the important case where the invocation is on a proxied interface method, but
104      * the attribute is defined on the target class
105      */

106     public void testTransactionAttributeDeclaredOnClassMethod() throws Exception JavaDoc {
107         Method JavaDoc classMethod = TestBean.class.getMethod("getAge", (Class JavaDoc[]) null);
108         Method JavaDoc interfaceMethod = ITestBean.class.getMethod("getAge", (Class JavaDoc[]) null);
109
110         TransactionAttribute txAtt = new DefaultTransactionAttribute();
111
112         MapAttributes ma = new MapAttributes();
113         AttributesTransactionAttributeSource atas = new AttributesTransactionAttributeSource(ma);
114         ma.register(classMethod, new Object JavaDoc[]{new Object JavaDoc(), "", txAtt, "er"});
115         // Target class implements ITestBean
116
TransactionAttribute actual = atas.getTransactionAttribute(interfaceMethod, TestBean.class);
117         assertEquals(txAtt, actual);
118     }
119
120     public void testTransactionAttributeDeclaredOnInterfaceMethodOnly() throws Exception JavaDoc {
121         Method JavaDoc interfaceMethod = ITestBean.class.getMethod("getAge", (Class JavaDoc[]) null);
122
123         TransactionAttribute txAtt = new DefaultTransactionAttribute();
124
125         MapAttributes ma = new MapAttributes();
126         AttributesTransactionAttributeSource atas = new AttributesTransactionAttributeSource(ma);
127         ma.register(interfaceMethod, new Object JavaDoc[]{new Object JavaDoc(), "", txAtt, "er"});
128         // Target class implements ITestBean
129
TransactionAttribute actual = atas.getTransactionAttribute(interfaceMethod, TestBean.class);
130         assertEquals(txAtt, actual);
131     }
132
133     public void testTransactionAttributeDeclaredOnTargetClassMethodTakesPrecedenceOverAttributeDeclaredOnInterfaceMethod() throws Exception JavaDoc {
134         Method JavaDoc classMethod = TestBean.class.getMethod("getAge", (Class JavaDoc[]) null);
135         Method JavaDoc interfaceMethod = ITestBean.class.getMethod("getAge", (Class JavaDoc[]) null);
136
137         TransactionAttribute interfaceAtt = new DefaultTransactionAttribute();
138         TransactionAttribute classAtt = new DefaultTransactionAttribute();
139
140         MapAttributes ma = new MapAttributes();
141         AttributesTransactionAttributeSource atas = new AttributesTransactionAttributeSource(ma);
142         ma.register(interfaceMethod, new Object JavaDoc[]{new Object JavaDoc(), "", interfaceAtt, "er"});
143         ma.register(classMethod, new Object JavaDoc[]{new Object JavaDoc(), "", classAtt, "er"});
144         // Target class implements ITestBean
145
TransactionAttribute actual = atas.getTransactionAttribute(interfaceMethod, TestBean.class);
146         assertEquals(classAtt, actual);
147     }
148
149     public void testRollbackRulesAreApplied() throws Exception JavaDoc {
150         Method JavaDoc method = TestBean.class.getMethod("getAge", (Class JavaDoc[]) null);
151
152         MapAttributes ma = new MapAttributes();
153         TransactionAttribute txAtt = new RuleBasedTransactionAttribute();
154         RollbackRuleAttribute rr = new RollbackRuleAttribute("java.lang.Exception");
155         RollbackRuleAttribute nrr = new NoRollbackRuleAttribute("ServletException");
156
157         AttributesTransactionAttributeSource atas = new AttributesTransactionAttributeSource(ma);
158
159         ma.register(method, new Object JavaDoc[]{new Object JavaDoc(), "", txAtt, rr, nrr, "er"});
160         TransactionAttribute actual = atas.getTransactionAttribute(method, method.getDeclaringClass());
161         assertEquals(txAtt, actual);
162         assertTrue(txAtt.rollbackOn(new Exception JavaDoc()));
163         assertFalse(txAtt.rollbackOn(new ServletException JavaDoc()));
164
165         assertSame(txAtt, atas.getTransactionAttribute(method, method.getDeclaringClass()));
166     }
167
168     /**
169      * Test that transaction attribute is inherited from class
170      * if not specified on method.
171      */

172     public void testDefaultsToClassTransactionAttribute() throws Exception JavaDoc {
173         Method JavaDoc method = TestBean.class.getMethod("getAge", (Class JavaDoc[]) null);
174
175         TransactionAttribute txAtt = new DefaultTransactionAttribute();
176         MapAttributes ma = new MapAttributes();
177         AttributesTransactionAttributeSource atas = new AttributesTransactionAttributeSource(ma);
178         ma.register(TestBean.class, new Object JavaDoc[]{new Object JavaDoc(), "", txAtt, "er"});
179         TransactionAttribute actual = atas.getTransactionAttribute(method, null);
180         assertEquals(txAtt, actual);
181     }
182
183     public void testSPR418UnboundedCacheSizeGrowth() throws Exception JavaDoc {
184         Attributes attributes = new CommonsAttributes();
185         AttributesTransactionAttributeSource attributeSource =
186                 new AttributesTransactionAttributeSource(attributes);
187
188         //Method m = PrototypeBean.class.getMethod("doNothing", new Class[0]);
189
for (int i = 0; i < 100; i++) {
190             PrototypeBean bean = new PrototypeBean();
191             Method JavaDoc m = bean.getClass().getMethod("doNothing", new Class JavaDoc[0]);
192             attributeSource.getTransactionAttribute(m, bean.getClass());
193             Map JavaDoc cache = (Map JavaDoc) getPrivateField(attributeSource,
194                     AbstractFallbackTransactionAttributeSource.class, "cache");
195             assertEquals("Cache size should not increase: i=" + i, 1, cache.size());
196         }
197     }
198
199     private static Object JavaDoc getPrivateField(Object JavaDoc obj, Class JavaDoc clazz,
200             String JavaDoc fieldName) throws NoSuchFieldException JavaDoc,
201             IllegalAccessException JavaDoc {
202         Field JavaDoc field = clazz.getDeclaredField(fieldName);
203         field.setAccessible(true);
204         return field.get(obj);
205     }
206
207
208     public static class TestBeanWithOverloadedMethod extends TestBean {
209
210         public int getAge(int i) {
211             return i;
212         }
213     }
214
215
216     private static class PrototypeBean {
217
218         public void doNothing() {
219         }
220     }
221
222 }
223
Popular Tags