(no commit message)
[utils] / test / enterprise / src / test / java / org / wamblee / test / transactions / SimpleTransactionManagerTest.java
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()));
+    }
+
+}