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