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/23 04:55:07 UTC
svn commit: r177908 - in /incubator/derby/code/branches/datasource: ./
src/java/org/apache/derby/api/ src/java/org/apache/derby/impl/ src/test/
src/test/org/ src/test/org/apache/ src/test/org/apache/derby/
src/test/org/apache/derby/api/ src/test/org/apache/derby/impl/
src/test/org/apache/derby/util/
Author: jboynes
Date: Sun May 22 19:55:06 2005
New Revision: 177908
URL: http://svn.apache.org/viewcvs?rev=177908&view=rev
Log:
initial impl for connection factories; JavaDoc'ed properties
Added:
incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/ConnectionFactory.java
- copied, changed from r169495, incubator/derby/code/branches/datasource/src/java/org/apache/derby/impl/ConnectionFactory.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/
incubator/derby/code/branches/datasource/src/test/org/
incubator/derby/code/branches/datasource/src/test/org/apache/
incubator/derby/code/branches/datasource/src/test/org/apache/derby/
incubator/derby/code/branches/datasource/src/test/org/apache/derby/api/
incubator/derby/code/branches/datasource/src/test/org/apache/derby/impl/
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/
incubator/derby/code/branches/datasource/src/test/org/apache/derby/util/ClientTestSuite.java
Removed:
incubator/derby/code/branches/datasource/src/java/org/apache/derby/impl/ConnectionFactory.java
Modified:
incubator/derby/code/branches/datasource/project.xml
incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/BasicDataSource.java
incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/DerbyDataSource.java
incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/DerbyDriver.java
Modified: incubator/derby/code/branches/datasource/project.xml
URL: http://svn.apache.org/viewcvs/incubator/derby/code/branches/datasource/project.xml?rev=177908&r1=177907&r2=177908&view=diff
==============================================================================
--- incubator/derby/code/branches/datasource/project.xml (original)
+++ incubator/derby/code/branches/datasource/project.xml Sun May 22 19:55:06 2005
@@ -32,9 +32,15 @@
<currentVersion>10.1.0.SNAPSHOT</currentVersion>
+ <dependencies>
+ <dependency>
+ <groupId>derby</groupId>
+ <artifactId>derby-all</artifactId>
+ <version>10.1-SNAPSHOT</version>
+ </dependency>
+ </dependencies>
<build>
<sourceDirectory>${basedir}/src/java</sourceDirectory>
- <unitTestSourceDirectory>${basedir}/src/test</unitTestSourceDirectory>
<resources>
<resource>
<directory>${basedir}/src/java</directory>
@@ -43,5 +49,12 @@
</includes>
</resource>
</resources>
+
+ <unitTestSourceDirectory>${basedir}/src/test</unitTestSourceDirectory>
+ <unitTest>
+ <includes>
+ <include>**/*Test.java</include>
+ </includes>
+ </unitTest>
</build>
</project>
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=177908&r1=177907&r2=177908&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 Sun May 22 19:55:06 2005
@@ -18,6 +18,9 @@
import java.io.PrintWriter;
import java.io.Serializable;
+import java.sql.SQLException;
+
+import org.apache.derby.impl.DefaultConnectionFactory;
/**
* Base class for {@link javax.sql.DataSource} implementations that defines the appropriate properties.
@@ -29,18 +32,18 @@
private String databaseName;
private String dataSourceName;
private String description;
- private String networkProtocol;
+ private String user;
private String password;
- private int portNumber;
- private String roleName;
private String serverName;
- private String user;
+ private int portNumber = 1527;
// transient properties defined by the DataSource interface
private transient int loginTimeout;
private transient PrintWriter logWriter;
// Derby specific properties
+ private String connectionFactoryClass;
+ private transient ConnectionFactory connectionFactory;
private boolean createDatabase;
private boolean upgrade;
private boolean shutdownDatabase;
@@ -49,6 +52,13 @@
return databaseName;
}
+ /**
+ * Set the name of the database. This is the name of the database inside the server and corresponds to the path of
+ * the root directory; relative paths are resolved against the path specified in the derby.system.home system
+ * property set on the server.
+ *
+ * @param databaseName the name of the database
+ */
public void setDatabaseName(String databaseName) {
this.databaseName = databaseName;
}
@@ -57,6 +67,12 @@
return dataSourceName;
}
+ /**
+ * Set the name of this DataSource. This is typically used by a connection manager to identify connections from the
+ * same DataSource.
+ *
+ * @param dataSourceName the name of this DataSource
+ */
public void setDataSourceName(String dataSourceName) {
this.dataSourceName = dataSourceName;
}
@@ -65,62 +81,134 @@
return description;
}
+ /**
+ * Set the description for this DataSource.
+ */
public void setDescription(String description) {
this.description = description;
}
- public String getNetworkProtocol() {
- return networkProtocol;
+ public String getUser() {
+ return user;
}
- public void setNetworkProtocol(String networkProtocol) {
- this.networkProtocol = networkProtocol;
+ /**
+ * Set the identity used to connect to the server. This may be null if connecting to an embedded server.
+ *
+ * @param user the identity (username) used to connector to the server
+ */
+ public void setUser(String user) {
+ this.user = user;
}
public String getPassword() {
return password;
}
+ /**
+ * Set a password credential for authenticating to the server.
+ *
+ * @param password the password credential used to authenticate to the server
+ */
public void setPassword(String password) {
this.password = password;
}
- public int getPortNumber() {
- return portNumber;
- }
-
- public void setPortNumber(int portNumber) {
- this.portNumber = portNumber;
- }
-
- public String getRoleName() {
- return roleName;
+ public String getConnectionFactoryClass() {
+ return connectionFactoryClass;
}
- public void setRoleName(String roleName) {
- this.roleName = roleName;
+ /**
+ * Set the name of the class to be used to create connections. If this property is not set then an internal default
+ * implementation is used which will determined the type of connection based on the value of the serverName
+ * property:
+ * <p/>
+ * <ul>
+ * <li>if the property is set, attempt to connect to a server on the specified host using the Derby Network Client
+ * protocol</li>
+ * <li>if the property is null, connect to an embedded server</li> </ul>
+ * <p/>
+ * Users can supply their own implementation of {@link org.apache.derby.api.ConnectionFactory} to support custom
+ * protocols. Such an implementation must have a public no-arg constructor and will be loaded using first the Thread
+ * Context ClassLoader and then ClassLoader used to load this class.
+ *
+ * @param connectionFactoryClass name of a custom {@link org.apache.derby.api.ConnectionFactory} implementation;
+ * may be null
+ */
+ public void setConnectionFactoryClass(String connectionFactoryClass) {
+ this.connectionFactoryClass = connectionFactoryClass;
}
public String getServerName() {
return serverName;
}
+ /**
+ * Helper for instantating ConnectionFactory instances.
+ *
+ * @return a ConnectionFactory as defined by the connectionFactoryClass property
+ * @throws SQLException if there was a problem instantiating the factory
+ */
+ protected synchronized ConnectionFactory newConnectionFactory() throws SQLException {
+ if (connectionFactory == null) {
+ String name = connectionFactoryClass;
+ if (name == null) {
+ connectionFactory = new DefaultConnectionFactory();
+ } else {
+ ClassLoader cl = Thread.currentThread().getContextClassLoader();
+ if (cl == null) {
+ cl = this.getClass().getClassLoader();
+ }
+ try {
+ Class clazz = cl.loadClass(name);
+ connectionFactory = (ConnectionFactory) clazz.newInstance();
+ } catch (ClassNotFoundException e) {
+ throw new SQLException(e.getMessage());
+ } catch (InstantiationException e) {
+ throw new SQLException(e.getMessage());
+ } catch (IllegalAccessException e) {
+ throw new SQLException(e.getMessage());
+ }
+ }
+ }
+ return connectionFactory;
+ }
+
+ /**
+ * Set the host name of the server to connect to.
+ * If null, the default ConnectionFactory will connect to an embedded server.
+ *
+ * @param serverName the host name of the server to connect to; null means an embedded server
+ */
public void setServerName(String serverName) {
this.serverName = serverName;
}
- public String getUser() {
- return user;
+ public int getPortNumber() {
+ return portNumber;
}
- public void setUser(String user) {
- this.user = user;
+ /**
+ * Set the TCP/IP port for a network connection.
+ * Defaults to 1527 and is ignored if the server is embedded.
+ *
+ * @param portNumber the port number to connect to
+ */
+ public void setPortNumber(int portNumber) {
+ this.portNumber = portNumber;
}
public int getLoginTimeout() {
return loginTimeout;
}
+ /**
+ * Set the number of seconds to wait whilst attempted to establish a connection.
+ * The default value of zero implies no limit.
+ *
+ * @param loginTimeout number of seconds to wait when establishing a connection; zero means no timeout
+ * @see javax.sql.DataSource#setLoginTimeout(int)
+ */
public void setLoginTimeout(int loginTimeout) {
this.loginTimeout = loginTimeout;
}
@@ -129,6 +217,13 @@
return logWriter;
}
+ /**
+ * Set the PrintWriter to be used for all messages associated with this DataSource.
+ * The default is null indicating logging is disabled.
+ *
+ * @param logWriter the PrintWriter to be used for logging; null disables logging
+ * @see javax.sql.DataSource#setLogWriter(java.io.PrintWriter)
+ */
public void setLogWriter(PrintWriter logWriter) {
this.logWriter = logWriter;
}
@@ -158,22 +253,29 @@
}
/**
- * Calculate hashCode based on the following properties: <ul> <li>TBD</li> </ul>
+ * Calculate hashCode based on the following properties: <ul> <li>dataSourceName</li> </ul>
*
* @return hashCode for this DataSource
*/
public int hashCode() {
- return super.hashCode();
+ return dataSourceName == null ? 0 : dataSourceName.hashCode();
}
/**
- * Calculate equals based on the following properties: <ul> <li>TBD</li> </ul>
+ * Calculate equals based on the following properties: <ul> <li>dataSourceName</li> </ul>
*
* @param obj object to compare to
*
* @return true if both objects refer to the same DataSource
*/
public boolean equals(Object obj) {
- return super.equals(obj);
+ if (obj == this) return true;
+ if (obj instanceof BasicDataSource == false) return false;
+ final BasicDataSource other = (BasicDataSource) obj;
+ if (dataSourceName == null) {
+ return other.dataSourceName == null;
+ } else {
+ return dataSourceName.equals(other.dataSourceName);
+ }
}
}
Copied: incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/ConnectionFactory.java (from r169495, incubator/derby/code/branches/datasource/src/java/org/apache/derby/impl/ConnectionFactory.java)
URL: http://svn.apache.org/viewcvs/incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/ConnectionFactory.java?p2=incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/ConnectionFactory.java&p1=incubator/derby/code/branches/datasource/src/java/org/apache/derby/impl/ConnectionFactory.java&r1=169495&r2=177908&rev=177908&view=diff
==============================================================================
--- incubator/derby/code/branches/datasource/src/java/org/apache/derby/impl/ConnectionFactory.java (original)
+++ incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/ConnectionFactory.java Sun May 22 19:55:06 2005
@@ -14,30 +14,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-package org.apache.derby.impl;
+package org.apache.derby.api;
import java.sql.Connection;
import java.sql.SQLException;
-import org.apache.derby.api.BasicDataSource;
-
/**
- * Base class for factories that create Connections. Different factory implementations support different JDBC
- * specification versions as determined by the JVM in use. Initial support is provided for the JDBC 2.0, JDBC 3.0 and
- * JSR-169 APIs.
- * <p/>
- * Each subclass is responsible for creating connections as specified by the properties supplied in the DataSource.
- * These may be direct connections to an embedded engine, client connections to a remote server, or other transports as
- * they become supported.
+ * Interface to be implemented by classes that are capable of established a connection to a server
+ * based in the properties specified in a Derby DataSource.
*
* @version $Rev$ $Date$
*/
-public abstract class ConnectionFactory {
- public static ConnectionFactory getInstance() {
- // return the appropriate factory for JDBC 2.0, JDBC 3.0 or JSR-169 implementations of Connection
- throw new UnsupportedOperationException();
- }
-
+public interface ConnectionFactory {
/**
* Return a physical connection to the database specified by the DataSource.
*
@@ -47,7 +35,7 @@
*
* @throws SQLException if there was a problem establishing the connection
*/
- public abstract Connection getConnection(BasicDataSource ds) throws SQLException;
+ Connection getConnection(BasicDataSource ds) throws SQLException;
/**
* Return a physical connection to the database specified by the DataSource.
@@ -60,5 +48,5 @@
*
* @throws SQLException if there was a problem establishing the connection
*/
- public abstract Connection getConnection(BasicDataSource ds, String user, String password) throws SQLException;
+ Connection getConnection(BasicDataSource ds, String user, String password) throws SQLException;
}
Modified: incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/DerbyDataSource.java
URL: http://svn.apache.org/viewcvs/incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/DerbyDataSource.java?rev=177908&r1=177907&r2=177908&view=diff
==============================================================================
--- incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/DerbyDataSource.java (original)
+++ incubator/derby/code/branches/datasource/src/java/org/apache/derby/api/DerbyDataSource.java Sun May 22 19:55:06 2005
@@ -20,8 +20,6 @@
import java.sql.SQLException;
import javax.sql.DataSource;
-import org.apache.derby.impl.ConnectionFactory;
-
/**
* Main implementation of DataSource intended for use by end user applications.
* <p/>
@@ -32,10 +30,10 @@
*/
public class DerbyDataSource extends BasicDataSource implements DataSource {
public Connection getConnection() throws SQLException {
- return ConnectionFactory.getInstance().getConnection(this);
+ return newConnectionFactory().getConnection(this);
}
public Connection getConnection(String username, String password) throws SQLException {
- return ConnectionFactory.getInstance().getConnection(this, username, password);
+ return newConnectionFactory().getConnection(this, username, password);
}
}
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=177908&r1=177907&r2=177908&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 Sun May 22 19:55:06 2005
@@ -57,7 +57,7 @@
}
public boolean jdbcCompliant() {
- throw new UnsupportedOperationException();
+ return true;
}
public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) throws SQLException {
@@ -70,7 +70,7 @@
*/
private static class DriverDataSource extends BasicDataSource {
private Connection getConnection() throws SQLException {
- throw new UnsupportedOperationException();
+ return newConnectionFactory().getConnection(this);
}
}
}
Added: 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=177908&view=auto
==============================================================================
--- incubator/derby/code/branches/datasource/src/java/org/apache/derby/impl/ClientConnectionFactory.java (added)
+++ incubator/derby/code/branches/datasource/src/java/org/apache/derby/impl/ClientConnectionFactory.java Sun May 22 19:55:06 2005
@@ -0,0 +1,63 @@
+/**
+ *
+ * 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.sql.Connection;
+import java.sql.SQLException;
+
+import javax.sql.DataSource;
+
+import org.apache.derby.api.ConnectionFactory;
+import org.apache.derby.api.BasicDataSource;
+import org.apache.derby.jdbc.ClientBaseDataSource;
+import org.apache.derby.jdbc.ClientDataSource;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class ClientConnectionFactory implements ConnectionFactory {
+ public Connection getConnection(BasicDataSource ds) throws SQLException {
+ DataSource cds = getClientDataSource(ds);
+ return cds.getConnection();
+ }
+
+ public Connection getConnection(BasicDataSource ds, String user, String password) throws SQLException {
+ DataSource cds = getClientDataSource(ds);
+ return cds.getConnection(user, password);
+ }
+
+ private DataSource getClientDataSource(BasicDataSource ds) {
+ ClientDataSource cds = new ClientDataSource();
+ cds.setServerName(ds.getServerName());
+ cds.setPortNumber(ds.getPortNumber());
+ cds.setDatabaseName(ds.getDatabaseName());
+ cds.setUser(ds.getUser());
+ cds.setPassword(ds.getPassword());
+ StringBuffer attrs = new StringBuffer();
+ if (ds.getCreateDatabase()) {
+ attrs.append(";create=true");
+ }
+ if (ds.getShutdownDatabase()) {
+ attrs.append(";shutdown=true");
+ }
+ cds.setConnectionAttributes(attrs.toString());
+
+ cds.setLoginTimeout(ds.getLoginTimeout());
+ cds.setLogWriter(ds.getLogWriter());
+ return cds;
+ }
+}
Added: 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=177908&view=auto
==============================================================================
--- incubator/derby/code/branches/datasource/src/java/org/apache/derby/impl/DefaultConnectionFactory.java (added)
+++ incubator/derby/code/branches/datasource/src/java/org/apache/derby/impl/DefaultConnectionFactory.java Sun May 22 19:55:06 2005
@@ -0,0 +1,48 @@
+/**
+ *
+ * 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.sql.Connection;
+import java.sql.SQLException;
+
+import org.apache.derby.api.ConnectionFactory;
+import org.apache.derby.api.BasicDataSource;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class DefaultConnectionFactory implements ConnectionFactory {
+ public Connection getConnection(BasicDataSource ds) throws SQLException {
+ ConnectionFactory cf;
+ if (ds.getServerName() == null) {
+ cf = new ClientConnectionFactory();
+ } else {
+ cf = new EmbeddedConnectionFactory();
+ }
+ return cf.getConnection(ds);
+ }
+
+ public Connection getConnection(BasicDataSource ds, String user, String password) throws SQLException {
+ ConnectionFactory cf;
+ if (ds.getServerName() == null) {
+ cf = new ClientConnectionFactory();
+ } else {
+ cf = new EmbeddedConnectionFactory();
+ }
+ return cf.getConnection(ds, user, password);
+ }
+}
Added: 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=177908&view=auto
==============================================================================
--- incubator/derby/code/branches/datasource/src/java/org/apache/derby/impl/EmbeddedConnectionFactory.java (added)
+++ incubator/derby/code/branches/datasource/src/java/org/apache/derby/impl/EmbeddedConnectionFactory.java Sun May 22 19:55:06 2005
@@ -0,0 +1,52 @@
+/**
+ *
+ * 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.sql.Connection;
+import java.sql.SQLException;
+
+import org.apache.derby.api.ConnectionFactory;
+import org.apache.derby.api.BasicDataSource;
+import org.apache.derby.jdbc.EmbeddedSimpleDataSource;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class EmbeddedConnectionFactory implements ConnectionFactory {
+ public Connection getConnection(BasicDataSource ds) throws SQLException {
+ EmbeddedSimpleDataSource eds = getDataSource(ds);
+ return eds.getConnection();
+ }
+
+ public Connection getConnection(BasicDataSource ds, String user, String password) throws SQLException {
+ EmbeddedSimpleDataSource eds = getDataSource(ds);
+ return eds.getConnection(user, password);
+ }
+
+ EmbeddedSimpleDataSource getDataSource(BasicDataSource ds) throws SQLException {
+ EmbeddedSimpleDataSource eds = new EmbeddedSimpleDataSource();
+ 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.setLoginTimeout(ds.getLoginTimeout());
+ eds.setLogWriter(ds.getLogWriter());
+ return eds;
+ }
+}
Added: 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=177908&view=auto
==============================================================================
--- incubator/derby/code/branches/datasource/src/test/org/apache/derby/impl/ClientConnectionFactoryTest.java (added)
+++ incubator/derby/code/branches/datasource/src/test/org/apache/derby/impl/ClientConnectionFactoryTest.java Sun May 22 19:55:06 2005
@@ -0,0 +1,59 @@
+/**
+ *
+ * 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.sql.Connection;
+import java.sql.SQLException;
+
+import junit.framework.TestSuite;
+import junit.framework.TestCase;
+
+import org.apache.derby.util.ClientTestSuite;
+import org.apache.derby.api.BasicDataSource;
+import org.apache.derby.api.DerbyDataSource;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class ClientConnectionFactoryTest extends TestCase {
+ public static TestSuite suite() {
+ return new ClientTestSuite(ClientConnectionFactoryTest.class);
+ }
+
+ private BasicDataSource ds;
+ private ClientConnectionFactory cf;
+
+ public void testConnect() throws SQLException {
+ Connection c = cf.getConnection(ds);
+ try {
+ assertEquals("testuser", c.getMetaData().getUserName());
+ } finally {
+ c.close();
+ }
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ ds = new DerbyDataSource();
+ ds.setServerName("localhost");
+ ds.setDatabaseName("testdb");
+ ds.setCreateDatabase(true);
+ ds.setUser("testuser");
+ ds.setPassword("testpassword");
+ cf = new ClientConnectionFactory();
+ }
+}
Added: 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=177908&view=auto
==============================================================================
--- incubator/derby/code/branches/datasource/src/test/org/apache/derby/impl/EmbeddedConnectionFactoryTest.java (added)
+++ incubator/derby/code/branches/datasource/src/test/org/apache/derby/impl/EmbeddedConnectionFactoryTest.java Sun May 22 19:55:06 2005
@@ -0,0 +1,123 @@
+/**
+ *
+ * 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.SQLException;
+
+import junit.framework.TestCase;
+
+import org.apache.derby.api.BasicDataSource;
+import org.apache.derby.api.DerbyDataSource;
+import org.apache.derby.jdbc.EmbeddedSimpleDataSource;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class EmbeddedConnectionFactoryTest extends TestCase {
+ private String tmpDir;
+ private EmbeddedConnectionFactory cf;
+ private BasicDataSource ds;
+
+ public void testStandardOptions() throws SQLException {
+ ds.setDatabaseName("testdb");
+ ds.setUser("user");
+ ds.setPassword("password");
+ EmbeddedSimpleDataSource eds = cf.getDataSource(ds);
+ assertEquals("testdb", eds.getDatabaseName());
+ assertEquals("user", eds.getUser());
+ assertEquals("password", eds.getPassword());
+ }
+
+ public void testDerbyOptions() throws SQLException {
+ ds.setCreateDatabase(false);
+ ds.setShutdownDatabase(false);
+ EmbeddedSimpleDataSource eds = cf.getDataSource(ds);
+ assertNull(eds.getCreateDatabase());
+ assertNull(eds.getShutdownDatabase());
+
+ ds.setCreateDatabase(true);
+ ds.setShutdownDatabase(true);
+ eds = cf.getDataSource(ds);
+ assertEquals("create", eds.getCreateDatabase());
+ assertEquals("shutdown", eds.getShutdownDatabase());
+ }
+
+ public void testCreateDatabase() throws SQLException {
+ ds.setDatabaseName("testdb");
+ ds.setCreateDatabase(true);
+
+ // connect first time, should be no warning
+ Connection c = cf.getConnection(ds);
+ assertNull(c.getWarnings());
+ c.close();
+ File dbfile = new File(tmpDir, "testdb");
+ assertTrue(dbfile.exists());
+
+ // database should now be there so expect a warning
+ c = cf.getConnection(ds);
+ assertNotNull(c.getWarnings());
+ c.close();
+
+ // shut down
+ ds.setCreateDatabase(false);
+ ds.setShutdownDatabase(true);
+ try {
+ cf.getConnection(ds);
+ fail();
+ } catch (SQLException e) {
+ // expected exception on shutdown
+ assertEquals("08006", e.getSQLState());
+ }
+ }
+
+ protected void setUp() throws Exception {
+ super.setUp();
+ tmpDir = System.getProperty("java.io.tmpdir") + "/embedTest";
+ System.setProperty("derby.system.home", tmpDir);
+
+ cf = new EmbeddedConnectionFactory();
+ ds = new DerbyDataSource();
+ }
+
+ protected void tearDown() throws Exception {
+ // shutdown server
+ ds = new DerbyDataSource();
+ ds.setShutdownDatabase(true);
+ try {
+ cf.getConnection(ds);
+ } catch (SQLException e) {
+ if (!"XJ015".equals(e.getSQLState())) {
+ throw e;
+ }
+ }
+ delete(new File(tmpDir));
+ 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();
+ }
+}
Added: 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=177908&view=auto
==============================================================================
--- incubator/derby/code/branches/datasource/src/test/org/apache/derby/util/ClientTestSuite.java (added)
+++ incubator/derby/code/branches/datasource/src/test/org/apache/derby/util/ClientTestSuite.java Sun May 22 19:55:06 2005
@@ -0,0 +1,98 @@
+/**
+ *
+ * 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.util;
+
+import java.io.File;
+import java.io.PrintWriter;
+import java.sql.SQLException;
+
+import junit.framework.TestResult;
+import junit.framework.TestSuite;
+
+import org.apache.derby.api.DerbyDataSource;
+import org.apache.derby.drda.NetworkServerControl;
+import org.apache.derby.impl.EmbeddedConnectionFactory;
+
+/**
+ * @version $Rev$ $Date$
+ */
+public class ClientTestSuite extends TestSuite {
+ private NetworkServerControl nsc;
+ private String tmpDir;
+
+ public ClientTestSuite() {
+ }
+
+ public ClientTestSuite(Class aClass, String s) {
+ super(aClass, s);
+ }
+
+ public ClientTestSuite(Class aClass) {
+ super(aClass);
+ }
+
+ public ClientTestSuite(String s) {
+ super(s);
+ }
+
+ public void run(TestResult testResult) {
+ createServer();
+ try {
+ super.run(testResult);
+ } finally {
+ shutdownServer();
+ delete(new File(tmpDir));
+ }
+ }
+
+ protected void createServer() {
+ tmpDir = System.getProperty("java.io.tmpdir") + "/netTest";
+ System.setProperty("derby.system.home", tmpDir);
+ try {
+ nsc = new NetworkServerControl();
+ nsc.start(new PrintWriter(System.out));
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ }
+
+ protected void shutdownServer() {
+ try {
+ nsc.shutdown();
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ DerbyDataSource ds = new DerbyDataSource();
+ ds.setShutdownDatabase(true);
+ try {
+ new EmbeddedConnectionFactory().getConnection(ds);
+ } catch (SQLException e) {
+ // ok
+ }
+ }
+
+ 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();
+ }
+}