This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/commons-dbcp.git
The following commit(s) were added to refs/heads/master by this push: new f84bf38 [DBCP-552] org.apache.commons.dbcp2.DelegatingConnection.passivate() should close ALL of its resources even when an exception occurs. f84bf38 is described below commit f84bf38c0170f6b6b723293306a3a11df16d2d36 Author: Gary Gregory <gardgreg...@gmail.com> AuthorDate: Tue Jul 30 18:24:08 2019 -0400 [DBCP-552] org.apache.commons.dbcp2.DelegatingConnection.passivate() should close ALL of its resources even when an exception occurs. Also remove trailing whitespace. --- src/changes/changes.xml | 5 +- .../org/apache/commons/dbcp2/AbandonedTrace.java | 2 +- .../org/apache/commons/dbcp2/BasicDataSource.java | 4 +- .../commons/dbcp2/ConnectionFactoryFactory.java | 4 +- .../apache/commons/dbcp2/DelegatingConnection.java | 32 ++- .../apache/commons/dbcp2/DelegatingStatement.java | 10 +- .../org/apache/commons/dbcp2/DriverFactory.java | 2 +- .../org/apache/commons/dbcp2/SQLExceptionList.java | 14 +- .../dbcp2/datasources/InstanceKeyDataSource.java | 2 +- .../commons/dbcp2/managed/ManagedDataSource.java | 2 +- .../commons/dbcp2/TestDelegatingConnection.java | 120 +++++--- .../commons/dbcp2/TestDelegatingStatement.java | 2 +- .../org/apache/commons/dbcp2/TesterConnection.java | 315 +++++++++++---------- .../commons/dbcp2/TesterConnectionFactory.java | 2 +- .../commons/dbcp2/TesterPreparedStatement.java | 2 +- .../org/apache/commons/dbcp2/TesterResultSet.java | 8 +- .../org/apache/commons/dbcp2/TesterStatement.java | 16 +- .../dbcp2/cpdsadapter/TestDriverAdapterCPDS.java | 2 +- 18 files changed, 310 insertions(+), 234 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index b69108a..13e1356 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -125,7 +125,10 @@ The <action> type attribute can be add,update,fix,remove. Update tests from org.mockito:mockito-core 2.28.2 to 3.0.0. </action> <action dev="ggregory" type="update" issue="DBCP-551" due-to="Gary Gregory"> - org.apache.commons.dbcp2.DelegatingStatement.close() should try to close ALL of its result sets. + org.apache.commons.dbcp2.DelegatingStatement.close() should try to close ALL of its result sets even when an exception occurs. + </action> + <action dev="ggregory" type="update" issue="DBCP-552" due-to="Gary Gregory"> + org.apache.commons.dbcp2.DelegatingConnection.passivate() should close ALL of its resources even when an exception occurs. </action> </release> <release version="2.6.0" date="2019-02-14" description="This is a minor release, including bug fixes and enhancements."> diff --git a/src/main/java/org/apache/commons/dbcp2/AbandonedTrace.java b/src/main/java/org/apache/commons/dbcp2/AbandonedTrace.java index aa55882..a00593c 100644 --- a/src/main/java/org/apache/commons/dbcp2/AbandonedTrace.java +++ b/src/main/java/org/apache/commons/dbcp2/AbandonedTrace.java @@ -168,7 +168,7 @@ public class AbandonedTrace implements TrackedUse { /** * Removes this object the source object is tracing. - * + * * @param source The object tracing * @since 2.7.0 */ diff --git a/src/main/java/org/apache/commons/dbcp2/BasicDataSource.java b/src/main/java/org/apache/commons/dbcp2/BasicDataSource.java index 243a29f..337c40d 100644 --- a/src/main/java/org/apache/commons/dbcp2/BasicDataSource.java +++ b/src/main/java/org/apache/commons/dbcp2/BasicDataSource.java @@ -1541,14 +1541,14 @@ public class BasicDataSource implements DataSource, BasicDataSourceMXBean, MBean * Logs the given throwable. * @param message TODO * @param throwable the throwable. - * + * * @since 2.7.0 */ protected void log(String message, Throwable throwable) { if (logWriter != null) { logWriter.println(message); throwable.printStackTrace(logWriter); - } + } } @Override diff --git a/src/main/java/org/apache/commons/dbcp2/ConnectionFactoryFactory.java b/src/main/java/org/apache/commons/dbcp2/ConnectionFactoryFactory.java index 394549c..d50aa74 100644 --- a/src/main/java/org/apache/commons/dbcp2/ConnectionFactoryFactory.java +++ b/src/main/java/org/apache/commons/dbcp2/ConnectionFactoryFactory.java @@ -23,7 +23,7 @@ import java.util.Properties; /* * Creates {@link ConnectionFactory} instances. - * + * * @since 2.7.0 */ class ConnectionFactoryFactory { @@ -31,7 +31,7 @@ class ConnectionFactoryFactory { /** * Creates a new {@link DriverConnectionFactory} allowing for an override through * {@link BasicDataSource#getDriverClassName()}. - * + * * @param basicDataSource Configures creation. * @param driver The JDBC driver. * @return a new {@link DriverConnectionFactory} allowing for a {@link BasicDataSource#getDriverClassName()} diff --git a/src/main/java/org/apache/commons/dbcp2/DelegatingConnection.java b/src/main/java/org/apache/commons/dbcp2/DelegatingConnection.java index 2b8853f..02b5ac1 100644 --- a/src/main/java/org/apache/commons/dbcp2/DelegatingConnection.java +++ b/src/main/java/org/apache/commons/dbcp2/DelegatingConnection.java @@ -34,6 +34,7 @@ import java.sql.SQLXML; import java.sql.Savepoint; import java.sql.Statement; import java.sql.Struct; +import java.util.ArrayList; import java.util.Collections; import java.util.Iterator; import java.util.List; @@ -246,22 +247,17 @@ public class DelegatingConnection<C extends Connection> extends AbandonedTrace i } protected void handleException(final SQLException e) throws SQLException { - handleException(e, true); + throw e; } /** - * Rethrows the given {@code SQLException} if {@code rethrow} is true. - * + * Handles the given {@code SQLException}. + * * @param e The SQLException - * @param rethrow Wether or not to rethrow * @return the given {@code SQLException} - * @throws SQLException the given {@code SQLException} * @since 2.7.0 */ - protected SQLException handleException(final SQLException e, final boolean rethrow) throws SQLException { - if (rethrow) { - throw e; - } + protected <T extends Throwable> T handleExceptionNoThrow(final T e) { return e; } @@ -620,23 +616,35 @@ public class DelegatingConnection<C extends Connection> extends AbandonedTrace i } protected void passivate() throws SQLException { - // The JDBC spec requires that a Connection close any open + // The JDBC specification requires that a Connection close any open // Statement's when it is closed. // DBCP-288. Not all the traced objects will be statements final List<AbandonedTrace> traces = getTrace(); if (traces != null && !traces.isEmpty()) { + final List<Exception> thrown = new ArrayList<>(); final Iterator<AbandonedTrace> traceIter = traces.iterator(); while (traceIter.hasNext()) { final Object trace = traceIter.next(); if (trace instanceof Statement) { - ((Statement) trace).close(); + try { + ((Statement) trace).close(); + } catch (Exception e) { + thrown.add(e); + } } else if (trace instanceof ResultSet) { // DBCP-265: Need to close the result sets that are // generated via DatabaseMetaData - ((ResultSet) trace).close(); + try { + ((ResultSet) trace).close(); + } catch (Exception e) { + thrown.add(e); + } } } clearTrace(); + if (!thrown.isEmpty()) { + throw new SQLExceptionList(thrown); + } } setLastUsed(0); } diff --git a/src/main/java/org/apache/commons/dbcp2/DelegatingStatement.java b/src/main/java/org/apache/commons/dbcp2/DelegatingStatement.java index 88f01e5..e806fab 100644 --- a/src/main/java/org/apache/commons/dbcp2/DelegatingStatement.java +++ b/src/main/java/org/apache/commons/dbcp2/DelegatingStatement.java @@ -126,7 +126,7 @@ public class DelegatingStatement extends AbandonedTrace implements Statement { if (isClosed()) { return; } - final List<SQLException> thrown = new ArrayList<>(); + final List<Exception> thrown = new ArrayList<>(); try { if (connection != null) { connection.removeTrace(this); @@ -145,10 +145,10 @@ public class DelegatingStatement extends AbandonedTrace implements Statement { if (resultSet != null) { try { resultSet.close(); - } catch (SQLException e) { + } catch (Exception e) { if (connection != null) { // Does not rethrow e. - connection.handleException(e, false); + connection.handleExceptionNoThrow(e); } thrown.add(e); } @@ -159,10 +159,10 @@ public class DelegatingStatement extends AbandonedTrace implements Statement { if (statement != null) { try { statement.close(); - } catch (SQLException e) { + } catch (Exception e) { if (connection != null) { // Does not rethrow e. - connection.handleException(e, false); + connection.handleExceptionNoThrow(e); } thrown.add(e); } diff --git a/src/main/java/org/apache/commons/dbcp2/DriverFactory.java b/src/main/java/org/apache/commons/dbcp2/DriverFactory.java index 2a05453..0b1e92f 100644 --- a/src/main/java/org/apache/commons/dbcp2/DriverFactory.java +++ b/src/main/java/org/apache/commons/dbcp2/DriverFactory.java @@ -23,7 +23,7 @@ import java.sql.SQLException; /* * Creates {@link Driver} instances. - * + * * @since 2.7.0 */ class DriverFactory { diff --git a/src/main/java/org/apache/commons/dbcp2/SQLExceptionList.java b/src/main/java/org/apache/commons/dbcp2/SQLExceptionList.java index 1df9356..8a35dc6 100644 --- a/src/main/java/org/apache/commons/dbcp2/SQLExceptionList.java +++ b/src/main/java/org/apache/commons/dbcp2/SQLExceptionList.java @@ -21,30 +21,30 @@ import java.sql.SQLException; import java.util.List; /** - * A SQLException based on a list of SQLException causes. + * A SQLException based on a list of Throwable causes. * <p> * The first exception in the list is used as this exception's cause and is accessible with the usual * {@link #getCause()} while the complete list is accessible with {@link #getCauseList()}. * </p> - * + * * @since 2.7.0 */ public class SQLExceptionList extends SQLException { private static final long serialVersionUID = 1L; - private final List<? extends SQLException> causeList; + private final List<? extends Throwable> causeList; /** * Creates a new exception caused by a list of exceptions. - * + * * @param causeList a list of cause exceptions. */ - public SQLExceptionList(List<? extends SQLException> causeList) { - super(String.format("%,d SQLExceptions: %s", causeList.size(), causeList), causeList.get(0)); + public SQLExceptionList(List<? extends Throwable> causeList) { + super(String.format("%,d exceptions: %s", causeList.size(), causeList), causeList.get(0)); this.causeList = causeList; } - public List<? extends SQLException> getCauseList() { + public List<? extends Throwable> getCauseList() { return causeList; } diff --git a/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSource.java b/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSource.java index 52ef8b3..9c1cb9f 100644 --- a/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSource.java +++ b/src/main/java/org/apache/commons/dbcp2/datasources/InstanceKeyDataSource.java @@ -146,7 +146,7 @@ public abstract class InstanceKeyDataSource implements DataSource, Referenceable /** * Throws an IllegalStateException, if a PooledConnection has already been requested. - * + * * @throws IllegalStateException Thrown if a PooledConnection has already been requested. */ protected void assertInitializationAllowed() throws IllegalStateException { diff --git a/src/main/java/org/apache/commons/dbcp2/managed/ManagedDataSource.java b/src/main/java/org/apache/commons/dbcp2/managed/ManagedDataSource.java index 791e384..280bdeb 100644 --- a/src/main/java/org/apache/commons/dbcp2/managed/ManagedDataSource.java +++ b/src/main/java/org/apache/commons/dbcp2/managed/ManagedDataSource.java @@ -66,7 +66,7 @@ public class ManagedDataSource<C extends Connection> extends PoolingDataSource<C /** * Gets the transaction registry. - * + * * @return The transaction registry. * @see #setTransactionRegistry(TransactionRegistry) * @since 2.6.0 diff --git a/src/test/java/org/apache/commons/dbcp2/TestDelegatingConnection.java b/src/test/java/org/apache/commons/dbcp2/TestDelegatingConnection.java index fe3aa57..56bb998 100644 --- a/src/test/java/org/apache/commons/dbcp2/TestDelegatingConnection.java +++ b/src/test/java/org/apache/commons/dbcp2/TestDelegatingConnection.java @@ -18,8 +18,8 @@ package org.apache.commons.dbcp2; import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @@ -28,6 +28,7 @@ import java.sql.PreparedStatement; import java.sql.SQLException; import org.apache.commons.pool2.impl.GenericKeyedObjectPool; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -40,32 +41,39 @@ public class TestDelegatingConnection { * Used to validate fix for DBCP-241 */ static class RTEGeneratingConnection extends TesterConnection { + public RTEGeneratingConnection() { super("",""); } + @Override public String toString() { throw new RuntimeException("bang!"); } } - private DelegatingConnection<? extends Connection> conn = null; - private Connection delegateConn = null; - private Connection delegateConn2 = null; + + private DelegatingConnection<? extends Connection> delegatingConnection; + private Connection connection; + private Connection connection2; + private TesterStatement testerStatement; + private TesterResultSet testerResultSet; @BeforeEach public void setUp() throws Exception { - delegateConn = new TesterConnection("test", "test"); - delegateConn2 = new TesterConnection("test", "test"); - conn = new DelegatingConnection<>(delegateConn); + connection = new TesterConnection("test", "test"); + connection2 = new TesterConnection("test", "test"); + delegatingConnection = new DelegatingConnection<>(connection); + testerStatement = new TesterStatement(delegatingConnection); + testerResultSet = new TesterResultSet(testerStatement); } @Test public void testCheckOpen() throws Exception { - conn.checkOpen(); - conn.close(); + delegatingConnection.checkOpen(); + delegatingConnection.close(); try { - conn.checkOpen(); + delegatingConnection.checkOpen(); fail("Expecting SQLException"); } catch (final SQLException ex) { // expected @@ -78,38 +86,38 @@ public class TestDelegatingConnection { @Test public void testCheckOpenNull() throws Exception { try { - conn.close(); - conn.checkOpen(); + delegatingConnection.close(); + delegatingConnection.checkOpen(); fail("Expecting SQLException"); } catch (final SQLException ex) { assertTrue(ex.getMessage().endsWith("is closed.")); } try { - conn = new DelegatingConnection<>(null); - conn.setClosedInternal(true); - conn.checkOpen(); + delegatingConnection = new DelegatingConnection<>(null); + delegatingConnection.setClosedInternal(true); + delegatingConnection.checkOpen(); fail("Expecting SQLException"); } catch (final SQLException ex) { assertTrue(ex.getMessage().endsWith("is null.")); } try { - final PoolingConnection pc = new PoolingConnection(delegateConn2); + final PoolingConnection pc = new PoolingConnection(connection2); pc.setStatementPool(new GenericKeyedObjectPool<>(pc)); - conn = new DelegatingConnection<>(pc); + delegatingConnection = new DelegatingConnection<>(pc); pc.close(); - conn.close(); - try (PreparedStatement ps = conn.prepareStatement("")){} + delegatingConnection.close(); + try (PreparedStatement ps = delegatingConnection.prepareStatement("")){} fail("Expecting SQLException"); } catch (final SQLException ex) { assertTrue(ex.getMessage().endsWith("is closed.")); } try { - conn = new DelegatingConnection<>(new RTEGeneratingConnection()); - conn.close(); - conn.checkOpen(); + delegatingConnection = new DelegatingConnection<>(new RTEGeneratingConnection()); + delegatingConnection.close(); + delegatingConnection.checkOpen(); fail("Expecting SQLException"); } catch (final SQLException ex) { assertTrue(ex.getMessage().endsWith("is closed.")); @@ -118,30 +126,78 @@ public class TestDelegatingConnection { @Test public void testConnectionToString() throws Exception { - final String s = conn.toString(); + final String s = delegatingConnection.toString(); assertNotNull(s); assertTrue(s.length() > 0); } @Test public void testGetDelegate() throws Exception { - assertEquals(delegateConn,conn.getDelegate()); + assertEquals(connection,delegatingConnection.getDelegate()); } @Test public void testIsClosed() throws Exception { - conn.checkOpen(); - assertFalse(conn.isClosed()); - conn.close(); - assertTrue(conn.isClosed()); + delegatingConnection.checkOpen(); + assertFalse(delegatingConnection.isClosed()); + delegatingConnection.close(); + assertTrue(delegatingConnection.isClosed()); } @Test public void testIsClosedNullDelegate() throws Exception { - conn.checkOpen(); - assertFalse(conn.isClosed()); - conn.setDelegate(null); - assertTrue(conn.isClosed()); + delegatingConnection.checkOpen(); + assertFalse(delegatingConnection.isClosed()); + delegatingConnection.setDelegate(null); + assertTrue(delegatingConnection.isClosed()); + } + + @Test + public void testPassivateWithResultSetCloseException() { + try { + testerResultSet.setSqlExceptionOnClose(true); + delegatingConnection.addTrace(testerResultSet); + delegatingConnection.passivate(); + Assertions.fail("Expected SQLExceptionList"); + } catch (SQLException e) { + Assertions.assertTrue(e instanceof SQLExceptionList); + Assertions.assertEquals(1, ((SQLExceptionList) e).getCauseList().size()); + } finally { + testerResultSet.setSqlExceptionOnClose(false); + } + } + + @Test + public void testPassivateWithResultSetCloseExceptionAndStatementCloseException() { + try { + testerStatement.setSqlExceptionOnClose(true); + testerResultSet.setSqlExceptionOnClose(true); + delegatingConnection.addTrace(testerStatement); + delegatingConnection.addTrace(testerResultSet); + delegatingConnection.passivate(); + Assertions.fail("Expected SQLExceptionList"); + } catch (SQLException e) { + Assertions.assertTrue(e instanceof SQLExceptionList); + Assertions.assertEquals(2, ((SQLExceptionList) e).getCauseList().size()); + } finally { + testerStatement.setSqlExceptionOnClose(false); + testerResultSet.setSqlExceptionOnClose(false); + } + } + + @Test + public void testPassivateWithStatementCloseException() { + try { + testerStatement.setSqlExceptionOnClose(true); + delegatingConnection.addTrace(testerStatement); + delegatingConnection.passivate(); + Assertions.fail("Expected SQLExceptionList"); + } catch (SQLException e) { + Assertions.assertTrue(e instanceof SQLExceptionList); + Assertions.assertEquals(1, ((SQLExceptionList) e).getCauseList().size()); + } finally { + testerStatement.setSqlExceptionOnClose(false); + } } } diff --git a/src/test/java/org/apache/commons/dbcp2/TestDelegatingStatement.java b/src/test/java/org/apache/commons/dbcp2/TestDelegatingStatement.java index f187cbf..e44505b 100644 --- a/src/test/java/org/apache/commons/dbcp2/TestDelegatingStatement.java +++ b/src/test/java/org/apache/commons/dbcp2/TestDelegatingStatement.java @@ -49,7 +49,7 @@ public class TestDelegatingStatement { return false; } } - + private DelegatingConnection<Connection> delegatingConnection; private TesterConnection testerConnection; private Statement mockedStatement; diff --git a/src/test/java/org/apache/commons/dbcp2/TesterConnection.java b/src/test/java/org/apache/commons/dbcp2/TesterConnection.java index dae4380..2180b1a 100644 --- a/src/test/java/org/apache/commons/dbcp2/TesterConnection.java +++ b/src/test/java/org/apache/commons/dbcp2/TesterConnection.java @@ -39,30 +39,44 @@ import java.util.concurrent.Executor; * A dummy {@link Connection}, for testing purposes. */ public class TesterConnection extends AbandonedTrace implements Connection { - + protected boolean _open = true; protected boolean _autoCommit = true; protected int _transactionIsolation = 1; protected DatabaseMetaData _metaData = new TesterDatabaseMetaData(); - protected String _catalog = null; + protected String _catalog; protected String schema; - protected Map<String,Class<?>> _typeMap = null; - protected boolean _readOnly = false; - protected SQLWarning warnings = null; - protected String userName = null; + protected Map<String,Class<?>> _typeMap; + protected boolean _readOnly; + protected SQLWarning warnings; + protected String userName; protected Exception failure; + protected boolean sqlExceptionOnClose; - public TesterConnection(final String userName, + TesterConnection(final String userName, @SuppressWarnings("unused") final String password) { this.userName = userName; } - public String getUserName() { - return this.userName; + @Override + public void abort(final Executor executor) throws SQLException { + throw new SQLException("Not implemented."); } - public void setWarnings(final SQLWarning warning) { - this.warnings = warning; + protected void checkFailure() throws SQLException { + if (failure != null) { + if(failure instanceof SQLException) { + throw (SQLException)failure; + } + throw new SQLException("TesterConnection failure", failure); + } + } + + protected void checkOpen() throws SQLException { + if(!_open) { + throw new SQLException("Connection is closed."); + } + checkFailure(); } @Override @@ -86,208 +100,166 @@ public class TesterConnection extends AbandonedTrace implements Connection { } @Override - public Statement createStatement() throws SQLException { - checkOpen(); - return new TesterStatement(this); + public Array createArrayOf(final String typeName, final Object[] elements) throws SQLException { + throw new SQLException("Not implemented."); } @Override - public Statement createStatement(final int resultSetType, final int resultSetConcurrency) throws SQLException { - checkOpen(); - return new TesterStatement(this); + public Blob createBlob() throws SQLException { + throw new SQLException("Not implemented."); } @Override - public boolean getAutoCommit() throws SQLException { - checkOpen(); - return _autoCommit; + public Clob createClob() throws SQLException { + throw new SQLException("Not implemented."); } @Override - public String getCatalog() throws SQLException { - checkOpen(); - return _catalog; + public NClob createNClob() throws SQLException { + throw new SQLException("Not implemented."); } @Override - public DatabaseMetaData getMetaData() throws SQLException { - checkOpen(); - return _metaData; + public SQLXML createSQLXML() throws SQLException { + throw new SQLException("Not implemented."); } @Override - public int getTransactionIsolation() throws SQLException { + public Statement createStatement() throws SQLException { checkOpen(); - return _transactionIsolation; + return new TesterStatement(this); } @Override - public Map<String,Class<?>> getTypeMap() throws SQLException { + public Statement createStatement(final int resultSetType, final int resultSetConcurrency) throws SQLException { checkOpen(); - return _typeMap; + return new TesterStatement(this); } @Override - public SQLWarning getWarnings() throws SQLException { - checkOpen(); - return warnings; + public Statement createStatement(final int resultSetType, + final int resultSetConcurrency, + final int resultSetHoldability) + throws SQLException { + return createStatement(); } @Override - public boolean isClosed() throws SQLException { - checkFailure(); - return !_open; + public Struct createStruct(final String typeName, final Object[] attributes) throws SQLException { + throw new SQLException("Not implemented."); } @Override - public boolean isReadOnly() throws SQLException { + public boolean getAutoCommit() throws SQLException { checkOpen(); - return _readOnly; + return _autoCommit; } @Override - public String nativeSQL(final String sql) throws SQLException { + public String getCatalog() throws SQLException { checkOpen(); - return sql; + return _catalog; } @Override - public CallableStatement prepareCall(final String sql) throws SQLException { - checkOpen(); - if ("warning".equals(sql)) { - setWarnings(new SQLWarning("warning in prepareCall")); - } - return new TesterCallableStatement(this, sql); + public Properties getClientInfo() throws SQLException { + throw new SQLException("Not implemented."); } @Override - public CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency) throws SQLException { - checkOpen(); - return new TesterCallableStatement(this, sql, resultSetType, resultSetConcurrency); + public String getClientInfo(final String name) throws SQLException { + throw new SQLException("Not implemented."); } @Override - public PreparedStatement prepareStatement(final String sql) throws SQLException { - checkOpen(); - if("null".equals(sql)) { - return null; - } if("invalid".equals(sql)) { - throw new SQLException("invalid query"); - } if ("broken".equals(sql)) { - throw new SQLException("broken connection"); - } - return new TesterPreparedStatement(this, sql); + public int getHoldability() throws SQLException { + throw new SQLException("Not implemented."); } @Override - public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency) throws SQLException { + public DatabaseMetaData getMetaData() throws SQLException { checkOpen(); - return new TesterPreparedStatement(this, sql, resultSetType, resultSetConcurrency); + return _metaData; } @Override - public void rollback() throws SQLException { - checkOpen(); - if (isReadOnly()) { - throw new SQLException("Cannot rollback a readonly connection"); - } + public int getNetworkTimeout() throws SQLException { + throw new SQLException("Not implemented."); } @Override - public void setAutoCommit(final boolean autoCommit) throws SQLException { + public String getSchema() throws SQLException { checkOpen(); - _autoCommit = autoCommit; + return schema; } @Override - public void setCatalog(final String catalog) throws SQLException { + public int getTransactionIsolation() throws SQLException { checkOpen(); - _catalog = catalog; + return _transactionIsolation; } @Override - public void setReadOnly(final boolean readOnly) throws SQLException { + public Map<String,Class<?>> getTypeMap() throws SQLException { checkOpen(); - _readOnly = readOnly; + return _typeMap; } - @Override - public void setTransactionIsolation(final int level) throws SQLException { - checkOpen(); - _transactionIsolation = level; + public String getUserName() { + return this.userName; } @Override - public void setTypeMap(final Map<String,Class<?>> map) throws SQLException { + public SQLWarning getWarnings() throws SQLException { checkOpen(); - _typeMap = map; - } - - protected void checkOpen() throws SQLException { - if(!_open) { - throw new SQLException("Connection is closed."); - } - checkFailure(); - } - - protected void checkFailure() throws SQLException { - if (failure != null) { - if(failure instanceof SQLException) { - throw (SQLException)failure; - } - throw new SQLException("TesterConnection failure", failure); - } - } - - public void setFailure(final Exception failure) { - this.failure = failure; + return warnings; } @Override - public int getHoldability() throws SQLException { - throw new SQLException("Not implemented."); + public boolean isClosed() throws SQLException { + checkFailure(); + return !_open; } @Override - public void setHoldability(final int holdability) throws SQLException { - throw new SQLException("Not implemented."); + public boolean isReadOnly() throws SQLException { + checkOpen(); + return _readOnly; } - @Override - public java.sql.Savepoint setSavepoint() throws SQLException { - throw new SQLException("Not implemented."); + public boolean isSqlExceptionOnClose() { + return sqlExceptionOnClose; } @Override - public java.sql.Savepoint setSavepoint(final String name) throws SQLException { - throw new SQLException("Not implemented."); + public boolean isValid(final int timeout) throws SQLException { + return _open; } @Override - public void rollback(final java.sql.Savepoint savepoint) throws SQLException { + public boolean isWrapperFor(final Class<?> iface) throws SQLException { throw new SQLException("Not implemented."); } @Override - public void releaseSavepoint(final java.sql.Savepoint savepoint) throws SQLException { - throw new SQLException("Not implemented."); + public String nativeSQL(final String sql) throws SQLException { + checkOpen(); + return sql; } @Override - public Statement createStatement(final int resultSetType, - final int resultSetConcurrency, - final int resultSetHoldability) - throws SQLException { - return createStatement(); + public CallableStatement prepareCall(final String sql) throws SQLException { + checkOpen(); + if ("warning".equals(sql)) { + setWarnings(new SQLWarning("warning in prepareCall")); + } + return new TesterCallableStatement(this, sql); } @Override - public PreparedStatement prepareStatement(final String sql, final int resultSetType, - final int resultSetConcurrency, - final int resultSetHoldability) - throws SQLException { + public CallableStatement prepareCall(final String sql, final int resultSetType, final int resultSetConcurrency) throws SQLException { checkOpen(); - return new TesterPreparedStatement(this, sql, resultSetType, resultSetConcurrency, resultSetHoldability); + return new TesterCallableStatement(this, sql, resultSetType, resultSetConcurrency); } @Override @@ -300,6 +272,19 @@ public class TesterConnection extends AbandonedTrace implements Connection { } @Override + public PreparedStatement prepareStatement(final String sql) throws SQLException { + checkOpen(); + if("null".equals(sql)) { + return null; + } if("invalid".equals(sql)) { + throw new SQLException("invalid query"); + } if ("broken".equals(sql)) { + throw new SQLException("broken connection"); + } + return new TesterPreparedStatement(this, sql); + } + + @Override public PreparedStatement prepareStatement(final String sql, final int autoGeneratedKeys) throws SQLException { checkOpen(); @@ -313,74 +298,95 @@ public class TesterConnection extends AbandonedTrace implements Connection { } @Override - public PreparedStatement prepareStatement(final String sql, final String columnNames[]) + public PreparedStatement prepareStatement(final String sql, final int resultSetType, final int resultSetConcurrency) throws SQLException { + checkOpen(); + return new TesterPreparedStatement(this, sql, resultSetType, resultSetConcurrency); + } + + @Override + public PreparedStatement prepareStatement(final String sql, final int resultSetType, + final int resultSetConcurrency, + final int resultSetHoldability) throws SQLException { - return new TesterPreparedStatement(this, sql, columnNames); + checkOpen(); + return new TesterPreparedStatement(this, sql, resultSetType, resultSetConcurrency, resultSetHoldability); } @Override - public boolean isWrapperFor(final Class<?> iface) throws SQLException { - throw new SQLException("Not implemented."); + public PreparedStatement prepareStatement(final String sql, final String columnNames[]) + throws SQLException { + return new TesterPreparedStatement(this, sql, columnNames); } @Override - public <T> T unwrap(final Class<T> iface) throws SQLException { + public void releaseSavepoint(final java.sql.Savepoint savepoint) throws SQLException { throw new SQLException("Not implemented."); } @Override - public Array createArrayOf(final String typeName, final Object[] elements) throws SQLException { - throw new SQLException("Not implemented."); + public void rollback() throws SQLException { + checkOpen(); + if (isReadOnly()) { + throw new SQLException("Cannot rollback a readonly connection"); + } } @Override - public Blob createBlob() throws SQLException { + public void rollback(final java.sql.Savepoint savepoint) throws SQLException { throw new SQLException("Not implemented."); } @Override - public Clob createClob() throws SQLException { - throw new SQLException("Not implemented."); + public void setAutoCommit(final boolean autoCommit) throws SQLException { + checkOpen(); + _autoCommit = autoCommit; } @Override - public NClob createNClob() throws SQLException { - throw new SQLException("Not implemented."); + public void setCatalog(final String catalog) throws SQLException { + checkOpen(); + _catalog = catalog; } @Override - public SQLXML createSQLXML() throws SQLException { - throw new SQLException("Not implemented."); + public void setClientInfo(final Properties properties) throws SQLClientInfoException { + throw new SQLClientInfoException(); } @Override - public Struct createStruct(final String typeName, final Object[] attributes) throws SQLException { - throw new SQLException("Not implemented."); + public void setClientInfo(final String name, final String value) throws SQLClientInfoException { + throw new SQLClientInfoException(); + } + + public void setFailure(final Exception failure) { + this.failure = failure; } @Override - public boolean isValid(final int timeout) throws SQLException { - return _open; + public void setHoldability(final int holdability) throws SQLException { + throw new SQLException("Not implemented."); } @Override - public void setClientInfo(final String name, final String value) throws SQLClientInfoException { - throw new SQLClientInfoException(); + public void setNetworkTimeout(final Executor executor, final int milliseconds) + throws SQLException { + throw new SQLException("Not implemented."); } @Override - public void setClientInfo(final Properties properties) throws SQLClientInfoException { - throw new SQLClientInfoException(); + public void setReadOnly(final boolean readOnly) throws SQLException { + checkOpen(); + _readOnly = readOnly; } @Override - public Properties getClientInfo() throws SQLException { + public java.sql.Savepoint setSavepoint() throws SQLException { throw new SQLException("Not implemented."); } @Override - public String getClientInfo(final String name) throws SQLException { + public java.sql.Savepoint setSavepoint(final String name) throws SQLException { throw new SQLException("Not implemented."); } @@ -390,25 +396,28 @@ public class TesterConnection extends AbandonedTrace implements Connection { this.schema= schema; } + public void setSqlExceptionOnClose(boolean sqlExceptionOnClose) { + this.sqlExceptionOnClose = sqlExceptionOnClose; + } + @Override - public String getSchema() throws SQLException { + public void setTransactionIsolation(final int level) throws SQLException { checkOpen(); - return schema; + _transactionIsolation = level; } @Override - public void abort(final Executor executor) throws SQLException { - throw new SQLException("Not implemented."); + public void setTypeMap(final Map<String,Class<?>> map) throws SQLException { + checkOpen(); + _typeMap = map; } - @Override - public void setNetworkTimeout(final Executor executor, final int milliseconds) - throws SQLException { - throw new SQLException("Not implemented."); + public void setWarnings(final SQLWarning warning) { + this.warnings = warning; } @Override - public int getNetworkTimeout() throws SQLException { + public <T> T unwrap(final Class<T> iface) throws SQLException { throw new SQLException("Not implemented."); } } diff --git a/src/test/java/org/apache/commons/dbcp2/TesterConnectionFactory.java b/src/test/java/org/apache/commons/dbcp2/TesterConnectionFactory.java index 7718e02..f21da7a 100644 --- a/src/test/java/org/apache/commons/dbcp2/TesterConnectionFactory.java +++ b/src/test/java/org/apache/commons/dbcp2/TesterConnectionFactory.java @@ -26,7 +26,7 @@ import java.util.Properties; * Dummy {@link ConnectionFactory} for testing purpose. */ public class TesterConnectionFactory implements ConnectionFactory { - + private final String connectionString; private final Driver driver; private final Properties properties; diff --git a/src/test/java/org/apache/commons/dbcp2/TesterPreparedStatement.java b/src/test/java/org/apache/commons/dbcp2/TesterPreparedStatement.java index 5ee864d..be671b2 100644 --- a/src/test/java/org/apache/commons/dbcp2/TesterPreparedStatement.java +++ b/src/test/java/org/apache/commons/dbcp2/TesterPreparedStatement.java @@ -39,7 +39,7 @@ import java.sql.SQLXML; * A dummy {@link PreparedStatement}, for testing purposes. */ public class TesterPreparedStatement extends TesterStatement implements PreparedStatement { - + private final ResultSetMetaData _resultSetMetaData = null; private String _sql = null; private String _catalog = null; diff --git a/src/test/java/org/apache/commons/dbcp2/TesterResultSet.java b/src/test/java/org/apache/commons/dbcp2/TesterResultSet.java index 0d37499..3d90277 100644 --- a/src/test/java/org/apache/commons/dbcp2/TesterResultSet.java +++ b/src/test/java/org/apache/commons/dbcp2/TesterResultSet.java @@ -41,7 +41,7 @@ import java.sql.SQLXML; * A dummy {@link ResultSet}, for testing purposes. */ public class TesterResultSet extends AbandonedTrace implements ResultSet { - + protected int _type = ResultSet.TYPE_FORWARD_ONLY; protected int _concurrency = ResultSet.CONCUR_READ_ONLY; protected Object[][] _data = null; @@ -102,7 +102,7 @@ public class TesterResultSet extends AbandonedTrace implements ResultSet { if (_sqlExceptionOnClose) { throw new SQLException("TestSQLExceptionOnClose"); } - + if (!_open) { return; } @@ -1185,13 +1185,13 @@ public java.sql.Date getDate(final int columnIndex, final Calendar cal) throws S public void updateTimestamp(final int columnIndex, final java.sql.Timestamp x) throws SQLException { checkOpen(); } - + @Override public void updateTimestamp(final String columnName, final java.sql.Timestamp x) throws SQLException { checkOpen(); } - + @Override public boolean wasNull() throws SQLException { checkOpen(); diff --git a/src/test/java/org/apache/commons/dbcp2/TesterStatement.java b/src/test/java/org/apache/commons/dbcp2/TesterStatement.java index 5a70f64..3032396 100644 --- a/src/test/java/org/apache/commons/dbcp2/TesterStatement.java +++ b/src/test/java/org/apache/commons/dbcp2/TesterStatement.java @@ -27,34 +27,34 @@ import java.sql.Statement; * A dummy {@link Statement}, for testing purposes. */ public class TesterStatement extends AbandonedTrace implements Statement { - - protected Connection _connection = null; + + protected Connection _connection; protected boolean _open = true; protected long _rowsUpdated = 1; protected boolean _executeResponse = true; protected int _maxFieldSize = 1024; protected long _maxRows = 1024; - protected boolean _escapeProcessing = false; + protected boolean _escapeProcessing; protected int _queryTimeout = 1000; - protected String _cursorName = null; + protected String _cursorName; protected int _fetchDirection = 1; protected int _fetchSize = 1; protected int _resultSetConcurrency = 1; protected int _resultSetType = 1; private int _resultSetHoldability = 1; - protected ResultSet _resultSet = null; - protected boolean _sqlExceptionOnClose = false; + protected ResultSet _resultSet; + protected boolean _sqlExceptionOnClose; public TesterStatement(final Connection conn) { _connection = conn; } - + public TesterStatement(final Connection conn, final int resultSetType, final int resultSetConcurrency) { _connection = conn; _resultSetType = resultSetType; _resultSetConcurrency = resultSetConcurrency; } - + public TesterStatement(final Connection conn, final int resultSetType, final int resultSetConcurrency, final int resultSetHoldability) { _connection = conn; diff --git a/src/test/java/org/apache/commons/dbcp2/cpdsadapter/TestDriverAdapterCPDS.java b/src/test/java/org/apache/commons/dbcp2/cpdsadapter/TestDriverAdapterCPDS.java index ab0a4dd..60b05ec 100644 --- a/src/test/java/org/apache/commons/dbcp2/cpdsadapter/TestDriverAdapterCPDS.java +++ b/src/test/java/org/apache/commons/dbcp2/cpdsadapter/TestDriverAdapterCPDS.java @@ -367,7 +367,7 @@ public class TestDriverAdapterCPDS { final Object o = pcds.getObjectInstance(ref, null, null, null); assertEquals(pcds.getDescription(), ((DriverAdapterCPDS) o).getDescription()); } - + @Test public void testToStringWithoutConnectionProperties() throws ClassNotFoundException {