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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.harmony.sql.tests.java.sql;
20 import dalvik.annotation.TestTargetClass;
22 import java.sql.Driver;
23 import java.sql.DriverManager;
24 import java.sql.SQLException;
25 import java.util.logging.Logger;
27 import junit.framework.TestCase;
29 @TestTargetClass(DriverManager.class)
31 * Helper class for the Driver manager tes - it allows the test code to be
32 * loaded under a different classloader, necessary for testing the
33 * DeregisterDriver function of DriverManager
36 public class TestHelper_DriverManager extends TestCase {
38 static Driver testDriver = null;
40 static TestHelper_DriverManager theHelper;
43 theHelper = new TestHelper_DriverManager();
44 // theHelper.testDeregister();
47 public TestHelper_DriverManager() {
49 } // end constructor TestHelper_DriverManager()
51 public static void setDriver(Driver theDriver) {
52 testDriver = theDriver;
53 // Logger.global.info("TestHelper_DriverManager: Test Driver set!");
55 theHelper.checkDeregister();
56 } // end method setDriver( Driver )
58 public void checkDeregister() {
60 String baseURL = "jdbc:mikes1";
62 // Logger.global.info("Calling checkDeregister in TestHelper_DriverManager....");
66 // Logger.global.info("checkDeregister classloader: this.getClass().getClassLoader()");
68 // Try to get a driver from the general pool... this should fail
70 aDriver = DriverManager.getDriver(baseURL);
71 fail("testDeregisterDriver: Didn't get exception when getting valid driver from other classloader.");
72 } catch (SQLException e) {
73 // e.printStackTrace();
75 "testDeregisterDriver: Got exception when getting valid driver from other classloader.",
80 // OK, now THIS driver was loaded by someone else....
83 // printClassLoader( aDriver );
85 // Deregister this driver
87 DriverManager.deregisterDriver(aDriver);
88 // We shouldn't get here - but if we do, we need to re-register the
90 // prevent subsequent tests from failing due to inability to get to
92 DriverManager.registerDriver(aDriver);
93 fail("checkDeregisterDriver: Didn't get Security Exception deregistering invalid driver.");
94 } catch (SecurityException s) {
95 // This is the exception we should get...
96 // System.out.println("checkDeregisterDriver: got expected Security
98 } catch (Exception e) {
99 fail("checkDeregisterDriver: Got wrong exception type when deregistering invalid driver.");
102 } // end method testDeRegister
104 static void printClassLoader(Object theObject) {
105 Class<? extends Object> theClass = theObject.getClass();
106 ClassLoader theClassLoader = theClass.getClassLoader();
107 System.out.println("ClassLoader is: " + theClassLoader.toString()
108 + " for object: " + theObject.toString());
109 } // end method printClassLoader( Object )
111 } // end class TestHelper_DriverManager