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 da...@apache.org on 2006/03/17 00:24:59 UTC
svn commit: r386483 - in
/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util:
BaseJDBCTestCase.java BaseTestCase.java JDBCClient.java
TestConfiguration.java
Author: davidvc
Date: Thu Mar 16 15:24:56 2006
New Revision: 386483
URL: http://svn.apache.org/viewcvs?rev=386483&view=rev
Log:
DERBY-919 : Add support for base classes for JUnit tests. Contributed
by Kristian Waagan.
Added:
db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseJDBCTestCase.java (with props)
db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseTestCase.java (with props)
db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/JDBCClient.java (with props)
db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/TestConfiguration.java (with props)
Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseJDBCTestCase.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseJDBCTestCase.java?rev=386483&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseJDBCTestCase.java (added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseJDBCTestCase.java Thu Mar 16 15:24:56 2006
@@ -0,0 +1,136 @@
+/*
+ *
+ * Derby - Class BaseJDBCTestCase
+ *
+ * Copyright 2006 The Apache Software Foundation or its
+ * licensors, as applicable.
+ *
+ * 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.derbyTesting.functionTests.util;
+
+import java.sql.*;
+
+/**
+ * Base class for JDBC JUnit tests.
+ * A method for getting a default connection is provided, along with methods
+ * for telling if a specific JDBC client is used.
+ */
+public class BaseJDBCTestCase
+ 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.
+ */
+ public BaseJDBCTestCase(String name) {
+ super(name);
+ }
+
+ /**
+ * 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 static Connection getConnection()
+ throws SQLException {
+ Connection con = null;
+ JDBCClient client = CONFIG.getJDBCClient();
+ if (HAVE_DRIVER) {
+ loadJDBCDriver(client.getJDBCDriverName());
+ con = DriverManager.getConnection(
+ CONFIG.getJDBCUrl() + ";create=true",
+ CONFIG.getUserName(),
+ CONFIG.getUserPassword());
+ } else {
+ throw new UnsupportedOperationException(
+ "Creating a connection in a JSR-169 " +
+ "environment is not yet supported. " +
+ "Please implement :)");
+ }
+ return con;
+ }
+
+ /**
+ * Tell if the client is embedded.
+ *
+ * @return <code>true</code> if using the embedded client
+ * <code>false</code> otherwise.
+ */
+ public static boolean usingEmbedded() {
+ return (CONFIG.getJDBCClient() == JDBCClient.EMBEDDED);
+ }
+
+ /**
+ * Tell if the client is DerbyNetClient.
+ *
+ * @return <code>true</code> if using the DerbyNetClient client
+ * <code>false</code> otherwise.
+ */
+ public static boolean usingDerbyNetClient() {
+ return (CONFIG.getJDBCClient() == JDBCClient.DERBYNETCLIENT);
+ }
+
+ /**
+ * Tell if the client is DerbyNet.
+ *
+ * @return <code>true</code> if using the DerbyNet client
+ * <code>false</code> otherwise.
+ */
+ public static boolean usingDerbyNet() {
+ return (CONFIG.getJDBCClient() == JDBCClient.DERBYNET);
+ }
+
+ /**
+ * 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());
+ }
+ }
+
+} // Enc class BaseJDBCTestCase
Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseJDBCTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseTestCase.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseTestCase.java?rev=386483&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseTestCase.java (added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseTestCase.java Thu Mar 16 15:24:56 2006
@@ -0,0 +1,57 @@
+/*
+ *
+ * Derby - Class BaseTestCase
+ *
+ * Copyright 2006 The Apache Software Foundation or its
+ * licensors, as applicable.
+ *
+ * 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.derbyTesting.functionTests.util;
+
+import junit.framework.TestCase;
+
+/**
+ * Base class for JUnit tests.
+ */
+public class BaseTestCase
+ extends TestCase {
+
+ /**
+ * Configuration for the test case.
+ * The configuration is created based on system properties.
+ *
+ * @see TestConfiguration
+ */
+ public static final TestConfiguration CONFIG =
+ TestConfiguration.DERBY_TEST_CONFIG;
+
+ /**
+ * No argument constructor made private to enforce naming of test cases.
+ * According to JUnit documentation, this constructor is provided for
+ * serialization, which we don't currently use.
+ *
+ * @see #BaseTestCase(String)
+ */
+ private BaseTestCase() {}
+
+ /**
+ * Create a test case with the given name.
+ *
+ * @param name name of the test case.
+ */
+ public BaseTestCase(String name) {
+ super(name);
+ }
+
+} // End class BaseTestCase
Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseTestCase.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/JDBCClient.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/JDBCClient.java?rev=386483&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/JDBCClient.java (added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/JDBCClient.java Thu Mar 16 15:24:56 2006
@@ -0,0 +1,110 @@
+/*
+ *
+ * Derby - Class JDBCClient
+ *
+ * Copyright 2006 The Apache Software Foundation or its
+ * licensors, as applicable.
+ *
+ * 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.derbyTesting.functionTests.util;
+
+/**
+ * Type-safe enumerator of valid JDBC clients.
+ * Each JDBC client definition consists of the client name, the name of the
+ * JDBC driver class, the name of a DataSource class and the base JDBC url.
+ */
+public final class JDBCClient {
+
+ /**
+ * The embedded JDBC client.
+ */
+ public static JDBCClient EMBEDDED = new JDBCClient(
+ "Embedded",
+ "org.apache.derby.jdbc.EmbeddedDriver",
+ "org.apache.derby.jdbc.EmbeddedDataSource",
+ "jdbc:derby:");
+
+ /**
+ * The Derby network client.
+ */
+ public static JDBCClient DERBYNETCLIENT= new JDBCClient(
+ "DerbyNetClient",
+ "org.apache.derby.jdbc.ClientDriver",
+ "org.apache.derby.jdbc.ClientDataSource",
+ "jdbc:derby://");
+
+ /**
+ * The JCC network client (the "old net" client for Derby).
+ */
+ public static JDBCClient DERBYNET= new JDBCClient(
+ "DerbyNet",
+ "com.ibm.db2.jcc.DB2Driver",
+ null,
+ "jdbc:derby:net://");
+
+ /**
+ * Get JDBC driver class name.
+ *
+ * @return class name for JDBC driver.
+ */
+ public String getJDBCDriverName() {
+ return driverClassName;
+ }
+
+ /**
+ * Get DataSource class name.
+ *
+ * @return class name for DataSource implementation.
+ */
+ public String getDataSourceClassName() {
+ return dsClassName;
+ }
+
+ /**
+ * Return the base JDBC url.
+ * The JDBC base url specifies the protocol and possibly the subprotcol
+ * in the JDBC connection string.
+ *
+ * @return JDBC base url.
+ */
+ public String getUrlBase() {
+ return urlBase;
+ }
+
+ /**
+ * Return string representation of this object.
+ *
+ * @return string representation of this object.
+ */
+ public String toString() {
+ return frameWork;
+ }
+
+ /**
+ * Create a JDBC client definition.
+ */
+ private JDBCClient(String frameWork, String driverClassName,
+ String dataSourceClassName, String urlBase) {
+ this.frameWork = frameWork;
+ this.driverClassName = driverClassName;
+ this.dsClassName = dataSourceClassName;
+ this.urlBase = urlBase;
+ }
+
+ private final String frameWork;
+ private final String driverClassName;
+ private final String dsClassName;
+ private final String urlBase;
+
+}
Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/JDBCClient.java
------------------------------------------------------------------------------
svn:eol-style = native
Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/TestConfiguration.java
URL: http://svn.apache.org/viewcvs/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/TestConfiguration.java?rev=386483&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/TestConfiguration.java (added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/TestConfiguration.java Thu Mar 16 15:24:56 2006
@@ -0,0 +1,211 @@
+/*
+ *
+ * Derby - Class TestConfiguration
+ *
+ * Copyright 2006 The Apache Software Foundation or its
+ * licensors, as applicable.
+ *
+ * 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.derbyTesting.functionTests.util;
+
+import java.security.AccessController;
+import java.security.PrivilegedAction;
+import java.util.Properties;
+
+/**
+ * Class which holds information about the configuration of a Test.
+ */
+public class TestConfiguration {
+
+ /**
+ * Default Derby test configuration object.
+ */
+ public static final TestConfiguration DERBY_TEST_CONFIG =
+ new TestConfiguration(getSystemProperties());
+
+ /**
+ * This constructor creates a TestConfiguration from a Properties object.
+ *
+ * @throws NumberFormatException if the port specification is not an integer.
+ */
+ private TestConfiguration(Properties props)
+ throws NumberFormatException {
+ dbName = props.getProperty(KEY_DBNAME, DEFAULT_DBNAME);
+ userName = props.getProperty(KEY_USER_NAME, DEFAULT_USER_NAME);
+ userPassword = props.getProperty(KEY_USER_PASSWORD,
+ DEFAULT_USER_PASSWORD);
+ hostName = props.getProperty(KEY_HOSTNAME, DEFAULT_HOSTNAME);
+ isVerbose = Boolean.valueOf(props.getProperty(KEY_VERBOSE)).booleanValue();
+ String portStr = props.getProperty(KEY_PORT);
+ if (portStr != null) {
+ try {
+ port = Integer.parseInt(portStr);
+ } catch (NumberFormatException nfe) {
+ // We lose stacktrace here, but it is not important.
+ throw new NumberFormatException(
+ "Port number must be an integer. Value: " + portStr);
+ }
+ } else {
+ port = DEFAULT_PORT;
+ }
+
+ String framework = props.getProperty(KEY_FRAMEWORK, DEFAULT_FRAMEWORK);
+
+ if ("DerbyNetClient".equals(framework)) {
+ jdbcClient = JDBCClient.DERBYNETCLIENT;
+ } else if ("DerbyNet".equals(framework)) {
+ jdbcClient = JDBCClient.DERBYNET;
+ } else {
+ jdbcClient = JDBCClient.EMBEDDED;
+ }
+ url = createJDBCUrlWithDatabaseName(dbName);
+ }
+
+ /**
+ * Get the system properties in a privileged block.
+ *
+ * @return the system properties.
+ */
+ private static final Properties getSystemProperties() {
+ // Fetch system properties in a privileged block.
+ Properties sysProps = (Properties)AccessController.doPrivileged(
+ new PrivilegedAction() {
+ public Object run() {
+ return System.getProperties();
+ }
+ });
+ return sysProps;
+ }
+
+ /**
+ * Create JDBC connection url, including the name of the database.
+ *
+ * @return JDBC connection url, without attributes.
+ */
+ private String createJDBCUrlWithDatabaseName(String name) {
+ if (jdbcClient == JDBCClient.EMBEDDED) {
+ return jdbcClient.getUrlBase() + dbName;
+ } else {
+ return jdbcClient.getUrlBase() + hostName + ":" + port + "/" + name;
+ }
+ }
+
+ /**
+ * Get configured JDBCClient object.
+ *
+ * @return JDBCClient
+ */
+ public JDBCClient getJDBCClient() {
+ return jdbcClient;
+ }
+
+
+ /**
+ * Return the jdbc url for connecting to the default database.
+ *
+ * @return JDBC url.
+ */
+ public String getJDBCUrl() {
+ return url;
+ }
+
+ /**
+ * Return the jdbc url for a connecting to the database.
+ *
+ * @param databaseName name of database.
+ * @return JDBC connection url, including database name.
+ */
+ public String getJDBCUrl(String databaseName) {
+ return createJDBCUrlWithDatabaseName(databaseName);
+ }
+
+ /**
+ * Return the default database name.
+ *
+ * @return default database name.
+ */
+ public String getDatabaseName() {
+ return dbName;
+ }
+
+ /**
+ * Return the user name.
+ *
+ * @return user name.
+ */
+ public String getUserName() {
+ return userName;
+ }
+
+ /**
+ * Return the user password.
+ *
+ * @return user password.
+ */
+ public String getUserPassword() {
+ return userPassword;
+ }
+
+ /**
+ * Get port number for network server.
+ *
+ * @return port number.
+ */
+ public int getPort() {
+ return port;
+ }
+
+ /**
+ * Return verbose flag.
+ *
+ * @return verbose flag.
+ */
+ public boolean isVerbose() {
+ return isVerbose;
+ }
+
+ /**
+ * Immutable data members in test configuration
+ */
+ private final String dbName;
+ private final String url;
+ private final String userName;
+ private final String userPassword;
+ private final int port;
+ private final String hostName;
+ private final JDBCClient jdbcClient;
+ private final boolean isVerbose;
+
+ /**
+ * Default values for configurations
+ */
+ private final static String DEFAULT_DBNAME = "wombat";
+ private final static String DEFAULT_USER_NAME = "APP";
+ private final static String DEFAULT_USER_PASSWORD = "APP";
+ private final static int DEFAULT_PORT = 1527;
+ private final static String DEFAULT_FRAMEWORK = "embedded";
+ private final static String DEFAULT_HOSTNAME = "localhost";
+
+ /**
+ * Keys to use to look up values in properties files.
+ */
+ private final static String KEY_DBNAME = "databaseName";
+ private final static String KEY_FRAMEWORK = "framework";
+ private final static String KEY_USER_PASSWORD = "password";
+ private final static String KEY_USER_NAME = "user";
+ private final static String KEY_HOSTNAME = "hostName";
+ private final static String KEY_PORT = "port";
+ private final static String KEY_VERBOSE = "derby.tests.debug";
+
+}
Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/TestConfiguration.java
------------------------------------------------------------------------------
svn:eol-style = native