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 2007/01/11 21:30:41 UTC

svn commit: r495364 - /db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/AutoloadTest.java

Author: djd
Date: Thu Jan 11 12:30:40 2007
New Revision: 495364

URL: http://svn.apache.org/viewvc?view=rev&rev=495364
Log:
DERBY-1952 (partial) Copy & refactor AutoloadTest as jdbcapi.AutoloadTest as it
can be used on non-JDBC 4 environments as long as it is not compiled using Java SE 6.
Split into a number of test cases including one to ensure the junit setup is not
autoloading the test (thus potentially hiding real issues) when autoloading is
not enabled. Test runs successfully without the old harness but not yet added
to any ant target as it needs to set the property jdbc.drivers for effective testing.
The jdbc4 version is not removed until this version is running as part of
the ant targets.

Added:
    db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/AutoloadTest.java   (with props)

Added: db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/AutoloadTest.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/AutoloadTest.java?view=auto&rev=495364
==============================================================================
--- db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/AutoloadTest.java (added)
+++ db/derby/code/trunk/java/testing/org/apache/derbyTesting/functionTests/tests/jdbcapi/AutoloadTest.java Thu Jan 11 12:30:40 2007
@@ -0,0 +1,171 @@
+/*
+
+   Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.AutoloadTest
+
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to you 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.tests.jdbcapi;
+
+import java.sql.Driver;
+import java.sql.DriverManager;
+import java.sql.SQLException;
+
+import junit.framework.Test;
+import junit.framework.TestSuite;
+
+import org.apache.derbyTesting.junit.BaseJDBCTestCase;
+import org.apache.derbyTesting.junit.JDBC;
+import org.apache.derbyTesting.junit.TestConfiguration;
+
+/**
+ * This JUnit test verifies the autoloading of the jdbc driver.
+ * A driver may be autoloaded due to JDBC 4 autoloading from
+ * jar files or the driver's name is listed in the system
+ * property jdbc.drivers when DriverManager is first loaded.
+ * 
+ * This test must be run in its own VM because we want to verify that the
+ * driver was not accidentally loaded by some other test.
+ */
+public class AutoloadTest extends BaseJDBCTestCase
+{		
+	public	AutoloadTest( String name ) { super( name ); }
+
+    
+    /**
+     * Only run a test if the driver will be auto-loaded.
+     * See class desciption for details.
+     */
+    public static Test suite() {
+        if (!JDBC.vmSupportsJDBC2())
+            return new TestSuite("empty: no java.sql.DriverManager");
+
+        if (JDBC.vmSupportsJDBC4() && TestConfiguration.loadingFromJars()) {
+            // test client & embedded
+            return TestConfiguration.defaultSuite(AutoloadTest.class);
+        }
+        // Simple test to see if the driver class is
+        // in the value. Could get fancy and see if it is
+        // correctly formatted but not worth it.
+
+        try {
+            String jdbcDrivers = getSystemProperty("jdbc.drivers");
+            if (jdbcDrivers == null)
+                jdbcDrivers = "";
+
+            boolean embedded = jdbcDrivers
+                    .indexOf("org.apache.derby.jdbc.EmbeddedDriver") != -1;
+
+            boolean clientServer = jdbcDrivers
+                    .indexOf("org.apache.derby.jdbc.ClientDriver") != -1;
+
+            if (embedded && clientServer)
+                return TestConfiguration.defaultSuite(AutoloadTest.class);
+
+            if (embedded)
+                return TestConfiguration.embeddedSuite(AutoloadTest.class);
+
+            if (clientServer)
+                return TestConfiguration.clientServerSuite(AutoloadTest.class);
+        } catch (SecurityException se) {
+            // assume there is no autoloading if
+            // we can't read the value of jdbc.drivers.
+        }
+
+        // Run a single test that ensures that the driver is
+        // not loaded implicitly by some other means.
+        TestSuite suite = new TestSuite("AutoloadTest: no autoloading expected");
+        
+        suite.addTest(new AutoloadTest("noloadTestNodriverLoaded"));
+        suite.addTest(TestConfiguration.clientServerDecorator(
+                new AutoloadTest("noloadTestNodriverLoaded")));
+        
+        return suite;
+    }
+
+	// ///////////////////////////////////////////////////////////
+	//
+	// TEST ENTRY POINTS
+	//
+	// ///////////////////////////////////////////////////////////
+    
+    /**
+     * @throws SQLException
+     * 
+     */
+    public void testRegisteredDriver() throws SQLException
+    {
+        String protocol =
+            getTestConfiguration().getJDBCClient().getUrlBase();
+        
+        System.out.println("PROTOCOL " + protocol);
+            
+        Driver driver = DriverManager.getDriver(protocol);
+        assertNotNull("Expected registered driver", driver);
+    }
+
+	/**
+     * Test we can connect successfully to a database.
+	 */
+	public void testSuccessfulConnect()
+       throws SQLException
+	{
+		println( "We ARE autoloading..." );
+       
+        // Test we can connect successfully to a database!
+        String url = getTestConfiguration().getJDBCUrl();
+        url = url.concat(";create=true");
+        String user = getTestConfiguration().getUserName();
+        String password = getTestConfiguration().getUserPassword();
+        DriverManager.getConnection(url, user, password).close();
+	}
+    /**
+     * Test the error code on an unsuccessful connect
+     * to ensure it is not one returned by DriverManager.
+     */
+    public void testUnsuccessfulConnect()
+       throws SQLException
+    {     
+        // Test we can connect successfully to a database!
+        String url = getTestConfiguration().getJDBCUrl("nonexistentDatabase");
+        String user = getTestConfiguration().getUserName();
+        String password = getTestConfiguration().getUserPassword();
+        try {
+            DriverManager.getConnection(url, user, password).close();
+            fail("connected to nonexistentDatabase");
+        } catch (SQLException e) {
+            String expectedError = usingEmbedded() ? "XJ004" : "08004";
+            
+            assertSQLState(expectedError, e);
+        }
+    }
+    
+    /**
+     * Simple test when auto-loading is not expected.
+     * This is to basically test that the junit setup code
+     * itself does not load the driver, thus defeating the
+     * real auto-loading testing.
+     */
+    public void noloadTestNodriverLoaded() {
+        try {
+            testRegisteredDriver();
+            fail("Derby junit setup code is loading driver!");
+        } catch (SQLException e) {
+        }
+    }
+}
+

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