(no commit message)
[utils] / test / enterprise / src / test / java / org / wamblee / test / transactions / SimpleTransactionManagerTest.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 java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
25
26 import javax.transaction.UserTransaction;
27
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.invocation.InvocationOnMock;
32 import org.mockito.stubbing.Answer;
33 import org.wamblee.general.ValueHolder;
34
35 public class SimpleTransactionManagerTest {
36
37     private List<UserTransactionCallback> callbacks;
38     private UserTransactionFactory factory;
39     private SimpleTransactionManager manager;
40
41     @Before
42     public void setUp() {
43         callbacks = new ArrayList<UserTransactionCallback>();
44         factory = mock(UserTransactionFactory.class);
45         when(
46             factory.create(any(UserTransactionCallback.class), any(List.class)))
47             .thenAnswer(new Answer() {
48                 @Override
49                 public Object answer(InvocationOnMock aInvocation)
50                     throws Throwable {
51                     callbacks.add((UserTransactionCallback) aInvocation
52                         .getArguments()[0]);
53                     return mock(UserTransaction.class);
54                 }
55             });
56         manager = new SimpleTransactionManager(factory);
57     }
58
59     @After
60     public void tearDown() {
61         // Empty.
62     }
63
64     @Test
65     public void testTransactionsAreThreadSpecific() throws Exception {
66         UserTransaction transaction = manager.getTransaction();
67         assertNotNull(transaction);
68         assertSame(transaction, manager.getTransaction());
69         final ValueHolder<UserTransaction> transaction2 = new ValueHolder<UserTransaction>();
70         Thread t = new Thread(new Runnable() {
71             @Override
72             public void run() {
73                 transaction2.setValue(manager.getTransaction());
74             }
75         });
76         t.start();
77         t.join();
78         assertNotNull(transaction);
79         assertNotNull(transaction2.getValue());
80         assertTrue(transaction != transaction2.getValue());
81     }
82
83     @Test
84     public void testTransactionLifeCycle() {
85         UserTransaction transaction = manager.getTransaction();
86         assertNotNull(transaction);
87         assertEquals(1, callbacks.size());
88         callbacks.get(0).transactionFinished();
89         UserTransaction transaction2 = manager.getTransaction();
90         assertNotNull(transaction2);
91         assert (transaction != transaction2);
92     }
93
94     @Test
95     public void testVerifyResourcesArePassedToFactory() {
96
97         final ValueHolder<Object[]> actual = new ValueHolder<Object[]>();
98         when(factory.create(any(UserTransactionCallback.class), (List) any()))
99             .thenAnswer(new Answer() {
100                 @Override
101                 public Object answer(InvocationOnMock aInvocation)
102                     throws Throwable {
103                     Object[] value = ((List)aInvocation.getArguments()[1]).toArray();
104                     actual.setValue(value);
105                     return null;
106                 }
107             });
108
109         TransactionResource resource1 = mock(TransactionResource.class);
110         TransactionResource resource2 = mock(TransactionResource.class);
111         manager.addResource(resource1);
112         manager.addResource(resource2);
113
114         UserTransaction transaction = manager.getTransaction();
115         Object[] expected = new Object[] { resource1, resource2 };
116         assertTrue(Arrays.equals(expected, actual.getValue()));
117     }
118
119 }