View Javadoc
1   /*
2    *    Copyright 2009-2025 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.mybatis.guice.datasource.dbcp;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  
20  import com.google.inject.AbstractModule;
21  import com.google.inject.Guice;
22  import com.google.inject.Injector;
23  import com.google.inject.name.Names;
24  
25  import java.sql.Connection;
26  
27  import javax.sql.ConnectionPoolDataSource;
28  
29  import org.apache.commons.dbcp2.datasources.SharedPoolDataSource;
30  import org.junit.jupiter.api.Test;
31  import org.junit.jupiter.api.extension.ExtendWith;
32  import org.mockito.Mock;
33  import org.mockito.junit.jupiter.MockitoExtension;
34  
35  @ExtendWith(MockitoExtension.class)
36  class SharedPoolDataSourceProviderTest {
37    @Mock
38    private ClassLoader driverClassLoader;
39    @Mock
40    private ConnectionPoolDataSource connectionPoolDataSource;
41  
42    @Test
43    void get() throws Throwable {
44      final boolean autoCommit = true;
45      final int loginTimeout = 10;
46      final boolean defaultReadOnly = true;
47      final int defaultTransactionIsolation = Connection.TRANSACTION_READ_COMMITTED;
48      final String description = "test_description";
49      final int defaultMinEvictableIdleTimeMillis = 30;
50      final int defaultNumTestsPerEvictionRun = 40;
51      final boolean rollbackAfterValidation = true;
52      final boolean defaultTestOnBorrow = true;
53      final boolean defaultTestOnReturn = true;
54      final boolean defaultTestWhileIdle = true;
55      final int defaultTimeBetweenEvictionRunsMillis = 50;
56      final String validationQuery = "SELECT 1";
57      final int defaultMaxTotal = 60;
58      final int defaultMaxIdle = 70;
59      final int defaultMaxWaitMillis = 80;
60      Injector injector = Guice.createInjector(new AbstractModule() {
61        @Override
62        protected void configure() {
63          bind(ConnectionPoolDataSource.class).toInstance(connectionPoolDataSource);
64          bindConstant().annotatedWith(Names.named("JDBC.autoCommit")).to(autoCommit);
65          bindConstant().annotatedWith(Names.named("JDBC.loginTimeout")).to(loginTimeout);
66          bindConstant().annotatedWith(Names.named("DBCP.defaultReadOnly")).to(defaultReadOnly);
67          bindConstant().annotatedWith(Names.named("DBCP.defaultTransactionIsolation")).to(defaultTransactionIsolation);
68          bindConstant().annotatedWith(Names.named("DBCP.description")).to(description);
69          bindConstant().annotatedWith(Names.named("DBCP.defaultMinEvictableIdleTimeMillis"))
70              .to(defaultMinEvictableIdleTimeMillis);
71          bindConstant().annotatedWith(Names.named("DBCP.defaultNumTestsPerEvictionRun"))
72              .to(defaultNumTestsPerEvictionRun);
73          bindConstant().annotatedWith(Names.named("DBCP.rollbackAfterValidation")).to(rollbackAfterValidation);
74          bindConstant().annotatedWith(Names.named("DBCP.defaultTestOnBorrow")).to(defaultTestOnBorrow);
75          bindConstant().annotatedWith(Names.named("DBCP.defaultTestOnReturn")).to(defaultTestOnReturn);
76          bindConstant().annotatedWith(Names.named("DBCP.defaultTestWhileIdle")).to(defaultTestWhileIdle);
77          bindConstant().annotatedWith(Names.named("DBCP.defaultTimeBetweenEvictionRunsMillis"))
78              .to(defaultTimeBetweenEvictionRunsMillis);
79          bindConstant().annotatedWith(Names.named("DBCP.validationQuery")).to(validationQuery);
80          bindConstant().annotatedWith(Names.named("DBCP.defaultMaxTotal")).to(defaultMaxTotal);
81          bindConstant().annotatedWith(Names.named("DBCP.defaultMaxIdle")).to(defaultMaxIdle);
82          bindConstant().annotatedWith(Names.named("DBCP.defaultMaxWaitMillis")).to(defaultMaxWaitMillis);
83        }
84      });
85      SharedPoolDataSourceProvider provider = injector.getInstance(SharedPoolDataSourceProvider.class);
86  
87      SharedPoolDataSource dataSource = (SharedPoolDataSource) provider.get();
88  
89      assertEquals(connectionPoolDataSource, dataSource.getConnectionPoolDataSource());
90      assertEquals(autoCommit, dataSource.isDefaultAutoCommit());
91      assertEquals(defaultReadOnly, dataSource.isDefaultReadOnly());
92      assertEquals(defaultTransactionIsolation, dataSource.getDefaultTransactionIsolation());
93      assertEquals(description, dataSource.getDescription());
94      assertEquals(loginTimeout, dataSource.getLoginTimeoutDuration().getSeconds());
95      assertEquals(defaultMinEvictableIdleTimeMillis, dataSource.getDefaultMinEvictableIdleDuration().toMillis());
96      assertEquals(defaultNumTestsPerEvictionRun, dataSource.getDefaultNumTestsPerEvictionRun());
97      assertEquals(rollbackAfterValidation, dataSource.isRollbackAfterValidation());
98      assertEquals(defaultTestOnBorrow, dataSource.getDefaultTestOnBorrow());
99      assertEquals(defaultTestOnReturn, dataSource.getDefaultTestOnReturn());
100     assertEquals(defaultTestWhileIdle, dataSource.getDefaultTestWhileIdle());
101     assertEquals(defaultTimeBetweenEvictionRunsMillis, dataSource.getDefaultDurationBetweenEvictionRuns().toMillis());
102     assertEquals(validationQuery, dataSource.getValidationQuery());
103     assertEquals(defaultMaxTotal, dataSource.getDefaultMaxTotal());
104     assertEquals(defaultMaxIdle, dataSource.getDefaultMaxIdle());
105     assertEquals(defaultMaxWaitMillis, dataSource.getDefaultMaxWait().toMillis());
106   }
107 
108   @Test
109   void get_OtherValues() throws Throwable {
110     final boolean autoCommit = false;
111     final int loginTimeout = 11;
112     final boolean defaultReadOnly = false;
113     final int defaultTransactionIsolation = Connection.TRANSACTION_REPEATABLE_READ;
114     final String description = "test_description2";
115     final int defaultMinEvictableIdleTimeMillis = 31;
116     final int defaultNumTestsPerEvictionRun = 41;
117     final boolean rollbackAfterValidation = false;
118     final boolean defaultTestOnBorrow = false;
119     final boolean defaultTestOnReturn = false;
120     final boolean defaultTestWhileIdle = false;
121     final int defaultTimeBetweenEvictionRunsMillis = 51;
122     final String validationQuery = "SELECT 2";
123     final int defaultMaxTotal = 61;
124     final int defaultMaxIdle = 71;
125     final int defaultMaxWaitMillis = 81;
126     Injector injector = Guice.createInjector(new AbstractModule() {
127       @Override
128       protected void configure() {
129         bind(ConnectionPoolDataSource.class).toInstance(connectionPoolDataSource);
130         bindConstant().annotatedWith(Names.named("JDBC.autoCommit")).to(autoCommit);
131         bindConstant().annotatedWith(Names.named("JDBC.loginTimeout")).to(loginTimeout);
132         bindConstant().annotatedWith(Names.named("DBCP.defaultReadOnly")).to(defaultReadOnly);
133         bindConstant().annotatedWith(Names.named("DBCP.defaultTransactionIsolation")).to(defaultTransactionIsolation);
134         bindConstant().annotatedWith(Names.named("DBCP.description")).to(description);
135         bindConstant().annotatedWith(Names.named("DBCP.defaultMinEvictableIdleTimeMillis"))
136             .to(defaultMinEvictableIdleTimeMillis);
137         bindConstant().annotatedWith(Names.named("DBCP.defaultNumTestsPerEvictionRun"))
138             .to(defaultNumTestsPerEvictionRun);
139         bindConstant().annotatedWith(Names.named("DBCP.rollbackAfterValidation")).to(rollbackAfterValidation);
140         bindConstant().annotatedWith(Names.named("DBCP.defaultTestOnBorrow")).to(defaultTestOnBorrow);
141         bindConstant().annotatedWith(Names.named("DBCP.defaultTestOnReturn")).to(defaultTestOnReturn);
142         bindConstant().annotatedWith(Names.named("DBCP.defaultTestWhileIdle")).to(defaultTestWhileIdle);
143         bindConstant().annotatedWith(Names.named("DBCP.defaultTimeBetweenEvictionRunsMillis"))
144             .to(defaultTimeBetweenEvictionRunsMillis);
145         bindConstant().annotatedWith(Names.named("DBCP.validationQuery")).to(validationQuery);
146         bindConstant().annotatedWith(Names.named("DBCP.defaultMaxTotal")).to(defaultMaxTotal);
147         bindConstant().annotatedWith(Names.named("DBCP.defaultMaxIdle")).to(defaultMaxIdle);
148         bindConstant().annotatedWith(Names.named("DBCP.defaultMaxWaitMillis")).to(defaultMaxWaitMillis);
149       }
150     });
151     SharedPoolDataSourceProvider provider = injector.getInstance(SharedPoolDataSourceProvider.class);
152 
153     SharedPoolDataSource dataSource = (SharedPoolDataSource) provider.get();
154 
155     assertEquals(connectionPoolDataSource, dataSource.getConnectionPoolDataSource());
156     assertEquals(autoCommit, dataSource.isDefaultAutoCommit());
157     assertEquals(defaultReadOnly, dataSource.isDefaultReadOnly());
158     assertEquals(defaultTransactionIsolation, dataSource.getDefaultTransactionIsolation());
159     assertEquals(description, dataSource.getDescription());
160     assertEquals(loginTimeout, dataSource.getLoginTimeoutDuration().getSeconds());
161     assertEquals(defaultMinEvictableIdleTimeMillis, dataSource.getDefaultMinEvictableIdleDuration().toMillis());
162     assertEquals(defaultNumTestsPerEvictionRun, dataSource.getDefaultNumTestsPerEvictionRun());
163     assertEquals(rollbackAfterValidation, dataSource.isRollbackAfterValidation());
164     assertEquals(defaultTestOnBorrow, dataSource.getDefaultTestOnBorrow());
165     assertEquals(defaultTestOnReturn, dataSource.getDefaultTestOnReturn());
166     assertEquals(defaultTestWhileIdle, dataSource.getDefaultTestWhileIdle());
167     assertEquals(defaultTimeBetweenEvictionRunsMillis, dataSource.getDefaultDurationBetweenEvictionRuns().toMillis());
168     assertEquals(validationQuery, dataSource.getValidationQuery());
169     assertEquals(defaultMaxTotal, dataSource.getDefaultMaxTotal());
170     assertEquals(defaultMaxIdle, dataSource.getDefaultMaxIdle());
171     assertEquals(defaultMaxWaitMillis, dataSource.getDefaultMaxWait().toMillis());
172   }
173 
174   @Test
175   void get_Jndi() throws Throwable {
176     final String jndiKey = "test_key";
177     final String jndiValue = "test_value";
178     Injector injector = Guice.createInjector(new AbstractModule() {
179       @Override
180       protected void configure() {
181         bind(ConnectionPoolDataSource.class).toInstance(connectionPoolDataSource);
182         bindConstant().annotatedWith(Names.named("DBCP.jndi.key")).to(jndiKey);
183         bindConstant().annotatedWith(Names.named("DBCP.jndi.value")).to(jndiValue);
184       }
185     });
186     SharedPoolDataSourceProvider provider = injector.getInstance(SharedPoolDataSourceProvider.class);
187 
188     SharedPoolDataSource dataSource = (SharedPoolDataSource) provider.get();
189 
190     assertEquals(connectionPoolDataSource, dataSource.getConnectionPoolDataSource());
191     assertEquals(jndiValue, dataSource.getJndiEnvironment(jndiKey));
192   }
193 
194   @Test
195   void get_DataSourceName() throws Throwable {
196     final String name = "test_name";
197     Injector injector = Guice.createInjector(new AbstractModule() {
198       @Override
199       protected void configure() {
200         bindConstant().annotatedWith(Names.named("DBCP.name")).to(name);
201       }
202     });
203     SharedPoolDataSourceProvider provider = injector.getInstance(SharedPoolDataSourceProvider.class);
204 
205     SharedPoolDataSource dataSource = (SharedPoolDataSource) provider.get();
206 
207     assertEquals(name, dataSource.getDataSourceName());
208   }
209 }