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