1 package com.atlassian.sal.core.rdbms;
2
3 import com.atlassian.sal.api.rdbms.RdbmsException;
4 import org.junit.Test;
5
6 import java.sql.SQLException;
7
8 import static org.hamcrest.Matchers.nullValue;
9 import static org.hamcrest.core.Is.is;
10 import static org.junit.Assert.assertThat;
11 import static org.mockito.Matchers.any;
12 import static org.mockito.Mockito.*;
13
14 public class TestDefaultTransactionalExecutor extends TestDefaultTransactionalExecutorBase
15 {
16 @Test
17 public void changeProperties()
18 {
19 defaultTransactionalExecutor.readOnly();
20 assertThat(defaultTransactionalExecutor.readOnly, is(true));
21
22 defaultTransactionalExecutor.readWrite();
23 assertThat(defaultTransactionalExecutor.readOnly, is(false));
24
25 defaultTransactionalExecutor.newTransaction();
26 assertThat(defaultTransactionalExecutor.newTransaction, is(true));
27
28 defaultTransactionalExecutor.existingTransaction();
29 assertThat(defaultTransactionalExecutor.newTransaction, is(false));
30 }
31
32 @Test
33 public void executeCommitSucceeds() throws SQLException
34 {
35 assertThat(defaultTransactionalExecutor.executeInternal(connection, callback), is(result));
36 assertThat(wrappedConnection.connection, nullValue());
37
38 verify(callback).execute(any(WrappedConnection.class));
39 verify(connection).commit();
40 verify(connection, never()).rollback();
41 }
42
43 @Test
44 public void executeRollbackSucceeds() throws SQLException
45 {
46 callbackThrows = new CallbackException("epic fail");
47
48 expectedException.expect(CallbackException.class);
49 expectedException.expectMessage("epic fail");
50
51 defaultTransactionalExecutor.executeInternal(connection, callback);
52 assertThat(wrappedConnection.connection, nullValue());
53
54 verify(callback).execute(any(WrappedConnection.class));
55 verify(connection, never()).commit();
56 verify(connection).rollback();
57 }
58
59 @Test
60 public void executeFailsOnAutoCommit() throws SQLException
61 {
62 when(connection.getAutoCommit()).thenReturn(true);
63
64 expectedException.expect(IllegalStateException.class);
65
66 defaultTransactionalExecutor.executeInternal(connection, callback);
67 }
68
69 @Test
70 public void executeAutoCommitFails() throws SQLException
71 {
72 SQLException exception = new SQLException("horrible getAutoCommit error");
73 doThrow(exception).when(connection).getAutoCommit();
74
75 expectedException.expect(RdbmsException.class);
76 expectedException.expectCause(is(exception));
77
78 defaultTransactionalExecutor.executeInternal(connection, callback);
79 verify(callback, never()).execute(any(WrappedConnection.class));
80 }
81
82 @Test
83 public void executeFailsOnAutoCommitGetFail() throws SQLException
84 {
85 when(connection.getAutoCommit()).thenThrow(new SQLException("horrible getAutoCommit exception"));
86
87 expectedException.expect(RdbmsException.class);
88
89 defaultTransactionalExecutor.executeInternal(connection, callback);
90 }
91 }