1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.dbcp2;
19
20 import java.sql.Array;
21 import java.sql.Blob;
22 import java.sql.CallableStatement;
23 import java.sql.Clob;
24 import java.sql.Connection;
25 import java.sql.DatabaseMetaData;
26 import java.sql.NClob;
27 import java.sql.PreparedStatement;
28 import java.sql.SQLClientInfoException;
29 import java.sql.SQLException;
30 import java.sql.SQLWarning;
31 import java.sql.SQLXML;
32 import java.sql.Statement;
33 import java.sql.Struct;
34 import java.util.Map;
35 import java.util.Properties;
36 import java.util.concurrent.Executor;
37
38
39
40
41 public class TesterConnection extends AbandonedTrace implements Connection {
42
43 protected boolean open = true;
44 protected boolean aborted;
45 protected boolean autoCommit = true;
46 protected int transactionIsolation = 1;
47 protected final DatabaseMetaData metaData = new TesterDatabaseMetaData();
48 protected String catalog;
49 protected String schema;
50 protected Map<String, Class<?>> typeMap;
51 protected boolean readOnly;
52 protected SQLWarning warnings;
53 protected final String userName;
54 protected Exception failure;
55 protected boolean sqlExceptionOnClose;
56
57 TesterConnection(final String userName, @SuppressWarnings("unused") final String password) {
58 this.userName = userName;
59 }
60
61 @Override
62 public void abort(final Executor executor) throws SQLException {
63 checkFailure();
64 aborted = true;
65 open = false;
66 }
67
68 protected void checkFailure() throws SQLException {
69 if (failure != null) {
70 if (failure instanceof SQLException) {
71 throw (SQLException) failure;
72 }
73 throw new SQLException("TesterConnection failure", failure);
74 }
75 }
76
77 protected void checkOpen() throws SQLException {
78 if (!open) {
79 throw new SQLException("Connection is closed.");
80 }
81 checkFailure();
82 }
83
84 @Override
85 public void clearWarnings() throws SQLException {
86 checkOpen();
87 warnings = null;
88 }
89
90 @Override
91 public void close() throws SQLException {
92 checkFailure();
93 open = false;
94 }
95
96 @Override
97 public void commit() throws SQLException {
98 checkOpen();
99 if (isReadOnly()) {
100 throw new SQLException("Cannot commit a readonly connection");
101 }
102 }
103
104 @Override
105 public Array createArrayOf(final String typeName, final Object[] elements) throws SQLException {
106 throw new SQLException("Not implemented.");
107 }
108
109 @Override
110 public Blob createBlob() throws SQLException {
111 throw new SQLException("Not implemented.");
112 }
113
114 @Override
115 public Clob createClob() throws SQLException {
116 throw new SQLException("Not implemented.");
117 }
118
119 @Override
120 public NClob createNClob() throws SQLException {
121 throw new SQLException("Not implemented.");
122 }
123
124 @Override
125 public SQLXML createSQLXML() throws SQLException {
126 throw new SQLException("Not implemented.");
127 }
128
129 @Override
130 public Statement createStatement() throws SQLException {
131 checkOpen();
132 return new TesterStatement(this);
133 }
134
135 @Override
136 public Statement createStatement(final int resultSetType, final int resultSetConcurrency) throws SQLException {
137 checkOpen();
138 return new TesterStatement(this);
139 }
140
141 @Override
142 public Statement createStatement(final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) throws SQLException {
143 return createStatement();
144 }
145
146 @Override
147 public Struct createStruct(final String typeName, final Object[] attributes) throws SQLException {
148 throw new SQLException("Not implemented.");
149 }
150
151 @Override
152 public boolean getAutoCommit() throws SQLException {
153 checkOpen();
154 return autoCommit;
155 }
156
157 @Override
158 public String getCatalog() throws SQLException {
159 checkOpen();
160 return catalog;
161 }
162
163 @Override
164 public Properties getClientInfo() throws SQLException {
165 throw new SQLException("Not implemented.");
166 }
167
168 @Override
169 public String getClientInfo(final String name) throws SQLException {
170 throw new SQLException("Not implemented.");
171 }
172
173 @Override
174 public int getHoldability() throws SQLException {
175 throw new SQLException("Not implemented.");
176 }
177
178 @Override
179 public DatabaseMetaData getMetaData() throws SQLException {
180 checkOpen();
181 return metaData;
182 }
183
184 @Override
185 public int getNetworkTimeout() throws SQLException {
186 throw new SQLException("Not implemented.");
187 }
188
189 @Override
190 public String getSchema() throws SQLException {
191 checkOpen();
192 return schema;
193 }
194
195 @Override
196 public int getTransactionIsolation() throws SQLException {
197 checkOpen();
198 return transactionIsolation;
199 }
200
201 @Override
202 public Map<String, Class<?>> getTypeMap() throws SQLException {
203 checkOpen();
204 return typeMap;
205 }
206
207 public String getUserName() {
208 return this.userName;
209 }
210
211 @Override
212 public SQLWarning getWarnings() throws SQLException {
213 checkOpen();
214 return warnings;
215 }
216
217 public boolean isAborted() throws SQLException {
218 checkFailure();
219 return aborted;
220 }
221
222 @Override
223 public boolean isClosed() throws SQLException {
224 checkFailure();
225 return !open;
226 }
227
228 @Override
229 public boolean isReadOnly() throws SQLException {
230 checkOpen();
231 return readOnly;
232 }
233
234 public boolean isSqlExceptionOnClose() {
235 return sqlExceptionOnClose;
236 }
237
238 @Override
239 public boolean isValid(final int timeout) throws SQLException {
240 return open;
241 }
242
243 @Override
244 public boolean isWrapperFor(final Class<?> iface) throws SQLException {
245 throw new SQLException("Not implemented.");
246 }
247
248 @Override
249 public String nativeSQL(final String sql) throws SQLException {
250 checkOpen();
251 return sql;
252 }
253
254 @Override
255 public CallableStatement prepareCall(final String sql) throws SQLException {
256 checkOpen();
257 if ("warning".equals(sql)) {
258 setWarnings(new SQLWarning("warning in prepareCall"));
259 }
260 return new TesterCallableStatement(this, sql);
261 }
262
263 @Override
264 public CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency) throws SQLException {
265 checkOpen();
266 return new TesterCallableStatement(this, sql, resultSetType, resultSetConcurrency);
267 }
268
269 @Override
270 public CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability)
271 throws SQLException {
272 checkOpen();
273 return new TesterCallableStatement(this, sql, resultSetType, resultSetConcurrency, resultSetHoldability);
274 }
275
276 @Override
277 public PreparedStatement prepareStatement(final String sql) throws SQLException {
278 checkOpen();
279 switch (sql) {
280 case "null":
281 return null;
282 case "invalid":
283 throw new SQLException("invalid query");
284 case "broken":
285 throw new SQLException("broken connection");
286 default:
287 break;
288 }
289 return new TesterPreparedStatement(this, sql);
290 }
291
292 @Override
293 public PreparedStatement prepareStatement(final String sql, final int autoGeneratedKeys) throws SQLException {
294 checkOpen();
295 return new TesterPreparedStatement(this, sql, autoGeneratedKeys);
296 }
297
298 @Override
299 public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency) throws SQLException {
300 checkOpen();
301 return new TesterPreparedStatement(this, sql, resultSetType, resultSetConcurrency);
302 }
303
304 @Override
305 public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability)
306 throws SQLException {
307 checkOpen();
308 return new TesterPreparedStatement(this, sql, resultSetType, resultSetConcurrency, resultSetHoldability);
309 }
310
311 @Override
312 public PreparedStatement prepareStatement(final String sql, final int[] columnIndexes) throws SQLException {
313 return new TesterPreparedStatement(this, sql, columnIndexes);
314 }
315
316 @Override
317 public PreparedStatement prepareStatement(final String sql, final String[] columnNames) throws SQLException {
318 return new TesterPreparedStatement(this, sql, columnNames);
319 }
320
321 @Override
322 public void releaseSavepoint(final java.sql.Savepoint savepoint) throws SQLException {
323 throw new SQLException("Not implemented.");
324 }
325
326 @Override
327 public void rollback() throws SQLException {
328 checkOpen();
329 if (isReadOnly()) {
330 throw new SQLException("Cannot rollback a readonly connection");
331 }
332 if (getAutoCommit()) {
333 throw new SQLException("Cannot rollback a connection in auto-commit");
334 }
335 }
336
337 @Override
338 public void rollback(final java.sql.Savepoint savepoint) throws SQLException {
339 throw new SQLException("Not implemented.");
340 }
341
342 @Override
343 public void setAutoCommit(final boolean autoCommit) throws SQLException {
344 checkOpen();
345 this.autoCommit = autoCommit;
346 }
347
348 @Override
349 public void setCatalog(final String catalog) throws SQLException {
350 checkOpen();
351 this.catalog = catalog;
352 }
353
354 @Override
355 public void setClientInfo(final Properties properties) throws SQLClientInfoException {
356 throw new SQLClientInfoException();
357 }
358
359 @Override
360 public void setClientInfo(final String name, final String value) throws SQLClientInfoException {
361 throw new SQLClientInfoException();
362 }
363
364 public void setFailure(final Exception failure) {
365 this.failure = failure;
366 }
367
368 @Override
369 public void setHoldability(final int holdability) throws SQLException {
370 throw new SQLException("Not implemented.");
371 }
372
373 @Override
374 public void setNetworkTimeout(final Executor executor, final int milliseconds) throws SQLException {
375 throw new SQLException("Not implemented.");
376 }
377
378 @Override
379 public void setReadOnly(final boolean readOnly) throws SQLException {
380 checkOpen();
381 this.readOnly = readOnly;
382 }
383
384 @Override
385 public java.sql.Savepoint setSavepoint() throws SQLException {
386 throw new SQLException("Not implemented.");
387 }
388
389 @Override
390 public java.sql.Savepoint setSavepoint(final String name) throws SQLException {
391 throw new SQLException("Not implemented.");
392 }
393
394 @Override
395 public void setSchema(final String schema) throws SQLException {
396 checkOpen();
397 this.schema = schema;
398 }
399
400 public void setSqlExceptionOnClose(final boolean sqlExceptionOnClose) {
401 this.sqlExceptionOnClose = sqlExceptionOnClose;
402 }
403
404 @Override
405 public void setTransactionIsolation(final int level) throws SQLException {
406 checkOpen();
407 this.transactionIsolation = level;
408 }
409
410 @Override
411 public void setTypeMap(final Map<String, Class<?>> map) throws SQLException {
412 checkOpen();
413 this.typeMap = map;
414 }
415
416 public void setWarnings(final SQLWarning warning) {
417 this.warnings = warning;
418 }
419
420 @Override
421 public <T> T unwrap(final Class<T> iface) throws SQLException {
422 throw new SQLException("Not implemented.");
423 }
424 }