View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * A dummy {@link Connection}, for testing purposes.
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         switch (sql) {
286         case "null":
287             return null;
288         case "invalid":
289             throw new SQLException("invalid query");
290         case "broken":
291             throw new SQLException("broken connection");
292         default:
293             break;
294         }
295         return new TesterPreparedStatement(this, sql);
296     }
297 
298     @Override
299     public PreparedStatement prepareStatement(final String sql, final int autoGeneratedKeys)
300         throws SQLException {
301         checkOpen();
302         return new TesterPreparedStatement(this, sql, autoGeneratedKeys);
303     }
304 
305     @Override
306     public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency) throws SQLException {
307         checkOpen();
308         return new TesterPreparedStatement(this, sql, resultSetType, resultSetConcurrency);
309     }
310 
311     @Override
312     public PreparedStatement prepareStatement(final String sql, final int resultSetType,
313                                               final int resultSetConcurrency,
314                                               final int resultSetHoldability)
315         throws SQLException {
316         checkOpen();
317         return new TesterPreparedStatement(this, sql, resultSetType, resultSetConcurrency, resultSetHoldability);
318     }
319 
320     @Override
321     public PreparedStatement prepareStatement(final String sql, final int[] columnIndexes)
322         throws SQLException {
323         return new TesterPreparedStatement(this, sql, columnIndexes);
324     }
325 
326     @Override
327     public PreparedStatement prepareStatement(final String sql, final String[] columnNames)
328         throws SQLException {
329         return new TesterPreparedStatement(this, sql, columnNames);
330     }
331 
332     @Override
333     public void releaseSavepoint(final java.sql.Savepoint savepoint) throws SQLException {
334         throw new SQLException("Not implemented.");
335     }
336 
337     @Override
338     public void rollback() throws SQLException {
339         checkOpen();
340         if (isReadOnly()) {
341             throw new SQLException("Cannot rollback a readonly connection");
342         }
343         if (getAutoCommit()) {
344             throw new SQLException("Cannot rollback a connection in auto-commit");
345         }
346     }
347 
348     @Override
349     public void rollback(final java.sql.Savepoint savepoint) throws SQLException {
350         throw new SQLException("Not implemented.");
351     }
352 
353     @Override
354     public void setAutoCommit(final boolean autoCommit) throws SQLException {
355         checkOpen();
356         this.autoCommit = autoCommit;
357     }
358 
359     @Override
360     public void setCatalog(final String catalog) throws SQLException {
361         checkOpen();
362         this.catalog = catalog;
363     }
364 
365     @Override
366     public void setClientInfo(final Properties properties) throws SQLClientInfoException {
367         throw new SQLClientInfoException();
368     }
369 
370     @Override
371     public void setClientInfo(final String name, final String value) throws SQLClientInfoException {
372         throw new SQLClientInfoException();
373     }
374 
375     public void setFailure(final Exception failure) {
376         this.failure = failure;
377     }
378 
379     @Override
380     public void setHoldability(final int holdability) throws SQLException {
381         throw new SQLException("Not implemented.");
382     }
383 
384     @Override
385     public void setNetworkTimeout(final Executor executor, final int milliseconds)
386             throws SQLException {
387         throw new SQLException("Not implemented.");
388     }
389 
390     @Override
391     public void setReadOnly(final boolean readOnly) throws SQLException {
392         checkOpen();
393         this.readOnly = readOnly;
394     }
395 
396     @Override
397     public java.sql.Savepoint setSavepoint() throws SQLException {
398         throw new SQLException("Not implemented.");
399     }
400 
401     @Override
402     public java.sql.Savepoint setSavepoint(final String name) throws SQLException {
403         throw new SQLException("Not implemented.");
404     }
405 
406     @Override
407     public void setSchema(final String schema) throws SQLException {
408         checkOpen();
409         this.schema= schema;
410     }
411 
412     public void setSqlExceptionOnClose(final boolean sqlExceptionOnClose) {
413         this.sqlExceptionOnClose = sqlExceptionOnClose;
414     }
415 
416     @Override
417     public void setTransactionIsolation(final int level) throws SQLException {
418         checkOpen();
419         this.transactionIsolation = level;
420     }
421 
422     @Override
423     public void setTypeMap(final Map<String,Class<?>> map) throws SQLException {
424         checkOpen();
425         this.typeMap = map;
426     }
427 
428     public void setWarnings(final SQLWarning warning) {
429         this.warnings = warning;
430     }
431 
432     @Override
433     public <T> T unwrap(final Class<T> iface) throws SQLException {
434         throw new SQLException("Not implemented.");
435     }
436 }