001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.sql.tests.java.sql;
019:
020: import java.sql.Driver;
021: import java.sql.DriverManager;
022: import java.sql.SQLException;
023:
024: import junit.framework.TestCase;
025:
026: /**
027: * Helper class for the Driver manager tes - it allows the test code to be
028: * loaded under a different classloader, necessary for testing the
029: * DeregisterDriver function of DriverManager
030: *
031: */
032: public class TestHelper_DriverManager extends TestCase {
033:
034: static Driver testDriver = null;
035:
036: static TestHelper_DriverManager theHelper;
037:
038: static {
039: theHelper = new TestHelper_DriverManager();
040: // theHelper.testDeregister();
041: } // end static
042:
043: public TestHelper_DriverManager() {
044: super ();
045: } // end constructor TestHelper_DriverManager()
046:
047: public static void setDriver(Driver theDriver) {
048: testDriver = theDriver;
049: // System.out.println("TestHelper_DriverManager: Test Driver set!");
050:
051: theHelper.checkDeregister();
052: } // end method setDriver( Driver )
053:
054: public void checkDeregister() {
055:
056: String baseURL = "jdbc:mikes1";
057:
058: // System.out.println("Calling checkDeregister in
059: // TestHelper_DriverManager....");
060:
061: Driver aDriver;
062:
063: // System.out.println("checkDeregister classloader: " +
064: // this.getClass().getClassLoader() );
065:
066: // Try to get a driver from the general pool... this should fail
067: try {
068: aDriver = DriverManager.getDriver(baseURL);
069: fail("testDeregisterDriver: Didn't get exception when getting valid driver from other classloader.");
070: } catch (SQLException e) {
071: // e.printStackTrace();
072: assertTrue(
073: "testDeregisterDriver: Got exception when getting valid driver from other classloader.",
074: true);
075: // return;
076: } // end try
077:
078: // OK, now THIS driver was loaded by someone else....
079: aDriver = testDriver;
080:
081: // printClassLoader( aDriver );
082:
083: // Deregister this driver
084: try {
085: DriverManager.deregisterDriver(aDriver);
086: // We shouldn't get here - but if we do, we need to re-register the
087: // driver to
088: // prevent subsequent tests from failing due to inability to get to
089: // this driver...
090: DriverManager.registerDriver(aDriver);
091: fail("checkDeregisterDriver: Didn't get Security Exception deregistering invalid driver.");
092: } catch (SecurityException s) {
093: // This is the exception we should get...
094: // System.out.println("checkDeregisterDriver: got expected Security
095: // Exception");
096: } catch (Exception e) {
097: fail("checkDeregisterDriver: Got wrong exception type when deregistering invalid driver.");
098: } // end try
099:
100: } // end method testDeRegister
101:
102: static void printClassLoader(Object theObject) {
103: Class<? extends Object> theClass = theObject.getClass();
104: ClassLoader theClassLoader = theClass.getClassLoader();
105: System.out.println("ClassLoader is: "
106: + theClassLoader.toString() + " for object: "
107: + theObject.toString());
108: } // end method printClassLoader( Object )
109:
110: } // end class TestHelper_DriverManager
|