From: erik Date: Sat, 17 Jul 2010 20:25:55 +0000 (+0000) Subject: (no commit message) X-Git-Tag: wamblee-utils-0.3~30 X-Git-Url: http://wamblee.org/gitweb/?a=commitdiff_plain;h=118a1ea317e65e150f64358ffb13c79fe280eae2;p=utils --- diff --git a/test/enterprise/src/test/java/org/wamblee/test/transactions/SimpleTransactionManagerIntegrationTest.java b/test/enterprise/src/test/java/org/wamblee/test/transactions/SimpleTransactionManagerIntegrationTest.java new file mode 100644 index 00000000..1ad1a2b3 --- /dev/null +++ b/test/enterprise/src/test/java/org/wamblee/test/transactions/SimpleTransactionManagerIntegrationTest.java @@ -0,0 +1,68 @@ +/* + * 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 javax.transaction.Status; +import javax.transaction.UserTransaction; + +import org.junit.Before; +import org.junit.Test; + +public class SimpleTransactionManagerIntegrationTest { + + + private TransactionResource resource1; + private TransactionResource resource2; + private Object tx1; + private Object tx2; + + private SimpleTransactionManager manager; + + @Before + public void setUp() { + UserTransactionFactory factory = new DefaultUserTransactionFactory(); + manager = new SimpleTransactionManager(factory); + resource1 = mock(TransactionResource.class); + resource2 = mock(TransactionResource.class); + tx1 = mock(Object.class); + tx2 = mock(Object.class); + when(resource1.begin()).thenReturn(tx1); + when(resource2.begin()).thenReturn(tx2); + } + + @Test + public void testMultipleResources() throws Exception { + manager.addResource(resource1); + manager.addResource(resource2); + UserTransaction tx = manager.getTransaction(); + tx.begin(); + verify(resource1).begin(); + verify(resource2).begin(); + verifyNoMoreInteractions(resource1, resource2); + reset(resource1, resource2); + assertEquals(Status.STATUS_ACTIVE, tx.getStatus()); + tx.commit(); + verify(resource1).commit(same(tx1)); + verify(resource2).commit(same(tx2)); + + verifyNoMoreInteractions(resource1, resource2); + } + +} diff --git a/test/enterprise/src/test/java/org/wamblee/test/transactions/SimpleTransactionManagerTest.java b/test/enterprise/src/test/java/org/wamblee/test/transactions/SimpleTransactionManagerTest.java new file mode 100644 index 00000000..97c0359d --- /dev/null +++ b/test/enterprise/src/test/java/org/wamblee/test/transactions/SimpleTransactionManagerTest.java @@ -0,0 +1,119 @@ +/* + * 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.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 { + UserTransaction transaction = manager.getTransaction(); + assertNotNull(transaction); + assertSame(transaction, manager.getTransaction()); + final ValueHolder transaction2 = new ValueHolder(); + Thread t = new Thread(new Runnable() { + @Override + public void run() { + transaction2.setValue(manager.getTransaction()); + } + }); + t.start(); + t.join(); + assertNotNull(transaction); + assertNotNull(transaction2.getValue()); + assertTrue(transaction != transaction2.getValue()); + } + + @Test + public void testTransactionLifeCycle() { + UserTransaction transaction = manager.getTransaction(); + assertNotNull(transaction); + assertEquals(1, callbacks.size()); + callbacks.get(0).transactionFinished(); + UserTransaction transaction2 = manager.getTransaction(); + assertNotNull(transaction2); + assert (transaction != transaction2); + } + + @Test + public void testVerifyResourcesArePassedToFactory() { + + 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 null; + } + }); + + TransactionResource resource1 = mock(TransactionResource.class); + TransactionResource resource2 = mock(TransactionResource.class); + manager.addResource(resource1); + manager.addResource(resource2); + + UserTransaction transaction = manager.getTransaction(); + Object[] expected = new Object[] { resource1, resource2 }; + assertTrue(Arrays.equals(expected, actual.getValue())); + } + +} diff --git a/test/enterprise/src/test/java/org/wamblee/test/transactions/SimpleUserTransactionTest.java b/test/enterprise/src/test/java/org/wamblee/test/transactions/SimpleUserTransactionTest.java new file mode 100644 index 00000000..fa7bfa57 --- /dev/null +++ b/test/enterprise/src/test/java/org/wamblee/test/transactions/SimpleUserTransactionTest.java @@ -0,0 +1,170 @@ +/* + * 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 javax.transaction.HeuristicMixedException; +import javax.transaction.NotSupportedException; +import javax.transaction.RollbackException; +import javax.transaction.Status; + +import org.junit.Before; +import org.junit.Test; + +public class SimpleUserTransactionTest { + + private UserTransactionCallback callback; + private TransactionResource resource1; + private TransactionResource resource2; + private Object tx1; + private Object tx2; + + private SimpleUserTransaction tx; + + @Before + public void setUp() { + callback = mock(UserTransactionCallback.class); + resource1 = mock(TransactionResource.class); + resource2 = mock(TransactionResource.class); + tx1 = mock(Object.class); + tx2 = mock(Object.class); + when(resource1.begin()).thenReturn(tx1); + when(resource2.begin()).thenReturn(tx2); + } + + @Test + public void testTransactionBegin() throws Exception { + tx = new SimpleUserTransaction(callback, resource1); + assertEquals(Status.STATUS_NO_TRANSACTION, tx.getStatus()); + tx.begin(); + verify(resource1).begin(); + verifyNoMoreInteractions(resource1); + reset(resource1); + assertEquals(Status.STATUS_ACTIVE, tx.getStatus()); + + } + + @Test + public void testTransactionCommit() throws Exception { + testTransactionBegin(); + tx.commit(); + verify(resource1).commit(same(tx1)); + verifyNoMoreInteractions(resource1); + verify(callback).transactionFinished(); + assertEquals(Status.STATUS_NO_TRANSACTION, tx.getStatus()); + } + + @Test + public void testTransactionRollback() throws Exception { + testTransactionBegin(); + tx.rollback(); + verify(resource1).rollback(same(tx1)); + verifyNoMoreInteractions(resource1); + assertEquals(Status.STATUS_NO_TRANSACTION, tx.getStatus()); + verify(callback).transactionFinished(); + } + + @Test + public void testTransactionStatusSetrollbackOnly() throws Exception { + testTransactionBegin(); + tx.setRollbackOnly(); + assertEquals(Status.STATUS_MARKED_ROLLBACK, tx.getStatus()); + verifyNoMoreInteractions(callback); + } + + @Test + public void testTransactionRollbackWhenSetRollbackOnly() throws Exception { + testTransactionStatusSetrollbackOnly(); + tx.rollback(); + assertEquals(Status.STATUS_NO_TRANSACTION, tx.getStatus()); + verify(resource1).rollback(same(tx1)); + verifyNoMoreInteractions(resource1); + } + + @Test + public void testTransactionCommitWhenSetRollbackOnly() throws Exception { + testTransactionStatusSetrollbackOnly(); + try { + tx.commit(); + fail(); + } catch (RollbackException e) { + assertEquals(Status.STATUS_NO_TRANSACTION, tx.getStatus()); + } + } + + @Test + public void testMultipleResources() throws Exception { + tx = new SimpleUserTransaction(callback, resource1, resource2); + tx.begin(); + verify(resource1).begin(); + verify(resource2).begin(); + verifyNoMoreInteractions(resource1, resource2); + reset(resource1, resource2); + assertEquals(Status.STATUS_ACTIVE, tx.getStatus()); + tx.commit(); + verify(resource1).commit(same(tx1)); + verify(resource2).commit(same(tx2)); + + verifyNoMoreInteractions(resource1, resource2); + verify(callback).transactionFinished(); + } + + @Test + public void testSetTransactionTimeoutIgnored() throws Exception { + testTransactionBegin(); + tx.setTransactionTimeout(1000); + } + + @Test(expected = NotSupportedException.class) + public void testNestedTransaction() throws Exception { + testTransactionBegin(); + tx.begin(); + } + + @Test(expected = IllegalStateException.class) + public void testCommitWhileNotInATransaction() throws Exception { + tx = new SimpleUserTransaction(callback, resource1); + tx.commit(); + } + + @Test + public void testCommitFailsOnSomeResources() throws Exception { + tx = new SimpleUserTransaction(callback, resource1, resource2); + tx.begin(); + doThrow(new RuntimeException("failed")).when(resource1).commit(any()); + try { + tx.commit(); + fail(); + } catch (HeuristicMixedException e) { + verify(resource2).rollback(same(tx2)); + } + } + + @Test(expected = IllegalStateException.class) + public void testRollbackWhileNotInATransaction() throws Exception { + tx = new SimpleUserTransaction(callback, resource1); + tx.rollback(); + } + + @Test(expected = IllegalStateException.class) + public void testSetrollbackonlyWhileNotInATransaction() throws Exception { + tx = new SimpleUserTransaction(callback, resource1); + tx.setRollbackOnly(); + } +} diff --git a/test/enterprise/src/test/java/org/wamblee/test/transactions/TransactionProxyFactoryTestBase.java b/test/enterprise/src/test/java/org/wamblee/test/transactions/TransactionProxyFactoryTestBase.java new file mode 100644 index 00000000..22bb3ff9 --- /dev/null +++ b/test/enterprise/src/test/java/org/wamblee/test/transactions/TransactionProxyFactoryTestBase.java @@ -0,0 +1,104 @@ +/* + * 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.Mockito.*; + +import javax.persistence.EntityManager; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.wamblee.test.persistence.JpaTester; +import org.wamblee.test.persistence.MyPersistenceUnit; +import org.wamblee.test.persistence.JpaBuilder.JpaUnitOfWork; + +public class TransactionProxyFactoryTestBase { + + public static interface Service { + int execute(int aValue) throws Exception; + } + + private JpaTester jpaTester; + + @Before + public void setUp() throws Exception { + jpaTester = new JpaTester(new MyPersistenceUnit()); + jpaTester.start(); + } + + @After + public void tearDown() throws Exception { + jpaTester.stop(); + } + + @Test + public void testServiceIsInvoked() throws Exception { + TransactionProxyFactory factory = new TransactionProxyFactory( + jpaTester.getJpaBuilder(), Service.class); + Service service = mock(Service.class); + when(service.execute(10)).thenReturn(200); + Service proxy = factory.getProxy(service); + int res = proxy.execute(10); + assertEquals(200, res); + verify(service).execute(10); + } + + @Test + public void testServiceThrowsException() throws Exception { + TransactionProxyFactory factory = new TransactionProxyFactory( + jpaTester.getJpaBuilder(), Service.class); + Service service = mock(Service.class); + when(service.execute(10)).thenThrow(new RuntimeException("xyz")); + Service proxy = factory.getProxy(service); + try { + int res = proxy.execute(10); + fail(); + } catch (RuntimeException e) { + assertEquals("xyz", e.getMessage()); + } + } + + @Test + public void testEntityManagerIsPassed() throws Exception { + + + final TransactionProxyFactory factory = new TransactionProxyFactory( + jpaTester.getJpaBuilder(), Service.class); + Service service = new Service() { + private EntityManager em = factory.getTransactionScopedEntityManager(); + + @Override + public int execute(int aValue) throws Exception { + assertNotNull(em); + assertTrue(em.isOpen()); + return 0; + } + }; + + final Service proxy = factory.getProxy(service); + jpaTester.getJpaBuilder().execute(new JpaUnitOfWork() { + @Override + public Void execute(EntityManager aEm) throws Exception { + assert(aEm != null); + proxy.execute(10); + return null; + } + }); + } + +}