View Javadoc
1   /*
2    *    Copyright 2009-2021 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    *       http://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.*;
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 = null;
35      dataSource = new UnpooledDataSource("org.hsqldb.jdbcDriver", "jdbc:hsqldb:mem:multipledrivers", "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 = null;
48      UnpooledDataSource dataSource = null;
49      driverClassLoader = new URLClassLoader(new URL[] { new URL("jar:file:/PATH_TO/mysql-connector-java-5.1.25.jar!/") });
50      dataSource = new UnpooledDataSource(driverClassLoader, "com.mysql.jdbc.Driver", "jdbc:mysql://127.0.0.1/test", "root", "");
51      dataSource.getConnection().close();
52      assertEquals(before + 1, countRegisteredDrivers());
53      driverClassLoader = new URLClassLoader(new URL[] { new URL("jar:file:/PATH_TO/mysql-connector-java-5.1.25.jar!/") });
54      dataSource = new UnpooledDataSource(driverClassLoader, "com.mysql.jdbc.Driver", "jdbc:mysql://127.0.0.1/test", "root", "");
55      dataSource.getConnection().close();
56      assertEquals(before + 1, countRegisteredDrivers());
57    }
58  
59    int countRegisteredDrivers() {
60      Enumeration<Driver> drivers = DriverManager.getDrivers();
61      int count = 0;
62      while (drivers.hasMoreElements()) {
63        drivers.nextElement();
64        count++;
65      }
66      return count;
67    }
68  
69  }