db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d..@apache.org
Subject svn commit: r616171 - in /db/derby/code/trunk/java/testing/org/apache/derbyTesting: functionTests/tests/jdbcapi/DataSourceTest.java junit/JDBCDataSource.java
Date Tue, 29 Jan 2008 05:37:26 GMT
Author: djd
Date: Mon Jan 28 21:37:23 2008
New Revision: 616171

URL: http://svn.apache.org/viewvc?rev=616171&view=rev
Log:
Partial cleanup of jdbcapi.DataSourceTest to reduce uses of the static field dbName which
may hold the incorrect database name for the test by using the utility methods to get data
sources which are correctly configured.

Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/DataSourceTest.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBCDataSource.java

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/DataSourceTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/DataSourceTest.java?rev=616171&r1=616170&r2=616171&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/DataSourceTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/DataSourceTest.java
Mon Jan 28 21:37:23 2008
@@ -37,6 +37,9 @@
 import java.util.Iterator;
 
 import org.apache.derbyTesting.functionTests.tests.jdbcapi.AssertEventCatcher;
+
+import javax.sql.ConnectionEvent;
+import javax.sql.ConnectionEventListener;
 import javax.sql.ConnectionPoolDataSource;
 import javax.sql.DataSource;
 import javax.sql.PooledConnection;
@@ -75,7 +78,7 @@
  */
 public class DataSourceTest extends BaseJDBCTestCase {
 
-    protected static String dbName = 
+    private static final String dbName = 
         TestConfiguration.getCurrent().getDefaultDatabaseName();
     
     /**
@@ -567,7 +570,7 @@
         if (usingEmbedded())
             assertTenConnectionsUnique();
 
-        DataSource dscs = JDBCDataSource.getDataSource(dbName);
+        DataSource dscs = JDBCDataSource.getDataSource();
         if (usingEmbedded()) 
                 assertToString(dscs);
 
@@ -620,7 +623,6 @@
         aes1.resetState();
 
         XADataSource dsx = J2EEDataSource.getXADataSource();
-        JDBCDataSource.setBeanProperty(dsx, "DatabaseName", dbName);
         if (usingEmbedded())
             assertToString(dsx);
 
@@ -844,7 +846,6 @@
         // some of this may be tested elsewhere too.
 
         XADataSource dsx = J2EEDataSource.getXADataSource();
-        JDBCDataSource.setBeanProperty(dsx, "DatabaseName", dbName);
         XAConnection xac = dsx.getXAConnection();
         AssertEventCatcher aes6 = new AssertEventCatcher(6);
         xac.addConnectionEventListener(aes6);
@@ -1032,7 +1033,6 @@
         // handled correctly 
         // Some more isolation testing using SQL and JDBC api
         XADataSource dsx = J2EEDataSource.getXADataSource();
-        JDBCDataSource.setBeanProperty(dsx, "DatabaseName", dbName);
         XAConnection xac = dsx.getXAConnection();
         AssertEventCatcher aes6 = new AssertEventCatcher(6);
         xac.addConnectionEventListener(aes6);
@@ -1185,7 +1185,6 @@
             ResultSet.HOLD_CURSORS_OVER_COMMIT};
 
         XADataSource dsx = J2EEDataSource.getXADataSource();
-        JDBCDataSource.setBeanProperty(dsx, "DatabaseName", dbName);
         XAConnection xac = dsx.getXAConnection();
         AssertEventCatcher aes6 = new AssertEventCatcher(6);
         xac.addConnectionEventListener(aes6);
@@ -1786,7 +1785,7 @@
     // for embedded datasources.
     // This subtest does not run for network server, the database shutdown
     // is done using setDatabaseShutdown.
-    public static void testDSRequestAuthentication() throws SQLException {
+    public void testDSRequestAuthentication() throws SQLException {
 
         if (usingDerbyNetClient())
             return;
@@ -1969,55 +1968,53 @@
         String traceFile;
 
         // DataSource
-        ClientDataSource ds = new ClientDataSource();
-        ds.setDatabaseName(dbName);
+        DataSource ds = JDBCDataSource.getDataSource();
 
         // DataSource - setTransationAttributes
         traceFile = "trace1.out";
-        ds.setConnectionAttributes("traceFile="+traceFile);
+        JDBCDataSource.setBeanProperty(ds, "connectionAttributes",
+        		"traceFile="+traceFile);
+
         // In this scenario, we *only* get a tracefile, if we first get a 
         // successful connection, followed by an unsuccessful connection. 
         // So, we cannot just use ds.getConnection()
         dsGetBadConnection(ds);
-        ds.setConnectionAttributes(null);
+        JDBCDataSource.clearStringBeanProperty(ds, "connectionAttributes");
+
         // DataSource - setTraceFile
         traceFile = "trace2.out";
-        ds.setTraceFile(traceFile);
+        JDBCDataSource.setBeanProperty(ds, "traceFile", traceFile);
         ds.getConnection();
-        ds.setTraceFile(null);
-        ds.setDatabaseName(null);
+        ds = null;
 
         // now with ConnectionPoolDataSource
-        ClientConnectionPoolDataSource cpds = new ClientConnectionPoolDataSource();
-        cpds.setDatabaseName(dbName);
+        ConnectionPoolDataSource cpds = J2EEDataSource.getConnectionPoolDataSource();
 
         traceFile = "trace3.out";
-        cpds.setConnectionAttributes("traceFile="+traceFile);
+        JDBCDataSource.setBeanProperty(cpds, "connectionAttributes",
+        		"traceFile="+traceFile);
         // DERBY-2468 - trace3.out does not get created
-        cpds.getConnection();
-        cpds.setConnectionAttributes(null);
+        ((ClientConnectionPoolDataSource) cpds).getConnection();
+        JDBCDataSource.clearStringBeanProperty(cpds, "connectionAttributes");
 
         traceFile = "trace4.out";
-        cpds.setTraceFile(traceFile);
-        cpds.getConnection();
-        cpds.setTraceFile(null);
-        cpds.setDatabaseName(null);
+        JDBCDataSource.setBeanProperty(cpds, "traceFile", traceFile);
+        ((ClientConnectionPoolDataSource) cpds).getConnection();
+        cpds = null;
 
         // now with XADataSource
-        ClientXADataSource xads = new ClientXADataSource();
-        xads.setDatabaseName(dbName);
+        XADataSource xads = J2EEDataSource.getXADataSource();
 
         traceFile = "trace5.out";
-        xads.setConnectionAttributes("traceFile="+traceFile);
-        xads.getConnection();
+        JDBCDataSource.setBeanProperty(xads, "connectionAttributes",
+        		"traceFile="+traceFile);
+        ((ClientXADataSource) xads).getConnection();
         // DERBY-2468 - trace5.out does not get created
-        xads.setConnectionAttributes(null);
+        JDBCDataSource.clearStringBeanProperty(xads, "connectionAttributes");
 
         traceFile = "trace6.out";
-        xads.setTraceFile(traceFile);
-        xads.getConnection();
-        xads.setTraceFile(null);
-        xads.setDatabaseName(null);
+        JDBCDataSource.setBeanProperty(xads, "traceFile", traceFile);
+        ((ClientXADataSource) xads).getConnection();
 
         assertTraceFilesExist();
     }
@@ -2161,44 +2158,36 @@
      *  
      * @throws SQLException
      */
-    public void testClientDescriptionConnectionAttribute() 
+    public void testDescriptionProperty() 
     throws SQLException, Exception {
-
-        if (usingEmbedded())
-            return;
         
-        // DataSource
-        String setDescription = 
-            "Everything you ever wanted to know about this datasource";
-        String getDescription;
-
         // DataSource - setDescription
-        ClientDataSource ds = new ClientDataSource();
-        ds.setDatabaseName(dbName);
-        ds.setDescription(setDescription);
-        ds.getConnection();
-        getDescription = ds.getDescription();
-        assertEquals(setDescription, getDescription);
-        ds.setDescription(null);
+        subTestDataSourceDescription(JDBCDataSource.getDataSource());
 
         // ConnectionPoolDataSource - setDescription
-        ClientConnectionPoolDataSource cpds = 
-            new ClientConnectionPoolDataSource();
-        cpds.setDatabaseName(dbName);
-        cpds.setDescription(setDescription);
-        cpds.getConnection();
-        getDescription = cpds.getDescription();
-        assertEquals(setDescription, getDescription);
-        cpds.setDescription(null);
+        subTestDataSourceDescription(
+        		(DataSource) J2EEDataSource.getConnectionPoolDataSource());
 
         // XADataSource - setDescription
-        ClientXADataSource xads = new ClientXADataSource();
-        xads.setDatabaseName(dbName);
-        xads.setDescription(setDescription);
-        xads.getConnection();
-        getDescription = xads.getDescription();
-        assertEquals(setDescription, getDescription);
-        xads.setDescription(null);
+        subTestDataSourceDescription(
+        		(DataSource) J2EEDataSource.getXADataSource());
+
+    }
+    
+    /**
+     * Utility method for testing setting and fetching the description
+     * property on a data source.
+     */
+    private void subTestDataSourceDescription(DataSource ds) throws Exception
+    {
+        String setDescription = 
+            "Everything you ever wanted to know about this datasource";
+        
+        JDBCDataSource.setBeanProperty(ds, "description", setDescription);
+        ds.getConnection();
+        assertEquals(setDescription, JDBCDataSource.getBeanProperty(ds, "description"));
+        JDBCDataSource.clearStringBeanProperty(ds, "description");
+        assertNull(JDBCDataSource.getBeanProperty(ds, "description"));    	
     }
 
     /* ------------------ JDBC30 (and up) Fixtures ------------------ */

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBCDataSource.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBCDataSource.java?rev=616171&r1=616170&r2=616171&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBCDataSource.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBCDataSource.java Mon
Jan 28 21:37:23 2008
@@ -206,6 +206,22 @@
     }
     
     /**
+     * Get a bean property for a data source. This code can be used
+     * on any data source type.
+     * @param ds DataSource to fetch property from
+     * @param property name of property.
+     */
+    public static Object getBeanProperty(Object ds, String property)
+        throws Exception
+    {
+        String getterName = getGetterName(property);
+
+        Method getter = ds.getClass().getMethod(getterName,
+                    new Class[0]);
+        return getter.invoke(ds, new Object[0]);
+    }
+    
+    /**
      * Clear a String Java bean property by setting it to null.
      * @param ds ds DataSource to have property cleared
      * @param property name of property.
@@ -224,6 +240,10 @@
     
     private static String getSetterName(String attribute) {
         return "set" + Character.toUpperCase(attribute.charAt(0))
+                + attribute.substring(1);
+    }
+    private static String getGetterName(String attribute) {
+        return "get" + Character.toUpperCase(attribute.charAt(0))
                 + attribute.substring(1);
     }
     



Mime
View raw message