1 21 22 package org.apache.derbyTesting.functionTests.tests.jdbcapi; 23 import org.apache.derbyTesting.functionTests.util.TestUtil; 24 25 import java.io.File ; 26 import java.sql.DatabaseMetaData ; 27 import java.sql.Driver ; 28 import java.sql.DriverManager ; 29 import java.sql.Connection ; 30 import java.sql.ResultSet ; 31 import java.sql.SQLException ; 32 import java.util.Properties ; 33 import org.apache.derby.tools.JDBCDisplayUtil; 34 35 43 44 public class checkDriver { 45 46 private static String hostName; 47 private static String EMBEDDED_URL = "jdbc:derby:wombat;create=true"; 48 private static String CLIENT_URL; 49 private static String JCC_URL; 50 private static String INVALID_URL = "jdbc:db2j:wombat;create=true"; 51 52 private static String DERBY_SYSTEM_HOME = System.getProperty("derby.system.home"); 53 54 private static String CLIENT_URL_WITH_COLON1; 55 private static String CLIENT_URL_WITH_COLON2; 56 private static String CLIENT_URL_WITH_DOUBLE_QUOTES1; 57 private static String CLIENT_URL_WITH_DOUBLE_QUOTES2; 58 private static String CLIENT_URL_WITH_SINGLE_QUOTES1; 59 private static String CLIENT_URL_WITH_SINGLE_QUOTES2; 60 61 private static String DB_NAME_WITH_SPACES = "db name with spaces"; 63 private static String EMBEDDED_URL_WITH_SPACES = "jdbc:derby:" + 64 DB_NAME_WITH_SPACES + ";create=true"; 65 private static String CLIENT_URL_WITH_SPACES; 66 private static String JCC_URL_WITH_SPACES; 67 68 71 private static String frameworkPrefix; 72 73 private static int frameworkOffset; 76 77 private static int EMBEDDED_OFFSET = 0; 78 private static int DERBYNETCLIENT_OFFSET = 1; 79 private static int DERBYNET_OFFSET = 2; 81 static { 82 frameworkPrefix = TestUtil.getJdbcUrlPrefix(); 83 if (TestUtil.isEmbeddedFramework()) 84 frameworkOffset = EMBEDDED_OFFSET; 85 else if (TestUtil.isDerbyNetClientFramework()) 86 frameworkOffset = DERBYNETCLIENT_OFFSET; 87 else if (TestUtil.isJCCFramework()) 88 frameworkOffset = DERBYNET_OFFSET; 90 hostName = TestUtil.getHostName(); 91 CLIENT_URL = "jdbc:derby://" + hostName + ":1527/wombat;create=true"; 92 JCC_URL = "jdbc:derby:net://" + hostName + ":1527/wombat;create=true"; 93 CLIENT_URL_WITH_COLON1 = "jdbc:derby://" + hostName + ":1527/wombat:create=true"; 94 CLIENT_URL_WITH_COLON2 = "jdbc:derby://" + hostName + ":1527/"+ DERBY_SYSTEM_HOME + File.separator +"wombat:create=true"; 95 CLIENT_URL_WITH_DOUBLE_QUOTES1 = "jdbc:derby://" + hostName + ":1527/\"wombat\";create=true"; 96 CLIENT_URL_WITH_DOUBLE_QUOTES2 = "jdbc:derby://" + hostName + ":1527/\"" + DERBY_SYSTEM_HOME + File.separator + "wombat\";create=true"; 97 CLIENT_URL_WITH_SINGLE_QUOTES1 = "jdbc:derby://" + hostName + ":1527/'" + DERBY_SYSTEM_HOME + File.separator + "wombat';create=true"; 98 CLIENT_URL_WITH_SINGLE_QUOTES2 = "jdbc:derby://" + hostName + ":1527/'wombat';create=true"; 99 100 CLIENT_URL_WITH_SPACES = "jdbc:derby://" + hostName + ":1527/" + DB_NAME_WITH_SPACES + ";create=true"; 101 JCC_URL_WITH_SPACES = "jdbc:derby:net://" + hostName + ":1527/" + DB_NAME_WITH_SPACES + ";create=true"; 102 } 103 104 private static String [] urls = new String [] 106 { 107 EMBEDDED_URL, 108 CLIENT_URL, 109 JCC_URL, 110 INVALID_URL, 111 }; 112 113 private static String [] clientUrls = new String [] 115 { 116 CLIENT_URL_WITH_COLON1, 117 CLIENT_URL_WITH_SINGLE_QUOTES2 122 }; 123 124 125 private static boolean[][] acceptsURLTable = new boolean[][] 128 { 129 { true , false , false }, 131 { false , true , false }, 132 { false , false , true }, 133 { false , false , false } 134 }; 135 136 137 138 public static void main(String [] args) { 139 140 try { 141 Driver driver = loadAndCheckDriverForFramework(); 142 checkAcceptsURL(driver); 143 testEmbeddedAttributes(driver); 144 testClientAttributes(driver); 145 doClientURLTest(driver); 146 testDbNameWithSpaces(driver); 147 } 148 catch (SQLException se) 149 { 150 while (se != null) 151 { 152 se.printStackTrace(System.out); 153 se = se.getNextException(); 154 } 155 } 156 catch (Throwable e) 157 { 158 e.printStackTrace(System.out); 159 } 160 161 } 162 163 164 172 private static void testClientAttributes(Driver driver) throws SQLException 173 { 174 if (!TestUtil.isDerbyNetClientFramework()) 175 return; 176 177 System.out.println("\ntestClientAttributes()"); 178 Properties info = new Properties (); 179 180 String traceDirectory = DERBY_SYSTEM_HOME 185 + File.separator; 186 String traceFile= traceDirectory + "trace.out"; 187 188 testConnect(driver, frameworkPrefix + "testpropdb;traceFile=" + 190 traceFile,info); 191 assertTraceFileExists(traceFile); 192 193 traceFile = traceDirectory + "trace2.out"; 194 195 info.setProperty("traceFile",traceFile); 197 testConnect(driver, frameworkPrefix + "testpropdb",info); 198 assertTraceFileExists(traceFile); 199 200 } 201 202 203 204 209 private static void assertTraceFileExists(String filename) 210 { 211 File traceFile = new File (filename); 212 boolean exists = traceFile.exists(); 215 if (! exists) 216 new Exception ("FAILED trace file: " + filename + " does not exist").printStackTrace(System.out); 217 else 218 System.out.println(" trace file exists"); 219 220 } 221 222 223 230 private static void testEmbeddedAttributes(Driver driver) throws SQLException 231 { 232 if (TestUtil.isJCCFramework()) 235 return; 236 237 System.out.println("\ntestEmbeddedAttributes()"); 238 Properties info = new Properties (); 239 info.setProperty("create","true"); 241 testConnect(driver, frameworkPrefix + "testcreatedb1", info); 242 243 testConnect(driver, frameworkPrefix + "testcreatedb2;create=true", null); 245 246 info.clear(); 249 info.setProperty("user","APP"); 250 info.setProperty("password", "xxxx"); 251 testConnect(driver, frameworkPrefix + "testpropdb", info); 252 253 testConnect(driver, frameworkPrefix + "testpropdb;user=testuser;password=testpass", null); 255 256 info.clear(); 258 info.setProperty("password","testpass"); 259 testConnect(driver,frameworkPrefix + "testpropdb;user=testusr",info); 260 261 info.clear(); 263 info.setProperty("user","APP"); 264 info.setProperty("password","xxxx"); 265 testConnect(driver, frameworkPrefix + "testpropdb;user=testuser;password=testpass", null); 266 267 info.clear(); 269 info.setProperty("shutdown","true"); 270 try { 271 testConnect(driver,frameworkPrefix + "testcreatedb1", info); 272 } catch (SQLException se) 273 { 274 System.out.println("Expected Exception:" + se.getSQLState() + ":" + se.getMessage()); 275 } 276 } 277 278 279 286 private static void checkAcceptsURL(Driver driver) throws SQLException { 287 for (int u = 0; u < urls.length;u++) 288 { 289 String url = urls[u]; 290 boolean expectedAcceptance = acceptsURLTable[u][frameworkOffset]; 292 boolean actualAcceptance = driver.acceptsURL(url); 293 System.out.println("checking acceptsURL(" + url + ")" ); 294 assertExpectedURLAcceptance(url, expectedAcceptance, actualAcceptance); 295 296 } 297 298 } 299 300 301 306 private static Driver loadAndCheckDriverForFramework() throws Exception 307 { 308 TestUtil.loadDriver(); 309 String frameworkURL = TestUtil.getJdbcUrlPrefix() + "testpropdb;create=true"; 310 311 Driver driver = DriverManager.getDriver(frameworkURL); 313 Properties props = new Properties (); 314 props.put("user","testuser"); 315 props.put("password","testpass"); 316 Connection conn = DriverManager.getConnection(frameworkURL, props); 317 DatabaseMetaData dbmd = conn.getMetaData(); 318 System.out.println("jdbcCompliant() = " + driver.jdbcCompliant()); 319 320 323 int majorVersion = driver.getMajorVersion(); 324 if (majorVersion == dbmd.getDriverMajorVersion()) 325 System.out.println("driver.getMajorVersion() = EXPECTED VERSION"); 326 else 327 new Exception ("FAILED: unexpected value for getMajorVersion(): " + 328 majorVersion).printStackTrace(); 329 330 int minorVersion = driver.getMinorVersion(); 331 if (minorVersion == dbmd.getDriverMinorVersion()) 332 System.out.println("driver.getMinorVersion() = EXPECTED VERSION"); 333 else 334 new Exception ("FAILED: unexpected value for getMinorVersion()" + 335 minorVersion).printStackTrace(System.out); 336 337 conn.close(); 338 return driver; 339 } 340 341 342 343 344 353 private static void assertExpectedURLAcceptance(String url, boolean expectedAcceptance, 354 boolean actualAcceptance) 355 { 356 if (actualAcceptance != expectedAcceptance) 357 { 358 new Exception ("FAILED acceptsURL check. url = " + url + 359 " expectedAcceptance = " + expectedAcceptance + 360 " actualAcceptance = " + actualAcceptance).printStackTrace(System.out); 361 } 362 363 } 364 365 371 private static void doClientURLTest(Driver driver){ 372 if (!TestUtil.isDerbyNetClientFramework()) 373 return; 374 375 System.out.println("doClientURLTest()"); 376 Properties info = null; 378 for (int i = 0; i < clientUrls.length;i++) 379 { 380 String url = clientUrls[i]; 381 System.out.println("doClientURLTest with url: " + replaceSystemHome(url)); 382 try{ 383 Connection conn = testConnect(driver,url,info); 384 if(conn != null) 385 System.out.println("PASSED:Connection Successful with url: " + replaceSystemHome(url) ); 386 } 387 catch(SQLException se){ 388 System.out.println("EXPECTED EXCEPTION:"+replaceSystemHome(se.getMessage())); 389 } 390 } 391 } 392 393 411 private static void testDbNameWithSpaces(Driver driver) throws SQLException { 412 System.out.println("START testDbNameWithSpaces ..."); 413 414 Connection conn = null; 415 Properties info = null; 416 String url = null; 417 418 if(TestUtil.isEmbeddedFramework()) 419 url = EMBEDDED_URL_WITH_SPACES; 420 else if(TestUtil.isDerbyNetClientFramework()) 421 url = CLIENT_URL_WITH_SPACES; 422 else if(TestUtil.isJCCFramework()) { 423 url = JCC_URL_WITH_SPACES; 424 info = new Properties (); 426 info.put("user", "tester"); 427 info.put("password", "testpass"); 428 } 429 430 conn = testConnect(driver, url, info); 431 if(conn != null) 432 System.out.println("PASSED:Connection Successful with url: " + url ); 433 } 434 435 445 private static Connection testConnect(Driver driver, String url, Properties info) throws SQLException 446 { 447 String infoString = null; 448 if (info != null) 449 infoString = replaceSystemHome(info.toString()); 450 String urlString = replaceSystemHome(url); 451 Connection conn = driver.connect(url,info); 452 453 if(conn == null){ 454 System.out.println("Null connection returned for url "+urlString); 455 return conn; 456 } 457 458 System.out.println("\nConnection info for connect(" + urlString + ", " + infoString +")"); 459 String getUrlValue = conn.getMetaData().getURL(); 460 getUrlValue = replaceSystemHome(getUrlValue); 463 System.out.println("getURL() = " + getUrlValue); 464 System.out.println("getUserName() = " + conn.getMetaData().getUserName()); 465 ResultSet rs = conn.createStatement().executeQuery("VALUES(CURRENT SCHEMA)"); 467 rs.next(); 468 System.out.println("CURRENT SCHEMA = " + rs.getString(1)); 469 conn.close(); 470 return conn; 471 } 472 473 474 479 private static String replaceSystemHome(String origString) { 480 String replaceString = DERBY_SYSTEM_HOME + File.separator; 481 int offset = origString.indexOf(replaceString); 482 if (offset == -1) 483 return origString; 484 else 485 return origString.substring(0,offset) + "[DERBY_SYSTEM_HOME]/"+ 486 origString.substring(offset + replaceString.length()); 487 } 488 489 } | Popular Tags |