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 dj...@apache.org on 2006/07/21 18:02:28 UTC
svn commit: r424368 - in
/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util:
BaseJDBCTestCase.java TestConfiguration.java
Author: djd
Date: Fri Jul 21 09:02:27 2006
New Revision: 424368
URL: http://svn.apache.org/viewvc?rev=424368&view=rev
Log:
DERBY-1555 (partial) Move the code to obtain a default connection into TestConfiuration
for the JUnit test setup to allow it to be shared across multiple classes.
Modified:
db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseJDBCTestCase.java
db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/TestConfiguration.java
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseJDBCTestCase.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseJDBCTestCase.java?rev=424368&r1=424367&r2=424368&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseJDBCTestCase.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseJDBCTestCase.java Fri Jul 21 09:02:27 2006
@@ -33,23 +33,6 @@
extends BaseTestCase {
/**
- * Tell if we are allowed to use DriverManager to create database
- * connections.
- */
- private static final boolean HAVE_DRIVER;
-
- static {
- // See if java.sql.Driver is available. If it is not, we must use
- // DataSource to create connections.
- boolean haveDriver = false;
- try {
- Class.forName("java.sql.Driver");
- haveDriver = true;
- } catch (Exception e) {}
- HAVE_DRIVER = haveDriver;
- }
-
- /**
* Create a test case with the given name.
*
* @param name of the test case.
@@ -67,26 +50,10 @@
*/
public static Connection getConnection()
throws SQLException {
- Connection con = null;
- JDBCClient client = CONFIG.getJDBCClient();
- if (HAVE_DRIVER) {
- loadJDBCDriver(client.getJDBCDriverName());
- if (!CONFIG.isSingleLegXA()) {
- con = DriverManager.getConnection(
- CONFIG.getJDBCUrl() + ";create=true",
- CONFIG.getUserName(),
- CONFIG.getUserPassword());
- }
- else {
- con = TestDataSourceFactory.getXADataSource().getXAConnection (CONFIG.getUserName(),
- CONFIG.getUserPassword()).getConnection();
- }
- } else {
- //Use DataSource for JSR169
- con = TestDataSourceFactory.getDataSource().getConnection();
- }
- return con;
+ return CONFIG.getDefaultConnection();
}
+
+
/**
* Tell if the client is embedded.
@@ -244,28 +211,6 @@
*/
public static void assertSQLState(String expected, SQLException exception) {
assertSQLState("Unexpected SQL state.", expected, exception);
- }
-
- /**
- * Load the specified JDBC driver
- *
- * @param driverClass name of the JDBC driver class.
- * @throws SQLException if loading the driver fails.
- */
- private static void loadJDBCDriver(String driverClass)
- throws SQLException {
- 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());
- }
}
} // End class BaseJDBCTestCase
Modified: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/TestConfiguration.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/TestConfiguration.java?rev=424368&r1=424367&r2=424368&view=diff
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/TestConfiguration.java (original)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/TestConfiguration.java Fri Jul 21 09:02:27 2006
@@ -20,6 +20,9 @@
package org.apache.derbyTesting.functionTests.util;
import java.security.*;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.SQLException;
import java.util.Properties;
import org.apache.derby.iapi.services.info.JVMInfo;
@@ -32,10 +35,27 @@
/**
* Default Derby test configuration object.
*/
- public static final TestConfiguration DERBY_TEST_CONFIG =
+ static final TestConfiguration DERBY_TEST_CONFIG =
new TestConfiguration(getSystemProperties());
/**
+ * Tell if we are allowed to use DriverManager to create database
+ * connections.
+ */
+ private static final boolean HAVE_DRIVER;
+
+ static {
+ // See if java.sql.Driver is available. If it is not, we must use
+ // DataSource to create connections.
+ boolean haveDriver = false;
+ try {
+ Class.forName("java.sql.Driver");
+ haveDriver = true;
+ } catch (Exception e) {}
+ HAVE_DRIVER = haveDriver;
+ }
+
+ /**
* This constructor creates a TestConfiguration from a Properties object.
*
* @throws NumberFormatException if the port specification is not an integer.
@@ -188,6 +208,36 @@
}
/**
+ * Get connection to the default database.
+ * If the database does not exist, it will be created.
+ * A default username and password will be used for the connection.
+ *
+ * @return connection to default database.
+ */
+ public Connection getDefaultConnection()
+ throws SQLException {
+ Connection con = null;
+ JDBCClient client =getJDBCClient();
+ if (HAVE_DRIVER) {
+ loadJDBCDriver(client.getJDBCDriverName());
+ if (!isSingleLegXA()) {
+ con = DriverManager.getConnection(
+ getJDBCUrl() + ";create=true",
+ getUserName(),
+ getUserPassword());
+ }
+ else {
+ con = TestDataSourceFactory.getXADataSource().getXAConnection (getUserName(),
+ getUserPassword()).getConnection();
+ }
+ } else {
+ //Use DataSource for JSR169
+ con = TestDataSourceFactory.getDataSource().getConnection();
+ }
+ return con;
+ }
+
+ /**
* Set the verbosity, i.e., whether debug statements print.
*/
public void setVerbosity( boolean isChatty ) { isVerbose = isChatty; }
@@ -348,6 +398,28 @@
attrs.setProperty("databaseName", DERBY_TEST_CONFIG.getDatabaseName());
attrs.setProperty("connectionAttributes", "create=true");
return attrs;
+ }
+
+ /**
+ * Load the specified JDBC driver
+ *
+ * @param driverClass name of the JDBC driver class.
+ * @throws SQLException if loading the driver fails.
+ */
+ private static void loadJDBCDriver(String driverClass)
+ throws SQLException {
+ 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());
+ }
}
}