(no commit message)
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Sat, 17 Jul 2010 20:25:55 +0000 (20:25 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Sat, 17 Jul 2010 20:25:55 +0000 (20:25 +0000)
test/enterprise/src/test/java/org/wamblee/test/transactions/SimpleTransactionManagerIntegrationTest.java [new file with mode: 0644]
test/enterprise/src/test/java/org/wamblee/test/transactions/SimpleTransactionManagerTest.java [new file with mode: 0644]
test/enterprise/src/test/java/org/wamblee/test/transactions/SimpleUserTransactionTest.java [new file with mode: 0644]
test/enterprise/src/test/java/org/wamblee/test/transactions/TransactionProxyFactoryTestBase.java [new file with mode: 0644]

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 (file)
index 0000000..1ad1a2b
--- /dev/null
@@ -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 (file)
index 0000000..97c0359
--- /dev/null
@@ -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<UserTransactionCallback> callbacks;
+    private UserTransactionFactory factory;
+    private SimpleTransactionManager manager;
+
+    @Before
+    public void setUp() {
+        callbacks = new ArrayList<UserTransactionCallback>();
+        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<UserTransaction> transaction2 = new ValueHolder<UserTransaction>();
+        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<Object[]> actual = new ValueHolder<Object[]>();
+        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 (file)
index 0000000..fa7bfa5
--- /dev/null
@@ -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 (file)
index 0000000..22bb3ff
--- /dev/null
@@ -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<Service> factory = new TransactionProxyFactory<Service>(
+            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<Service> factory = new TransactionProxyFactory<Service>(
+            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<Service> factory = new TransactionProxyFactory<Service>(
+            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<Void>() {
+            @Override
+            public Void execute(EntityManager aEm) throws Exception {
+                assert(aEm != null); 
+                proxy.execute(10);
+                return null; 
+            }
+        });
+    }
+
+}