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 {
23 @Mock
24 private Connection mockConnection;
25
26 private WrappedConnection wrappedConnection;
27
28 @Rule
29 public ExpectedException expectedException = ExpectedException.none();
30
31 @Before
32 public void before() throws Exception
33 {
34 wrappedConnection = new WrappedConnection(mockConnection);
35 }
36
37 @Test
38 public void expire() throws SQLException
39 {
40 expectedException.expect(IllegalStateException.class);
41
42 wrappedConnection.expire();
43
44 wrappedConnection.getMetaData();
45 }
46
47
48
49
50 @Test
51 public void setAutoCommit() throws SQLException
52 {
53 expectedException.expect(UnsupportedOperationException.class);
54 wrappedConnection.setAutoCommit(true);
55 verifyZeroInteractions(mockConnection);
56 }
57
58 @Test
59 public void commit() throws SQLException
60 {
61 expectedException.expect(UnsupportedOperationException.class);
62 wrappedConnection.commit();
63 verifyZeroInteractions(mockConnection);
64 }
65
66 @Test
67 public void close() throws SQLException
68 {
69 wrappedConnection.close();
70 verifyZeroInteractions(mockConnection);
71
72 expectedException.expect(IllegalStateException.class);
73 wrappedConnection.getMetaData();
74 }
75
76 @Test
77 public void close_with() throws SQLException
78 {
79 WrappedConnection localWrappedConnectionCopy;
80 try (WrappedConnection localWrappedConnection = new WrappedConnection(mockConnection)) {
81 localWrappedConnectionCopy = localWrappedConnection;
82 }
83 verifyZeroInteractions(mockConnection);
84
85 expectedException.expect(IllegalStateException.class);
86 localWrappedConnectionCopy.getMetaData();
87 }
88
89 @Test
90 public void rollback() throws SQLException
91 {
92 expectedException.expect(UnsupportedOperationException.class);
93 wrappedConnection.rollback();
94 verifyZeroInteractions(mockConnection);
95 }
96
97 @Test
98 public void setReadOnly() throws SQLException
99 {
100 expectedException.expect(UnsupportedOperationException.class);
101 wrappedConnection.setReadOnly(true);
102 verifyZeroInteractions(mockConnection);
103 }
104
105 @Test
106 public void abort() throws SQLException
107 {
108 expectedException.expect(UnsupportedOperationException.class);
109 wrappedConnection.abort(mock(Executor.class));
110 verifyZeroInteractions(mockConnection);
111 }
112
113 @Test
114 public void setCatalog() throws SQLException
115 {
116 expectedException.expect(UnsupportedOperationException.class);
117 wrappedConnection.setCatalog("bazza");
118 verifyZeroInteractions(mockConnection);
119 }
120
121 @Test
122 public void setSchema() throws SQLException
123 {
124 expectedException.expect(UnsupportedOperationException.class);
125 wrappedConnection.setSchema("mckenzie");
126 verifyZeroInteractions(mockConnection);
127 }
128
129 @Test
130 public void setTransactionIsolation() throws SQLException
131 {
132 expectedException.expect(UnsupportedOperationException.class);
133 wrappedConnection.setTransactionIsolation(Connection.TRANSACTION_NONE);
134 verifyZeroInteractions(mockConnection);
135 }
136
137 @Test
138 public void setNetworkTimeout() throws SQLException
139 {
140 expectedException.expect(UnsupportedOperationException.class);
141 wrappedConnection.setNetworkTimeout(mock(Executor.class), 11);
142 verifyZeroInteractions(mockConnection);
143 }
144
145 @Test
146 public void setSavepoint() throws SQLException
147 {
148 expectedException.expect(UnsupportedOperationException.class);
149 wrappedConnection.setSavepoint();
150 verifyZeroInteractions(mockConnection);
151 }
152
153 @Test
154 public void setSavepointAlt() throws SQLException
155 {
156 expectedException.expect(UnsupportedOperationException.class);
157 wrappedConnection.setSavepoint("bleh");
158 verifyZeroInteractions(mockConnection);
159 }
160
161 @Test
162 public void rollbackSavepoint() throws SQLException
163 {
164 expectedException.expect(UnsupportedOperationException.class);
165 wrappedConnection.rollback(mock(Savepoint.class));
166 verifyZeroInteractions(mockConnection);
167 }
168
169 @Test
170 public void releaseSavepoint() throws SQLException
171 {
172 expectedException.expect(UnsupportedOperationException.class);
173 wrappedConnection.releaseSavepoint(mock(Savepoint.class));
174 verifyZeroInteractions(mockConnection);
175 }
176
177
178
179
180 @Test
181 public void getMetaData() throws SQLException
182 {
183 wrappedConnection.getMetaData();
184 verify(mockConnection).getMetaData();
185 }
186 }