You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-commits@db.apache.org by dj...@apache.org on 2007/07/06 20:50:31 UTC
svn commit: r553994 -
/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java
Author: djd
Date: Fri Jul 6 11:50:26 2007
New Revision: 553994
URL: http://svn.apache.org/viewvc?view=rev&rev=553994
Log:
DERBY-2907 Close Statement objects opened by utility methods in BaseJDBCTestCase at tearDown time.
Modified:
db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java?view=diff&rev=553994&r1=553993&r2=553994
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java Fri Jul 6 11:50:26 2007
@@ -32,6 +32,9 @@
import java.security.PrivilegedActionException;
import java.net.URL;
import java.sql.*;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
import junit.framework.AssertionFailedError;
@@ -56,6 +59,13 @@
private Connection conn;
/**
+ * Maintain a list of statement objects that
+ * were returned by utility methods and close
+ * them at teardown.
+ */
+ private List statements;
+
+ /**
* Create a test case with the given name.
*
* @param name of the test case.
@@ -114,17 +124,34 @@
/**
* Utility method to create a Statement using the connection
* returned by getConnection.
+ * The returned statement object will be closed automatically
+ * at tearDown() but may be closed earlier by the test if required.
* @return Statement object from getConnection.createStatement()
* @throws SQLException
*/
public Statement createStatement() throws SQLException
{
- return getConnection().createStatement();
+ Statement s = getConnection().createStatement();
+ addStatement(s);
+ return s;
+ }
+
+ /**
+ * Add a statement into the list we will close
+ * at tearDown.
+ */
+ private void addStatement(Statement s)
+ {
+ if (statements == null)
+ statements = new ArrayList();
+ statements.add(s);
}
/**
* Utility method to create a Statement using the connection
* returned by getConnection.
+ * The returned statement object will be closed automatically
+ * at tearDown() but may be closed earlier by the test if required.
* @return Statement object from
* getConnection.createStatement(resultSetType, resultSetConcurrency)
* @throws SQLException
@@ -132,24 +159,33 @@
public Statement createStatement(int resultSetType,
int resultSetConcurrency) throws SQLException
{
- return getConnection().createStatement(resultSetType, resultSetConcurrency);
+ Statement s =
+ getConnection().createStatement(resultSetType, resultSetConcurrency);
+ addStatement(s);
+ return s;
}
/**
* Utility method to create a PreparedStatement using the connection
* returned by getConnection.
+ * The returned statement object will be closed automatically
+ * at tearDown() but may be closed earlier by the test if required.
* @return Statement object from
* getConnection.prepareStatement(sql)
* @throws SQLException
*/
public PreparedStatement prepareStatement(String sql) throws SQLException
{
- return getConnection().prepareStatement(sql);
+ PreparedStatement ps = getConnection().prepareStatement(sql);
+ addStatement(ps);
+ return ps;
}
/**
* Utility method to create a PreparedStatement using the connection
* returned by getConnection and a flag that signals the driver whether
* the auto-generated keys produced by this Statement object should be
* made available for retrieval.
+ * The returned statement object will be closed automatically
+ * at tearDown() but may be closed earlier by the test if required.
* @return Statement object from
* prepareStatement(sql, autoGeneratedKeys)
* @throws SQLException
@@ -157,7 +193,11 @@
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
throws SQLException
{
- return getConnection().prepareStatement(sql, autoGeneratedKeys);
+ PreparedStatement ps =
+ getConnection().prepareStatement(sql, autoGeneratedKeys);
+
+ addStatement(ps);
+ return ps;
}
/**
@@ -165,6 +205,8 @@
* returned by getConnection and an array of column indexes that
* indicates which auto-generated keys produced by this Statement
* object should be made available for retrieval.
+ * The returned statement object will be closed automatically
+ * at tearDown() but may be closed earlier by the test if required.
*
* @return Statement object from:
* prepareStatement(sql, columnIndexes)
@@ -174,7 +216,10 @@
public PreparedStatement prepareStatement(String sql,
int [] columnIndexes) throws SQLException
{
- return getConnection().prepareStatement(sql, columnIndexes);
+ PreparedStatement ps =
+ getConnection().prepareStatement(sql, columnIndexes);
+ addStatement(ps);
+ return ps;
}
/**
@@ -182,6 +227,8 @@
* returned by getConnection and an array of column names that
* indicates which auto-generated keys produced by this Statement
* object should be made available for retrieval.
+ * The returned statement object will be closed automatically
+ * at tearDown() but may be closed earlier by the test if required.
*
* @return Statement object from:
* prepareStatement(sql, columnNames)
@@ -191,19 +238,28 @@
public PreparedStatement prepareStatement(String sql,
String [] columnNames) throws SQLException
{
- return getConnection().prepareStatement(sql, columnNames);
- }
+ PreparedStatement ps =
+ getConnection().prepareStatement(sql, columnNames);
+ addStatement(ps);
+ return ps;
+ }
/**
* Utility method to create a CallableStatement using the connection
* returned by getConnection.
+ * The returned statement object will be closed automatically
+ * at tearDown() but may be closed earlier by the test if required.
* @return Statement object from
* getConnection().prepareCall(sql)
* @throws SQLException
*/
public CallableStatement prepareCall(String sql) throws SQLException
{
- return getConnection().prepareCall(sql);
+ CallableStatement cs =
+ getConnection().prepareCall(sql);
+ addStatement(cs);
+ return cs;
+
}
/**
@@ -227,11 +283,22 @@
/**
* Tear down this fixture, sub-classes should call
* super.tearDown(). This cleanups & closes the connection
- * if it is open.
+ * if it is open and any statement objects returned through
+ * the utility methods.
*/
protected void tearDown()
throws java.lang.Exception
{
+ if (statements != null) {
+ for (Iterator i = statements.iterator(); i.hasNext(); )
+ {
+ Statement s = (Statement) i.next();
+ s.close();
+ }
+ // Allow gc'ing of all those statements.
+ statements = null;
+ }
+
JDBC.cleanup(conn);
conn = null;
}
@@ -382,14 +449,9 @@
}
/**
- * Run a set of SQL commands from a String discarding the output,
- * intended for setup code, not testing (assert) code.
+ * Run a set of SQL commands from a String discarding the output.
* Commands are separated by a semi-colon. Connection used
- * is this objects default connection. Note that assertions
- * are not performed by this method. Code may assert the number
- * of failures returned, typically as zero. Exceptions
- * thrown by statements are not thrown through this method.
- * For single statements a Statement object should be used directly.
+ * is this objects default connection.
* @param sqlCommands
* @return Number of errors executing the script.
* @throws UnsupportedEncodingException
@@ -629,16 +691,16 @@
}
/**
- * Assert that the query does not compile and throws
+ * Assert that the SQL statement does not compile and throws
* a SQLException with the expected state.
*
* @param sqlState expected sql state.
- * @param query the query to compile.
+ * @param sql the SQL to compile.
*/
- public void assertCompileError(String sqlState, String query) {
+ public void assertCompileError(String sqlState, String sql) {
try {
- PreparedStatement pSt = prepareStatement(query);
+ PreparedStatement pSt = prepareStatement(sql);
if (usingDerbyNet())
{
/* For JCC the prepares are deferred until execution,
@@ -847,23 +909,23 @@
}
/**
- * Take a Statement object and a SQL query, execute it
+ * Take a Statement object and a SQL statement, execute it
* via the "executeUpdate()" method, and assert that the
* resultant row count matches the received row count.
*
- * Assumption is that 'query' does *not* have parameters
+ * Assumption is that 'sql' does *not* have parameters
* that need binding and that it can be executed using a
* simple Statement.executeUpdate() call.
*
* @param st Statement object on which to execute.
* @param expectedRC Expected row count.
- * @param query Query to execute.
+ * @param sql SQL to execute.
*/
public static void assertUpdateCount(Statement st,
- int expectedRC, String query) throws SQLException
+ int expectedRC, String sql) throws SQLException
{
assertEquals("Update count does not match:",
- expectedRC, st.executeUpdate(query));
+ expectedRC, st.executeUpdate(sql));
}
/**