KickJava   Java API By Example, From Geeks To Geeks.

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


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.Method JavaDoc;
20
21 import junit.framework.TestCase;
22
23 import org.springframework.transaction.TransactionDefinition;
24
25 /**
26  * Format is
27  * <code>FQN.Method=tx attribute representation</code>
28  *
29  * @author Rod Johnson
30  * @since 26.04.2003
31  */

32 public class TransactionAttributeSourceEditorTests extends TestCase {
33
34     public void testNull() throws Exception JavaDoc {
35         TransactionAttributeSourceEditor pe = new TransactionAttributeSourceEditor();
36         pe.setAsText(null);
37         TransactionAttributeSource tas = (TransactionAttributeSource) pe.getValue();
38
39         Method JavaDoc m = Object JavaDoc.class.getMethod("hashCode", (Class JavaDoc[]) null);
40         assertTrue(tas.getTransactionAttribute(m, null) == null);
41     }
42
43     public void testInvalid() throws Exception JavaDoc {
44         TransactionAttributeSourceEditor pe = new TransactionAttributeSourceEditor();
45         try {
46             pe.setAsText("foo=bar");
47             fail();
48         }
49         catch (IllegalArgumentException JavaDoc ex) {
50             // Ok
51
}
52     }
53     
54     public void testMatchesSpecific() throws Exception JavaDoc {
55         TransactionAttributeSourceEditor pe = new TransactionAttributeSourceEditor();
56         // TODO need FQN?
57
pe.setAsText(
58             "java.lang.Object.hashCode=PROPAGATION_REQUIRED\n" +
59             "java.lang.Object.equals=PROPAGATION_MANDATORY\n" +
60             "java.lang.Object.*it=PROPAGATION_SUPPORTS\n" +
61             "java.lang.Object.notify=PROPAGATION_SUPPORTS\n" +
62             "java.lang.Object.not*=PROPAGATION_REQUIRED");
63         TransactionAttributeSource tas = (TransactionAttributeSource) pe.getValue();
64
65         checkTransactionProperties(tas, Object JavaDoc.class.getMethod("hashCode", (Class JavaDoc[]) null),
66             TransactionDefinition.PROPAGATION_REQUIRED);
67         checkTransactionProperties(tas, Object JavaDoc.class.getMethod("equals", new Class JavaDoc[] { Object JavaDoc.class }),
68             TransactionDefinition.PROPAGATION_MANDATORY);
69         checkTransactionProperties(tas, Object JavaDoc.class.getMethod("wait", (Class JavaDoc[]) null),
70             TransactionDefinition.PROPAGATION_SUPPORTS);
71         checkTransactionProperties(tas, Object JavaDoc.class.getMethod("wait", new Class JavaDoc[] { long.class }),
72             TransactionDefinition.PROPAGATION_SUPPORTS);
73         checkTransactionProperties(tas, Object JavaDoc.class.getMethod("wait", new Class JavaDoc[] { long.class, int.class }),
74             TransactionDefinition.PROPAGATION_SUPPORTS);
75         checkTransactionProperties(tas, Object JavaDoc.class.getMethod("notify", (Class JavaDoc[]) null),
76             TransactionDefinition.PROPAGATION_SUPPORTS);
77         checkTransactionProperties(tas, Object JavaDoc.class.getMethod("notifyAll", (Class JavaDoc[]) null),
78             TransactionDefinition.PROPAGATION_REQUIRED);
79         checkTransactionProperties(tas, Object JavaDoc.class.getMethod("toString", (Class JavaDoc[]) null), -1);
80     }
81
82     public void testMatchesAll() throws Exception JavaDoc {
83         TransactionAttributeSourceEditor pe = new TransactionAttributeSourceEditor();
84         pe.setAsText("java.lang.Object.*=PROPAGATION_REQUIRED");
85         TransactionAttributeSource tas = (TransactionAttributeSource) pe.getValue();
86
87         checkTransactionProperties(tas, Object JavaDoc.class.getMethod("hashCode", (Class JavaDoc[]) null),
88             TransactionDefinition.PROPAGATION_REQUIRED);
89         checkTransactionProperties(tas, Object JavaDoc.class.getMethod("equals", new Class JavaDoc[] { Object JavaDoc.class }),
90             TransactionDefinition.PROPAGATION_REQUIRED);
91         checkTransactionProperties(tas, Object JavaDoc.class.getMethod("wait", (Class JavaDoc[]) null),
92             TransactionDefinition.PROPAGATION_REQUIRED);
93         checkTransactionProperties(tas, Object JavaDoc.class.getMethod("wait", new Class JavaDoc[] { long.class }),
94             TransactionDefinition.PROPAGATION_REQUIRED);
95         checkTransactionProperties(tas, Object JavaDoc.class.getMethod("wait", new Class JavaDoc[] { long.class, int.class }),
96             TransactionDefinition.PROPAGATION_REQUIRED);
97         checkTransactionProperties(tas, Object JavaDoc.class.getMethod("notify", (Class JavaDoc[]) null),
98             TransactionDefinition.PROPAGATION_REQUIRED);
99         checkTransactionProperties(tas, Object JavaDoc.class.getMethod("notifyAll", (Class JavaDoc[]) null),
100             TransactionDefinition.PROPAGATION_REQUIRED);
101         checkTransactionProperties(tas, Object JavaDoc.class.getMethod("toString", (Class JavaDoc[]) null),
102             TransactionDefinition.PROPAGATION_REQUIRED);
103     }
104
105     private void checkTransactionProperties(TransactionAttributeSource tas, Method JavaDoc method, int propagationBehavior) {
106         TransactionAttribute ta = tas.getTransactionAttribute(method, null);
107         if (propagationBehavior >= 0) {
108             assertTrue(ta != null);
109             assertTrue(ta.getIsolationLevel() == TransactionDefinition.ISOLATION_DEFAULT);
110             assertTrue(ta.getPropagationBehavior() == propagationBehavior);
111         }
112         else {
113             assertTrue(ta == null);
114         }
115     }
116
117 }
118
Popular Tags