db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kahat...@apache.org
Subject svn commit: r1330200 - /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/AlterTableTest.java
Date Wed, 25 Apr 2012 11:20:14 GMT
Author: kahatlen
Date: Wed Apr 25 11:20:13 2012
New Revision: 1330200

URL: http://svn.apache.org/viewvc?rev=1330200&view=rev
Log:
DERBY-5713: AlterTableTest holds on to resources after completion

Use local variables instead of instance variables to prevent references from
being held after the test has finished.

Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/AlterTableTest.java

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/AlterTableTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/AlterTableTest.java?rev=1330200&r1=1330199&r2=1330200&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/AlterTableTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/AlterTableTest.java
Wed Apr 25 11:20:13 2012
@@ -20,11 +20,8 @@ limitations under the License.
 package org.apache.derbyTesting.functionTests.tests.lang;
 
 import java.io.InputStream;
-import java.sql.Blob;
 import java.sql.ResultSetMetaData;
 import java.sql.Statement;
-import java.sql.CallableStatement;
-import java.sql.Clob;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLWarning;
@@ -46,17 +43,6 @@ import junit.framework.Assert;
 
 public final class AlterTableTest extends BaseJDBCTestCase {
 
-    ResultSet rs = null;
-    ResultSetMetaData rsmd;
-    DatabaseMetaData dbmd;
-    SQLWarning sqlWarn = null;
-    PreparedStatement pSt;
-    CallableStatement cSt;
-    //Statement st;
-    Connection conn;
-    String[][] expRS;
-    String[] expColNames;
-
     /**
      * Public constructor required for running test as standalone JUnit.
      */
@@ -71,7 +57,7 @@ public final class AlterTableTest extend
     }
 
     private void createTestObjects(Statement st) throws SQLException {
-        conn = getConnection();
+        Connection conn = getConnection();
         conn.setAutoCommit(false);
         CleanDatabaseTestSetup.cleanDatabase(conn, false);
 
@@ -106,21 +92,16 @@ public final class AlterTableTest extend
 
     private void checkWarning(Statement st, String expectedWarning)
             throws Exception {
-        if ((sqlWarn == null) && (st != null)) {
-            sqlWarn = st.getWarnings();
-        }
-        if (sqlWarn == null) {
-            sqlWarn = getConnection().getWarnings();
-        }
+        SQLWarning sqlWarn = (st == null) ?
+                getConnection().getWarnings() : st.getWarnings();
         assertNotNull("Expected warning but found none", sqlWarn);
         assertSQLState(expectedWarning, sqlWarn);
-        sqlWarn = null;
     }
 
     public void testAddColumn() throws Exception {
         Statement st = createStatement();
         createTestObjects(st);
-        conn.commit();
+        commit();
 
         // add column negative tests alter a non-existing table
         assertStatementError("42Y55", st,
@@ -179,11 +160,11 @@ public final class AlterTableTest extend
         st.executeUpdate("alter table t1 add column c4 int check(c4 = 1)");
 
         // Newly-added column does not appear in existing view:
-        rs = st.executeQuery("select * from v1");
+        ResultSet rs = st.executeQuery("select * from v1");
         JDBC.assertColumnNames(rs, new String[]{"C1"});
         JDBC.assertFullResultSet(rs, new String[][]{{"1"}, {"2"}});
 
-        pSt = prepareStatement("select * from t2");
+        PreparedStatement pSt = prepareStatement("select * from t2");
 
         rs = pSt.executeQuery();
         JDBC.assertColumnNames(rs, new String[]{"C1"});
@@ -216,7 +197,7 @@ public final class AlterTableTest extend
         JDBC.assertFullResultSet(rs, new String[][]{{"1", "0"}});
 
         st.executeUpdate("drop table t0");
-        conn.rollback();
+        rollback();
         rs = st.executeQuery(" select  * from t0");
         JDBC.assertColumnNames(rs, new String[]{"C1"});
         JDBC.assertDrainResults(rs, 0);
@@ -237,7 +218,7 @@ public final class AlterTableTest extend
         JDBC.assertColumnNames(rs, new String[]{"C1", "C2"});
         JDBC.assertFullResultSet(rs, new String[][]{{"1", "1"}});
 
-        conn.rollback();
+        rollback();
 
         // add unique constraint to 0 and 1 row tables and verify
 
@@ -271,7 +252,7 @@ public final class AlterTableTest extend
                 "SYSCS_UTIL.SYSCS_CHECK_TABLE('" + DerbyConstants.TEST_DBO +
                 "', tablename) from " + "sys.systables where tabletype = 'T'");
 
-        expRS = new String[][]{
+        String[][] expRS = {
                     {"T0", "1"},
                     {"T0_1", "1"},
                     {"T0_2", "1"},
@@ -285,7 +266,7 @@ public final class AlterTableTest extend
 
         JDBC.assertUnorderedResultSet(rs, expRS, true);
 
-        conn.rollback();
+        rollback();
 
         st.executeUpdate(
                 " create function countopens() returns varchar(128) " +
@@ -293,7 +274,7 @@ public final class AlterTableTest extend
                 "'org.apache.derbyTesting.functionTests.util.T_ConsistencyChecker." +
                 "countOpens'");
 
-        conn.commit();
+        commit();
         // do consistency check on scans, etc.
 
         rs = st.executeQuery("values countopens()");
@@ -343,7 +324,7 @@ public final class AlterTableTest extend
 
         // tr1 is dropped, tr2 still OK
         st.executeUpdate("drop trigger tr1");
-        rs = st.executeQuery("select * from tab5");
+        ResultSet rs = st.executeQuery("select * from tab5");
         JDBC.assertColumnNames(rs, new String[]{"C1"});
         JDBC.assertDrainResults(rs, 0);
 
@@ -363,7 +344,7 @@ public final class AlterTableTest extend
 
         rs = st.executeQuery("select * from tab2");
 
-        expColNames = new String[]{"C1", "C2", "C3", "C4", "C5"};
+        String[] expColNames = {"C1", "C2", "C3", "C4", "C5"};
         JDBC.assertColumnNames(rs, expColNames);
         JDBC.assertDrainResults(rs, 0);
 
@@ -392,7 +373,7 @@ public final class AlterTableTest extend
         expColNames = new String[]{"C2", "C3", "C4"};
         JDBC.assertColumnNames(rs, expColNames);
 
-        expRS = new String[][]{
+        String[][] expRS = {
                     {"9", "2.5", "88"},
                     {"10", "3.5", "99"},
                     {"8", "4.4", "8"},
@@ -491,7 +472,7 @@ public final class AlterTableTest extend
 
         st.executeUpdate("drop view vw4");
 
-        conn.rollback();
+        rollback();
 
         // check that dropping a column will drop backing index on 
         // referencing table
@@ -523,7 +504,7 @@ public final class AlterTableTest extend
                 "t.tablename = 'REFTT1'");
         JDBC.assertSingleValueResultSet(rs, "1");
 
-        conn.rollback();
+        rollback();
     }
 
     public void testAddConstraint() throws Exception {
@@ -571,7 +552,7 @@ public final class AlterTableTest extend
         //duplicating a key value in a primary key not allowed
         assertStatementError("23505", st, "insert into t0_1 values (1, 1)");
 
-        rs = st.executeQuery("select * from t0_1");
+        ResultSet rs = st.executeQuery("select * from t0_1");
         JDBC.assertColumnNames(rs, new String[]{"C1", "C2"});
         JDBC.assertFullResultSet(rs, new String[][]{{"1", "1"}});
 
@@ -641,10 +622,10 @@ public final class AlterTableTest extend
         rs = st.executeQuery(
                 " select * from t0_1");
 
-        expColNames = new String[]{"C1", "C2"};
+        String[] expColNames = {"C1", "C2"};
         JDBC.assertColumnNames(rs, expColNames);
 
-        expRS = new String[][]{
+        String[][] expRS = {
                     {"1", "1"},
                     {"2", "2"},
                     {"2", "2"},
@@ -723,7 +704,8 @@ public final class AlterTableTest extend
 
         assertCompileError("42Y55", "alter table xxx add check(c2 = 1)");
         st.executeUpdate("create table xxx(c1 int, c2 int)");
-        pSt = prepareStatement("alter table xxx add check(c2 = 1)");
+        PreparedStatement pSt =
+                prepareStatement("alter table xxx add check(c2 = 1)");
         assertUpdateCount(pSt, 0);
         st.executeUpdate("drop table xxx");
         st.executeUpdate("create table xxx(c1 int)");
@@ -801,15 +783,15 @@ public final class AlterTableTest extend
 
         // verify the consistency of the indexes on the user tables
 
-        rs = st.executeQuery(
+        ResultSet rs = st.executeQuery(
                 "select tablename, " + "SYSCS_UTIL.SYSCS_CHECK_TABLE('" +
                 DerbyConstants.TEST_DBO + "', tablename) from " +
                 "sys.systables where tabletype = 'T' and tablename = 'T0_1'");
 
-        expColNames = new String[]{"TABLENAME", "2"};
+        String[] expColNames = {"TABLENAME", "2"};
         JDBC.assertColumnNames(rs, expColNames);
 
-        expRS = new String[][]{
+        String[][] expRS = {
                     {"T0_1", "1"}
                 };
 
@@ -817,7 +799,8 @@ public final class AlterTableTest extend
 
         // verify that alter table works after drop/recreate of table
 
-        pSt = prepareStatement("alter table t0_1 drop constraint p2");
+        PreparedStatement pSt =
+                prepareStatement("alter table t0_1 drop constraint p2");
 
         assertUpdateCount(pSt, 0);
 
@@ -962,7 +945,7 @@ public final class AlterTableTest extend
 
         // schemaname should be DerbyConstants.TEST_DBO
 
-        rs = st.executeQuery(
+        ResultSet rs = st.executeQuery(
                 "select schemaname, constraintname from " +
                 "sys.sysconstraints c, sys.sysschemas s where " +
                 "s.schemaid = c.schemaid order by 1");
@@ -1098,12 +1081,12 @@ public final class AlterTableTest extend
         st.executeUpdate("alter table t1 drop primary key");
         st.executeUpdate("insert into t1 values (1, 1)");
 
-        rs = st.executeQuery("select * from t1");
+        ResultSet rs = st.executeQuery("select * from t1");
 
-        expColNames = new String[]{"C1", "C2"};
+        String[] expColNames = {"C1", "C2"};
         JDBC.assertColumnNames(rs, expColNames);
 
-        expRS = new String[][]{
+        String[][] expRS = {
                     {"1", "1"},
                     {"1", "1"}
                 };
@@ -1128,7 +1111,7 @@ public final class AlterTableTest extend
                 " alter table t1 add constraint C1_PLUS_C2 check " +
                 "((c1 + c2) < 100)");
 
-        pSt = prepareStatement(
+        PreparedStatement pSt = prepareStatement(
                 "alter table t1 drop constraint C1_PLUS_C2");
 
         st.executeUpdate(
@@ -1246,12 +1229,12 @@ public final class AlterTableTest extend
 
         st.executeUpdate("insert into atmcn_1 values (1,1)");
 
-        rs = st.executeQuery("select * from atmcn_1");
+        ResultSet rs = st.executeQuery("select * from atmcn_1");
 
-        expColNames = new String[]{"A", "B"};
+        String[] expColNames = {"A", "B"};
         JDBC.assertColumnNames(rs, expColNames);
 
-        expRS = new String[][]{
+        String[][] expRS = {
                     {"1", "1"}
                 };
 
@@ -1445,7 +1428,7 @@ public final class AlterTableTest extend
 
         st.executeUpdate("rename column renc_2.c1 to c2");
 
-        dbmd = conn.getMetaData();
+        DatabaseMetaData dbmd = getConnection().getMetaData();
         rs = dbmd.getColumns(null, null, "RENC_2", "C2");
         assertTrue(rs.next());
         assertEquals("C2", rs.getString("COLUMN_NAME"));
@@ -1459,7 +1442,7 @@ public final class AlterTableTest extend
         assertFalse(rs.next());
 
         if (usingEmbedded()) {
-            dbmd = conn.getMetaData();
+            dbmd = getConnection().getMetaData();
             rs = dbmd.getIndexInfo(null, null, "RENC_2", false, false);
             assertTrue(rs.next());
             assertEquals("RENC_2", rs.getString("TABLE_NAME"));
@@ -1488,7 +1471,7 @@ public final class AlterTableTest extend
         st.executeUpdate(
                 "rename column renc_3.b to newbie");
 
-        dbmd = conn.getMetaData();
+        dbmd = getConnection().getMetaData();
         rs = dbmd.getColumns(null, null, "RENC_3", "NEWBIE");
         assertTrue(rs.next());
         assertEquals("NEWBIE", rs.getString("COLUMN_NAME"));
@@ -1502,7 +1485,7 @@ public final class AlterTableTest extend
         assertFalse(rs.next());
 
         if (usingEmbedded()) {
-            dbmd = conn.getMetaData();
+            dbmd = getConnection().getMetaData();
             rs = dbmd.getIndexInfo(null, null, "RENC_3", false, false);
             assertTrue(rs.next());
             assertEquals("RENC_3", rs.getString("TABLE_NAME"));
@@ -1556,7 +1539,7 @@ public final class AlterTableTest extend
                 "rename column renc_4.c1 to unq_c1");
         
         if (usingEmbedded()) {
-            dbmd = conn.getMetaData();
+            dbmd = getConnection().getMetaData();
             rs = dbmd.getIndexInfo(null, null, "RENC_4", false, false);
             assertTrue(rs.next());
             assertEquals("RENC_4", rs.getString("TABLE_NAME"));
@@ -1581,7 +1564,7 @@ public final class AlterTableTest extend
                 "rename column renc_5.c5 to unq_c5");
         
         if (usingEmbedded()) {
-            dbmd = conn.getMetaData();
+            dbmd = getConnection().getMetaData();
             rs = dbmd.getIndexInfo(null, null, "RENC_5", false, false);
             assertTrue(rs.next());
             assertEquals("RENC_5", rs.getString("TABLE_NAME"));
@@ -1701,12 +1684,13 @@ public final class AlterTableTest extend
         // prepared statement on it. The rename of the column will 
         // be successful; the open statement will get errors when 
         // it tries to re-execute.
-        conn.setAutoCommit(false);
-        pSt = prepareStatement("select * from renc_6 where a = ?");
+        setAutoCommit(false);
+        PreparedStatement pSt =
+                prepareStatement("select * from renc_6 where a = ?");
         rs = st.executeQuery("values (30)");
 
         rs.next();
-        rsmd = rs.getMetaData();
+        ResultSetMetaData rsmd = rs.getMetaData();
         for (int i = 1;
                 i <= rsmd.getColumnCount(); i++) {
             pSt.setObject(i, rs.getObject(i));
@@ -1732,8 +1716,7 @@ public final class AlterTableTest extend
         assertStatementError(
                 "42X04", pSt);
 
-        conn.setAutoCommit(
-                true);
+        setAutoCommit(true);
 
         // Demonstrate that you cannot rename a column in a 
         // synonym, and demonstrate that renaming a column in the 
@@ -1794,7 +1777,7 @@ public final class AlterTableTest extend
         // cause the following test to fail. Right now, the following
         // test accepts the incorrect metadata length obtained through
         // the resultset's metadata after ALTER TABLE has been performed.
-        conn.setAutoCommit(false);
+        setAutoCommit(false);
         //Create table and load data
         st.executeUpdate(
                 "create table derby_3823_t1 (c11 int, c12 varchar(5))");
@@ -1804,7 +1787,7 @@ public final class AlterTableTest extend
         	ps.setInt(1, i); 
         	ps.executeUpdate(); 
     	} 
-        conn.commit();
+        commit();
         //Open a resultset on the table which will be altered because
         // the resultset has been exhausted. The alter table will fail
         // in embedded mode because of the open resulset but will succeed
@@ -2115,7 +2098,7 @@ public final class AlterTableTest extend
         JDBC.assertFullResultSet(st.executeQuery(" select * from atdc_1"),
                 new String[][]{{"1", "1"}});
 
-        rs =
+        ResultSet rs =
                 st.executeQuery(
                 " select columnname,columnnumber,columndatatype from " +
                 "sys.syscolumns where referenceid in (select tableid " +
@@ -2218,18 +2201,7 @@ public final class AlterTableTest extend
         st.executeUpdate("alter table atdc_1_01 drop column c cascade");
 
         if (usingEmbedded()) {
-            if ((sqlWarn == null) && (st != null)) {
-                sqlWarn = st.getWarnings();
-            }
-
-            if (sqlWarn == null) {
-                sqlWarn = getConnection().getWarnings();
-            }
-
-            assertNotNull("Expected warning but found none", sqlWarn);
-            assertSQLState("01500", sqlWarn);
-            sqlWarn =
-                    null;
+            checkWarning(st, "01500");
         }
 
         st.executeUpdate(
@@ -2243,33 +2215,7 @@ public final class AlterTableTest extend
                 " alter table atdc_1_02 drop column a cascade");
 
         if (usingEmbedded()) {
-            if ((sqlWarn == null) && (st != null)) {
-                sqlWarn = st.getWarnings();
-            }
-
-            if (sqlWarn == null) {
-                sqlWarn = getConnection().getWarnings();
-            }
-
-            assertNotNull("Expected warning but found none", sqlWarn);
-            assertSQLState("01500", sqlWarn);
-            sqlWarn =
-                    null;
-        }
-
-        if (usingEmbedded()) {
-            if ((sqlWarn == null) && (st != null)) {
-                sqlWarn = st.getWarnings();
-            }
-
-            if (sqlWarn == null) {
-                sqlWarn = getConnection().getWarnings();
-            }
-
-            assertNotNull("Expected warning but found none", sqlWarn);
-            assertSQLState("01500", sqlWarn);
-            sqlWarn =
-                    null;
+            checkWarning(st, "01500");
         }
 
 // drop column restrict should fail because column is used 
@@ -2488,8 +2434,7 @@ public final class AlterTableTest extend
         rs =
                 st.executeQuery("select * from atdc_vw_1");
 
-        expColNames =
-                new String[]{"VW_B"};
+        String[] expColNames = {"VW_B"};
         JDBC.assertColumnNames(rs, expColNames);
         JDBC.assertDrainResults(rs, 0);
 
@@ -3231,7 +3176,7 @@ public final class AlterTableTest extend
                 "create table d3175 (x varchar(12), y varchar(12), " +
                 "id int primary key generated by default as identity)");
 
-        rs =
+        ResultSet rs =
                 st.executeQuery(
                 " select COLUMNNAME, COLUMNNUMBER, COLUMNDATATYPE, " +
                 " COLUMNDEFAULT, AUTOINCREMENTVALUE, AUTOINCREMENTSTART, " +
@@ -3320,7 +3265,7 @@ public final class AlterTableTest extend
                 "Message_Data_ID INTEGER NOT NULL , CONSTRAINT " +
                 "d3177_MESSAGES_id_pk PRIMARY KEY(id) )");
 
-        rs =
+        ResultSet rs =
                 st.executeQuery(
                 " select COLUMNNAME, COLUMNNUMBER, COLUMNDATATYPE, " +
                 " COLUMNDEFAULT, AUTOINCREMENTVALUE, AUTOINCREMENTSTART, " +
@@ -3625,16 +3570,15 @@ public final class AlterTableTest extend
         
         //increase the maximum size of the clob 
         
-        Clob clob = null;
-        Blob blob=null;
         int val = 1;
         int size = 15 * 1024;
         InputStream stream;
                
         st.executeUpdate("create table clob_tab(c1 int,clob_col clob(10K))");
-        conn.commit();
+        commit();
         
-        pSt=conn.prepareStatement("INSERT INTO clob_tab values (?,?)");   
+        PreparedStatement pSt =
+                prepareStatement("INSERT INTO clob_tab values (?,?)");
         stream = new TestInputStream(size, val);
         
         //this insert fails(size>10K) 
@@ -3643,12 +3587,12 @@ public final class AlterTableTest extend
         assertStatementError("XJ001", pSt);
         pSt.close();
         
-        conn.rollback();
+        rollback();
         
         st.executeUpdate("ALTER TABLE clob_tab ALTER COLUMN "
                 +"clob_col SET DATA TYPE clob(20K)");
         
-        pSt=conn.prepareStatement("INSERT INTO clob_tab values (?,?)");
+        pSt = prepareStatement("INSERT INTO clob_tab values (?,?)");
         stream = new TestInputStream(size, val);
         
         //this insert succeed (maximum blob size not increased to 20K)
@@ -3663,9 +3607,9 @@ public final class AlterTableTest extend
         st.executeUpdate("CREATE TABLE blob_tab ( C1 INTEGER," +
                                 "blob_col BLOB(10K) NOT NULL)");
         
-        conn.commit();
+        commit();
         
-        pSt=conn.prepareStatement("INSERT INTO blob_tab values (?,?)");
+        pSt = prepareStatement("INSERT INTO blob_tab values (?,?)");
         stream = new TestInputStream(size, val);
         
         //this insert fails(size>10K) 
@@ -3674,12 +3618,12 @@ public final class AlterTableTest extend
         assertStatementError("22001", pSt);
         pSt.close();
         
-        conn.rollback();
+        rollback();
         
         st.executeUpdate("ALTER TABLE blob_tab ALTER COLUMN "
                 +"blob_col SET DATA TYPE blob(20K)");  
         
-        pSt=conn.prepareStatement("INSERT INTO blob_tab values (?,?)");
+        pSt = prepareStatement("INSERT INTO blob_tab values (?,?)");
         stream = new TestInputStream(size, val);
         
         //this insert succeed (maximum blob size not increased to 20K)
@@ -3688,7 +3632,7 @@ public final class AlterTableTest extend
         pSt.executeUpdate();
         pSt.close();   
         
-        conn.rollback();
+        rollback();
     }
 
     /**



Mime
View raw message