OSDN Git Service

original
[gb-231r1-is01/Gingerbread_2.3.3_r1_IS01.git] / libcore / luni / src / test / java / org / apache / harmony / sql / tests / java / sql / TestHelper_DriverManager.java
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 dalvik.annotation.TestTargetClass;
21
22 import java.sql.Driver;
23 import java.sql.DriverManager;
24 import java.sql.SQLException;
25 import java.util.logging.Logger;
26
27 import junit.framework.TestCase;
28
29 @TestTargetClass(DriverManager.class)
30 /**
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
34  *
35  */
36 public class TestHelper_DriverManager extends TestCase {
37
38     static Driver testDriver = null;
39
40     static TestHelper_DriverManager theHelper;
41
42     static {
43         theHelper = new TestHelper_DriverManager();
44         // theHelper.testDeregister();
45     } // end static
46
47     public TestHelper_DriverManager() {
48         super();
49     } // end constructor TestHelper_DriverManager()
50
51     public static void setDriver(Driver theDriver) {
52         testDriver = theDriver;
53       //  Logger.global.info("TestHelper_DriverManager: Test Driver set!");
54
55         theHelper.checkDeregister();
56     } // end method setDriver( Driver )
57
58     public void checkDeregister() {
59
60         String baseURL = "jdbc:mikes1";
61
62       //  Logger.global.info("Calling checkDeregister in TestHelper_DriverManager....");
63
64         Driver aDriver;
65
66        // Logger.global.info("checkDeregister classloader: this.getClass().getClassLoader()");
67
68         // Try to get a driver from the general pool... this should fail
69         try {
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();
74             assertTrue(
75                     "testDeregisterDriver: Got exception when getting valid driver from other classloader.",
76                     true);
77             // return;
78         } // end try
79
80         // OK, now THIS driver was loaded by someone else....
81         aDriver = testDriver;
82
83         // printClassLoader( aDriver );
84
85         // Deregister this driver
86         try {
87             DriverManager.deregisterDriver(aDriver);
88             // We shouldn't get here - but if we do, we need to re-register the
89             // driver to
90             // prevent subsequent tests from failing due to inability to get to
91             // this driver...
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
97             // Exception");
98         } catch (Exception e) {
99             fail("checkDeregisterDriver: Got wrong exception type when deregistering invalid driver.");
100         } // end try
101
102     } // end method testDeRegister
103
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 )
110
111 } // end class TestHelper_DriverManager
112