1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.datasource.unpooled;
17
18 import static org.junit.jupiter.api.Assertions.assertEquals;
19
20 import java.net.URL;
21 import java.net.URLClassLoader;
22 import java.sql.Driver;
23 import java.sql.DriverManager;
24 import java.util.Enumeration;
25
26 import org.junit.jupiter.api.Disabled;
27 import org.junit.jupiter.api.Test;
28
29 class UnpooledDataSourceTest {
30
31 @Test
32 void shouldNotRegisterTheSameDriverMultipleTimes() throws Exception {
33
34 UnpooledDataSource dataSource = new UnpooledDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:multipledrivers",
35 "sa", "");
36 dataSource.getConnection().close();
37 int before = countRegisteredDrivers();
38 dataSource = new UnpooledDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:multipledrivers", "sa", "");
39 dataSource.getConnection().close();
40 assertEquals(before, countRegisteredDrivers());
41 }
42
43 @Disabled("Requires MySQL server and a driver.")
44 @Test
45 void shouldRegisterDynamicallyLoadedDriver() throws Exception {
46 int before = countRegisteredDrivers();
47 ClassLoader driverClassLoader = new URLClassLoader(
48 new URL[] { new URL("jar:file:/PATH_TO/mysql-connector-java-5.1.25.jar!/") });
49 UnpooledDataSource dataSource = new UnpooledDataSource(driverClassLoader, "com.mysql.jdbc.Driver",
50 "jdbc:mysql://127.0.0.1/test", "root", "");
51 dataSource.getConnection().close();
52 assertEquals(before + 1, countRegisteredDrivers());
53 driverClassLoader = new URLClassLoader(
54 new URL[] { new URL("jar:file:/PATH_TO/mysql-connector-java-5.1.25.jar!/") });
55 dataSource = new UnpooledDataSource(driverClassLoader, "com.mysql.jdbc.Driver", "jdbc:mysql://127.0.0.1/test",
56 "root", "");
57 dataSource.getConnection().close();
58 assertEquals(before + 1, countRegisteredDrivers());
59 }
60
61 int countRegisteredDrivers() {
62 Enumeration<Driver> drivers = DriverManager.getDrivers();
63 int count = 0;
64 while (drivers.hasMoreElements()) {
65 drivers.nextElement();
66 count++;
67 }
68 return count;
69 }
70
71 }