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 jb...@apache.org on 2005/05/24 00:03:20 UTC

svn commit: r178044 - in /incubator/derby/code/branches/datasource/src: java/org/apache/derby/api/ java/org/apache/derby/impl/ test/org/apache/derby/api/ test/org/apache/derby/impl/ test/org/apache/derby/util/

Author: jboynes
Date: Mon May 23 15:03:18 2005
New Revision: 178044

URL: http://svn.apache.org/viewcvs?rev=178044&view=rev
Log:
implement driver; share parsing with references

Added:
    incubator/derby/code/branches/datasource/src/test/org/apache/derby/api/ParsingTest.java
    incubator/derby/code/branches/datasource/src/test/org/apache/derby/api/ReferenceTest.java
    incubator/derby/code/branches/datasource/src/test/org/apache/derby/impl/EmbeddedDriverTest.java
Modified:
    incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/BasicDataSource.java
    incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/DerbyDriver.java
    incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/PooledDataSource.java
    incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/ReferenceFactory.java
    incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/ReferenceableDataSource.java
    incubator/derby/code/branches/datasource/src/java/org/apache/derby/impl/ClientConnectionFactory.java
    incubator/derby/code/branches/datasource/src/java/org/apache/derby/impl/DefaultConnectionFactory.java
    incubator/derby/code/branches/datasource/src/java/org/apache/derby/impl/EmbeddedConnectionFactory.java
    incubator/derby/code/branches/datasource/src/test/org/apache/derby/impl/ClientConnectionFactoryTest.java
    incubator/derby/code/branches/datasource/src/test/org/apache/derby/impl/EmbeddedConnectionFactoryTest.java
    incubator/derby/code/branches/datasource/src/test/org/apache/derby/util/ClientTestSuite.java

Modified: incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/BasicDataSource.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/BasicDataSource.java?rev=178044&r1=178043&r2=178044&view=diff
==============================================================================
--- incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/BasicDataSource.java (original)
+++ incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/BasicDataSource.java Mon May 23 15:03:18 2005
@@ -19,6 +19,10 @@
 import java.io.PrintWriter;
 import java.io.Serializable;
 import java.sql.SQLException;
+import java.util.Properties;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.StringTokenizer;
 
 import org.apache.derby.impl.DefaultConnectionFactory;
 
@@ -44,9 +48,9 @@
     // Derby specific properties
     private String connectionFactoryClass;
     private transient ConnectionFactory connectionFactory;
-    private boolean createDatabase;
+    private boolean create;
     private boolean upgrade;
-    private boolean shutdownDatabase;
+    private boolean shutdown;
 
     public String getDatabaseName() {
         return databaseName;
@@ -228,12 +232,12 @@
         this.logWriter = logWriter;
     }
 
-    public boolean getCreateDatabase() {
-        return createDatabase;
+    public boolean getCreate() {
+        return create;
     }
 
-    public void setCreateDatabase(boolean createDatabase) {
-        this.createDatabase = createDatabase;
+    public void setCreate(boolean create) {
+        this.create = create;
     }
 
     public boolean getUpgrade() {
@@ -244,12 +248,12 @@
         this.upgrade = upgrade;
     }
 
-    public boolean getShutdownDatabase() {
-        return shutdownDatabase;
+    public boolean getShutdown() {
+        return shutdown;
     }
 
-    public void setShutdownDatabase(boolean shutdownDatabase) {
-        this.shutdownDatabase = shutdownDatabase;
+    public void setShutdown(boolean shutdown) {
+        this.shutdown = shutdown;
     }
 
     /**
@@ -276,6 +280,145 @@
             return other.dataSourceName == null;
         } else {
             return dataSourceName.equals(other.dataSourceName);
+        }
+    }
+
+    /**
+     * Package protected method for initializing from a set of Properties.
+     *
+     * @param props properties to load
+     */
+    void loadProperties(Properties props) {
+        for (Iterator i = props.entrySet().iterator(); i.hasNext();) {
+            Map.Entry entry = (Map.Entry) i.next();
+            String key = (String) entry.getKey();
+            String value = (String) entry.getValue();
+            if ("serverName".equals(key)) {
+                setServerName(value);
+            } else if ("portNumber".equals(key)) {
+                setPortNumber(Integer.valueOf(value).intValue());
+            } else if ("databaseName".equals(key)) {
+                setDatabaseName(value);
+            } else if ("user".equals(key)) {
+                setUser(value);
+            } else if ("password".equals(key)) {
+                setPassword(value);
+            } else if ("connectionFactoryClass".equals(key)) {
+                setConnectionFactoryClass(value);
+            } else if ("create".equals(key)) {
+                setCreate(Boolean.valueOf(value).booleanValue());
+            } else if ("upgrade".equals(key)) {
+                setUpgrade(Boolean.valueOf(value).booleanValue());
+            } else if ("shutdown".equals(key)) {
+                setShutdown(Boolean.valueOf(value).booleanValue());
+            }
+        }
+    }
+
+    /**
+     * Package protected method for initializing from a JDBC URL.
+     *
+     * @param url the URL to load from
+     */
+    void loadURL(String url) {
+        // remove jdbc:derby: prefix
+        url = url.substring(11);
+        if (url.startsWith("//")) {
+            // serverName specified in url
+            int index = url.indexOf(';');
+            String serverName;
+            if (index == -1) {
+                serverName = url.substring(2);
+                url = "";
+            } else {
+                serverName = url.substring(2, index);
+                url = url.substring(index);
+            }
+
+            // extract databaseName
+            index = serverName.indexOf('/');
+            if (index != -1) {
+                setDatabaseName(serverName.substring(index+1).trim());
+                serverName = serverName.substring(0, index);
+            }
+
+            // extract portNumber
+            index = serverName.indexOf(':');
+            if (index != -1) {
+                setPortNumber(Integer.valueOf(serverName.substring(index+1)).intValue());
+                serverName = serverName.substring(0, index);
+            }
+            setServerName(serverName.trim());
+        } else {
+            String name;
+            int index = url.indexOf(';');
+            if (index == -1) {
+                name = url;
+            } else {
+                name = url.substring(0, index);
+                url = url.substring(index);
+            }
+            name = name.trim();
+            if (name.length() > 0) {
+                setDatabaseName(name);
+            }
+        }
+
+        // extract properties from URL
+        Properties props = new Properties();
+        StringTokenizer tok = new StringTokenizer(url, ";");
+        while (tok.hasMoreTokens()) {
+            String pair = tok.nextToken();
+            int index = pair.indexOf('=');
+            if (index == -1) {
+                continue;
+            }
+            String key = pair.substring(0, index);
+            String value = pair.substring(index+1);
+            props.setProperty(key.trim(), value.trim());
+        }
+        loadProperties(props);
+    }
+
+    /**
+     * Package protected method for converting a DataSource to a Derby JDBC URL.
+     *
+     * @return a URL enconded form of this DataSource
+     */
+    String toURL() {
+        StringBuffer buf = new StringBuffer(256);
+        buf.append("jdbc:derby:");
+        if (serverName != null) {
+            append(buf, "serverName", serverName);
+            append(buf, "portNumber", portNumber, 1527);
+        }
+        append(buf, "databaseName", databaseName);
+        append(buf, "description", description);
+        append(buf, "user", user);
+        append(buf, "password", password);
+        append(buf, "dataSourceName", dataSourceName);
+        append(buf, "connectionFactoryClass", connectionFactoryClass);
+        append(buf, "create", create);
+        append(buf, "upgrade", upgrade);
+        append(buf, "shutdown", shutdown);
+        return buf.toString();
+    }
+
+    private void append(StringBuffer buf, String key, Object value) {
+        if (value != null) {
+            buf.append(';').append(key).append('=').append(value);
+        }
+    }
+
+    private void append(StringBuffer buf, String key, int value, int def) {
+        if (value != def) {
+            buf.append(';').append(key).append('=').append(value);
+        }
+    }
+
+    private void append(StringBuffer buf, String key, boolean value) {
+        if (value) {
+            buf.append(';').append(key).append("=true");
         }
     }
 }

Modified: incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/DerbyDriver.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/DerbyDriver.java?rev=178044&r1=178043&r2=178044&view=diff
==============================================================================
--- incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/DerbyDriver.java (original)
+++ incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/DerbyDriver.java Mon May 23 15:03:18 2005
@@ -22,6 +22,11 @@
 import java.sql.DriverPropertyInfo;
 import java.sql.SQLException;
 import java.util.Properties;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+import org.apache.derby.jdbc.InternalDriver;
 
 /**
  * Implementation of legacy JDBC Driver.
@@ -29,6 +34,8 @@
  * @version $Rev$ $Date$
  */
 public class DerbyDriver implements Driver {
+    private static final String SQLJ_INTERNAL = "jdbc:default:connection";
+
     static {
         DerbyDriver driver = new DerbyDriver();
         try {
@@ -39,21 +46,34 @@
     }
 
     public boolean acceptsURL(String url) throws SQLException {
-        return url.startsWith("jdbc:derby:") || url.equals("jdbc:default:connection");
+        if (url == null) {
+            return false;
+        }
+        return url.startsWith("jdbc:derby:") || url.equals(SQLJ_INTERNAL);
     }
 
     public Connection connect(String url, Properties info) throws SQLException {
+        if (SQLJ_INTERNAL.equals(url)) {
+            InternalDriver driver = InternalDriver.activeDriver();
+            if (driver == null) {
+                // database is not booted yet
+                return null;
+            }
+            return driver.connect(url, info);
+        }
+
         DriverDataSource ds = new DriverDataSource();
-        // decompose url and properties to initialize the DriverDataSource
+        ds.loadProperties(info);
+        ds.loadURL(url);
         return ds.getConnection();
     }
 
     public int getMajorVersion() {
-        throw new UnsupportedOperationException();
+        return 10; // todo get from properties file
     }
 
     public int getMinorVersion() {
-        throw new UnsupportedOperationException();
+        return 1; // todo get from properties file
     }
 
     public boolean jdbcCompliant() {

Modified: incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/PooledDataSource.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/PooledDataSource.java?rev=178044&r1=178043&r2=178044&view=diff
==============================================================================
--- incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/PooledDataSource.java (original)
+++ incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/PooledDataSource.java Mon May 23 15:03:18 2005
@@ -17,28 +17,21 @@
 package org.apache.derby.api;
 
 import java.sql.SQLException;
-import javax.naming.NamingException;
-import javax.naming.Reference;
-import javax.naming.Referenceable;
 import javax.sql.ConnectionPoolDataSource;
 import javax.sql.PooledConnection;
 
 /**
  * Implementation of a DataSource supporting pooled connections. This is intended for use by connection pooling
- * middleware rather than end user applications. As such, we assume that javax.naming is available.
+ * middleware rather than end user applications.
  *
  * @version $Rev$ $Date$
  */
-public class PooledDataSource extends BasicDataSource implements ConnectionPoolDataSource, Referenceable {
+public class PooledDataSource extends BasicDataSource implements ConnectionPoolDataSource {
     public PooledConnection getPooledConnection() throws SQLException {
         throw new UnsupportedOperationException();
     }
 
     public PooledConnection getPooledConnection(String user, String password) throws SQLException {
         throw new UnsupportedOperationException();
-    }
-
-    public Reference getReference() throws NamingException {
-        return ReferenceFactory.getReference(this);
     }
 }

Modified: incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/ReferenceFactory.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/ReferenceFactory.java?rev=178044&r1=178043&r2=178044&view=diff
==============================================================================
--- incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/ReferenceFactory.java (original)
+++ incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/ReferenceFactory.java Mon May 23 15:03:18 2005
@@ -19,8 +19,8 @@
 import java.util.Hashtable;
 import javax.naming.Context;
 import javax.naming.Name;
-import javax.naming.NamingException;
 import javax.naming.Reference;
+import javax.naming.StringRefAddr;
 import javax.naming.spi.ObjectFactory;
 
 /**
@@ -29,11 +29,22 @@
  * @version $Rev$ $Date$
  */
 public final class ReferenceFactory implements ObjectFactory {
-    static Reference getReference(BasicDataSource ds) throws NamingException {
-        throw new UnsupportedOperationException();
-    }
-
     public Object getObjectInstance(Object obj, Name name, Context nameCtx, Hashtable environment) throws Exception {
-        throw new UnsupportedOperationException();
+        // extract the URL from the reference
+        Reference ref = (Reference) obj;
+        StringRefAddr addr = (StringRefAddr) ref.get("Derby URL");
+        String url = (String) addr.getContent();
+
+        // instantiate the DataSource
+        ClassLoader cl = Thread.currentThread().getContextClassLoader();
+        if (cl == null) {
+            cl = getClass().getClassLoader();
+        }
+        Class dsClass = cl.loadClass(ref.getClassName());
+
+        // load the properties into the DataSource
+        BasicDataSource ds = (BasicDataSource) dsClass.newInstance();
+        ds.loadURL(url);
+        return ds;
     }
 }

Modified: incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/ReferenceableDataSource.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/ReferenceableDataSource.java?rev=178044&r1=178043&r2=178044&view=diff
==============================================================================
--- incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/ReferenceableDataSource.java (original)
+++ incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/ReferenceableDataSource.java Mon May 23 15:03:18 2005
@@ -19,6 +19,8 @@
 import javax.naming.NamingException;
 import javax.naming.Reference;
 import javax.naming.Referenceable;
+import javax.naming.RefAddr;
+import javax.naming.StringRefAddr;
 
 /**
  * Specialization of DerbyDataSource that can be bound to JNDI as a Reference. This is intended for use by application
@@ -31,7 +33,8 @@
  * @version $Rev$ $Date$
  */
 public class ReferenceableDataSource extends DerbyDataSource implements Referenceable {
-    public Reference getReference() throws NamingException {
-        return ReferenceFactory.getReference(this);
+    public Reference getReference() {
+        RefAddr addr = new StringRefAddr("Derby URL", toURL());
+        return new Reference(getClass().getName(), addr);
     }
 }

Modified: incubator/derby/code/branches/datasource/src/java/org/apache/derby/impl/ClientConnectionFactory.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/branches/datasource/src/java/org/apache/derby/impl/ClientConnectionFactory.java?rev=178044&r1=178043&r2=178044&view=diff
==============================================================================
--- incubator/derby/code/branches/datasource/src/java/org/apache/derby/impl/ClientConnectionFactory.java (original)
+++ incubator/derby/code/branches/datasource/src/java/org/apache/derby/impl/ClientConnectionFactory.java Mon May 23 15:03:18 2005
@@ -48,10 +48,10 @@
         cds.setUser(ds.getUser());
         cds.setPassword(ds.getPassword());
         StringBuffer attrs = new StringBuffer();
-        if (ds.getCreateDatabase()) {
+        if (ds.getCreate()) {
             attrs.append(";create=true");
         }
-        if (ds.getShutdownDatabase()) {
+        if (ds.getShutdown()) {
             attrs.append(";shutdown=true");
         }
         cds.setConnectionAttributes(attrs.toString());

Modified: incubator/derby/code/branches/datasource/src/java/org/apache/derby/impl/DefaultConnectionFactory.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/branches/datasource/src/java/org/apache/derby/impl/DefaultConnectionFactory.java?rev=178044&r1=178043&r2=178044&view=diff
==============================================================================
--- incubator/derby/code/branches/datasource/src/java/org/apache/derby/impl/DefaultConnectionFactory.java (original)
+++ incubator/derby/code/branches/datasource/src/java/org/apache/derby/impl/DefaultConnectionFactory.java Mon May 23 15:03:18 2005
@@ -23,12 +23,15 @@
 import org.apache.derby.api.BasicDataSource;
 
 /**
+ * Defautl ConnectionFactory implementation that chooses between Client and Embedded connection depending
+ * on whether the serverName property is set or not.
+ *  
  * @version $Rev$ $Date$
  */
 public class DefaultConnectionFactory implements ConnectionFactory {
     public Connection getConnection(BasicDataSource ds) throws SQLException {
         ConnectionFactory cf;
-        if (ds.getServerName() == null) {
+        if (ds.getServerName() != null) {
             cf = new ClientConnectionFactory();
         } else {
             cf = new EmbeddedConnectionFactory();
@@ -38,7 +41,7 @@
 
     public Connection getConnection(BasicDataSource ds, String user, String password) throws SQLException {
         ConnectionFactory cf;
-        if (ds.getServerName() == null) {
+        if (ds.getServerName() != null) {
             cf = new ClientConnectionFactory();
         } else {
             cf = new EmbeddedConnectionFactory();

Modified: incubator/derby/code/branches/datasource/src/java/org/apache/derby/impl/EmbeddedConnectionFactory.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/branches/datasource/src/java/org/apache/derby/impl/EmbeddedConnectionFactory.java?rev=178044&r1=178043&r2=178044&view=diff
==============================================================================
--- incubator/derby/code/branches/datasource/src/java/org/apache/derby/impl/EmbeddedConnectionFactory.java (original)
+++ incubator/derby/code/branches/datasource/src/java/org/apache/derby/impl/EmbeddedConnectionFactory.java Mon May 23 15:03:18 2005
@@ -42,8 +42,8 @@
         eds.setDatabaseName(ds.getDatabaseName());
         eds.setUser(ds.getUser());
         eds.setPassword(ds.getPassword());
-        eds.setCreateDatabase(ds.getCreateDatabase() ? "create" : null);
-        eds.setShutdownDatabase(ds.getShutdownDatabase() ? "shutdown" : null);
+        eds.setCreateDatabase(ds.getCreate() ? "create" : null);
+        eds.setShutdownDatabase(ds.getShutdown() ? "shutdown" : null);
 
         eds.setLoginTimeout(ds.getLoginTimeout());
         eds.setLogWriter(ds.getLogWriter());

Added: incubator/derby/code/branches/datasource/src/test/org/apache/derby/api/ParsingTest.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/branches/datasource/src/test/org/apache/derby/api/ParsingTest.java?rev=178044&view=auto
==============================================================================
--- incubator/derby/code/branches/datasource/src/test/org/apache/derby/api/ParsingTest.java (added)
+++ incubator/derby/code/branches/datasource/src/test/org/apache/derby/api/ParsingTest.java Mon May 23 15:03:18 2005
@@ -0,0 +1,135 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed 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.derby.api;
+
+import java.sql.SQLException;
+import java.util.Properties;
+
+import junit.framework.TestCase;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class ParsingTest extends TestCase {
+    private BasicDataSource mockds;
+    private Properties props;
+
+    public void testServerName() throws SQLException {
+        mockds.loadURL("jdbc:derby://testHost");
+        assertEquals("testHost", mockds.getServerName());
+        assertEquals(1527, mockds.getPortNumber());
+        assertNull(mockds.getDatabaseName());
+    }
+
+    public void testServerAndDatabaseName() throws SQLException {
+        mockds.loadURL("jdbc:derby://testHost/testdb");
+        assertEquals("testHost", mockds.getServerName());
+        assertEquals(1527, mockds.getPortNumber());
+        assertEquals("testdb", mockds.getDatabaseName());
+    }
+
+    public void testServerNameAndPort() throws SQLException {
+        mockds.loadURL("jdbc:derby://testHost2:1234");
+        assertEquals("testHost2",  mockds.getServerName());
+        assertEquals(1234, mockds.getPortNumber());
+        assertNull(mockds.getDatabaseName());
+    }
+
+    public void testServerNameDatabaseNameAndPort() throws SQLException {
+        mockds.loadURL("jdbc:derby://testHost2:1234/testdb");
+        assertEquals("testHost2", mockds.getServerName());
+        assertEquals(1234, mockds.getPortNumber());
+        assertEquals("testdb", mockds.getDatabaseName());
+    }
+
+    public void testNoServerName() throws SQLException {
+        mockds.loadURL("jdbc:derby://");
+        assertEquals("", mockds.getServerName());
+        assertEquals(1527, mockds.getPortNumber());
+        assertNull(mockds.getDatabaseName());
+    }
+
+    public void testDatabaseNameInURL() throws SQLException {
+        mockds.loadURL("jdbc:derby:");
+        assertEquals(null, mockds.getServerName());
+        assertEquals(null, mockds.getDatabaseName());
+
+        mockds.loadURL("jdbc:derby:testdb");
+        assertEquals(null, mockds.getServerName());
+        assertEquals("testdb", mockds.getDatabaseName());
+
+        mockds.loadURL("jdbc:derby:/tmp/testdb");
+        assertEquals(null, mockds.getServerName());
+        assertEquals("/tmp/testdb", mockds.getDatabaseName());
+
+        mockds.loadURL("jdbc:derby:C:\\Temp\\testdb");
+        assertEquals(null, mockds.getServerName());
+        assertEquals("C:\\Temp\\testdb", mockds.getDatabaseName());
+    }
+
+    public void testDefaultProperties() throws SQLException {
+        mockds.loadURL("jdbc:derby:");
+        assertNull(mockds.getServerName());
+        assertEquals(1527, mockds.getPortNumber());
+        assertNull(mockds.getDatabaseName());
+        assertNull(mockds.getUser());
+        assertNull(mockds.getPassword());
+        assertFalse(mockds.getCreate());
+        assertFalse(mockds.getUpgrade());
+        assertFalse(mockds.getShutdown());
+    }
+
+    public void testPropertiesInURL() throws SQLException {
+        mockds.loadURL("jdbc:derby:;serverName=testHost;portNumber=1234;databaseName=testdb;user=testuser;password=testpw;create=true;upgrade=true;shutdown=true");
+        assertEquals("testHost", mockds.getServerName());
+        assertEquals(1234, mockds.getPortNumber());
+        assertEquals("testdb", mockds.getDatabaseName());
+        assertEquals("testuser", mockds.getUser());
+        assertEquals("testpw", mockds.getPassword());
+        assertTrue(mockds.getCreate());
+        assertTrue(mockds.getUpgrade());
+        assertTrue(mockds.getShutdown());
+    }
+
+    public void testProperties() throws SQLException {
+        props.setProperty("serverName", "testHost");
+        props.setProperty("portNumber", "1234");
+        props.setProperty("databaseName", "testdb");
+        props.setProperty("user", "testuser");
+        props.setProperty("password", "testpw");
+        props.setProperty("create", "true");
+        props.setProperty("upgrade", "true");
+        props.setProperty("shutdown", "true");
+
+        mockds.loadProperties(props);
+        assertEquals("testHost", mockds.getServerName());
+        assertEquals(1234, mockds.getPortNumber());
+        assertEquals("testdb", mockds.getDatabaseName());
+        assertEquals("testuser", mockds.getUser());
+        assertEquals("testpw", mockds.getPassword());
+        assertTrue(mockds.getCreate());
+        assertTrue(mockds.getUpgrade());
+        assertTrue(mockds.getShutdown());
+    }
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        props = new Properties();
+        mockds = new BasicDataSource(){
+        };
+    }
+}

Added: incubator/derby/code/branches/datasource/src/test/org/apache/derby/api/ReferenceTest.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/branches/datasource/src/test/org/apache/derby/api/ReferenceTest.java?rev=178044&view=auto
==============================================================================
--- incubator/derby/code/branches/datasource/src/test/org/apache/derby/api/ReferenceTest.java (added)
+++ incubator/derby/code/branches/datasource/src/test/org/apache/derby/api/ReferenceTest.java Mon May 23 15:03:18 2005
@@ -0,0 +1,53 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed 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.derby.api;
+
+import javax.naming.Reference;
+import javax.naming.RefAddr;
+import javax.naming.StringRefAddr;
+
+import junit.framework.TestCase;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class ReferenceTest extends TestCase {
+    public void testCreateReference() {
+        ReferenceableDataSource ds = new ReferenceableDataSource();
+        ds.setServerName("testhost");
+        ds.setDatabaseName("testdb");
+        ds.setCreate(true);
+        Reference ref = ds.getReference();
+        assertEquals(1, ref.size());
+        RefAddr addr = (StringRefAddr) ref.get(0);
+        assertEquals("Derby URL", addr.getType());
+        assertEquals("jdbc:derby:;serverName=testhost;databaseName=testdb;create=true", addr.getContent());
+    }
+
+    public void testCreateObject() throws Exception {
+        StringRefAddr addr = new StringRefAddr("Derby URL", "jdbc:derby:;serverName=testhost;databaseName=testdb;create=true");
+        Reference ref = new Reference(ReferenceableDataSource.class.getName(), addr);
+        ReferenceFactory factory = new ReferenceFactory();
+        Object o = factory.getObjectInstance(ref, null, null, null);
+        assertTrue(o instanceof ReferenceableDataSource);
+        ReferenceableDataSource ds = (ReferenceableDataSource) o;
+        assertEquals("testhost", ds.getServerName());
+        assertEquals("testdb", ds.getDatabaseName());
+        assertTrue(ds.getCreate());
+        assertFalse(ds.getShutdown());
+    }
+}

Modified: incubator/derby/code/branches/datasource/src/test/org/apache/derby/impl/ClientConnectionFactoryTest.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/branches/datasource/src/test/org/apache/derby/impl/ClientConnectionFactoryTest.java?rev=178044&r1=178043&r2=178044&view=diff
==============================================================================
--- incubator/derby/code/branches/datasource/src/test/org/apache/derby/impl/ClientConnectionFactoryTest.java (original)
+++ incubator/derby/code/branches/datasource/src/test/org/apache/derby/impl/ClientConnectionFactoryTest.java Mon May 23 15:03:18 2005
@@ -51,7 +51,7 @@
         ds = new DerbyDataSource();
         ds.setServerName("localhost");
         ds.setDatabaseName("testdb");
-        ds.setCreateDatabase(true);
+        ds.setCreate(true);
         ds.setUser("testuser");
         ds.setPassword("testpassword");
         cf = new ClientConnectionFactory();

Modified: incubator/derby/code/branches/datasource/src/test/org/apache/derby/impl/EmbeddedConnectionFactoryTest.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/branches/datasource/src/test/org/apache/derby/impl/EmbeddedConnectionFactoryTest.java?rev=178044&r1=178043&r2=178044&view=diff
==============================================================================
--- incubator/derby/code/branches/datasource/src/test/org/apache/derby/impl/EmbeddedConnectionFactoryTest.java (original)
+++ incubator/derby/code/branches/datasource/src/test/org/apache/derby/impl/EmbeddedConnectionFactoryTest.java Mon May 23 15:03:18 2005
@@ -45,14 +45,14 @@
     }
 
     public void testDerbyOptions() throws SQLException {
-        ds.setCreateDatabase(false);
-        ds.setShutdownDatabase(false);
+        ds.setCreate(false);
+        ds.setShutdown(false);
         EmbeddedSimpleDataSource eds = cf.getDataSource(ds);
         assertNull(eds.getCreateDatabase());
         assertNull(eds.getShutdownDatabase());
 
-        ds.setCreateDatabase(true);
-        ds.setShutdownDatabase(true);
+        ds.setCreate(true);
+        ds.setShutdown(true);
         eds = cf.getDataSource(ds);
         assertEquals("create", eds.getCreateDatabase());
         assertEquals("shutdown", eds.getShutdownDatabase());
@@ -60,7 +60,7 @@
 
     public void testCreateDatabase() throws SQLException {
         ds.setDatabaseName("testdb");
-        ds.setCreateDatabase(true);
+        ds.setCreate(true);
 
         // connect first time, should be no warning
         Connection c = cf.getConnection(ds);
@@ -75,8 +75,8 @@
         c.close();
 
         // shut down
-        ds.setCreateDatabase(false);
-        ds.setShutdownDatabase(true);
+        ds.setCreate(false);
+        ds.setShutdown(true);
         try {
             cf.getConnection(ds);
             fail();
@@ -98,7 +98,7 @@
     protected void tearDown() throws Exception {
         // shutdown server
         ds = new DerbyDataSource();
-        ds.setShutdownDatabase(true);
+        ds.setShutdown(true);
         try {
             cf.getConnection(ds);
         } catch (SQLException e) {

Added: incubator/derby/code/branches/datasource/src/test/org/apache/derby/impl/EmbeddedDriverTest.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/branches/datasource/src/test/org/apache/derby/impl/EmbeddedDriverTest.java?rev=178044&view=auto
==============================================================================
--- incubator/derby/code/branches/datasource/src/test/org/apache/derby/impl/EmbeddedDriverTest.java (added)
+++ incubator/derby/code/branches/datasource/src/test/org/apache/derby/impl/EmbeddedDriverTest.java Mon May 23 15:03:18 2005
@@ -0,0 +1,73 @@
+/**
+ *
+ * Copyright 2005 The Apache Software Foundation
+ *
+ *  Licensed 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.derby.impl;
+
+import java.io.File;
+import java.sql.Connection;
+import java.sql.Driver;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+
+import junit.framework.TestCase;
+
+import org.apache.derby.api.DerbyDriver;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class EmbeddedDriverTest extends TestCase {
+    private String tmpDir;
+
+    public void testConnect() throws SQLException {
+        Connection c = DriverManager.getConnection("jdbc:derby:testdb;create=true", null, null);
+        c.close();
+    }
+
+    protected void setUp() throws Exception {
+        super.setUp();
+        new DerbyDriver();
+        tmpDir = System.getProperty("java.io.tmpdir") + "/embedTest";
+        System.setProperty("derby.system.home", tmpDir);
+    }
+
+    protected void tearDown() throws Exception {
+        // shutdown server
+        try {
+            DriverManager.getConnection("jdbc:derby:;shutdown=true");
+        } catch (SQLException e) {
+            if (!"XJ015".equals(e.getSQLState())) {
+                throw e;
+            }
+        }
+        delete(new File(tmpDir));
+        Driver driver = DriverManager.getDriver("jdbc:derby:");
+        DriverManager.deregisterDriver(driver);
+
+        super.tearDown();
+    }
+
+    private void delete(File dir) {
+        if (dir.isDirectory()) {
+            File[] files = dir.listFiles();
+            for (int i = 0; i < files.length; i++) {
+                File file = files[i];
+                delete(file);
+            }
+        }
+        dir.delete();
+    }
+}

Modified: incubator/derby/code/branches/datasource/src/test/org/apache/derby/util/ClientTestSuite.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/branches/datasource/src/test/org/apache/derby/util/ClientTestSuite.java?rev=178044&r1=178043&r2=178044&view=diff
==============================================================================
--- incubator/derby/code/branches/datasource/src/test/org/apache/derby/util/ClientTestSuite.java (original)
+++ incubator/derby/code/branches/datasource/src/test/org/apache/derby/util/ClientTestSuite.java Mon May 23 15:03:18 2005
@@ -77,7 +77,7 @@
             e.printStackTrace();
         }
         DerbyDataSource ds = new DerbyDataSource();
-        ds.setShutdownDatabase(true);
+        ds.setShutdown(true);
         try {
             new EmbeddedConnectionFactory().getConnection(ds);
         } catch (SQLException e) {