KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > transaction > annotation > AnnotationTransactionAttributeSourceTests


1 /*
2  * Created on Aug 27, 2004
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */

7
8 package org.springframework.transaction.annotation;
9
10 import java.io.IOException JavaDoc;
11 import java.lang.reflect.Method JavaDoc;
12
13 import junit.framework.TestCase;
14
15 import org.springframework.transaction.interceptor.NoRollbackRuleAttribute;
16 import org.springframework.transaction.interceptor.RollbackRuleAttribute;
17 import org.springframework.transaction.interceptor.RuleBasedTransactionAttribute;
18 import org.springframework.transaction.interceptor.TransactionAttribute;
19
20 /**
21  * @author Colin Sampaleanu
22  */

23 public class AnnotationTransactionAttributeSourceTests extends TestCase {
24     
25     public void testNullOrEmpty() throws Exception JavaDoc {
26         
27         Method JavaDoc method = Empty.class.getMethod("getAge", (Class JavaDoc[]) null);
28         
29         AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
30         assertNull(atas.getTransactionAttribute(method, null));
31         
32         // Try again in case of caching
33
assertNull(atas.getTransactionAttribute(method, null));
34     }
35     
36     /**
37      * Test the important case where the invocation is on a proxied interface method, but
38      * the attribute is defined on the target class
39      */

40     public void testTransactionAttributeDeclaredOnClassMethod() throws Exception JavaDoc {
41         Method JavaDoc classMethod = TestBean1.class.getMethod("getAge", (Class JavaDoc[]) null);
42         
43         AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
44         TransactionAttribute actual = atas.getTransactionAttribute(classMethod, TestBean1.class);
45         
46         RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
47         rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception JavaDoc.class));
48         assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules());
49     }
50     
51     /**
52      * Test case where attribute is on the interface method
53      */

54     public void testTransactionAttributeDeclaredOnInterfaceMethodOnly() throws Exception JavaDoc {
55         Method JavaDoc interfaceMethod = ITestBean2.class.getMethod("getAge", (Class JavaDoc[]) null);
56
57         AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
58         TransactionAttribute actual = atas.getTransactionAttribute(interfaceMethod, TestBean2.class);
59         
60         RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
61             assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules());
62     }
63     
64     /**
65      * Test that when an attribute exists on both class and interface, class takes precedence
66      */

67     public void testTransactionAttributeOnTargetClassMethodOverridesAttributeOnInterfaceMethod() throws Exception JavaDoc {
68         Method JavaDoc classMethod = TestBean3.class.getMethod("getAge", (Class JavaDoc[]) null);
69         Method JavaDoc interfaceMethod = ITestBean3.class.getMethod("getAge", (Class JavaDoc[]) null);
70
71         AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
72         TransactionAttribute actual = atas.getTransactionAttribute(interfaceMethod, TestBean3.class);
73         assertEquals(TransactionAttribute.PROPAGATION_REQUIRES_NEW, actual.getPropagationBehavior());
74
75         RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
76         rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception JavaDoc.class));
77         rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException JavaDoc.class));
78         assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules());
79     }
80     
81
82     public void testRollbackRulesAreApplied() throws Exception JavaDoc {
83         Method JavaDoc method = TestBean3.class.getMethod("getAge", (Class JavaDoc[]) null);
84         
85         AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
86         TransactionAttribute actual = atas.getTransactionAttribute(method, TestBean3.class);
87
88         RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
89         rbta.getRollbackRules().add(new RollbackRuleAttribute("java.lang.Exception"));
90         rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException JavaDoc.class));
91
92         assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules());
93         assertTrue(actual.rollbackOn(new Exception JavaDoc()));
94         assertFalse(actual.rollbackOn(new IOException JavaDoc()));
95         
96         actual = atas.getTransactionAttribute(method, method.getDeclaringClass());
97
98         rbta = new RuleBasedTransactionAttribute();
99         rbta.getRollbackRules().add(new RollbackRuleAttribute("java.lang.Exception"));
100         rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException JavaDoc.class));
101         
102         assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules());
103         assertTrue(actual.rollbackOn(new Exception JavaDoc()));
104         assertFalse(actual.rollbackOn(new IOException JavaDoc()));
105     }
106
107     /**
108      * Test that transaction attribute is inherited from class
109      * if not specified on method
110      */

111     public void testDefaultsToClassTransactionAttribute() throws Exception JavaDoc {
112         Method JavaDoc method = TestBean4.class.getMethod("getAge", (Class JavaDoc[]) null);
113
114         AnnotationTransactionAttributeSource atas = new AnnotationTransactionAttributeSource();
115         TransactionAttribute actual = atas.getTransactionAttribute(method, TestBean4.class);
116         
117         RuleBasedTransactionAttribute rbta = new RuleBasedTransactionAttribute();
118         rbta.getRollbackRules().add(new RollbackRuleAttribute(Exception JavaDoc.class));
119         rbta.getRollbackRules().add(new NoRollbackRuleAttribute(IOException JavaDoc.class));
120         assertEquals(rbta.getRollbackRules(), ((RuleBasedTransactionAttribute) actual).getRollbackRules());
121     }
122
123     
124     public interface ITestBean {
125         
126         int getAge();
127         
128         void setAge(int age);
129         
130         String JavaDoc getName();
131         
132         void setName(String JavaDoc name);
133     }
134     
135     public interface ITestBean2 {
136         
137         @Transactional
138         int getAge();
139         
140         void setAge(int age);
141         
142         String JavaDoc getName();
143         
144         void setName(String JavaDoc name);
145     }
146
147     public interface ITestBean3 {
148         
149         @Transactional()
150         int getAge();
151         
152         void setAge(int age);
153         
154         String JavaDoc getName();
155         
156         void setName(String JavaDoc name);
157     }
158
159     
160     public static class Empty implements ITestBean {
161
162         private String JavaDoc name;
163
164         private int age;
165
166         public Empty() {
167         }
168
169         public Empty(String JavaDoc name, int age) {
170             this.name = name;
171             this.age = age;
172         }
173
174         public String JavaDoc getName() {
175             return name;
176         }
177
178         public void setName(String JavaDoc name) {
179             this.name = name;
180         }
181
182         public int getAge() {
183             return age;
184         }
185
186         public void setAge(int age) {
187             this.age = age;
188         }
189     }
190     
191     
192     public static class TestBean1 implements ITestBean {
193
194         private String JavaDoc name;
195
196         private int age;
197
198         public TestBean1() {
199         }
200
201         public TestBean1(String JavaDoc name, int age) {
202             this.name = name;
203             this.age = age;
204         }
205
206         public String JavaDoc getName() {
207             return name;
208         }
209
210         public void setName(String JavaDoc name) {
211             this.name = name;
212         }
213
214         @Transactional(rollbackFor=Exception JavaDoc.class)
215         public int getAge() {
216             return age;
217         }
218
219         public void setAge(int age) {
220             this.age = age;
221         }
222     }
223
224
225     public static class TestBean2 implements ITestBean2 {
226
227         private String JavaDoc name;
228
229         private int age;
230
231         public TestBean2() {
232         }
233
234         public TestBean2(String JavaDoc name, int age) {
235             this.name = name;
236             this.age = age;
237         }
238
239         public String JavaDoc getName() {
240             return name;
241         }
242
243         public void setName(String JavaDoc name) {
244             this.name = name;
245         }
246
247         public int getAge() {
248             return age;
249         }
250
251         public void setAge(int age) {
252             this.age = age;
253         }
254     }
255
256
257     public static class TestBean3 implements ITestBean3 {
258
259         private String JavaDoc name;
260
261         private int age;
262
263         public TestBean3() {
264         }
265
266         public TestBean3(String JavaDoc name, int age) {
267             this.name = name;
268             this.age = age;
269         }
270
271         public String JavaDoc getName() {
272             return name;
273         }
274
275         public void setName(String JavaDoc name) {
276             this.name = name;
277         }
278
279         @Transactional(propagation=Propagation.REQUIRES_NEW, rollbackFor=Exception JavaDoc.class, noRollbackFor={IOException JavaDoc.class})
280         public int getAge() {
281             return age;
282         }
283
284         public void setAge(int age) {
285             this.age = age;
286         }
287     }
288
289
290     @Transactional(rollbackFor=Exception JavaDoc.class, noRollbackFor={IOException JavaDoc.class})
291     public static class TestBean4 implements ITestBean3 {
292
293         private String JavaDoc name;
294
295         private int age;
296
297         public TestBean4() {
298         }
299
300         public TestBean4(String JavaDoc name, int age) {
301             this.name = name;
302             this.age = age;
303         }
304
305         public String JavaDoc getName() {
306             return name;
307         }
308
309         public void setName(String JavaDoc name) {
310             this.name = name;
311         }
312
313         public int getAge() {
314             return age;
315         }
316
317         public void setAge(int age) {
318             this.age = age;
319         }
320     }
321
322 }
323
Popular Tags