KickJava   Java API By Example, From Geeks To Geeks.

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


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 javax.servlet.ServletException JavaDoc;
20
21 import junit.framework.TestCase;
22
23 import org.springframework.mail.MailSendException;
24 import org.springframework.transaction.TransactionDefinition;
25
26 /**
27  * Tests to check conversion from String to TransactionAttribute.
28  *
29  * @author Rod Johnson
30  * @author Juergen Hoeller
31  * @since 26.04.2003
32  */

33 public class TransactionAttributeEditorTests extends TestCase {
34     
35     public void testNull() {
36         TransactionAttributeEditor pe = new TransactionAttributeEditor();
37         pe.setAsText(null);
38         TransactionAttribute ta = (TransactionAttribute) pe.getValue();
39         assertTrue(ta == null);
40     }
41     
42     public void testEmptyString() {
43         TransactionAttributeEditor pe = new TransactionAttributeEditor();
44         pe.setAsText("");
45         TransactionAttribute ta = (TransactionAttribute) pe.getValue();
46         assertTrue(ta == null);
47     }
48     
49     public void testValidPropagationCodeOnly() {
50         TransactionAttributeEditor pe = new TransactionAttributeEditor();
51         pe.setAsText("PROPAGATION_REQUIRED");
52         TransactionAttribute ta = (TransactionAttribute) pe.getValue();
53         assertTrue(ta != null);
54         assertTrue(ta.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRED);
55         assertTrue(ta.getIsolationLevel() == TransactionDefinition.ISOLATION_DEFAULT);
56         assertTrue(!ta.isReadOnly());
57     }
58     
59     public void testInvalidPropagationCodeOnly() {
60         TransactionAttributeEditor pe = new TransactionAttributeEditor();
61         try {
62             pe.setAsText("XXPROPAGATION_REQUIRED");
63             fail("Should have failed with bogus propagation code");
64         }
65         catch (IllegalArgumentException JavaDoc ex) {
66             // Ok
67
}
68     }
69     
70     public void testValidPropagationCodeAndIsolationCode() {
71         TransactionAttributeEditor pe = new TransactionAttributeEditor();
72         pe.setAsText("PROPAGATION_REQUIRED, ISOLATION_READ_UNCOMMITTED");
73         TransactionAttribute ta = (TransactionAttribute) pe.getValue();
74         assertTrue(ta != null);
75         assertTrue(ta.getPropagationBehavior() == TransactionDefinition.PROPAGATION_REQUIRED);
76         assertTrue(ta.getIsolationLevel() == TransactionDefinition.ISOLATION_READ_UNCOMMITTED);
77     }
78     
79     public void testValidPropagationAndIsolationCodesAndInvalidRollbackRule() {
80         TransactionAttributeEditor pe = new TransactionAttributeEditor();
81         try {
82             pe.setAsText("PROPAGATION_REQUIRED,ISOLATION_READ_UNCOMMITTED,XXX");
83             fail("Should have failed with bogus rollback rule");
84         }
85         catch (IllegalArgumentException JavaDoc ex) {
86             // Ok
87
}
88     }
89     
90     public void testValidPropagationCodeAndIsolationCodeAndRollbackRules1() {
91         TransactionAttributeEditor pe = new TransactionAttributeEditor();
92         pe.setAsText("PROPAGATION_MANDATORY,ISOLATION_REPEATABLE_READ,timeout_10,-ServletException,+MailSendException");
93         TransactionAttribute ta = (TransactionAttribute) pe.getValue();
94         assertNotNull(ta);
95         assertEquals(ta.getPropagationBehavior(), TransactionDefinition.PROPAGATION_MANDATORY);
96         assertEquals(ta.getIsolationLevel(), TransactionDefinition.ISOLATION_REPEATABLE_READ);
97         assertEquals(ta.getTimeout(), 10);
98         assertFalse(ta.isReadOnly());
99         assertTrue(ta.rollbackOn(new RuntimeException JavaDoc()));
100         assertFalse(ta.rollbackOn(new Exception JavaDoc()));
101         // Check for our bizarre customized rollback rules
102
assertTrue(ta.rollbackOn(new ServletException JavaDoc()));
103         assertTrue(!ta.rollbackOn(new MailSendException("")));
104     }
105
106     public void testValidPropagationCodeAndIsolationCodeAndRollbackRules2() {
107         TransactionAttributeEditor pe = new TransactionAttributeEditor();
108         pe.setAsText("+ServletException,readOnly,ISOLATION_READ_COMMITTED,-MailSendException,PROPAGATION_SUPPORTS");
109         TransactionAttribute ta = (TransactionAttribute) pe.getValue();
110         assertNotNull(ta);
111         assertEquals(ta.getPropagationBehavior(), TransactionDefinition.PROPAGATION_SUPPORTS);
112         assertEquals(ta.getIsolationLevel(), TransactionDefinition.ISOLATION_READ_COMMITTED);
113         assertEquals(ta.getTimeout(), TransactionDefinition.TIMEOUT_DEFAULT);
114         assertTrue(ta.isReadOnly());
115         assertTrue(ta.rollbackOn(new RuntimeException JavaDoc()));
116         assertFalse(ta.rollbackOn(new Exception JavaDoc()));
117         // Check for our bizarre customized rollback rules
118
assertFalse(ta.rollbackOn(new ServletException JavaDoc()));
119         assertTrue(ta.rollbackOn(new MailSendException("")));
120     }
121
122     public void testDefaultTransactionAttributeToString() {
123         DefaultTransactionAttribute source = new DefaultTransactionAttribute();
124         source.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
125         source.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ);
126         source.setTimeout(10);
127         source.setReadOnly(true);
128
129         TransactionAttributeEditor pe = new TransactionAttributeEditor();
130         pe.setAsText(source.toString());
131         TransactionAttribute ta = (TransactionAttribute) pe.getValue();
132         assertEquals(ta, source);
133         assertEquals(ta.getPropagationBehavior(), TransactionDefinition.PROPAGATION_SUPPORTS);
134         assertEquals(ta.getIsolationLevel(), TransactionDefinition.ISOLATION_REPEATABLE_READ);
135         assertEquals(ta.getTimeout(), 10);
136         assertTrue(ta.isReadOnly());
137         assertTrue(ta.rollbackOn(new RuntimeException JavaDoc()));
138         assertFalse(ta.rollbackOn(new Exception JavaDoc()));
139
140         source.setTimeout(9);
141         assertNotSame(ta, source);
142         source.setTimeout(10);
143         assertEquals(ta, source);
144     }
145
146     public void testRuleBasedTransactionAttributeToString() {
147         RuleBasedTransactionAttribute source = new RuleBasedTransactionAttribute();
148         source.setPropagationBehavior(TransactionDefinition.PROPAGATION_SUPPORTS);
149         source.setIsolationLevel(TransactionDefinition.ISOLATION_REPEATABLE_READ);
150         source.setTimeout(10);
151         source.setReadOnly(true);
152         source.getRollbackRules().add(new RollbackRuleAttribute("IllegalArgumentException"));
153         source.getRollbackRules().add(new NoRollbackRuleAttribute("IllegalStateException"));
154
155         TransactionAttributeEditor pe = new TransactionAttributeEditor();
156         pe.setAsText(source.toString());
157         TransactionAttribute ta = (TransactionAttribute) pe.getValue();
158         assertEquals(ta, source);
159         assertEquals(ta.getPropagationBehavior(), TransactionDefinition.PROPAGATION_SUPPORTS);
160         assertEquals(ta.getIsolationLevel(), TransactionDefinition.ISOLATION_REPEATABLE_READ);
161         assertEquals(ta.getTimeout(), 10);
162         assertTrue(ta.isReadOnly());
163         assertTrue(ta.rollbackOn(new IllegalArgumentException JavaDoc()));
164         assertFalse(ta.rollbackOn(new IllegalStateException JavaDoc()));
165
166         source.getRollbackRules().clear();
167         assertNotSame(ta, source);
168         source.getRollbackRules().add(new RollbackRuleAttribute("IllegalArgumentException"));
169         source.getRollbackRules().add(new NoRollbackRuleAttribute("IllegalStateException"));
170         assertEquals(ta, source);
171     }
172
173 }
174
Popular Tags