db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d..@apache.org
Subject svn commit: r610605 - /db/derby/code/branches/10.3/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java
Date Wed, 09 Jan 2008 23:08:03 GMT
Author: djd
Date: Wed Jan  9 15:07:24 2008
New Revision: 610605

URL: http://svn.apache.org/viewvc?rev=610605&view=rev
Log:
DERBY-2907 Automatically close statement objects returned through utility methods in junit
tests. Merge of 553994 from trunk.

Modified:
    db/derby/code/branches/10.3/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java

Modified: db/derby/code/branches/10.3/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java
URL: http://svn.apache.org/viewvc/db/derby/code/branches/10.3/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java?rev=610605&r1=610604&r2=610605&view=diff
==============================================================================
--- db/derby/code/branches/10.3/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java
(original)
+++ db/derby/code/branches/10.3/java/testing/org/apache/derbyTesting/junit/BaseJDBCTestCase.java
Wed Jan  9 15:07:24 2008
@@ -31,6 +31,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;
 
@@ -55,6 +58,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.
@@ -113,17 +123,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
@@ -131,24 +158,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
@@ -156,7 +192,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;
     }    
 
     /**
@@ -164,6 +204,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)
@@ -173,7 +215,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;
     }
 
     /**
@@ -181,6 +226,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)
@@ -190,19 +237,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;
+ 
     }
     
     /**
@@ -226,11 +282,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;
     }
@@ -381,14 +448,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
@@ -628,16 +690,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,
@@ -846,23 +908,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));
     }
 
     /**



Mime
View raw message