+/*
+ * 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();
+ }
+}