2 * Copyright 2005-2010 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.test.transactions;
18 import static junit.framework.Assert.*;
19 import static org.mockito.Matchers.*;
20 import static org.mockito.Mockito.*;
22 import javax.transaction.HeuristicMixedException;
23 import javax.transaction.NotSupportedException;
24 import javax.transaction.RollbackException;
25 import javax.transaction.Status;
27 import org.junit.Before;
28 import org.junit.Test;
30 public class SimpleUserTransactionTest {
32 private UserTransactionCallback callback;
33 private TransactionResource resource1;
34 private TransactionResource resource2;
38 private SimpleUserTransaction tx;
42 callback = mock(UserTransactionCallback.class);
43 resource1 = mock(TransactionResource.class);
44 resource2 = mock(TransactionResource.class);
45 tx1 = mock(Object.class);
46 tx2 = mock(Object.class);
47 when(resource1.begin()).thenReturn(tx1);
48 when(resource2.begin()).thenReturn(tx2);
52 public void testTransactionBegin() throws Exception {
53 tx = new SimpleUserTransaction(callback, resource1);
54 assertEquals(Status.STATUS_NO_TRANSACTION, tx.getStatus());
56 verify(resource1).begin();
57 verifyNoMoreInteractions(resource1);
59 assertEquals(Status.STATUS_ACTIVE, tx.getStatus());
64 public void testTransactionCommit() throws Exception {
65 testTransactionBegin();
67 verify(resource1).commit(same(tx1));
68 verifyNoMoreInteractions(resource1);
69 verify(callback).transactionFinished();
70 assertEquals(Status.STATUS_NO_TRANSACTION, tx.getStatus());
74 public void testTransactionRollback() throws Exception {
75 testTransactionBegin();
77 verify(resource1).rollback(same(tx1));
78 verifyNoMoreInteractions(resource1);
79 assertEquals(Status.STATUS_NO_TRANSACTION, tx.getStatus());
80 verify(callback).transactionFinished();
84 public void testTransactionStatusSetrollbackOnly() throws Exception {
85 testTransactionBegin();
87 assertEquals(Status.STATUS_MARKED_ROLLBACK, tx.getStatus());
88 verifyNoMoreInteractions(callback);
92 public void testTransactionRollbackWhenSetRollbackOnly() throws Exception {
93 testTransactionStatusSetrollbackOnly();
95 assertEquals(Status.STATUS_NO_TRANSACTION, tx.getStatus());
96 verify(resource1).rollback(same(tx1));
97 verifyNoMoreInteractions(resource1);
101 public void testTransactionCommitWhenSetRollbackOnly() throws Exception {
102 testTransactionStatusSetrollbackOnly();
106 } catch (RollbackException e) {
107 assertEquals(Status.STATUS_NO_TRANSACTION, tx.getStatus());
112 public void testMultipleResources() throws Exception {
113 tx = new SimpleUserTransaction(callback, resource1, resource2);
115 verify(resource1).begin();
116 verify(resource2).begin();
117 verifyNoMoreInteractions(resource1, resource2);
118 reset(resource1, resource2);
119 assertEquals(Status.STATUS_ACTIVE, tx.getStatus());
121 verify(resource1).commit(same(tx1));
122 verify(resource2).commit(same(tx2));
124 verifyNoMoreInteractions(resource1, resource2);
125 verify(callback).transactionFinished();
129 public void testSetTransactionTimeoutIgnored() throws Exception {
130 testTransactionBegin();
131 tx.setTransactionTimeout(1000);
134 @Test(expected = NotSupportedException.class)
135 public void testNestedTransaction() throws Exception {
136 testTransactionBegin();
140 @Test(expected = IllegalStateException.class)
141 public void testCommitWhileNotInATransaction() throws Exception {
142 tx = new SimpleUserTransaction(callback, resource1);
147 public void testCommitFailsOnSomeResources() throws Exception {
148 tx = new SimpleUserTransaction(callback, resource1, resource2);
150 doThrow(new RuntimeException("failed")).when(resource1).commit(any());
154 } catch (HeuristicMixedException e) {
155 verify(resource2).rollback(same(tx2));
159 @Test(expected = IllegalStateException.class)
160 public void testRollbackWhileNotInATransaction() throws Exception {
161 tx = new SimpleUserTransaction(callback, resource1);
165 @Test(expected = IllegalStateException.class)
166 public void testSetrollbackonlyWhileNotInATransaction() throws Exception {
167 tx = new SimpleUserTransaction(callback, resource1);
168 tx.setRollbackOnly();