KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > alfresco > util > transaction > SpringAwareUserTransactionTest


1 /*
2  * Copyright (C) 2005 Alfresco, Inc.
3  *
4  * Licensed under the Mozilla Public License version 1.1
5  * with a permitted attribution clause. You may obtain a
6  * copy of the License at
7  *
8  * http://www.alfresco.org/legal/license.txt
9  *
10  * Unless required by applicable law or agreed to in writing,
11  * software distributed under the License is distributed on an
12  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
13  * either express or implied. See the License for the specific
14  * language governing permissions and limitations under the
15  * License.
16  */

17 package org.alfresco.util.transaction;
18
19 import javax.transaction.RollbackException JavaDoc;
20 import javax.transaction.Status JavaDoc;
21 import javax.transaction.UserTransaction JavaDoc;
22
23 import junit.framework.TestCase;
24
25 import org.springframework.transaction.NoTransactionException;
26 import org.springframework.transaction.TransactionDefinition;
27 import org.springframework.transaction.interceptor.TransactionAspectSupport;
28 import org.springframework.transaction.support.AbstractPlatformTransactionManager;
29 import org.springframework.transaction.support.DefaultTransactionStatus;
30
31 /**
32  * @see org.alfresco.util.transaction.SpringAwareUserTransaction
33  *
34  * @author Derek Hulley
35  */

36 public class SpringAwareUserTransactionTest extends TestCase
37 {
38     private DummyTransactionManager transactionManager;
39     private UserTransaction JavaDoc txn;
40     
41     public SpringAwareUserTransactionTest()
42     {
43         super();
44     }
45     
46     @Override JavaDoc
47     protected void setUp() throws Exception JavaDoc
48     {
49         transactionManager = new DummyTransactionManager();
50         txn = getTxn();
51     }
52     
53     private UserTransaction JavaDoc getTxn()
54     {
55         return new SpringAwareUserTransaction(
56                 transactionManager,
57                 false,
58                 TransactionDefinition.ISOLATION_DEFAULT,
59                 TransactionDefinition.PROPAGATION_REQUIRED,
60                 TransactionDefinition.TIMEOUT_DEFAULT);
61     }
62     
63     public void testSetUp() throws Exception JavaDoc
64     {
65         assertNotNull(transactionManager);
66         assertNotNull(txn);
67     }
68     
69     private void checkNoStatusOnThread()
70     {
71         try
72         {
73             TransactionAspectSupport.currentTransactionStatus();
74             fail("Spring transaction info is present outside of transaction boundaries");
75         }
76         catch (NoTransactionException e)
77         {
78             // expected
79
}
80     }
81     
82     public void testNoTxnStatus() throws Exception JavaDoc
83     {
84         checkNoStatusOnThread();
85         assertEquals("Transaction status is not correct",
86                 Status.STATUS_NO_TRANSACTION,
87                 txn.getStatus());
88         assertEquals("Transaction manager not set up correctly",
89                 txn.getStatus(),
90                 transactionManager.getStatus());
91     }
92
93     public void testSimpleTxnWithCommit() throws Throwable JavaDoc
94     {
95         testNoTxnStatus();
96         try
97         {
98             txn.begin();
99             assertEquals("Transaction status is not correct",
100                     Status.STATUS_ACTIVE,
101                     txn.getStatus());
102             assertEquals("Transaction manager not called correctly",
103                     txn.getStatus(),
104                     transactionManager.getStatus());
105
106             txn.commit();
107             assertEquals("Transaction status is not correct",
108                     Status.STATUS_COMMITTED,
109                     txn.getStatus());
110             assertEquals("Transaction manager not called correctly",
111                     txn.getStatus(),
112                     transactionManager.getStatus());
113         }
114         catch (Throwable JavaDoc e)
115         {
116             // unexpected exception - attempt a cleanup
117
try
118             {
119                 txn.rollback();
120             }
121             catch (Throwable JavaDoc ee)
122             {
123                 e.printStackTrace();
124             }
125             throw e;
126         }
127         checkNoStatusOnThread();
128     }
129     
130     public void testSimpleTxnWithRollback() throws Exception JavaDoc
131     {
132         testNoTxnStatus();
133         try
134         {
135             txn.begin();
136
137             throw new Exception JavaDoc("Blah");
138         }
139         catch (Throwable JavaDoc e)
140         {
141             txn.rollback();
142         }
143         assertEquals("Transaction status is not correct",
144                 Status.STATUS_ROLLEDBACK,
145                 txn.getStatus());
146         assertEquals("Transaction manager not called correctly",
147                 txn.getStatus(),
148                 transactionManager.getStatus());
149         checkNoStatusOnThread();
150     }
151     
152     public void testNoBeginCommit() throws Exception JavaDoc
153     {
154         testNoTxnStatus();
155         try
156         {
157             txn.commit();
158             fail("Failed to detected no begin");
159         }
160         catch (IllegalStateException JavaDoc e)
161         {
162             // expected
163
}
164         checkNoStatusOnThread();
165     }
166     
167     public void testPostRollbackCommitDetection() throws Exception JavaDoc
168     {
169         testNoTxnStatus();
170
171         txn.begin();
172         txn.rollback();
173         try
174         {
175             txn.commit();
176             fail("Failed to detect rolled back txn");
177         }
178         catch (RollbackException JavaDoc e)
179         {
180             // expected
181
}
182         checkNoStatusOnThread();
183     }
184     
185     public void testPostSetRollbackOnlyCommitDetection() throws Exception JavaDoc
186     {
187         testNoTxnStatus();
188
189         txn.begin();
190         txn.setRollbackOnly();
191         try
192         {
193             txn.commit();
194             fail("Failed to detect set rollback");
195         }
196         catch (RollbackException JavaDoc e)
197         {
198             // expected
199
txn.rollback();
200         }
201         checkNoStatusOnThread();
202     }
203     
204     public void testMismatchedBeginCommit() throws Exception JavaDoc
205     {
206         UserTransaction JavaDoc txn1 = getTxn();
207         UserTransaction JavaDoc txn2 = getTxn();
208
209         testNoTxnStatus();
210
211         txn1.begin();
212         txn2.begin();
213         
214         txn2.commit();
215         txn1.commit();
216         
217         checkNoStatusOnThread();
218         
219         txn1 = getTxn();
220         txn2 = getTxn();
221         
222         txn1.begin();
223         txn2.begin();
224         
225         try
226         {
227             txn1.commit();
228             fail("Failure to detect mismatched transaction begin/commit");
229         }
230         catch (RuntimeException JavaDoc e)
231         {
232             // expected
233
}
234         txn2.commit();
235         txn1.commit();
236
237         checkNoStatusOnThread();
238     }
239     
240     /**
241      * Used to check that the transaction manager is being called correctly
242      *
243      * @author Derek Hulley
244      */

245     private static class DummyTransactionManager extends AbstractPlatformTransactionManager
246     {
247         private int status = Status.STATUS_NO_TRANSACTION;
248         private Object JavaDoc txn = new Object JavaDoc();
249         
250         /**
251          * @return Returns one of the {@link Status Status.STATUS_XXX} constants
252          */

253         public int getStatus()
254         {
255             return status;
256         }
257
258         protected void doBegin(Object JavaDoc arg0, TransactionDefinition arg1)
259         {
260             status = Status.STATUS_ACTIVE;
261         }
262
263         protected void doCommit(DefaultTransactionStatus arg0)
264         {
265             status = Status.STATUS_COMMITTED;
266         }
267
268         protected Object JavaDoc doGetTransaction()
269         {
270             return txn;
271         }
272
273         protected void doRollback(DefaultTransactionStatus arg0)
274         {
275             status = Status.STATUS_ROLLEDBACK;
276         }
277     }
278 }
279
Popular Tags