001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.jdbc4.AutoloadTest
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to you under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021: /**
022: * <p>
023: * This JUnit test verifies the autoloading of the jdbc driver under JDBC4.
024: * This test must be run in its own VM because we want to verify that the
025: * driver was not accidentally loaded by some other test.
026: * </p>
027: *
028: * @author Rick
029: */package org.apache.derbyTesting.functionTests.tests.jdbc4;
030:
031: import java.sql.*;
032: import java.util.*;
033: import junit.framework.*;
034:
035: import org.apache.derbyTesting.functionTests.util.SQLStateConstants;
036: import org.apache.derbyTesting.junit.BaseJDBCTestCase;
037: import org.apache.derbyTesting.junit.JDBC;
038: import org.apache.derbyTesting.junit.TestConfiguration;
039:
040: public class AutoloadTest extends BaseJDBCTestCase {
041: /////////////////////////////////////////////////////////////
042: //
043: // CONSTANTS
044: //
045: /////////////////////////////////////////////////////////////
046:
047: private static final String NONEXISTENT_DATABASE = "nonexistentDatabase";
048:
049: /////////////////////////////////////////////////////////////
050: //
051: // STATE
052: //
053: /////////////////////////////////////////////////////////////
054:
055: /////////////////////////////////////////////////////////////
056: //
057: // CONSTRUCTOR
058: //
059: /////////////////////////////////////////////////////////////
060:
061: public AutoloadTest(String name) {
062: super (name);
063: }
064:
065: /////////////////////////////////////////////////////////////
066: //
067: // ENTRY POINT
068: //
069: /////////////////////////////////////////////////////////////
070:
071: /////////////////////////////////////////////////////////////
072: //
073: // JUnit BEHAVIOR
074: //
075: /////////////////////////////////////////////////////////////
076:
077: /**
078: * Only run a test if the driver will be auto-loaded.
079: */
080: public static Test suite() {
081: TestSuite suite = new TestSuite();
082:
083: // need DriverManager at least and Derby drivers
084: // no interest in testing DB2's client.
085: if (JDBC.vmSupportsJDBC2()
086: && (usingEmbedded() || usingDerbyNetClient())) {
087:
088: boolean autoloadingCurrentDriver = false;
089:
090: // Autoloading if the current driver is defined in the
091: // system property jdbc.drivers, see java.sql.DriverManager.
092: try {
093: String jdbcDrivers = getSystemProperty("jdbc.drivers");
094: if (jdbcDrivers != null) {
095: // Simple test to see if the driver class is
096: // in the value. Could get fancy and see if it is
097: // correctly formatted but not worth it.
098: String driver = TestConfiguration.getCurrent()
099: .getJDBCClient().getJDBCDriverName();
100:
101: if (jdbcDrivers.indexOf(driver) != -1)
102: autoloadingCurrentDriver = true;
103: }
104:
105: } catch (SecurityException e) {
106: // can't read property, assume not autoloading.
107: }
108:
109: // Also auto loading if this is JDBC 4 and loading from the
110: // jar files, due to the required manifest entry.
111: if (JDBC.vmSupportsJDBC4()
112: && TestConfiguration.loadingFromJars())
113: autoloadingCurrentDriver = true;
114:
115: if (autoloadingCurrentDriver)
116: suite.addTestSuite(AutoloadTest.class);
117:
118: }
119:
120: return suite;
121: }
122:
123: /////////////////////////////////////////////////////////////
124: //
125: // TEST ENTRY POINTS
126: //
127: /////////////////////////////////////////////////////////////
128:
129: /**
130: * <p>
131: * Tests the autoloading of the client driver by JDBC 4. This behavior
132: * is described in section 10.2.1 of the JDBC 4 spec. The driver is
133: * autoloaded if we are running under jdk1.6 or later and one of the
134: * following is true:
135: * </p>
136: *
137: * <ul>
138: * <li>Classes are being loaded out of the Derby jar files.</li>
139: * <li>OR the system property jdbc.drivers names the drivers.</li>
140: * </ul>
141: */
142: public void testAutoloading() throws Exception {
143: //CONFIG.setVerbosity( true );
144:
145: //
146: // We expect that the connection to the database will fail for
147: // one reason or another.
148: //
149:
150: println("We ARE autoloading...");
151:
152: //
153: // The DriverManager should have autoloaded the client driver.
154: // This means that the connection request is passed on to the
155: // server. The server then determines that the database does
156: // not exist. This raises a different error depending on whether
157: // we're running embedded or with the Derby client.
158: //
159: String expectedError = usingEmbedded() ? "XJ004" : "08004";
160:
161: failToConnect(expectedError);
162:
163: // Test we can connect successfully to a database!
164: String url = getTestConfiguration().getJDBCUrl();
165: url = url.concat(";create=true");
166: String user = getTestConfiguration().getUserName();
167: String password = getTestConfiguration().getUserPassword();
168: DriverManager.getConnection(url, user, password).close();
169:
170: }
171:
172: /**
173: * <p>
174: * Verify that we fail to connect to the database for the expected
175: * reason.
176: * </p>
177: */
178: private void failToConnect(String expectedSQLState)
179: throws Exception {
180: String connectionURL = getTestConfiguration().getJDBCUrl(
181: NONEXISTENT_DATABASE);
182: Properties properties = new Properties();
183: SQLException se = null;
184:
185: properties.put("user", getTestConfiguration().getUserName());
186: properties.put("password", getTestConfiguration()
187: .getUserPassword());
188:
189: try {
190: println("Attempting to connect with this URL: '"
191: + connectionURL + "'");
192:
193: DriverManager.getConnection(connectionURL, properties);
194:
195: fail("Connection succeed, expected to fail.");
196: } catch (SQLException e) {
197: se = e;
198: }
199:
200: println("Caught expected SQLException: " + se);
201:
202: assertSQLState(expectedSQLState, expectedSQLState, se);
203: }
204:
205: }
|