(no commit message)
[utils] / test / enterprise / src / test / java / org / wamblee / test / transactions / SimpleTransactionManagerIntegrationTest.java
1 /*
2  * Copyright 2005-2010 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 package org.wamblee.test.transactions;
17
18 import static junit.framework.Assert.*;
19 import static org.mockito.Matchers.*;
20 import static org.mockito.Mockito.*;
21
22 import javax.transaction.Status;
23 import javax.transaction.UserTransaction;
24
25 import org.junit.Before;
26 import org.junit.Test;
27
28 public class SimpleTransactionManagerIntegrationTest {
29
30     private TransactionResource resource1;
31     private TransactionResource resource2;
32     private Object tx1;
33     private Object tx2;
34
35     private SimpleTransactionManager manager;
36
37     @Before
38     public void setUp() {
39         UserTransactionFactory factory = new DefaultUserTransactionFactory();
40         manager = new SimpleTransactionManager(factory);
41         resource1 = mock(TransactionResource.class);
42         resource2 = mock(TransactionResource.class);
43         tx1 = mock(Object.class);
44         tx2 = mock(Object.class);
45         when(resource1.begin()).thenReturn(tx1);
46         when(resource2.begin()).thenReturn(tx2);
47     }
48
49     @Test
50     public void testMultipleResources() throws Exception {
51         manager.addResource(resource1);
52         manager.addResource(resource2);
53         UserTransaction tx = manager.getTransaction();
54         tx.begin();
55         verify(resource1).begin();
56         verify(resource2).begin();
57         verifyNoMoreInteractions(resource1, resource2);
58         reset(resource1, resource2);
59         assertEquals(Status.STATUS_ACTIVE, tx.getStatus());
60         tx.commit();
61         verify(resource1).commit(same(tx1));
62         verify(resource2).commit(same(tx2));
63
64         verifyNoMoreInteractions(resource1, resource2);
65     }
66
67     @Test
68     public void testTwoTransactions() throws Exception {
69         UserTransaction transaction = manager.getTransaction();
70         transaction.begin();
71         transaction.commit();
72
73         transaction.begin();
74         transaction.commit();
75     }
76
77 }