/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
- /*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- package org.apache.harmony.sql.tests.java.sql;
- import java.sql.Driver;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import junit.framework.TestCase;
- /**
- * Helper class for the Driver manager tes - it allows the test code to be
- * loaded under a different classloader, necessary for testing the
- * DeregisterDriver function of DriverManager
- *
- */
- public class TestHelper_DriverManager extends TestCase {
- static Driver testDriver = null;
- static TestHelper_DriverManager theHelper;
- static {
- theHelper = new TestHelper_DriverManager();
- // theHelper.testDeregister();
- } // end static
- public TestHelper_DriverManager() {
- super();
- } // end constructor TestHelper_DriverManager()
- public static void setDriver(Driver theDriver) {
- testDriver = theDriver;
- // System.out.println("TestHelper_DriverManager: Test Driver set!");
- theHelper.checkDeregister();
- } // end method setDriver( Driver )
- public void checkDeregister() {
- String baseURL = "jdbc:mikes1";
- // System.out.println("Calling checkDeregister in
- // TestHelper_DriverManager....");
- Driver aDriver;
- // System.out.println("checkDeregister classloader: " +
- // this.getClass().getClassLoader() );
- // Try to get a driver from the general pool... this should fail
- try {
- aDriver = DriverManager.getDriver(baseURL);
- fail("testDeregisterDriver: Didn't get exception when getting valid driver from other classloader.");
- } catch (SQLException e) {
- // e.printStackTrace();
- assertTrue(
- "testDeregisterDriver: Got exception when getting valid driver from other classloader.",
- true);
- // return;
- } // end try
- // OK, now THIS driver was loaded by someone else....
- aDriver = testDriver;
- // printClassLoader( aDriver );
- // Deregister this driver
- try {
- DriverManager.deregisterDriver(aDriver);
- // We shouldn't get here - but if we do, we need to re-register the
- // driver to
- // prevent subsequent tests from failing due to inability to get to
- // this driver...
- DriverManager.registerDriver(aDriver);
- fail("checkDeregisterDriver: Didn't get Security Exception deregistering invalid driver.");
- } catch (SecurityException s) {
- // This is the exception we should get...
- // System.out.println("checkDeregisterDriver: got expected Security
- // Exception");
- } catch (Exception e) {
- fail("checkDeregisterDriver: Got wrong exception type when deregistering invalid driver.");
- } // end try
- } // end method testDeRegister
- static void printClassLoader(Object theObject) {
- Class<? extends Object> theClass = theObject.getClass();
- ClassLoader theClassLoader = theClass.getClassLoader();
- System.out.println("ClassLoader is: " + theClassLoader.toString()
- + " for object: " + theObject.toString());
- } // end method printClassLoader( Object )
- } // end class TestHelper_DriverManager