db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d..@apache.org
Subject svn commit: r472187 - in /db/derby/code/trunk/java/testing/org/apache/derbyTesting: functionTests/tests/lang/ junit/
Date Tue, 07 Nov 2006 18:18:55 GMT
Author: djd
Date: Tue Nov  7 10:18:54 2006
New Revision: 472187

URL: http://svn.apache.org/viewvc?view=rev&rev=472187
Log:
DERBY-2036 DERBY-2043 DERBY-2047 (partial) Re-work connection handling in JUnit tests.
Create connection factory Connector with two implementations, DataSourceConnector and DriverManagerConnector.
This ensures that running tests does not attempt to load DriverManager when running tests
with JSR169.
Ensure DataSource implementation sets user name and password from configuration for the getConnection()
method to ensure the ChangerUserTestSetup works.
Refactor the setting of data source Java bean properties to be from a HashMap rather than
a Properties
object to ensure that no special code exists at the lowest level to deal with different property
types.

Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/Connector.java   (with
props)
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DataSourceConnector.java
  (with props)
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DriverManagerConnector.java
  (with props)
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBCDataSource.java   (with
props)
Modified:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DatabaseClassLoadingTest.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBCClient.java
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DatabaseClassLoadingTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DatabaseClassLoadingTest.java?view=diff&rev=472187&r1=472186&r2=472187
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DatabaseClassLoadingTest.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/DatabaseClassLoadingTest.java
Tue Nov  7 10:18:54 2006
@@ -39,23 +39,29 @@
     {
         final TestSuite suite = new TestSuite("DatabaseClassLoadingTest");
         
-        suite.addTest(new DatabaseClassLoadingTest("testWithNoInstalledJars"));
-        suite.addTest(
+        // Need DriverManager to execute the add contact procedure
+        // as it uses server side jdbc.
+        if (JDBC.vmSupportsJDBC3()) {
+        
+        
+          suite.addTest(new DatabaseClassLoadingTest("testWithNoInstalledJars"));
+          suite.addTest(
                 SecurityManagerSetup.noSecurityManager(
                 new DatabaseClassLoadingTest("testWithNoClasspath")));
-        suite.addTest(
+          suite.addTest(
                 SecurityManagerSetup.noSecurityManager(
                         new DatabaseClassLoadingTest("testSetClasspath")));
         
-        // Need DriverManager to execute the add contact procedure
-        // as it uses server side jdbc.
-        if (JDBC.vmSupportsJDBC3()) {
+          
            suite.addTest(SecurityManagerSetup.noSecurityManager(
                 new DatabaseClassLoadingTest("testAddContact")));
-        }
         
-        suite.addTest(SecurityManagerSetup.noSecurityManager(
-                new DatabaseClassLoadingTest("testGetResource")));        
+           suite.addTest(SecurityManagerSetup.noSecurityManager(
+                new DatabaseClassLoadingTest("testGetResource")));
+           
+           suite.addTest(SecurityManagerSetup.noSecurityManager(
+                   new DatabaseClassLoadingTest("testAlterTable")));
+        }
         
         return new CleanDatabaseTestSetup(suite) {
             protected void decorateSQL(Statement s) throws SQLException
@@ -204,6 +210,43 @@
         JDBC.assertSingleValueResultSet(ps.executeQuery(), null);
         
         ps.close();
+    }
+    
+    /**
+     * Alter the table to add a column, the add contact procedure
+     * should still work.
+     * @throws SQLException
+     */
+    public void testAlterTable() throws SQLException
+    {
+        Statement s = createStatement();
+        s.executeUpdate("ALTER TABLE EMC.CONTACTS ADD COLUMN OK SMALLINT");
+        JDBC.assertFullResultSet(
+                s.executeQuery("SELECT id, e_mail, ok from EMC.CONTACTS ORDER BY 1"),
+                new String[][] {
+                    {"0", "now@classpathchange.com", null},
+                    {"1", "bill@ruletheworld.com", null},
+                    {"2", "penguin@antartic.com", null},
+                    });
+        
+        // well written application, INSERT used explicit column names
+        // ok defaults to NULL
+        CallableStatement cs = prepareCall("CALL EMC.ADDCONTACT(?, ?)");
+        cs.setInt(1, 3);
+        cs.setString(2, "big@blue.com");
+        cs.executeUpdate();
+        cs.close();
+
+        JDBC.assertFullResultSet(
+                s.executeQuery("SELECT id, e_mail, ok from EMC.CONTACTS ORDER BY 1"),
+                new String[][] {
+                    {"0", "now@classpathchange.com", null},
+                    {"1", "bill@ruletheworld.com", null},
+                    {"2", "penguin@antartic.com", null},
+                    {"3", "big@blue.com", null},
+                    });
+      
+        s.close();
     }
     
     private void setDBClasspath(String cp) throws SQLException

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java?view=diff&rev=472187&r1=472186&r2=472187
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/lang/_Suite.java
Tue Nov  7 10:18:54 2006
@@ -59,6 +59,7 @@
         // suite.addTest(largeCodeGen.suite());
 
 
+        //suite.addTest(DatabaseClassLoadingTest.suite());
         suite.addTest(GroupByExpressionTest.suite());
 		suite.addTest(LangScripts.suite());
         suite.addTest(MathTrigFunctionsTest.suite());

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/Connector.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/Connector.java?view=auto&rev=472187
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/Connector.java (added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/Connector.java Tue Nov
 7 10:18:54 2006
@@ -0,0 +1,76 @@
+/*
+ *
+ * Derby - Class org.apache.derbyTesting.junit.Connector
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, 
+ * software distributed under the License is distributed on an 
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
+ * either express or implied. See the License for the specific 
+ * language governing permissions and limitations under the License.
+ */
+package org.apache.derbyTesting.junit;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+
+/**
+ * Factory for getting connections within the tests that is designed
+ * for the simple working case for most tests. Most tests just
+ * need to connect or shutdown the database, this hides through
+ * BaseJDBCTestCase and TestConfiguration the details of how
+ * those operations are performed.
+ * <P>
+ * Tests that need finer control over the connection handling
+ * should use the JDBC classes directly, such as DriverManager
+ * or DataSource.
+ * <P>
+ * This is split out into an interface and sub-classes to
+ * ensure that no ClassNotFoundExceptions are thrown when
+ * running in an JSR 169 environment and DriverManager is
+ * not available.
+ */
+interface Connector {
+    
+    /**
+     * Link this connector to the given configuration.
+     * Should be called once upon setup.
+     */
+    abstract void setConfiguration(TestConfiguration config);
+    
+    /**
+     * Open a connection with the database, user and password
+     * defined by the configuration passed to setConfiguration.
+     * If the database does not exist then it should be created.
+     */
+    abstract Connection openConnection() throws SQLException;
+   
+    /**
+     * Open a connection to the database
+     * defined by the configuration passed to setConfiguration.
+     * If the database does not exist then it should be created.
+     */
+     abstract Connection openConnection(String user, String password)
+         throws SQLException;
+
+    /**
+     * Shutdown the running default database using user and password
+     * defined by the configuration passed to setConfiguration.
+     * @return Nothing, exception is expected to be thrown with SQLState 08006
+     */
+    abstract void shutDatabase() throws SQLException;
+   
+    /**
+     * Shutdown the running derby system.
+     * @return Nothing, exception is expected to be thrown with SQLState XJ015
+     */
+    abstract void shutEngine() throws SQLException;
+}

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/Connector.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DataSourceConnector.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DataSourceConnector.java?view=auto&rev=472187
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DataSourceConnector.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DataSourceConnector.java
Tue Nov  7 10:18:54 2006
@@ -0,0 +1,106 @@
+/*
+ *
+ * Derby - Class org.apache.derbyTesting.junit.DataSourceConnector
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, 
+ * software distributed under the License is distributed on an 
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
+ * either express or implied. See the License for the specific 
+ * language governing permissions and limitations under the License.
+ */
+package org.apache.derbyTesting.junit;
+
+import java.sql.Connection;
+import java.sql.SQLException;
+import java.util.HashMap;
+
+import javax.sql.DataSource;
+
+import junit.framework.Assert;
+
+/**
+ * Connection factory using javax.sql.DataSource.
+ * Should work for an Derby data source, including JSR169 support.
+ *
+ */
+public class DataSourceConnector implements Connector {
+    
+    private TestConfiguration config;
+    /**
+     * DataSource that maps to the database for the
+     * configuration. The no-arg getConnection() method
+     * maps to the default user and password for the
+     * configuration.
+     */
+    private DataSource ds;
+
+    public void setConfiguration(TestConfiguration config) {
+        
+        this.config = config;
+        ds = JDBCDataSource.getDataSource(config, (HashMap) null);
+    }
+
+    public Connection openConnection() throws SQLException {
+        try {
+            return ds.getConnection();
+        } catch (SQLException e) {
+            // Expected state for database not found.
+            // For the client the generic 08004 is returned,
+            // will just retry on that.
+            String expectedState = 
+                config.getJDBCClient().isEmbedded() ? "XJ004" : "08004";
+
+            // If there is a database not found exception
+            // then retry the connection request with
+            // a new DataSource with the createDtabase property set.
+            if (!expectedState.equals(e.getSQLState()))
+                throw e;
+            return singleUseDS("createDatabase", "create").getConnection(); 
+       }
+    }
+
+    public Connection openConnection(String user, String password)
+            throws SQLException {
+        try {
+            return ds.getConnection(user, password);
+        } catch (SQLException e) {
+            // If there is a database not found exception
+            // then retry the connection request with
+            // a new DataSource with the createDatabase property set.
+            if (!"XJ004".equals(e.getSQLState()))
+                throw e;
+            return singleUseDS(
+                    "createDatabase", "create").getConnection(user, password); 
+       }
+    }
+
+    public void shutDatabase() throws SQLException {
+        singleUseDS("shutdownDatabase", "shutdown").getConnection();     
+    }
+
+    public void shutEngine() throws SQLException {
+        Assert.fail("shutdown engine not implemened");
+    }
+    
+    /**
+     * Get a connection from a single use DataSource configured
+     * from the configuration but with the passed in property set.
+     */
+    private DataSource singleUseDS(String property, String value)
+       throws SQLException {
+        HashMap hm = JDBCDataSource.getDataSourceProperties(config);
+        hm.put(property, value);
+        DataSource sds = JDBCDataSource.getDataSource(config, hm);
+        return sds;
+    }
+
+}

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DataSourceConnector.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DriverManagerConnector.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DriverManagerConnector.java?view=auto&rev=472187
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DriverManagerConnector.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DriverManagerConnector.java
Tue Nov  7 10:18:54 2006
@@ -0,0 +1,133 @@
+/*
+ *
+ * Derby - Class org.apache.derbyTesting.junit.DriverManagerConnector
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, 
+ * software distributed under the License is distributed on an 
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
+ * either express or implied. See the License for the specific 
+ * language governing permissions and limitations under the License.
+ */
+package org.apache.derbyTesting.junit;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+
+/**
+ * Connection factory using DriverManager.
+ *
+ */
+public class DriverManagerConnector implements Connector {
+
+    private TestConfiguration config;
+
+    public DriverManagerConnector() {
+    }
+
+    public void setConfiguration(TestConfiguration config) {
+        this.config = config;
+    }
+
+    public Connection openConnection() throws SQLException {
+        return openConnection(config.getUserName(), config.getUserPassword());
+    }
+
+    /**
+     * Open a connection using the DriverManager.
+     * <BR>
+     * The JDBC driver is only loaded if DriverManager.getDriver()
+     * for the JDBC URL throws an exception indicating no driver is loaded.
+     * <BR>
+     * If the connection request fails with SQLState XJ004
+     * (database not found) then the connection is retried
+     * with attributes create=true.
+     */
+    public Connection openConnection(String user, String password)
+            throws SQLException {
+
+        String url = config.getJDBCUrl();
+
+        try {
+            DriverManager.getDriver(url);
+        } catch (SQLException e) {
+            loadJDBCDriver();
+        }
+
+        try {
+            return DriverManager.getConnection(url, user, password);
+        } catch (SQLException e) {
+            
+            // Expected state for database not found.
+            // For the client the generic 08004 is returned,
+            // will just retry on that.
+            String expectedState = 
+                config.getJDBCClient().isEmbedded() ? "XJ004" : "08004";
+
+            // If there is a database not found exception
+            // then retry the connection request with
+            // a create attribute.
+            if (!expectedState.equals(e.getSQLState()))
+                throw e;
+            
+            url = url.concat(";create=true");
+            return DriverManager.getConnection(url, user, password);
+        }
+    }
+
+    /**
+     * Shutdown the database using the attributes shutdown=true
+     * with the user and password defined by the configuration.
+     */
+    public void shutDatabase() throws SQLException {
+        String url = config.getJDBCUrl();
+        url = url.concat(";shutdown=true");
+        DriverManager.getConnection(url,
+                config.getUserName(),
+                config.getUserPassword());
+    }
+
+    /**
+     * Shutdown the engine using the attributes shutdown=true
+     * and no database name with the user and password defined
+     * by the configuration.
+     */
+    public void shutEngine() throws SQLException {
+        String url = config.getJDBCClient().getUrlBase();
+        url = url.concat(";shutdown=true");
+        DriverManager.getConnection("jdbc:derby:;shutdown",
+                config.getUserName(),
+                config.getUserPassword());        
+    }
+
+    /**
+     * Load the JDBC driver defined by the JDBCClient for
+     * the configuration.
+     *
+     * @throws SQLException if loading the driver fails.
+     */
+    private void loadJDBCDriver() throws SQLException {
+        String driverClass = config.getJDBCClient().getJDBCDriverName();
+        try {
+            Class.forName(driverClass).newInstance();
+        } catch (ClassNotFoundException cnfe) {
+            throw new SQLException("Failed to load JDBC driver '" + driverClass
+                    + "': " + cnfe.getMessage());
+        } catch (IllegalAccessException iae) {
+            throw new SQLException("Failed to load JDBC driver '" + driverClass
+                    + "': " + iae.getMessage());
+        } catch (InstantiationException ie) {
+            throw new SQLException("Failed to load JDBC driver '" + driverClass
+                    + "': " + ie.getMessage());
+        }
+    }
+}

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/DriverManagerConnector.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBCClient.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBCClient.java?view=diff&rev=472187&r1=472186&r2=472187
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBCClient.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBCClient.java Tue Nov
 7 10:18:54 2006
@@ -19,6 +19,8 @@
  */
 package org.apache.derbyTesting.junit;
 
+import junit.framework.Assert;
+
 /**
  * Type-safe enumerator of valid JDBC clients.
  * Each JDBC client definition consists of the client name, the name of the
@@ -29,19 +31,68 @@
     /**
      * The embedded JDBC client.
      */
-    static final JDBCClient EMBEDDED = new JDBCClient(
-            "Embedded", 
+    private static final JDBCClient EMBEDDED_30= new JDBCClient(
+            "Embedded_30", 
             "org.apache.derby.jdbc.EmbeddedDriver", 
             "org.apache.derby.jdbc.EmbeddedDataSource", 
+            "org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource",
+            "org.apache.derby.jdbc.EmbeddedXADataSource",
+            "jdbc:derby:");
+    
+    /**
+     * The embedded JDBC client for JDBC 4.0.
+     */
+    private static final JDBCClient EMBEDDED_40 = new JDBCClient(
+            "Embedded_40", 
+            "org.apache.derby.jdbc.EmbeddedDriver", 
+            "org.apache.derby.jdbc.EmbeddedDataSource40", 
+            "org.apache.derby.jdbc.EmbeddedConnectionPoolDataSource40",
+            "org.apache.derby.jdbc.EmbeddedXADataSource40",
             "jdbc:derby:");
     
     /**
+     * The embedded JDBC client for JSR 169
+     */
+    private static final JDBCClient EMBEDDED_169 = new JDBCClient(
+            "Embedded_169", 
+            null, // No driver
+            "org.apache.derby.jdbc.EmbeddedSimpleDataSource", 
+            null, // No connection pooling
+            null, // No XA
+            null); // No JDBC URLs
+    
+    /**
+     * Get the default embedded client for this JVM.
+     * @return
+     */
+    static JDBCClient getDefaultEmbedded()
+    {
+        if (JDBC.vmSupportsJDBC4())
+            return EMBEDDED_40;
+        if (JDBC.vmSupportsJDBC2())
+            return EMBEDDED_30;
+        if (JDBC.vmSupportsJSR169())
+            return EMBEDDED_169;
+        
+        Assert.fail("Unknown JVM environment");
+        return null;
+    }
+    
+    /**
      * The Derby network client.
      */
     static final JDBCClient DERBYNETCLIENT= new JDBCClient(
             "DerbyNetClient",
             "org.apache.derby.jdbc.ClientDriver",
+            JDBC.vmSupportsJDBC4() ?
+            "org.apache.derby.jdbc.ClientDataSource40" :
             "org.apache.derby.jdbc.ClientDataSource",
+            JDBC.vmSupportsJDBC4() ?
+            "org.apache.derby.jdbc.ClientConnectionPoolDataSource40" :
+            "org.apache.derby.jdbc.ClientConnectionPoolDataSource",
+            JDBC.vmSupportsJDBC4() ?
+            "org.apache.derby.jdbc.ClientXADataSource40" :
+            "org.apache.derby.jdbc.ClientXADataSource",
             "jdbc:derby://");
     
     /**
@@ -52,7 +103,7 @@
     static final JDBCClient DB2CLIENT= new JDBCClient(
             "DerbyNet",
             "com.ibm.db2.jcc.DB2Driver",
-            null,
+            null, null, null,
             "jdbc:derby:net://");
     
     /**
@@ -60,7 +111,7 @@
     */
     public boolean isEmbedded()
     {
-    	return getName().equals(EMBEDDED.getName());
+    	return getName().startsWith("Embedded");
     }
     /**
      * Is this Derby's network client.
@@ -106,6 +157,24 @@
     }
 
     /**
+     * Get ConnectionPoolDataSource class name.
+     *
+     * @return class name for ConnectionPoolDataSource implementation.
+     */
+    public String getConnectionPoolDataSourceClassName() {
+        return poolDsClassName;
+    }
+
+    /**
+     * Get XADataSource class name.
+     *
+     * @return class name for XADataSource implementation.
+     */
+    public String getXADataSourceClassName() {
+        return xaDsClassName;
+    }
+
+    /**
      * Return the base JDBC url.
      * The JDBC base url specifies the protocol and possibly the subprotcol
      * in the JDBC connection string.
@@ -129,16 +198,23 @@
      * Create a JDBC client definition.
      */
     private JDBCClient(String frameWork, String driverClassName,
-                       String dataSourceClassName, String urlBase) {
+                       String dataSourceClassName,
+                       String connectionPoolDataSourceClassName,
+                       String xaDataSourceClassName,
+                       String urlBase) {
         this.frameWork          = frameWork;
         this.driverClassName    = driverClassName;
         this.dsClassName        = dataSourceClassName;
+        this.poolDsClassName    = connectionPoolDataSourceClassName;
+        this.xaDsClassName      = xaDataSourceClassName;
         this.urlBase            = urlBase;
     }
     
     private final String frameWork;
     private final String driverClassName;
     private final String dsClassName;
+    private final String poolDsClassName;
+    private final String xaDsClassName;
     private final String urlBase;
     
 }

Added: 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?view=auto&rev=472187
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBCDataSource.java (added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBCDataSource.java Tue
Nov  7 10:18:54 2006
@@ -0,0 +1,134 @@
+/*
+ *
+ * Derby - Class org.apache.derbyTesting.junit.JDBCDataSource
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, 
+ * software distributed under the License is distributed on an 
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
+ * either express or implied. See the License for the specific 
+ * language governing permissions and limitations under the License.
+ */
+package org.apache.derbyTesting.junit;
+
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.Iterator;
+
+import junit.framework.Assert;
+
+/**
+ * Utility methods related to JDBC DataSource objects.
+ *
+ */
+public class JDBCDataSource {
+    
+    /**
+     * Return a DataSource corresponding to the current
+     * configuration. The getConnection() method will return
+     * a connection identical to TestConfiguration.openDefaultConnection().
+     */
+    public static javax.sql.DataSource getDataSource()
+    {
+        return getDataSource(TestConfiguration.getCurrent(), (HashMap) null);
+    }
+    
+    /**
+     * Create a new DataSource object setup from the passed in TestConfiguration.
+     * The getConnection() method will return a connection identical to
+     * TestConfiguration.openDefaultConnection().
+     */
+    static javax.sql.DataSource getDataSource(TestConfiguration config,
+            HashMap beanProperties)
+    {
+        if (beanProperties == null)
+             beanProperties = getDataSourceProperties(config);
+        
+        String dataSourceClass = config.getJDBCClient().getDataSourceClassName();
+        
+        return (javax.sql.DataSource) getDataSourceObject(dataSourceClass,
+                beanProperties);
+    }
+    
+    /**
+     * Create a HashMap with the set of Derby DataSource
+     * Java bean properties corresponding to the configuration.
+     */
+    static HashMap getDataSourceProperties(TestConfiguration config) 
+    {
+        HashMap beanProperties = new HashMap();
+        
+        if (!config.getJDBCClient().isEmbedded()) {
+            beanProperties.put("serverName", config.getHostName());
+            beanProperties.put("portNumber", new Integer(config.getPort()));
+        }
+        
+        beanProperties.put("databaseName", config.getDatabaseName());
+        beanProperties.put("user", config.getUserName());
+        beanProperties.put("password", config.getUserPassword());
+
+        
+        return beanProperties;
+    }
+    
+    /**
+     * Return a DataSource object of the passsed in type
+     * configured with the passed in Java bean properties.
+     * This will actually work with an object that has Java bean
+     * setter methods.
+     */
+    static Object getDataSourceObject(String classname, HashMap beanProperties)
+    {
+
+        Object ds;
+        try {
+            ds = Class.forName(classname).newInstance();
+            
+            for (Iterator i = beanProperties.keySet().iterator();
+                i.hasNext(); )
+            {
+                String property = (String) i.next();
+                Object value = beanProperties.get(property);
+                
+                setBeanProperty(ds, property, value);
+            }
+            return ds;
+        } catch (Exception e) {
+            Assert.fail(e.getMessage());
+            return null;
+        }
+    }
+    
+    static void setBeanProperty(Object ds, String property, Object value)
+    {
+        String setterName = getSetterName(property);
+        
+        // Base the type of the setter method from the value's class.
+        Class clazz = value.getClass();      
+        if (Integer.class.equals(clazz))
+            clazz = Integer.TYPE;
+        else if (Boolean.class.equals(clazz))
+            clazz = Boolean.TYPE;
+
+        try {
+            Method setter = ds.getClass().getMethod(setterName,
+                    new Class[] {clazz});
+            setter.invoke(ds, new Object[] {value});
+        } catch (Exception e) {
+            Assert.fail(e.getMessage());
+        }   
+    }
+    
+    private static String getSetterName(String attribute) {
+        return "set" + Character.toUpperCase(attribute.charAt(0))
+                + attribute.substring(1);
+    }
+}

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/JDBCDataSource.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java?view=diff&rev=472187&r1=472186&r2=472187
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java
(original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/junit/TestConfiguration.java
Tue Nov  7 10:18:54 2006
@@ -344,8 +344,9 @@
         this.port = -1;
         this.singleLegXA = false;
         
-        this.jdbcClient = JDBCClient.EMBEDDED;
+        this.jdbcClient = JDBCClient.getDefaultEmbedded();
         url = createJDBCUrlWithDatabaseName(dbName);
+        initConnector();
  
     }
 
@@ -364,6 +365,7 @@
         this.hostName = hostName;
         
         this.url = createJDBCUrlWithDatabaseName(dbName);
+        initConnector();
     }
 
     
@@ -388,6 +390,7 @@
         this.hostName = copy.hostName;
         
         this.url = copy.url;
+        initConnector();
     }
     /**
      * Obtain a new configuration identical to the passed in
@@ -409,6 +412,7 @@
         this.hostName = copy.hostName;
         
         this.url = createJDBCUrlWithDatabaseName(dbName);
+        initConnector();
     }
     
     /**
@@ -447,9 +451,10 @@
         } else if ("DerbyNet".equals(framework)) {
             jdbcClient = JDBCClient.DB2CLIENT;
         } else {
-            jdbcClient = JDBCClient.EMBEDDED;
+            jdbcClient = JDBCClient.getDefaultEmbedded();
         }
         url = createJDBCUrlWithDatabaseName(dbName);
+        initConnector();
     }
 
     /**
@@ -474,11 +479,40 @@
      * @return JDBC connection url, without attributes.
      */
     private String createJDBCUrlWithDatabaseName(String name) {
-        if (jdbcClient == JDBCClient.EMBEDDED) {
-            return jdbcClient.getUrlBase() + name;
+        if (JDBC.vmSupportsJDBC2())
+        {
+           if (jdbcClient.isEmbedded()) {
+               return jdbcClient.getUrlBase() + name;
+           } else {
+               return jdbcClient.getUrlBase() + hostName + ":" + port + "/" + name;
+           }
+        }
+        // No DriverManager support so no URL support.
+        return null;
+    }
+    
+    /**
+     * Initialize the connection factory.
+     * Defaults to the DriverManager implementation
+     * if running JDBC 2.0 or higher, otherwise a
+     * DataSource implementation for JSR 169.
+     *
+     */
+    private void initConnector()
+    {
+        if (JDBC.vmSupportsJDBC2())
+        {
+            try {
+                connector = (Connector) Class.forName(
+                  "org.apache.derbyTesting.junit.DriverManagerConnector").newInstance();
+            } catch (Exception e) {
+                Assert.fail(e.getMessage());
+            }
+            
         } else {
-            return jdbcClient.getUrlBase() + hostName + ":" + port + "/" + name;
+            connector = new DataSourceConnector();
         }
+        connector.setConfiguration(this);
     }
 
     /**
@@ -564,7 +598,7 @@
      */
     Connection openDefaultConnection()
         throws SQLException {
-        return getDefaultConnection("create=true");
+        return connector.openConnection();
     }
     
     /**
@@ -581,19 +615,6 @@
     }
     
     /**
-     * Get a connection to the default database using the  specified connection
-     * attributes.
-     * 
-     * @param connAttrs connection attributes
-     * @return connection to database.
-     * @throws SQLException
-     */
-    private Connection getDefaultConnection(String connAttrs)
-        throws SQLException {
-        return getConnection(getDatabaseName(), connAttrs);
-    }
-    
-    /**
      * Get a connection to a database using the specified connection 
      * attributes.
      * 
@@ -637,7 +658,7 @@
     public void shutdownDatabase()
     {
         try {
-            getDefaultConnection("shutdown=true");
+            connector.shutDatabase();
             Assert.fail("Database failed to shut down");
         } catch (SQLException e) {
              BaseJDBCTestCase.assertSQLState("Database shutdown", "08006", e);
@@ -652,7 +673,7 @@
     public void shutdownEngine()
     {
         try {
-            getConnection("", "shutdown=true");
+            connector.shutEngine();
             Assert.fail("Engine failed to shut down");
         } catch (SQLException e) {
              BaseJDBCTestCase.assertSQLState("Engine shutdown", "XJ015", e);
@@ -772,6 +793,12 @@
     private boolean isVerbose;
     private final boolean singleLegXA;
     
+    /**
+     * Indirection for obtaining connections based upon
+     * this configuration.
+     */
+    private Connector connector;
+    
 
     /**
      * Generate properties which can be set on a
@@ -800,7 +827,7 @@
     	(String databaseName, String connAttrs) 
     {
         Properties attrs = new Properties();
-        if (!(getCurrent().getJDBCClient() == JDBCClient.EMBEDDED)) {
+        if (!getCurrent().getJDBCClient().isEmbedded()) {
             attrs.setProperty("serverName", getCurrent().getHostName());
             attrs.setProperty("portNumber", Integer.toString(getCurrent().getPort()));
         }



Mime
View raw message