KickJava   Java API By Example, From Geeks To Geeks.

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


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.rmi.RemoteException JavaDoc;
20 import java.util.Collections JavaDoc;
21 import java.util.LinkedList JavaDoc;
22 import java.util.List JavaDoc;
23
24 import javax.servlet.ServletException JavaDoc;
25
26 import junit.framework.TestCase;
27
28 import org.springframework.mail.MailSendException;
29 import org.springframework.transaction.TransactionDefinition;
30
31 /**
32  *
33  * @author Rod Johnson
34  * @since 09.04.2003
35  */

36 public class RuleBasedTransactionAttributeTests extends TestCase {
37
38     public void testDefaultRule() {
39         RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute();
40         assertTrue(rta.rollbackOn(new RuntimeException JavaDoc()));
41         assertTrue(rta.rollbackOn(new MailSendException("")));
42         assertTrue(!rta.rollbackOn(new Exception JavaDoc()));
43         assertTrue(!rta.rollbackOn(new ServletException JavaDoc()));
44     }
45     
46     /**
47      * Test one checked exception that should roll back
48      *
49      */

50     public void testRuleForRollbackOnChecked() {
51         List JavaDoc l = new LinkedList JavaDoc();
52         l.add(new RollbackRuleAttribute("javax.servlet.ServletException"));
53         RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, l);
54         
55         assertTrue(rta.rollbackOn(new RuntimeException JavaDoc()));
56         assertTrue(rta.rollbackOn(new MailSendException("")));
57         assertTrue(!rta.rollbackOn(new Exception JavaDoc()));
58         // Check that default behaviour is overridden
59
assertTrue(rta.rollbackOn(new ServletException JavaDoc()));
60     }
61     
62     public void testRuleForCommitOnUnchecked() {
63         List JavaDoc l = new LinkedList JavaDoc();
64         l.add(new NoRollbackRuleAttribute("org.springframework.mail.MailSendException"));
65         l.add(new RollbackRuleAttribute("javax.servlet.ServletException"));
66         RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, l);
67         
68         assertTrue(rta.rollbackOn(new RuntimeException JavaDoc()));
69         // Check default behaviour is overridden
70
assertTrue(!rta.rollbackOn(new MailSendException("")));
71         assertTrue(!rta.rollbackOn(new Exception JavaDoc()));
72         // Check that default behaviour is overridden
73
assertTrue(rta.rollbackOn(new ServletException JavaDoc()));
74     }
75     
76     public void testRuleForSelectiveRollbackOnCheckedWithString() {
77         List JavaDoc l = new LinkedList JavaDoc();
78         l.add(new RollbackRuleAttribute("java.rmi.RemoteException"));
79         RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, l);
80         testRuleForSelectiveRollbackOnChecked(rta);
81     }
82     
83     public void testRuleForSelectiveRollbackOnCheckedWithClass() {
84         List JavaDoc l = Collections.singletonList(new RollbackRuleAttribute(RemoteException JavaDoc.class));
85         RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, l);
86         testRuleForSelectiveRollbackOnChecked(rta);
87     }
88     
89     private void testRuleForSelectiveRollbackOnChecked(RuleBasedTransactionAttribute rta) {
90         assertTrue(rta.rollbackOn(new RuntimeException JavaDoc()));
91         // Check default behaviour is overridden
92
assertTrue(!rta.rollbackOn(new Exception JavaDoc()));
93         // Check that default behaviour is overridden
94
assertTrue(rta.rollbackOn(new RemoteException JavaDoc()));
95     }
96     
97     /**
98      * Check that a rule can cause commit on a ServletException
99      * when Exception prompts a rollback.
100      */

101     public void testRuleForCommitOnSubclassOfChecked() {
102         List JavaDoc l = new LinkedList JavaDoc();
103         // Note that it's important to ensure that we have this as
104
// a FQN: otherwise it will match everything!
105
l.add(new RollbackRuleAttribute("java.lang.Exception"));
106         l.add(new NoRollbackRuleAttribute("ServletException"));
107         RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, l);
108
109         assertTrue(rta.rollbackOn(new RuntimeException JavaDoc()));
110         assertTrue(rta.rollbackOn(new Exception JavaDoc()));
111         // Check that default behaviour is overridden
112
assertFalse(rta.rollbackOn(new ServletException JavaDoc()));
113     }
114     
115     public void testRollbackNever() {
116         List JavaDoc l = new LinkedList JavaDoc();
117         l.add(new NoRollbackRuleAttribute("Throwable"));
118         RuleBasedTransactionAttribute rta = new RuleBasedTransactionAttribute(TransactionDefinition.PROPAGATION_REQUIRED, l);
119     
120         assertTrue(!rta.rollbackOn(new Throwable JavaDoc()));
121         assertTrue(!rta.rollbackOn(new RuntimeException JavaDoc()));
122         assertTrue(!rta.rollbackOn(new MailSendException("")));
123         assertTrue(!rta.rollbackOn(new Exception JavaDoc()));
124         assertTrue(!rta.rollbackOn(new ServletException JavaDoc()));
125     }
126
127 }
128
Popular Tags