1 package com.atlassian.sal.core.rdbms;
2
3 import org.junit.Before;
4 import org.junit.Rule;
5 import org.junit.Test;
6 import org.junit.rules.ExpectedException;
7 import org.junit.runner.RunWith;
8 import org.mockito.Mock;
9 import org.mockito.runners.MockitoJUnitRunner;
10
11 import java.sql.Connection;
12 import java.sql.SQLException;
13 import java.sql.Savepoint;
14 import java.util.concurrent.Executor;
15
16 import static org.mockito.Mockito.mock;
17 import static org.mockito.Mockito.verify;
18 import static org.mockito.Mockito.verifyZeroInteractions;
19
20 @RunWith(MockitoJUnitRunner.class)
21 public class TestWrappedConnection {
22 @Mock
23 private Connection mockConnection;
24
25 private WrappedConnection wrappedConnection;
26
27 @Rule
28 public ExpectedException expectedException = ExpectedException.none();
29
30 @Before
31 public void before() throws Exception {
32 wrappedConnection = new WrappedConnection(mockConnection);
33 }
34
35 @Test
36 public void expire() throws SQLException {
37 expectedException.expect(IllegalStateException.class);
38
39 wrappedConnection.expire();
40
41 wrappedConnection.getMetaData();
42 }
43
44
45
46
47 @Test
48 public void setAutoCommit() throws SQLException {
49 expectedException.expect(UnsupportedOperationException.class);
50 wrappedConnection.setAutoCommit(true);
51 verifyZeroInteractions(mockConnection);
52 }
53
54 @Test
55 public void commit() throws SQLException {
56 expectedException.expect(UnsupportedOperationException.class);
57 wrappedConnection.commit();
58 verifyZeroInteractions(mockConnection);
59 }
60
61 @Test
62 public void close() throws SQLException {
63 wrappedConnection.close();
64 verifyZeroInteractions(mockConnection);
65
66 expectedException.expect(IllegalStateException.class);
67 wrappedConnection.getMetaData();
68 }
69
70 @Test
71 public void close_with() throws SQLException {
72 WrappedConnection localWrappedConnectionCopy;
73 try (WrappedConnection localWrappedConnection = new WrappedConnection(mockConnection)) {
74 localWrappedConnectionCopy = localWrappedConnection;
75 }
76 verifyZeroInteractions(mockConnection);
77
78 expectedException.expect(IllegalStateException.class);
79 localWrappedConnectionCopy.getMetaData();
80 }
81
82 @Test
83 public void rollback() throws SQLException {
84 expectedException.expect(UnsupportedOperationException.class);
85 wrappedConnection.rollback();
86 verifyZeroInteractions(mockConnection);
87 }
88
89 @Test
90 public void setReadOnly() throws SQLException {
91 expectedException.expect(UnsupportedOperationException.class);
92 wrappedConnection.setReadOnly(true);
93 verifyZeroInteractions(mockConnection);
94 }
95
96 @Test
97 public void abort() throws SQLException {
98 expectedException.expect(UnsupportedOperationException.class);
99 wrappedConnection.abort(mock(Executor.class));
100 verifyZeroInteractions(mockConnection);
101 }
102
103 @Test
104 public void setCatalog() throws SQLException {
105 expectedException.expect(UnsupportedOperationException.class);
106 wrappedConnection.setCatalog("bazza");
107 verifyZeroInteractions(mockConnection);
108 }
109
110 @Test
111 public void setSchema() throws SQLException {
112 expectedException.expect(UnsupportedOperationException.class);
113 wrappedConnection.setSchema("mckenzie");
114 verifyZeroInteractions(mockConnection);
115 }
116
117 @Test
118 public void setTransactionIsolation() throws SQLException {
119 expectedException.expect(UnsupportedOperationException.class);
120 wrappedConnection.setTransactionIsolation(Connection.TRANSACTION_NONE);
121 verifyZeroInteractions(mockConnection);
122 }
123
124 @Test
125 public void setNetworkTimeout() throws SQLException {
126 expectedException.expect(UnsupportedOperationException.class);
127 wrappedConnection.setNetworkTimeout(mock(Executor.class), 11);
128 verifyZeroInteractions(mockConnection);
129 }
130
131 @Test
132 public void setSavepoint() throws SQLException {
133 expectedException.expect(UnsupportedOperationException.class);
134 wrappedConnection.setSavepoint();
135 verifyZeroInteractions(mockConnection);
136 }
137
138 @Test
139 public void setSavepointAlt() throws SQLException {
140 expectedException.expect(UnsupportedOperationException.class);
141 wrappedConnection.setSavepoint("bleh");
142 verifyZeroInteractions(mockConnection);
143 }
144
145 @Test
146 public void rollbackSavepoint() throws SQLException {
147 expectedException.expect(UnsupportedOperationException.class);
148 wrappedConnection.rollback(mock(Savepoint.class));
149 verifyZeroInteractions(mockConnection);
150 }
151
152 @Test
153 public void releaseSavepoint() throws SQLException {
154 expectedException.expect(UnsupportedOperationException.class);
155 wrappedConnection.releaseSavepoint(mock(Savepoint.class));
156 verifyZeroInteractions(mockConnection);
157 }
158
159
160
161
162 @Test
163 public void getMetaData() throws SQLException {
164 wrappedConnection.getMetaData();
165 verify(mockConnection).getMetaData();
166 }
167 }