1 19 20 package org.netbeans.modules.db.explorer; 21 22 import java.io.File ; 23 import java.lang.ref.Reference ; 24 import java.lang.ref.WeakReference ; 25 import java.lang.reflect.InvocationHandler ; 26 import java.lang.reflect.Method ; 27 import java.lang.reflect.Proxy ; 28 import java.net.MalformedURLException ; 29 import java.net.URL ; 30 import java.sql.Connection ; 31 import java.sql.Driver ; 32 import java.sql.DriverManager ; 33 import java.sql.DriverPropertyInfo ; 34 import java.sql.SQLException ; 35 import java.util.Properties ; 36 import org.netbeans.api.db.explorer.JDBCDriver; 37 import org.netbeans.modules.db.test.TestBase; 38 39 43 public class DbDriverManagerTest extends TestBase { 44 45 public DbDriverManagerTest(String testName) { 46 super(testName); 47 } 48 49 52 public void testLoadFromDriverManager() throws Exception { 53 final String URL = "jdbc:testLoadFromDriverManager"; 54 55 Driver d = new DriverImpl(URL); 56 DriverManager.registerDriver(d); 57 try { 58 Driver found = DbDriverManager.getDefault().getDriver(URL, null); 59 assertSame(d, found); 60 Connection conn = DbDriverManager.getDefault().getConnection(URL, new Properties (), null); 61 assertNotNull(conn); 62 assertSame(d, ((ConnectionEx)conn).getDriver()); 63 } finally { 64 DriverManager.deregisterDriver(d); 65 } 66 } 67 68 71 public void testLoadRegisteredDriver() throws Exception { 72 final String URL = "jdbc:testLoadRegisteredDriver"; 73 74 Driver d = new DriverImpl(URL); 75 DbDriverManager.getDefault().registerDriver(d); 76 try { 77 Driver found = DbDriverManager.getDefault().getDriver(URL, null); 78 assertSame(d, found); 79 Connection conn = DbDriverManager.getDefault().getConnection(URL, new Properties (), 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 e) { 89 assertEquals("08001", e.getSQLState()); 90 } 91 } 92 93 96 public void testLoadJDBCDriver() throws Exception { 97 JDBCDriver drv = createJDBCDriver(); 98 Connection conn = DbDriverManager.getDefault().getConnection(DriverImpl.DEFAULT_URL, new Properties (), drv); 99 Driver d = ((ConnectionEx)conn).getDriver(); 100 assertSame(DriverImpl.class, d.getClass()); 101 assertTrue(d.acceptsURL(DriverImpl.DEFAULT_URL)); 102 } 103 104 108 public void testGetConnection() throws Exception { 109 Driver ddm = new DriverImpl(DriverImpl.DEFAULT_URL); 111 DriverManager.registerDriver(ddm); 112 try { 113 Driver dreg = new DriverImpl(DriverImpl.DEFAULT_URL); 115 DbDriverManager.getDefault().registerDriver(dreg); 116 try { 117 JDBCDriver drv = createJDBCDriver(); 119 120 Connection conn, newConn; 121 122 conn = DbDriverManager.getDefault().getConnection(DriverImpl.DEFAULT_URL, new Properties (), drv); 124 assertSame(dreg, ((ConnectionEx)conn).getDriver()); 125 newConn = DbDriverManager.getDefault().getSameDriverConnection(conn, DriverImpl.DEFAULT_URL, new Properties ()); 127 assertSame(((ConnectionEx)conn).getDriver(), ((ConnectionEx)newConn).getDriver()); 128 129 DbDriverManager.getDefault().deregisterDriver(dreg); 131 conn = DbDriverManager.getDefault().getConnection(DriverImpl.DEFAULT_URL, new Properties (), drv); 132 assertNotSame(dreg, ((ConnectionEx)conn).getDriver()); 133 assertNotSame(ddm, ((ConnectionEx)conn).getDriver()); 134 newConn = DbDriverManager.getDefault().getSameDriverConnection(conn, DriverImpl.DEFAULT_URL, new Properties ()); 136 assertSame(((ConnectionEx)conn).getDriver(), ((ConnectionEx)newConn).getDriver()); 137 138 conn = DbDriverManager.getDefault().getConnection(DriverImpl.DEFAULT_URL, new Properties (), null); 140 assertSame(ddm, ((ConnectionEx)conn).getDriver()); 141 newConn = DbDriverManager.getDefault().getSameDriverConnection(conn, DriverImpl.DEFAULT_URL, new Properties ()); 143 assertSame(((ConnectionEx)conn).getDriver(), ((ConnectionEx)newConn).getDriver()); 144 145 conn = dreg.connect(DriverImpl.DEFAULT_URL, new Properties ()); 147 try { 148 DbDriverManager.getDefault().getSameDriverConnection(conn, DriverImpl.DEFAULT_URL, new Properties ()); 149 fail(); 150 } catch (IllegalArgumentException e) { 151 } 153 } finally { 154 DbDriverManager.getDefault().deregisterDriver(dreg); 155 } 156 } finally { 157 DriverManager.deregisterDriver(ddm); 158 } 159 } 160 161 164 public void testGetDriverPriority() throws Exception { 165 Driver ddm = new DriverImpl(DriverImpl.DEFAULT_URL); 167 DriverManager.registerDriver(ddm); 168 try { 169 Driver dreg = new DriverImpl(DriverImpl.DEFAULT_URL); 171 DbDriverManager.getDefault().registerDriver(dreg); 172 try { 173 JDBCDriver drv = createJDBCDriver(); 175 176 Driver driver; 177 178 driver = DbDriverManager.getDefault().getDriver(DriverImpl.DEFAULT_URL, drv); 180 assertSame(dreg, driver); 181 182 DbDriverManager.getDefault().deregisterDriver(dreg); 184 driver = DbDriverManager.getDefault().getDriver(DriverImpl.DEFAULT_URL, drv); 185 assertNotSame(dreg, driver); 186 assertNotSame(ddm, driver); 187 188 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 { 200 JDBCDriver drv = createDummyJDBCDriver(getDataDir()); 201 Driver driver1 = DbDriverManager.getDefault().getDriver(DriverImpl.DEFAULT_URL, drv); 202 Driver driver2 = DbDriverManager.getDefault().getDriver(DriverImpl.DEFAULT_URL, drv); 203 assertSame(driver1.getClass(), driver2.getClass()); 204 } 205 206 public void testNoJDBCDriverLeaks() throws Exception { 207 JDBCDriver drv = createJDBCDriver(); 208 Driver driver = DbDriverManager.getDefault().getDriver(DriverImpl.DEFAULT_URL, drv); 209 Reference drvRef = new WeakReference (drv); 210 drv = null; 211 212 assertGC("Should be possible to GC the driver", drvRef); 213 } 214 215 private static JDBCDriver createJDBCDriver() { 216 URL url = DbDriverManagerTest.class.getProtectionDomain().getCodeSource().getLocation(); 217 return JDBCDriver.create("test_driver", "DbDriverManagerTest Driver", "org.netbeans.modules.db.explorer.DbDriverManagerTest$DriverImpl", new URL [] { url }); 218 } 219 220 private static JDBCDriver createDummyJDBCDriver(File dataDir) throws MalformedURLException { 221 URL url = dataDir.toURL(); 222 return JDBCDriver.create("test_driver", "DbDriverManagerTest DummyDriver", "DummyDriver", new URL [] { url }); 223 } 224 225 public static final class DriverImpl implements Driver { 226 227 public static final String DEFAULT_URL = "jdbc:DbDriverManagerTest"; 228 229 private String url; 230 231 public DriverImpl() { 232 this(DEFAULT_URL); 233 } 234 235 public DriverImpl(String url) { 236 this.url = url; 237 } 238 239 public DriverPropertyInfo [] getPropertyInfo(String url, Properties info) throws SQLException { 240 return new DriverPropertyInfo [0]; 241 } 242 243 public Connection connect(String url, Properties info) throws SQLException { 244 return (Connection )Proxy.newProxyInstance(DriverImpl.class.getClassLoader(), new Class [] { ConnectionEx.class }, new InvocationHandler () { 245 public Object invoke(Object proxy, Method m, Object [] args) { 246 String methodName = m.getName(); 247 if (methodName.equals("getDriver")) { 248 return DriverImpl.this; 249 } else if (methodName.equals("hashCode")) { 250 Integer i = new Integer (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 url) throws SQLException { 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 { 278 279 public Driver getDriver(); 280 } 281 } 282 | Popular Tags |