View Javadoc
1   /*
2    *    Copyright 2009-2023 the original author or authors.
3    *
4    *    Licensed under the Apache License, Version 2.0 (the "License");
5    *    you may not use this file except in compliance with the License.
6    *    You may obtain a copy of the License at
7    *
8    *       https://www.apache.org/licenses/LICENSE-2.0
9    *
10   *    Unless required by applicable law or agreed to in writing, software
11   *    distributed under the License is distributed on an "AS IS" BASIS,
12   *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *    See the License for the specific language governing permissions and
14   *    limitations under the License.
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      // https://github.com/mybatis/old-google-code-issues/issues/430
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  }