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