/* * Copyright 2005-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.wamblee.test.transactions; import static junit.framework.Assert.*; import static org.mockito.Matchers.*; import static org.mockito.Mockito.*; import java.util.ArrayList; import java.util.Arrays; import java.util.List; import javax.transaction.SystemException; import javax.transaction.UserTransaction; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.mockito.invocation.InvocationOnMock; import org.mockito.stubbing.Answer; import org.wamblee.general.ValueHolder; public class SimpleTransactionManagerTest { private List callbacks; private UserTransactionFactory factory; private SimpleTransactionManager manager; @Before public void setUp() { callbacks = new ArrayList(); factory = mock(UserTransactionFactory.class); when( factory.create(any(UserTransactionCallback.class), any(List.class))) .thenAnswer(new Answer() { @Override public Object answer(InvocationOnMock aInvocation) throws Throwable { callbacks.add((UserTransactionCallback) aInvocation .getArguments()[0]); return mock(UserTransaction.class); } }); manager = new SimpleTransactionManager(factory); } @After public void tearDown() { // Empty. } @Test public void testTransactionsAreThreadSpecific() throws Exception { manager.getTransaction().getStatus(); UserTransaction transaction = manager.getThreadSpecificTransaction(); assertNotNull(transaction); assertSame(transaction, manager.getThreadSpecificTransaction()); final ValueHolder transaction2 = new ValueHolder(); Thread t = new Thread(new Runnable() { @Override public void run() { try { manager.getTransaction().getStatus(); transaction2.setValue(manager .getThreadSpecificTransaction()); } catch (SystemException e) { fail("exception "); } } }); t.start(); t.join(); assertNotNull(transaction); assertNotNull(transaction2.getValue()); assertTrue(transaction != transaction2.getValue()); } @Test public void testTransactionLifeCycle() throws Exception { manager.getTransaction().getStatus(); UserTransaction transaction = manager.getThreadSpecificTransaction(); assertNotNull(transaction); assertEquals(1, callbacks.size()); callbacks.get(0).transactionFinished(); manager.getTransaction().getStatus(); UserTransaction transaction2 = manager.getThreadSpecificTransaction(); assertNotNull(transaction2); assert (transaction != transaction2); } @Test public void testVerifyResourcesArePassedToFactory() throws Exception { final ValueHolder actual = new ValueHolder(); when(factory.create(any(UserTransactionCallback.class), (List) any())) .thenAnswer(new Answer() { @Override public Object answer(InvocationOnMock aInvocation) throws Throwable { Object[] value = ((List) aInvocation.getArguments()[1]) .toArray(); actual.setValue(value); return mock(UserTransaction.class); } }); TransactionResource resource1 = mock(TransactionResource.class); TransactionResource resource2 = mock(TransactionResource.class); manager.addResource(resource1); manager.addResource(resource2); manager.getTransaction().getStatus(); Object[] expected = new Object[] { resource1, resource2 }; assertTrue(Arrays.equals(expected, actual.getValue())); } @Test public void testTwoTransactions() throws Exception { UserTransaction transaction = manager.getTransaction(); transaction.begin(); transaction.commit(); transaction.begin(); transaction.commit(); } }