db-derby-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From d..@apache.org
Subject svn commit: r424388 - in /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util: BaseJDBCTestSetup.java JDBC.java
Date Fri, 21 Jul 2006 16:54:16 GMT
Author: djd
Date: Fri Jul 21 09:54:14 2006
New Revision: 424388

URL: http://svn.apache.org/viewvc?rev=424388&view=rev
Log:
DERBY-1555 DERBY-1562 (partial) Add new classes BaseJDBCTestSetup and JDBC to support
JDBC Unit test development.
BaseJDBCTestSetup is a TestSetup decorator for setup that uses JDBC.
JDBC class is a holder for JDBC test utility methods.

Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseJDBCTestSetup.java
  (with props)
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/JDBC.java
  (with props)

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseJDBCTestSetup.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseJDBCTestSetup.java?rev=424388&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseJDBCTestSetup.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseJDBCTestSetup.java
Fri Jul 21 09:54:14 2006
@@ -0,0 +1,96 @@
+/*
+ *
+ * Derby - Class org.apache.derbyTesting.functionTests.util.BaseJDBCTestSetup
+ *
+ * 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.*;
+
+import junit.extensions.TestSetup;
+import junit.framework.Test;
+
+/**
+ * Base class for JDBC JUnit test decorators.
+ */
+public abstract class BaseJDBCTestSetup
+    extends TestSetup {
+	
+	public BaseJDBCTestSetup(Test test) {
+		super(test);
+	}
+	
+	/**
+	 * Maintain a single connection to the default
+	 * database, opened at the first call to getConnection.
+	 * Typical setup will just require a single connection.
+	 * @see BaseJDBCTestSetup#getConnection()
+	 */
+	private Connection conn;
+	
+    /**
+     * Return the current configuration for the test.
+     */
+    public final TestConfiguration getTestConfiguration()
+    {
+    	return TestConfiguration.DERBY_TEST_CONFIG;
+    }
+    
+    /**
+     * Obtain the connection to the default database.
+     * This class maintains a single connection returned
+     * by this class, it is opened on the first call to
+     * this method. Subsequent calls will return the same
+     * connection object unless it has been closed. In that
+     * case a new connection object will be returned.
+     * <P>
+     * The tearDown method will close the connection if
+     * it is open.
+     * @see TestConfiguration#getDefaultConnection()
+     */
+    public final Connection getConnection() throws SQLException
+    {
+    	if (conn != null)
+    	{
+    		if (!conn.isClosed())
+    			return conn;
+    		conn = null;
+    	}
+    	return conn = getTestConfiguration().getDefaultConnection();
+    }
+    
+    /**
+     * Print debug string.
+     * @param text String to print
+     */
+    public void println(final String text) {
+        if (getTestConfiguration().isVerbose()) {
+            System.out.println("DEBUG: " + text);
+        }
+    }
+    
+    /**
+     * Tear down this fixture, sub-classes should call
+     * super.tearDown(). This cleanups & closes the connection
+     * if it is open.
+     */
+    protected void tearDown()
+    throws java.lang.Exception
+    {
+    	JDBC.cleanup(conn);
+    }
+}
\ No newline at end of file

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/BaseJDBCTestSetup.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/JDBC.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/JDBC.java?rev=424388&view=auto
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/JDBC.java
(added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/JDBC.java
Fri Jul 21 09:54:14 2006
@@ -0,0 +1,66 @@
+/*
+ *
+ * Derby - Class org.apache.derbyTesting.functionTests.util.JDBC
+ *
+ * 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.*;
+
+/**
+ * JDBC utility methods for the JUnit tests.
+ *
+ */
+public class JDBC {
+	
+	/**
+	 * Rollback and close a connection for cleanup.
+	 * Test code that is expecting Connection.close to succeed
+	 * normally should just call conn.close().
+	 * 
+	 * <P>
+	 * If conn is not-null and isClosed() returns false
+	 * then both rollback and close will be called.
+	 * If both methods throw exceptions
+	 * then they will be chained together and thrown.
+	 * @throws SQLException Error closing connection.
+	 */
+	public static void cleanup(Connection conn) throws SQLException
+	{
+		if (conn == null)
+			return;
+		if (conn.isClosed())
+			return;
+		
+		SQLException sqle = null;
+		try {
+			conn.rollback();
+		} catch (SQLException e) {
+			sqle = e;
+		}
+		
+		try {
+			conn.close();
+		} catch (SQLException e) {
+			if (sqle == null)
+			    sqle = e;
+			else
+				sqle.setNextException(e);
+			throw sqle;
+		}
+	}
+}

Propchange: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/util/JDBC.java
------------------------------------------------------------------------------
    svn:eol-style = native



Mime
View raw message