/external/apache-harmony/sql/src/test/java/org/apache/harmony/sql/tests/java/sql/TestHelper_DriverManager.java

https://gitlab.com/brian0218/rk3188_rk3066_r-box_android4.4.2_sdk · Java · 110 lines · 46 code · 23 blank · 41 comment · 0 complexity · 032fce46e7b3b07eda66aff89c5de404 MD5 · raw file

  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. package org.apache.harmony.sql.tests.java.sql;
  18. import java.sql.Driver;
  19. import java.sql.DriverManager;
  20. import java.sql.SQLException;
  21. import junit.framework.TestCase;
  22. /**
  23. * Helper class for the Driver manager tes - it allows the test code to be
  24. * loaded under a different classloader, necessary for testing the
  25. * DeregisterDriver function of DriverManager
  26. *
  27. */
  28. public class TestHelper_DriverManager extends TestCase {
  29. static Driver testDriver = null;
  30. static TestHelper_DriverManager theHelper;
  31. static {
  32. theHelper = new TestHelper_DriverManager();
  33. // theHelper.testDeregister();
  34. } // end static
  35. public TestHelper_DriverManager() {
  36. super();
  37. } // end constructor TestHelper_DriverManager()
  38. public static void setDriver(Driver theDriver) {
  39. testDriver = theDriver;
  40. // System.out.println("TestHelper_DriverManager: Test Driver set!");
  41. theHelper.checkDeregister();
  42. } // end method setDriver( Driver )
  43. public void checkDeregister() {
  44. String baseURL = "jdbc:mikes1";
  45. // System.out.println("Calling checkDeregister in
  46. // TestHelper_DriverManager....");
  47. Driver aDriver;
  48. // System.out.println("checkDeregister classloader: " +
  49. // this.getClass().getClassLoader() );
  50. // Try to get a driver from the general pool... this should fail
  51. try {
  52. aDriver = DriverManager.getDriver(baseURL);
  53. fail("testDeregisterDriver: Didn't get exception when getting valid driver from other classloader.");
  54. } catch (SQLException e) {
  55. // e.printStackTrace();
  56. assertTrue(
  57. "testDeregisterDriver: Got exception when getting valid driver from other classloader.",
  58. true);
  59. // return;
  60. } // end try
  61. // OK, now THIS driver was loaded by someone else....
  62. aDriver = testDriver;
  63. // printClassLoader( aDriver );
  64. // Deregister this driver
  65. try {
  66. DriverManager.deregisterDriver(aDriver);
  67. // We shouldn't get here - but if we do, we need to re-register the
  68. // driver to
  69. // prevent subsequent tests from failing due to inability to get to
  70. // this driver...
  71. DriverManager.registerDriver(aDriver);
  72. fail("checkDeregisterDriver: Didn't get Security Exception deregistering invalid driver.");
  73. } catch (SecurityException s) {
  74. // This is the exception we should get...
  75. // System.out.println("checkDeregisterDriver: got expected Security
  76. // Exception");
  77. } catch (Exception e) {
  78. fail("checkDeregisterDriver: Got wrong exception type when deregistering invalid driver.");
  79. } // end try
  80. } // end method testDeRegister
  81. static void printClassLoader(Object theObject) {
  82. Class<? extends Object> theClass = theObject.getClass();
  83. ClassLoader theClassLoader = theClass.getClassLoader();
  84. System.out.println("ClassLoader is: " + theClassLoader.toString()
  85. + " for object: " + theObject.toString());
  86. } // end method printClassLoader( Object )
  87. } // end class TestHelper_DriverManager