1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package org.apache.harmony.sql.tests.java.sql; 19 20 import dalvik.annotation.KnownFailure; 21 import dalvik.annotation.TestTargetClass; 22 import dalvik.annotation.TestLevel; 23 import dalvik.annotation.TestTargetNew; 24 25 import java.io.ByteArrayOutputStream; 26 import java.io.PrintStream; 27 import java.io.PrintWriter; 28 import java.lang.reflect.Method; 29 import java.security.Permission; 30 import java.sql.Connection; 31 import java.sql.Driver; 32 import java.sql.DriverManager; 33 import java.sql.DriverPropertyInfo; 34 import java.sql.SQLException; 35 import java.sql.SQLPermission; 36 import java.util.Enumeration; 37 import java.util.Properties; 38 39 import junit.framework.TestCase; 40 import static tests.support.Support_Exec.javaProcessBuilder; 41 import static tests.support.Support_Exec.execAndGetOutput; 42 43 @TestTargetClass(DriverManager.class) 44 /** 45 * JUnit Testcase for the java.sql.DriverManager class 46 * 47 */ 48 public class DriverManagerTest extends TestCase { 49 50 // Set of driver names to use 51 static final String DRIVER1 = "org.apache.harmony.sql.tests.java.sql.TestHelper_Driver1"; 52 53 static final String DRIVER2 = "org.apache.harmony.sql.tests.java.sql.TestHelper_Driver2"; 54 55 static final String DRIVER3 = "org.apache.harmony.sql.tests.java.sql.TestHelper_Driver3"; 56 57 static final String DRIVER4 = "org.apache.harmony.sql.tests.java.sql.TestHelper_Driver4"; 58 59 static final String DRIVER5 = "org.apache.harmony.sql.tests.java.sql.TestHelper_Driver5"; 60 61 static final String INVALIDDRIVER1 = "abc.klm.Foo"; 62 63 static String[] driverNames = { DRIVER1, DRIVER2 }; 64 65 static int numberLoaded; 66 67 static String baseURL1 = "jdbc:mikes1"; 68 69 static String baseURL4 = "jdbc:mikes4"; 70 71 static final String JDBC_PROPERTY = "jdbc.drivers"; 72 73 static TestHelper_ClassLoader testClassLoader = new TestHelper_ClassLoader(); 74 75 // Static initializer to load the drivers so that they are available to all 76 // the 77 // test methods as needed. 78 @Override 79 public void setUp() { 80 numberLoaded = loadDrivers(); 81 } // end setUp() 82 83 /** 84 * Test for the method DriverManager.deregisterDriver 85 * @throws SQLException 86 */ 87 @TestTargetNew( 88 level = TestLevel.SUFFICIENT, 89 notes = "SQLException checking missed: not feasible.", 90 method = "deregisterDriver", 91 args = {java.sql.Driver.class} 92 ) 93 @KnownFailure("Not all Drivers are loaded in testsetup. Classloader issue in DriverManager.") 94 public void testDeregisterDriver() throws Exception { 95 // First get one of the drivers loaded by the test 96 Driver aDriver; 97 aDriver = DriverManager.getDriver(baseURL4); 98 99 // Deregister this driver 100 DriverManager.deregisterDriver(aDriver); 101 102 assertFalse("testDeregisterDriver: Driver was not deregistered.", 103 isDriverLoaded(aDriver)); 104 105 // Re-register this driver (so subsequent tests have it available) 106 DriverManager.registerDriver(aDriver); 107 assertTrue("testDeregisterDriver: Driver did not reload.", 108 isDriverLoaded(aDriver)); 109 110 // Test deregistering a null driver 111 DriverManager.deregisterDriver(null); 112 113 // Test deregistering a driver which was not loaded by this test's 114 // classloader 115 // TODO - need to load a driver with a different classloader!! 116 aDriver = DriverManager.getDriver(baseURL1); 117 118 try { 119 Class<?> driverClass = Class 120 .forName( 121 "org.apache.harmony.sql.tests.java.sql.TestHelper_DriverManager", 122 true, testClassLoader); 123 124 // Give the Helper class one of our drivers.... 125 Class<?>[] methodClasses = {Class.forName("java.sql.Driver")}; 126 Method theMethod = driverClass.getDeclaredMethod("setDriver", 127 methodClasses); 128 Object[] args = {aDriver}; 129 assertNotNull(args); 130 theMethod.invoke(null, args); 131 } catch (Exception e) { 132 fail("testDeregisterDriver: Got exception allocating TestHelper: " 133 + e.getMessage()); 134 e.printStackTrace(); 135 return; 136 } // end try 137 138 // Check that the driver was not deregistered 139 assertTrue( 140 "testDeregisterDriver: Driver was incorrectly deregistered.", 141 DriverManagerTest.isDriverLoaded(aDriver)); 142 143 } // end method testDeregisterDriver() 144 145 static void printClassLoader(Object theObject) { 146 Class<? extends Object> theClass = theObject.getClass(); 147 ClassLoader theClassLoader = theClass.getClassLoader(); 148 System.out.println("ClassLoader is: " + theClassLoader.toString() 149 + " for object: " + theObject.toString()); 150 } // end method printClassLoader( Object ) 151 152 static boolean isDriverLoaded(Driver theDriver) { 153 Enumeration<?> driverList = DriverManager.getDrivers(); 154 while (driverList.hasMoreElements()) { 155 if ((Driver) driverList.nextElement() == theDriver) { 156 return true; 157 } 158 } // end while 159 return false; 160 } // end method isDriverLoaded( Driver ) 161 162 /* 163 * Class under test for Connection getConnection(String) 164 */ 165 // valid connection - data1 does not require a user and password... 166 static String validConnectionURL = "jdbc:mikes1:data1"; 167 168 // invalid connection - data2 requires a user & password 169 static String invalidConnectionURL1 = "jdbc:mikes1:data2"; 170 171 // invalid connection - URL is gibberish 172 static String invalidConnectionURL2 = "xyz1:abc3:456q"; 173 174 // invalid connection - URL is null 175 static String invalidConnectionURL3 = null; 176 177 static String[] invalidConnectionURLs = { invalidConnectionURL2, 178 invalidConnectionURL3 }; 179 180 @TestTargetNew( 181 level = TestLevel.COMPLETE, 182 notes = "", 183 method = "getConnection", 184 args = {java.lang.String.class} 185 ) 186 public void testGetConnectionString() throws SQLException { 187 Connection theConnection = null; 188 // validConnection - no user & password required 189 theConnection = DriverManager.getConnection(validConnectionURL); 190 assertNotNull(theConnection); 191 assertNotNull(DriverManager.getConnection(invalidConnectionURL1)); 192 193 for (String element : invalidConnectionURLs) { 194 try { 195 theConnection = DriverManager 196 .getConnection(element); 197 fail("Should throw SQLException"); 198 } catch (SQLException e) { 199 //expected 200 } // end try 201 } // end for 202 } // end method testGetConnectionString() 203 204 /** 205 * @tests java.sql.DriverManager#getConnection(String, Properties) 206 */ 207 @TestTargetNew( 208 level = TestLevel.COMPLETE, 209 notes = "", 210 method = "getConnection", 211 args = {java.lang.String.class, java.util.Properties.class} 212 ) 213 public void test_getConnection_LStringLProperties() { 214 try { 215 DriverManager.getConnection("fff", 216 new Properties()); 217 fail("Should throw SQLException."); 218 } catch (SQLException e) { 219 assertEquals("08001", e.getSQLState()); 220 } 221 222 try { 223 DriverManager.getConnection(null, 224 new Properties()); 225 fail("Should throw SQLException."); 226 } catch (SQLException e) { 227 assertEquals("08001", e.getSQLState()); 228 } 229 } 230 231 /* 232 * Class under test for Connection getConnection(String, Properties) 233 */ 234 @TestTargetNew( 235 level = TestLevel.COMPLETE, 236 notes = "", 237 method = "getConnection", 238 args = {java.lang.String.class, java.util.Properties.class} 239 ) 240 public void testGetConnectionStringProperties() throws SQLException { 241 String validURL1 = "jdbc:mikes1:data2"; 242 String validuser1 = "theuser"; 243 String validpassword1 = "thepassword"; 244 String invalidURL1 = "xyz:abc1:foo"; 245 String invalidURL2 = "jdbc:mikes1:crazyone"; 246 String invalidURL3 = ""; 247 String invaliduser1 = "jonny nouser"; 248 String invalidpassword1 = "whizz"; 249 Properties nullProps = null; 250 Properties validProps = new Properties(); 251 validProps.setProperty("user", validuser1); 252 validProps.setProperty("password", validpassword1); 253 Properties invalidProps1 = new Properties(); 254 invalidProps1.setProperty("user", invaliduser1); 255 invalidProps1.setProperty("password", invalidpassword1); 256 String[] invalidURLs = { null, invalidURL1, 257 invalidURL2, invalidURL3 }; 258 Properties[] invalidProps = { nullProps, invalidProps1}; 259 260 261 262 Connection theConnection = null; 263 // validConnection - user & password required 264 theConnection = DriverManager.getConnection(validURL1, validProps); 265 assertNotNull(theConnection); 266 267 // invalid Connections 268 for (int i = 0; i < invalidURLs.length; i++) { 269 theConnection = null; 270 try { 271 theConnection = DriverManager.getConnection(invalidURLs[i], 272 validProps); 273 fail("Should throw SQLException"); 274 } catch (SQLException e) { 275 //expected 276 } // end try 277 } // end for 278 for (Properties invalidProp : invalidProps) { 279 assertNotNull(DriverManager.getConnection(validURL1, invalidProp)); 280 } 281 } // end method testGetConnectionStringProperties() 282 283 /* 284 * Class under test for Connection getConnection(String, String, String) 285 */ 286 @TestTargetNew( 287 level = TestLevel.COMPLETE, 288 notes = "", 289 method = "getConnection", 290 args = {java.lang.String.class, java.lang.String.class, java.lang.String.class} 291 ) 292 public void testGetConnectionStringStringString() throws SQLException { 293 String validURL1 = "jdbc:mikes1:data2"; 294 String validuser1 = "theuser"; 295 String validpassword1 = "thepassword"; 296 String invalidURL1 = "xyz:abc1:foo"; 297 String invaliduser1 = "jonny nouser"; 298 String invalidpassword1 = "whizz"; 299 String[] invalid1 = { null, validuser1, validpassword1 }; 300 String[] invalid2 = { validURL1, null, validpassword1 }; 301 String[] invalid3 = { validURL1, validuser1, null }; 302 String[] invalid4 = { invalidURL1, validuser1, validpassword1 }; 303 String[] invalid5 = { validURL1, invaliduser1, invalidpassword1 }; 304 String[] invalid6 = { validURL1, validuser1, invalidpassword1 }; 305 String[][] invalids1 = { invalid1, invalid4}; 306 String[][] invalids2 = {invalid2, invalid3, invalid5, invalid6 }; 307 308 Connection theConnection = null; 309 // validConnection - user & password required 310 theConnection = DriverManager.getConnection(validURL1, validuser1, 311 validpassword1); 312 assertNotNull(theConnection); 313 for (String[] theData : invalids1) { 314 theConnection = null; 315 try { 316 theConnection = DriverManager.getConnection(theData[0], 317 theData[1], theData[2]); 318 fail("Should throw SQLException."); 319 } catch (SQLException e) { 320 //expected 321 } // end try 322 } // end for 323 for (String[] theData : invalids2) { 324 assertNotNull(DriverManager.getConnection(theData[0], theData[1], 325 theData[2])); 326 } 327 } // end method testGetConnectionStringStringString() 328 329 static String validURL1 = "jdbc:mikes1"; 330 331 static String validURL2 = "jdbc:mikes2"; 332 333 static String invalidURL1 = "xyz:acb"; 334 335 static String invalidURL2 = null; 336 337 static String[] validURLs = { validURL1, validURL2 }; 338 339 static String[] invalidURLs = { invalidURL1, invalidURL2 }; 340 341 static String exceptionMsg1 = "No suitable driver"; 342 343 @TestTargetNew( 344 level = TestLevel.COMPLETE, 345 notes = "", 346 method = "getDriver", 347 args = {java.lang.String.class} 348 ) 349 public void testGetDriver() throws SQLException { 350 for (String element : validURLs) { 351 Driver validDriver = DriverManager.getDriver(element); 352 assertNotNull("Driver " + element + " not loaded", validDriver); 353 } // end for 354 355 for (String element : invalidURLs) { 356 try { 357 DriverManager.getDriver(element); 358 fail("Should throw SQLException"); 359 } catch (SQLException e) { 360 assertEquals("08001", e.getSQLState()); 361 assertEquals(exceptionMsg1, e.getMessage()); 362 } // end try 363 } // end for 364 365 } // end method testGetDriver() 366 367 @TestTargetNew( 368 level = TestLevel.COMPLETE, 369 notes = "test only passes in CTS host environment.", 370 method = "getDrivers", 371 args = {} 372 ) 373 public void testGetDrivers() { 374 // Load a driver manager 375 Enumeration<Driver> driverList = DriverManager.getDrivers(); 376 int i = 0; 377 while (driverList.hasMoreElements()) { 378 Driver theDriver = driverList.nextElement(); 379 assertNotNull(theDriver); 380 i++; 381 } // end while 382 383 // Check that all the drivers are in the list... 384 // There might be other drivers loaded in other classes 385 assertTrue("testGetDrivers: Don't see all the loaded drivers - ", 386 i >= numberLoaded); 387 } // end method testGetDrivers() 388 389 static int timeout1 = 25; 390 391 @TestTargetNew( 392 level = TestLevel.COMPLETE, 393 notes = "", 394 method = "getLoginTimeout", 395 args = {} 396 ) 397 public void testGetLoginTimeout() { 398 DriverManager.setLoginTimeout(timeout1); 399 assertEquals(timeout1, DriverManager.getLoginTimeout()); 400 } // end method testGetLoginTimeout() 401 402 @TestTargetNew( 403 level = TestLevel.COMPLETE, 404 notes = "", 405 method = "getLogStream", 406 args = {} 407 ) 408 @SuppressWarnings("deprecation") 409 public void testGetLogStream() { 410 assertNull(DriverManager.getLogStream()); 411 412 DriverManager.setLogStream(testPrintStream); 413 assertTrue(DriverManager.getLogStream() == testPrintStream); 414 415 DriverManager.setLogStream(null); 416 } // end method testGetLogStream() 417 418 @TestTargetNew( 419 level = TestLevel.COMPLETE, 420 notes = "", 421 method = "getLogWriter", 422 args = {} 423 ) 424 public void testGetLogWriter() { 425 assertNull(DriverManager.getLogWriter()); 426 427 DriverManager.setLogWriter(testPrintWriter); 428 429 assertTrue(DriverManager.getLogWriter() == testPrintWriter); 430 431 DriverManager.setLogWriter(null); 432 } // end method testGetLogWriter() 433 434 static String testMessage = "DriverManagerTest: test message for print stream"; 435 436 @TestTargetNew( 437 level = TestLevel.COMPLETE, 438 notes = "", 439 method = "println", 440 args = {java.lang.String.class} 441 ) 442 @SuppressWarnings("deprecation") 443 public void testPrintln() { 444 // System.out.println("testPrintln"); 445 DriverManager.println(testMessage); 446 447 DriverManager.setLogWriter(testPrintWriter); 448 DriverManager.println(testMessage); 449 450 String theOutput = outputStream.toString(); 451 // System.out.println("testPrintln: output= " + theOutput ); 452 assertTrue(theOutput.startsWith(testMessage)); 453 454 DriverManager.setLogWriter(null); 455 456 DriverManager.setLogStream(testPrintStream); 457 DriverManager.println(testMessage); 458 459 theOutput = outputStream2.toString(); 460 // System.out.println("testPrintln: output= " + theOutput ); 461 assertTrue(theOutput.startsWith(testMessage)); 462 463 DriverManager.setLogStream(null); 464 } // end method testPrintln() 465 466 @TestTargetNew( 467 level = TestLevel.COMPLETE, 468 notes = "SQLException checking missed: not feasible", 469 method = "registerDriver", 470 args = {java.sql.Driver.class} 471 ) 472 public void testRegisterDriver() throws ClassNotFoundException, 473 SQLException, IllegalAccessException, InstantiationException { 474 // This is DRIVER3 475 // String EXTRA_DRIVER_NAME = 476 // "org.apache.harmony.sql.tests.java.sql.TestHelper_Driver3"; 477 478 try { 479 DriverManager.registerDriver(null); 480 fail("Should throw NullPointerException."); 481 } catch (NullPointerException e) { 482 // expected 483 } // end try 484 485 Driver theDriver = null; 486 // Load another Driver that isn't in the basic set 487 Class<?> driverClass = Class.forName(DRIVER3); 488 theDriver = (Driver) driverClass.newInstance(); 489 DriverManager.registerDriver(theDriver); 490 491 assertTrue("testRegisterDriver: driver not in loaded set", 492 isDriverLoaded(theDriver)); 493 494 495 496 } // end testRegisterDriver() 497 498 static int validTimeout1 = 15; 499 500 static int validTimeout2 = 0; 501 502 static int[] validTimeouts = { validTimeout1, validTimeout2 }; 503 504 static int invalidTimeout1 = -10; 505 506 @TestTargetNew( 507 level = TestLevel.COMPLETE, 508 notes = "", 509 method = "setLoginTimeout", 510 args = {int.class} 511 ) 512 public void testSetLoginTimeout() { 513 for (int element : validTimeouts) { 514 DriverManager.setLoginTimeout(element); 515 516 assertEquals(element, DriverManager.getLoginTimeout()); 517 } // end for 518 // Invalid timeouts 519 DriverManager.setLoginTimeout(invalidTimeout1); 520 assertEquals(invalidTimeout1, DriverManager.getLoginTimeout()); 521 } // end testSetLoginTimeout() 522 523 static ByteArrayOutputStream outputStream2 = new ByteArrayOutputStream(); 524 525 static PrintStream testPrintStream = new PrintStream(outputStream2); 526 527 @TestTargetNew( 528 level = TestLevel.COMPLETE, 529 notes = "", 530 method = "setLogStream", 531 args = {java.io.PrintStream.class} 532 ) 533 @SuppressWarnings("deprecation") 534 public void testSetLogStream() { 535 // System.out.println("testSetLogStream"); 536 DriverManager.setLogStream(testPrintStream); 537 538 assertSame(testPrintStream, DriverManager.getLogStream()); 539 540 DriverManager.setLogStream(null); 541 542 assertNull(DriverManager.getLogStream()); 543 } // end method testSetLogStream() 544 545 static ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); 546 547 static PrintWriter testPrintWriter = new PrintWriter(outputStream); 548 549 /** 550 * Test for the setLogWriter method 551 */ 552 @TestTargetNew( 553 level = TestLevel.COMPLETE, 554 notes = "", 555 method = "setLogWriter", 556 args = {java.io.PrintWriter.class} 557 ) 558 public void testSetLogWriter() { 559 // System.out.println("testSetLogWriter"); 560 DriverManager.setLogWriter(testPrintWriter); 561 562 assertSame(testPrintWriter, DriverManager.getLogWriter()); 563 564 DriverManager.setLogWriter(null); 565 566 assertNull("testDriverManager: Log writer not null:", DriverManager 567 .getLogWriter()); 568 } // end method testSetLogWriter() 569 570 /* 571 * Method which loads a set of JDBC drivers ready for use by the various 572 * tests @return the number of drivers loaded 573 */ 574 static boolean driversLoaded = false; 575 576 private static int loadDrivers() { 577 if (driversLoaded) { 578 return numberLoaded; 579 } 580 /* 581 * First define a value for the System property "jdbc.drivers" - before 582 * the DriverManager class is loaded - this property defines a set of 583 * drivers which the DriverManager will load during its initialization 584 * and which will be loaded on the System ClassLoader - unlike the ones 585 * loaded later by this method which are loaded on the Application 586 * ClassLoader. 587 */ 588 int numberLoaded = 0; 589 String theSystemDrivers = DRIVER4 + ":" + DRIVER5 + ":" 590 + INVALIDDRIVER1; 591 System.setProperty(JDBC_PROPERTY, theSystemDrivers); 592 numberLoaded += 2; 593 594 for (String element : driverNames) { 595 try { 596 Class<?> driverClass = Class.forName(element); 597 assertNotNull(driverClass); 598 System.out.println("Loaded driver - classloader = " + 599 driverClass.getClassLoader()); 600 numberLoaded++; 601 } catch (ClassNotFoundException e) { 602 System.out.println("DriverManagerTest: failed to load Driver: " 603 + element); 604 } // end try 605 } // end for 606 driversLoaded = true; 607 return numberLoaded; 608 } // end method loadDrivers() 609 610 class TestSecurityManager extends SecurityManager { 611 612 boolean logAccess = true; 613 614 SQLPermission sqlPermission = new SQLPermission("setLog"); 615 616 RuntimePermission setManagerPermission = new RuntimePermission( 617 "setSecurityManager"); 618 619 TestSecurityManager() { 620 super(); 621 } // end method TestSecurityManager() 622 623 void setLogAccess(boolean allow) { 624 logAccess = allow; 625 } // end method setLogAccess( boolean ) 626 627 @Override 628 public void checkPermission(Permission thePermission) { 629 if (thePermission.equals(sqlPermission)) { 630 if (!logAccess) { 631 throw new SecurityException("Cannot set the sql Log Writer"); 632 } // end if 633 return; 634 } // end if 635 636 if (thePermission.equals(setManagerPermission)) { 637 return; 638 } // end if 639 // super.checkPermission( thePermission ); 640 } // end method checkPermission( Permission ) 641 642 } // end class TestSecurityManager 643 644 /** 645 * @tests {@link java.sql.DriverManager#registerDriver(Driver)} 646 * 647 * Registers a driver for multiple times and deregisters it only once. 648 * 649 * Regression for HARMONY-4205 650 */ 651 public void test_registerDriver_MultiTimes() throws SQLException { 652 int register_count = 10; 653 int deregister_count = 1; 654 655 Driver dummy = new DummyDriver(); 656 DriverManager.registerDriver(new BadDummyDriver()); 657 for (int i = 0; i < register_count; i++) { 658 DriverManager.registerDriver(dummy); 659 } 660 DriverManager.registerDriver(new BadDummyDriver()); 661 for (int i = 0; i < deregister_count; i++) { 662 DriverManager.deregisterDriver(dummy); 663 } 664 Driver d = DriverManager.getDriver("jdbc:dummy_protocol:dummy_subname"); 665 assertNotNull(d); 666 } 667 668 /** 669 * Regression for HARMONY-4303 670 */ 671 public void test_initClass() throws Exception { 672 ProcessBuilder builder = javaProcessBuilder(); 673 builder.command().add("org/apache/harmony/sql/tests/java/sql/TestMainForDriver"); 674 assertEquals("", execAndGetOutput(builder)); 675 } 676 677 private static class BadDummyDriver extends DummyDriver { 678 public boolean acceptsURL(String url) { 679 return false; 680 } 681 } 682 683 private static class DummyDriver implements Driver { 684 685 String goodurl = "jdbc:dummy_protocol:dummy_subname"; 686 687 public boolean acceptsURL(String url) { 688 return url.equals(goodurl); 689 } 690 691 public Connection connect(String url, Properties info) { 692 return null; 693 } 694 695 public DriverPropertyInfo[] getPropertyInfo(String url, Properties info) { 696 return null; 697 } 698 699 public int getMajorVersion() { 700 return 0; 701 } 702 703 public int getMinorVersion() { 704 return 0; 705 } 706 707 public boolean jdbcCompliant() { 708 return true; 709 } 710 711 } 712 713 } // end class DriverManagerTest 714