(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     
31     private TransactionResource resource1;
32     private TransactionResource resource2;
33     private Object tx1;
34     private Object tx2;
35
36     private SimpleTransactionManager manager; 
37     
38     @Before
39     public void setUp() {
40         UserTransactionFactory factory = new DefaultUserTransactionFactory(); 
41         manager = new SimpleTransactionManager(factory);     
42         resource1 = mock(TransactionResource.class);
43         resource2 = mock(TransactionResource.class);
44         tx1 = mock(Object.class);
45         tx2 = mock(Object.class);
46         when(resource1.begin()).thenReturn(tx1);
47         when(resource2.begin()).thenReturn(tx2);
48     }
49
50     @Test
51     public void testMultipleResources() throws Exception {
52         manager.addResource(resource1);
53         manager.addResource(resource2);
54         UserTransaction tx = manager.getTransaction();
55         tx.begin();
56         verify(resource1).begin();
57         verify(resource2).begin();
58         verifyNoMoreInteractions(resource1, resource2);
59         reset(resource1, resource2);
60         assertEquals(Status.STATUS_ACTIVE, tx.getStatus());
61         tx.commit();
62         verify(resource1).commit(same(tx1));
63         verify(resource2).commit(same(tx2));
64
65         verifyNoMoreInteractions(resource1, resource2);
66     }
67
68 }