KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > db > explorer > DbDriverManagerTest


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.modules.db.explorer;
21
22 import java.io.File JavaDoc;
23 import java.lang.ref.Reference JavaDoc;
24 import java.lang.ref.WeakReference JavaDoc;
25 import java.lang.reflect.InvocationHandler JavaDoc;
26 import java.lang.reflect.Method JavaDoc;
27 import java.lang.reflect.Proxy JavaDoc;
28 import java.net.MalformedURLException JavaDoc;
29 import java.net.URL JavaDoc;
30 import java.sql.Connection JavaDoc;
31 import java.sql.Driver JavaDoc;
32 import java.sql.DriverManager JavaDoc;
33 import java.sql.DriverPropertyInfo JavaDoc;
34 import java.sql.SQLException JavaDoc;
35 import java.util.Properties JavaDoc;
36 import org.netbeans.api.db.explorer.JDBCDriver;
37 import org.netbeans.modules.db.test.TestBase;
38
39 /**
40  *
41  * @author Andrei Badea
42  */

43 public class DbDriverManagerTest extends TestBase {
44     
45     public DbDriverManagerTest(String JavaDoc testName) {
46         super(testName);
47     }
48
49     /**
50      * Tests that drivers from DriverManager are loaded.
51      */

52     public void testLoadFromDriverManager() throws Exception JavaDoc {
53         final String JavaDoc URL = "jdbc:testLoadFromDriverManager";
54         
55         Driver JavaDoc d = new DriverImpl(URL);
56         DriverManager.registerDriver(d);
57         try {
58             Driver JavaDoc found = DbDriverManager.getDefault().getDriver(URL, null);
59             assertSame(d, found);
60             Connection JavaDoc conn = DbDriverManager.getDefault().getConnection(URL, new Properties JavaDoc(), null);
61             assertNotNull(conn);
62             assertSame(d, ((ConnectionEx)conn).getDriver());
63         } finally {
64             DriverManager.deregisterDriver(d);
65         }
66     }
67     
68     /**
69      * Tests that registered drivers are loaded.
70      */

71     public void testLoadRegisteredDriver() throws Exception JavaDoc {
72         final String JavaDoc URL = "jdbc:testLoadRegisteredDriver";
73         
74         Driver JavaDoc d = new DriverImpl(URL);
75         DbDriverManager.getDefault().registerDriver(d);
76         try {
77             Driver JavaDoc found = DbDriverManager.getDefault().getDriver(URL, null);
78             assertSame(d, found);
79             Connection JavaDoc conn = DbDriverManager.getDefault().getConnection(URL, new Properties JavaDoc(), null);
80             assertNotNull(conn);
81             assertSame(d, ((ConnectionEx)conn).getDriver());
82         } finally {
83             DbDriverManager.getDefault().deregisterDriver(d);
84         }
85         try {
86             DbDriverManager.getDefault().getDriver(URL, null);
87             fail("The driver is still registered.");
88         } catch (SQLException JavaDoc e) {
89             assertEquals("08001", e.getSQLState());
90         }
91     }
92     
93     /**
94      * Tests that the fallback JDBCDriver instance is used.
95      */

96     public void testLoadJDBCDriver() throws Exception JavaDoc {
97         JDBCDriver drv = createJDBCDriver();
98         Connection JavaDoc conn = DbDriverManager.getDefault().getConnection(DriverImpl.DEFAULT_URL, new Properties JavaDoc(), drv);
99         Driver JavaDoc d = ((ConnectionEx)conn).getDriver();
100         assertSame(DriverImpl.class, d.getClass());
101         assertTrue(d.acceptsURL(DriverImpl.DEFAULT_URL));
102     }
103
104     /**
105      * Tests the driver priority if there are multiple drivers which accept the same URL.
106      * Also tests the getSameDriverConnection() method.
107      */

108     public void testGetConnection() throws Exception JavaDoc {
109         // register a driver to DriverManager
110
Driver JavaDoc ddm = new DriverImpl(DriverImpl.DEFAULT_URL);
111         DriverManager.registerDriver(ddm);
112         try {
113             // register a driver to DbDriverManager
114
Driver JavaDoc dreg = new DriverImpl(DriverImpl.DEFAULT_URL);
115             DbDriverManager.getDefault().registerDriver(dreg);
116             try {
117                 // create a JDBC driver
118
JDBCDriver drv = createJDBCDriver();
119
120                 Connection JavaDoc conn, newConn;
121
122                 // the drivers registered with DbDriverManager have the greatest priority
123
conn = DbDriverManager.getDefault().getConnection(DriverImpl.DEFAULT_URL, new Properties JavaDoc(), drv);
124                 assertSame(dreg, ((ConnectionEx)conn).getDriver());
125                 // also test the getSameDriverConnection() method
126
newConn = DbDriverManager.getDefault().getSameDriverConnection(conn, DriverImpl.DEFAULT_URL, new Properties JavaDoc());
127                 assertSame(((ConnectionEx)conn).getDriver(), ((ConnectionEx)newConn).getDriver());
128
129                 // if nothing registered, try to load a driver from the JDBCDriver URLs
130
DbDriverManager.getDefault().deregisterDriver(dreg);
131                 conn = DbDriverManager.getDefault().getConnection(DriverImpl.DEFAULT_URL, new Properties JavaDoc(), drv);
132                 assertNotSame(dreg, ((ConnectionEx)conn).getDriver());
133                 assertNotSame(ddm, ((ConnectionEx)conn).getDriver());
134                 // also test the getSameDriverConnection() method
135
newConn = DbDriverManager.getDefault().getSameDriverConnection(conn, DriverImpl.DEFAULT_URL, new Properties JavaDoc());
136                 assertSame(((ConnectionEx)conn).getDriver(), ((ConnectionEx)newConn).getDriver());
137
138                 // if no JDBCDriver, try DriverManager
139
conn = DbDriverManager.getDefault().getConnection(DriverImpl.DEFAULT_URL, new Properties JavaDoc(), null);
140                 assertSame(ddm, ((ConnectionEx)conn).getDriver());
141                 // also test the getSameDriverConnection() method
142
newConn = DbDriverManager.getDefault().getSameDriverConnection(conn, DriverImpl.DEFAULT_URL, new Properties JavaDoc());
143                 assertSame(((ConnectionEx)conn).getDriver(), ((ConnectionEx)newConn).getDriver());
144                 
145                 // test if getSameDriverConnection() throws IAE when passed a conn not obtained from DbDriverManager
146
conn = dreg.connect(DriverImpl.DEFAULT_URL, new Properties JavaDoc());
147                 try {
148                     DbDriverManager.getDefault().getSameDriverConnection(conn, DriverImpl.DEFAULT_URL, new Properties JavaDoc());
149                     fail();
150                 } catch (IllegalArgumentException JavaDoc e) {
151                     // ok
152
}
153             } finally {
154                 DbDriverManager.getDefault().deregisterDriver(dreg);
155             }
156         } finally {
157             DriverManager.deregisterDriver(ddm);
158         }
159     }
160     
161     /**
162      * Tests the driver priority if there are multiple drivers which accept the same URL.
163      */

164     public void testGetDriverPriority() throws Exception JavaDoc {
165         // register a driver to DriverManager
166
Driver JavaDoc ddm = new DriverImpl(DriverImpl.DEFAULT_URL);
167         DriverManager.registerDriver(ddm);
168         try {
169             // register a driver to DbDriverManager
170
Driver JavaDoc dreg = new DriverImpl(DriverImpl.DEFAULT_URL);
171             DbDriverManager.getDefault().registerDriver(dreg);
172             try {
173                 // create a JDBC driver
174
JDBCDriver drv = createJDBCDriver();
175
176                 Driver JavaDoc driver;
177
178                 // the drivers registered with DbDriverManager have the greatest priority
179
driver = DbDriverManager.getDefault().getDriver(DriverImpl.DEFAULT_URL, drv);
180                 assertSame(dreg, driver);
181
182                 // if nothing registered, try to load a driver from the JDBCDriver URLs
183
DbDriverManager.getDefault().deregisterDriver(dreg);
184                 driver = DbDriverManager.getDefault().getDriver(DriverImpl.DEFAULT_URL, drv);
185                 assertNotSame(dreg, driver);
186                 assertNotSame(ddm, driver);
187
188                 // if no JDBCDriver, try DriverManager
189
driver = DbDriverManager.getDefault().getDriver(DriverImpl.DEFAULT_URL, null);
190                 assertSame(ddm, driver);
191             } finally {
192                 DbDriverManager.getDefault().deregisterDriver(dreg);
193             }
194         } finally {
195             DriverManager.deregisterDriver(ddm);
196         }
197     }
198     
199     public void testJDBCDriverCached() throws Exception JavaDoc {
200         JDBCDriver drv = createDummyJDBCDriver(getDataDir());
201         Driver JavaDoc driver1 = DbDriverManager.getDefault().getDriver(DriverImpl.DEFAULT_URL, drv);
202         Driver JavaDoc driver2 = DbDriverManager.getDefault().getDriver(DriverImpl.DEFAULT_URL, drv);
203         assertSame(driver1.getClass(), driver2.getClass());
204     }
205     
206     public void testNoJDBCDriverLeaks() throws Exception JavaDoc {
207         JDBCDriver drv = createJDBCDriver();
208         Driver JavaDoc driver = DbDriverManager.getDefault().getDriver(DriverImpl.DEFAULT_URL, drv);
209         Reference JavaDoc drvRef = new WeakReference JavaDoc(drv);
210         drv = null;
211
212         assertGC("Should be possible to GC the driver", drvRef);
213     }
214     
215     private static JDBCDriver createJDBCDriver() {
216         URL JavaDoc url = DbDriverManagerTest.class.getProtectionDomain().getCodeSource().getLocation();
217         return JDBCDriver.create("test_driver", "DbDriverManagerTest Driver", "org.netbeans.modules.db.explorer.DbDriverManagerTest$DriverImpl", new URL JavaDoc[] { url });
218     }
219     
220     private static JDBCDriver createDummyJDBCDriver(File JavaDoc dataDir) throws MalformedURLException JavaDoc {
221         URL JavaDoc url = dataDir.toURL();
222         return JDBCDriver.create("test_driver", "DbDriverManagerTest DummyDriver", "DummyDriver", new URL JavaDoc[] { url });
223     }
224     
225     public static final class DriverImpl implements Driver JavaDoc {
226         
227         public static final String JavaDoc DEFAULT_URL = "jdbc:DbDriverManagerTest";
228         
229         private String JavaDoc url;
230         
231         public DriverImpl() {
232             this(DEFAULT_URL);
233         }
234         
235         public DriverImpl(String JavaDoc url) {
236             this.url = url;
237         }
238         
239         public DriverPropertyInfo JavaDoc[] getPropertyInfo(String JavaDoc url, Properties JavaDoc info) throws SQLException JavaDoc {
240             return new DriverPropertyInfo JavaDoc[0];
241         }
242
243         public Connection JavaDoc connect(String JavaDoc url, Properties JavaDoc info) throws SQLException JavaDoc {
244             return (Connection JavaDoc)Proxy.newProxyInstance(DriverImpl.class.getClassLoader(), new Class JavaDoc[] { ConnectionEx.class }, new InvocationHandler JavaDoc() {
245                 public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc m, Object JavaDoc[] args) {
246                     String JavaDoc methodName = m.getName();
247                     if (methodName.equals("getDriver")) {
248                         return DriverImpl.this;
249                     } else if (methodName.equals("hashCode")) {
250                         Integer JavaDoc i = new Integer JavaDoc(System.identityHashCode(proxy));
251                         return i;
252                     } else if (methodName.equals("equals")) {
253                         return Boolean.valueOf(proxy == args[0]);
254                     }
255                     return null;
256                 }
257             });
258         }
259
260         public boolean acceptsURL(String JavaDoc url) throws SQLException JavaDoc {
261             return (this.url.equals(url));
262         }
263
264         public boolean jdbcCompliant() {
265             return true;
266         }
267
268         public int getMinorVersion() {
269             return 0;
270         }
271
272         public int getMajorVersion() {
273             return 0;
274         }
275     }
276     
277     private static interface ConnectionEx extends Connection JavaDoc {
278         
279         public Driver JavaDoc getDriver();
280     }
281 }
282
Popular Tags