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 ka...@apache.org on 2012/07/23 09:09:55 UTC

svn commit: r1364524 - in /db/derby/code/trunk/java: client/org/apache/derby/client/am/ testing/org/apache/derbyTesting/functionTests/tests/jdbc4/ testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/

Author: kahatlen
Date: Mon Jul 23 07:09:54 2012
New Revision: 1364524

URL: http://svn.apache.org/viewvc?rev=1364524&view=rev
Log:
DERBY-5872: Inconsistency between isWrapperFor() and unwrap() in logical statements

Make LogicalStatementEntity's isWrapperFor() only check the interfaces
implemented by the logical statement. This makes it consistent with the
unwrap() method, and also consistent with how isWrapperFor() works in
LogicalConnection40 and LogicalDatabaseMetaData40.

Add test cases for the new behaviour in PreparedStatementTest and
CallableStatementTest.

Enable ClosedObjectTest for logical statements.

Modified:
    db/derby/code/trunk/java/client/org/apache/derby/client/am/LogicalStatementEntity.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/CallableStatementTest.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/PreparedStatementTest.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ClosedObjectTest.java

Modified: db/derby/code/trunk/java/client/org/apache/derby/client/am/LogicalStatementEntity.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/client/org/apache/derby/client/am/LogicalStatementEntity.java?rev=1364524&r1=1364523&r2=1364524&view=diff
==============================================================================
--- db/derby/code/trunk/java/client/org/apache/derby/client/am/LogicalStatementEntity.java (original)
+++ db/derby/code/trunk/java/client/org/apache/derby/client/am/LogicalStatementEntity.java Mon Jul 23 07:09:54 2012
@@ -231,8 +231,8 @@ abstract class LogicalStatementEntity
      * instance implements {@code iface}
      */
     public boolean isWrapperFor(Class<?> iface) throws SQLException {
-        return ((org.apache.derby.client.am.Statement) getPhysStmt())
-                .isWrapperFor(iface);
+        getPhysStmt(); // Just to check that the statement is not closed.
+        return iface.isInstance(this);
     }
 
     /**

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/CallableStatementTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/CallableStatementTest.java?rev=1364524&r1=1364523&r2=1364524&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/CallableStatementTest.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/CallableStatementTest.java Mon Jul 23 07:09:54 2012
@@ -491,6 +491,10 @@ public class CallableStatementTest  exte
         assertFalse(cStmt.isWrapperFor(ResultSet.class));
     }
 
+    public void testIsWrapperForSelf() throws SQLException {
+        assertTrue(cStmt.isWrapperFor(cStmt.getClass()));
+    }
+
     public void testUnwrapStatement() throws SQLException {
         Statement stmt = cStmt.unwrap(Statement.class);
         assertSame("Unwrap returned wrong object.", cStmt, stmt);
@@ -506,6 +510,11 @@ public class CallableStatementTest  exte
         assertSame("Unwrap returned wrong object.", cStmt, cs);
     }
 
+    public void testUnwrapAsSelf() throws SQLException {
+        PreparedStatement cs = cStmt.unwrap(cStmt.getClass());
+        assertSame("Unwrap returned wrong object.", cStmt, cs);
+    }
+
     public void testUnwrapResultSet() {
         try {
             ResultSet rs = cStmt.unwrap(ResultSet.class);

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/PreparedStatementTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/PreparedStatementTest.java?rev=1364524&r1=1364523&r2=1364524&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/PreparedStatementTest.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbc4/PreparedStatementTest.java Mon Jul 23 07:09:54 2012
@@ -372,6 +372,10 @@ public class PreparedStatementTest exten
         assertFalse(ps.isWrapperFor(ResultSet.class));
     }
 
+    public void testIsWrapperForSelf() throws SQLException {
+        assertTrue(ps.isWrapperFor(ps.getClass()));
+    }
+
     public void testUnwrapStatement() throws SQLException {
         Statement stmt = ps.unwrap(Statement.class);
         assertSame("Unwrap returned wrong object.", ps, stmt);
@@ -382,6 +386,11 @@ public class PreparedStatementTest exten
         assertSame("Unwrap returned wrong object.", ps, ps2);
     }
 
+    public void testUnwrapAsSelf() throws SQLException {
+        PreparedStatement ps2 = ps.unwrap(ps.getClass());
+        assertSame("Unwrap returned wrong object.", ps, ps2);
+    }
+
     public void testUnwrapCallableStatement() {
         try {
             CallableStatement cs = ps.unwrap(CallableStatement.class);

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ClosedObjectTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ClosedObjectTest.java?rev=1364524&r1=1364523&r2=1364524&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ClosedObjectTest.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/ClosedObjectTest.java Mon Jul 23 07:09:54 2012
@@ -28,6 +28,9 @@ import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import java.sql.Statement;
+import java.util.Collections;
+import java.util.Iterator;
+import java.util.Map;
 import java.util.Properties;
 import javax.sql.ConnectionPoolDataSource;
 import javax.sql.DataSource;
@@ -117,9 +120,8 @@ public class ClosedObjectTest extends Ba
     /** Creates a suite with all tests in the class. */
     public static Test suite() {
         TestSuite suite = new TestSuite("ClosedObjectTest suite");
-        suite.addTest(baseSuite("ClosedObjectTest:embedded"));
-        suite.addTest(TestConfiguration.clientServerDecorator(
-            baseSuite("ClosedObjectTest:client")));
+        suite.addTest(baseSuite(false));
+        suite.addTest(baseSuite(true));
         return suite;
     }
 
@@ -128,11 +130,13 @@ public class ClosedObjectTest extends Ba
      * <code>DataSource</code>, <code>ConnectionPoolDataSource</code>
      * and <code>XADataSource</code> to obtain objects.
      *
+     * @param network whether or not to run tests with the network client
      * @return a <code>Test</code> value
      * @exception Exception if an error occurs while building the test suite
      */
-    private static Test baseSuite(String name)  {
-        TestSuite topSuite = new TestSuite(name);
+    private static Test baseSuite(boolean network) {
+        TestSuite topSuite = new TestSuite(
+            "ClosedObjectTest:" + (network ? "client" : "embedded"));
 
         TestSuite dsSuite = new TestSuite("ClosedObjectTest DataSource");
         DataSourceDecorator dsDecorator = new DataSourceDecorator(dsSuite);
@@ -141,21 +145,43 @@ public class ClosedObjectTest extends Ba
 
         // JDBC 3 required for ConnectionPoolDataSource and XADataSource
         if (JDBC.vmSupportsJDBC3()) {
-            
-            TestSuite poolSuite = new TestSuite(
-                    "ClosedObjectTest ConnectionPoolDataSource");
-            PoolDataSourceDecorator poolDecorator =
-                new PoolDataSourceDecorator(poolSuite);
-            topSuite.addTest(poolDecorator);
-            fillDataSourceSuite(poolSuite, poolDecorator);
-    
+
+            // Plain connection pool test.
+            topSuite.addTest(poolSuite(Collections.emptyMap()));
+
+            // The client driver has a variant of connection pool that caches
+            // and reuses JDBC statements. Test it here by setting the
+            // maxStatements property.
+            if (network) {
+                topSuite.addTest(poolSuite(Collections.singletonMap(
+                        "maxStatements", Integer.valueOf(5))));
+            }
+
             TestSuite xaSuite = new TestSuite("ClosedObjectTest XA");
             XADataSourceDecorator xaDecorator = new XADataSourceDecorator(xaSuite);
             topSuite.addTest(xaDecorator);
             fillDataSourceSuite(xaSuite, xaDecorator);
         }
 
-        return topSuite;
+        return network ?
+                TestConfiguration.clientServerDecorator(topSuite) :
+                topSuite;
+    }
+
+    /**
+     * Creates a suite that tests objects produced by a
+     * ConnectionPoolDataSource.
+     *
+     * @param dsProps properties to set on the data source
+     * @return a suite
+     */
+    private static Test poolSuite(Map dsProps) {
+        TestSuite poolSuite = new TestSuite(
+                "ClosedObjectTest ConnectionPoolDataSource");
+        PoolDataSourceDecorator poolDecorator =
+                new PoolDataSourceDecorator(poolSuite, dsProps);
+        fillDataSourceSuite(poolSuite, poolDecorator);
+        return poolDecorator;
     }
 
     /**
@@ -750,13 +776,17 @@ public class ClosedObjectTest extends Ba
      * <code>ConnectionPoolDataSource</code>.
      */
     private static class PoolDataSourceDecorator extends DataSourceDecorator {
+        private final Map dsProps;
+
         /**
          * Creates a new <code>PoolDataSourceDecorator</code> instance.
          *
          * @param test the test to decorate
+         * @param dsProps data source properties
          */
-        public PoolDataSourceDecorator(Test test) {
+        public PoolDataSourceDecorator(Test test, Map dsProps) {
             super(test);
+            this.dsProps = dsProps;
         }
 
         /**
@@ -768,6 +798,11 @@ public class ClosedObjectTest extends Ba
          */
         protected Connection newConnection_() throws SQLException {
             ConnectionPoolDataSource ds = J2EEDataSource.getConnectionPoolDataSource();
+            for (Iterator it = dsProps.entrySet().iterator(); it.hasNext(); ) {
+                Map.Entry e = (Map.Entry) it.next();
+                J2EEDataSource.setBeanProperty(
+                    ds, (String) e.getKey(), e.getValue());
+            }
             PooledConnection pc =
                 ds.getPooledConnection();
             return pc.getConnection();