2 * Copyright 2005-2010 the original author or authors.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package org.wamblee.test.transactions;
18 import static junit.framework.Assert.*;
19 import static org.mockito.Matchers.*;
20 import static org.mockito.Mockito.*;
22 import java.util.ArrayList;
23 import java.util.Arrays;
24 import java.util.List;
26 import javax.transaction.UserTransaction;
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;
35 public class SimpleTransactionManagerTest {
37 private List<UserTransactionCallback> callbacks;
38 private UserTransactionFactory factory;
39 private SimpleTransactionManager manager;
43 callbacks = new ArrayList<UserTransactionCallback>();
44 factory = mock(UserTransactionFactory.class);
46 factory.create(any(UserTransactionCallback.class), any(List.class)))
47 .thenAnswer(new Answer() {
49 public Object answer(InvocationOnMock aInvocation)
51 callbacks.add((UserTransactionCallback) aInvocation
53 return mock(UserTransaction.class);
56 manager = new SimpleTransactionManager(factory);
60 public void tearDown() {
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() {
73 transaction2.setValue(manager.getTransaction());
78 assertNotNull(transaction);
79 assertNotNull(transaction2.getValue());
80 assertTrue(transaction != transaction2.getValue());
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);
95 public void testVerifyResourcesArePassedToFactory() {
97 final ValueHolder<Object[]> actual = new ValueHolder<Object[]>();
98 when(factory.create(any(UserTransactionCallback.class), (List) any()))
99 .thenAnswer(new Answer() {
101 public Object answer(InvocationOnMock aInvocation)
103 Object[] value = ((List)aInvocation.getArguments()[1]).toArray();
104 actual.setValue(value);
109 TransactionResource resource1 = mock(TransactionResource.class);
110 TransactionResource resource2 = mock(TransactionResource.class);
111 manager.addResource(resource1);
112 manager.addResource(resource2);
114 UserTransaction transaction = manager.getTransaction();
115 Object[] expected = new Object[] { resource1, resource2 };
116 assertTrue(Arrays.equals(expected, actual.getValue()));