1 21 30 31 package org.apache.derbyTesting.functionTests.tests.jdbc4; 32 33 import java.sql.*; 34 import java.util.*; 35 import junit.framework.*; 36 37 import org.apache.derbyTesting.functionTests.util.SQLStateConstants; 38 import org.apache.derbyTesting.junit.BaseJDBCTestCase; 39 import org.apache.derbyTesting.junit.JDBC; 40 import org.apache.derbyTesting.junit.TestConfiguration; 41 42 public class AutoloadTest extends BaseJDBCTestCase 43 { 44 50 private static final String NONEXISTENT_DATABASE = "nonexistentDatabase"; 51 52 58 64 public AutoloadTest( String name ) { super( name ); } 65 66 72 78 81 public static Test suite() 82 { 83 TestSuite suite = new TestSuite(); 84 85 if (JDBC.vmSupportsJDBC2() && 88 (usingEmbedded() || usingDerbyNetClient())) 89 { 90 91 boolean autoloadingCurrentDriver = false; 92 93 try { 96 String jdbcDrivers = getSystemProperty("jdbc.drivers"); 97 if (jdbcDrivers != null) 98 { 99 String driver = 103 TestConfiguration.getCurrent().getJDBCClient().getJDBCDriverName(); 104 105 if (jdbcDrivers.indexOf(driver) != -1) 106 autoloadingCurrentDriver = true; 107 } 108 109 } catch (SecurityException e) { 110 } 112 113 if (JDBC.vmSupportsJDBC4() && 116 TestConfiguration.loadingFromJars()) 117 autoloadingCurrentDriver = true; 118 119 if (autoloadingCurrentDriver) 120 suite.addTestSuite(AutoloadTest.class); 121 122 } 123 124 return suite; 125 } 126 127 133 146 public void testAutoloading() 147 throws Exception 148 { 149 151 156 println( "We ARE autoloading..." ); 157 158 String expectedError = 166 usingEmbedded() ? "XJ004" : "08004"; 167 168 failToConnect(expectedError); 169 170 String url = getTestConfiguration().getJDBCUrl(); 172 url = url.concat(";create=true"); 173 String user = getTestConfiguration().getUserName(); 174 String password = getTestConfiguration().getUserPassword(); 175 DriverManager.getConnection(url, user, password).close(); 176 177 } 178 179 185 private void failToConnect( String expectedSQLState ) 186 throws Exception 187 { 188 String connectionURL = getTestConfiguration().getJDBCUrl( NONEXISTENT_DATABASE ); 189 Properties properties = new Properties(); 190 SQLException se = null; 191 192 properties.put( "user", getTestConfiguration().getUserName() ); 193 properties.put( "password", getTestConfiguration().getUserPassword() ); 194 195 try { 196 println( "Attempting to connect with this URL: '" + connectionURL + "'" ); 197 198 DriverManager.getConnection( connectionURL, properties ); 199 200 fail("Connection succeed, expected to fail."); 201 } 202 catch ( SQLException e ) { se = e; } 203 204 println( "Caught expected SQLException: " + se ); 205 206 assertSQLState( expectedSQLState, expectedSQLState, se ); 207 } 208 209 210 } 211 212 | Popular Tags |