View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  
18  package org.apache.commons.dbcp2.datasources;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  import static org.junit.jupiter.api.Assertions.assertNull;
24  import static org.junit.jupiter.api.Assertions.assertSame;
25  import static org.junit.jupiter.api.Assertions.assertThrows;
26  import static org.junit.jupiter.api.Assertions.assertTrue;
27  
28  import java.io.PrintWriter;
29  import java.sql.Connection;
30  import java.sql.SQLException;
31  import java.util.Properties;
32  
33  import org.apache.commons.dbcp2.cpdsadapter.DriverAdapterCPDS;
34  import org.junit.jupiter.api.AfterEach;
35  import org.junit.jupiter.api.BeforeEach;
36  import org.junit.jupiter.api.Test;
37  
38  /**
39   */
40  public class TestInstanceKeyDataSource {
41  
42      private static final class ThrowOnSetupDefaultsDataSource extends SharedPoolDataSource {
43  
44          private static final long serialVersionUID = -448025812063133259L;
45  
46          ThrowOnSetupDefaultsDataSource() {
47          }
48  
49          @Override
50          protected void setupDefaults(final Connection connection, final String userName) throws SQLException {
51              throw new SQLException("bang!");
52          }
53      }
54  
55      private final static String DRIVER = "org.apache.commons.dbcp2.TesterDriver";
56      private final static String URL = "jdbc:apache:commons:testdriver";
57      private final static String USER = "foo";
58      private final static String PASS = "bar";
59  
60      private DriverAdapterCPDS pcds;
61      private SharedPoolDataSource spds;
62  
63      @BeforeEach
64      public void setUp() throws ClassNotFoundException {
65          pcds = new DriverAdapterCPDS();
66          pcds.setDriver(DRIVER);
67          pcds.setUrl(URL);
68          pcds.setUser(USER);
69          pcds.setPassword(PASS);
70          pcds.setPoolPreparedStatements(false);
71          spds = new SharedPoolDataSource();
72          spds.setConnectionPoolDataSource(pcds);
73      }
74  
75      @AfterEach
76      public void tearDown() throws Exception {
77          spds.close();
78      }
79  
80      @Test
81      public void testConnection() throws SQLException, ClassNotFoundException {
82          spds = new SharedPoolDataSource();
83          pcds.setDriver(DRIVER);
84          pcds.setUrl(URL);
85          spds.setConnectionPoolDataSource(pcds);
86          final PooledConnectionAndInfo info = spds.getPooledConnectionAndInfo(null, null);
87          assertNull(info.getUserName());
88          assertNull(info.getPassword());
89          try (final Connection conn = spds.getConnection()) {
90              assertNotNull(conn);
91          }
92      }
93  
94      @Test
95      public void testConnectionPoolDataSource() {
96          assertEquals(pcds, spds.getConnectionPoolDataSource());
97      }
98  
99      @Test
100     public void testConnectionPoolDataSourceAlreadySet() {
101         assertThrows(IllegalStateException.class, () -> spds.setConnectionPoolDataSource(new DriverAdapterCPDS()));
102     }
103 
104     @Test
105     public void testConnectionPoolDataSourceAlreadySetUsingJndi() {
106         spds = new SharedPoolDataSource();
107         spds.setDataSourceName("anything");
108         assertThrows(IllegalStateException.class, () -> spds.setConnectionPoolDataSource(new DriverAdapterCPDS()));
109     }
110 
111     @Test
112     public void testDataSourceName() {
113         spds = new SharedPoolDataSource();
114         assertNull(spds.getDataSourceName());
115         spds.setDataSourceName("anything");
116         assertEquals("anything", spds.getDataSourceName());
117     }
118 
119     @Test
120     public void testDataSourceNameAlreadySet() {
121         assertThrows(IllegalStateException.class, () -> spds.setDataSourceName("anything"));
122     }
123 
124     @Test
125     public void testDataSourceNameAlreadySetUsingJndi() {
126         spds = new SharedPoolDataSource();
127         spds.setDataSourceName("anything");
128         assertThrows(IllegalStateException.class, () -> spds.setDataSourceName("anything"));
129     }
130 
131     @Test
132     public void testDefaultBlockWhenExhausted() {
133         spds.setDefaultBlockWhenExhausted(true);
134         assertTrue(spds.getDefaultBlockWhenExhausted());
135         spds.setDefaultBlockWhenExhausted(false);
136         assertFalse(spds.getDefaultBlockWhenExhausted());
137     }
138 
139     @Test
140     public void testDefaultEvictionPolicyClassName() {
141         spds.setDefaultEvictionPolicyClassName(Object.class.getName());
142         assertEquals(Object.class.getName(), spds.getDefaultEvictionPolicyClassName());
143     }
144 
145     @Test
146     public void testDefaultLifo() {
147         spds.setDefaultLifo(true);
148         assertTrue(spds.getDefaultLifo());
149         spds.setDefaultLifo(false);
150         assertFalse(spds.getDefaultLifo());
151     }
152 
153     @Test
154     public void testDefaultMinIdle() {
155         spds.setDefaultMinIdle(10);
156         assertEquals(10, spds.getDefaultMinIdle());
157     }
158 
159     @Test
160     public void testDefaultReadOnly() {
161         spds.setDefaultReadOnly(true);
162         assertTrue(spds.isDefaultReadOnly());
163         spds.setDefaultReadOnly(false);
164         assertFalse(spds.isDefaultReadOnly());
165     }
166 
167     @Test
168     public void testDefaultSoftMinEvictableIdleTimeMillis() {
169         spds.setDefaultSoftMinEvictableIdleTimeMillis(10);
170         assertEquals(10, spds.getDefaultSoftMinEvictableIdleTimeMillis());
171     }
172 
173     @Test
174     public void testDefaultTestOnCreate() {
175         spds.setDefaultTestOnCreate(false);
176         assertFalse(spds.getDefaultTestOnCreate());
177         spds.setDefaultTestOnCreate(true);
178         assertTrue(spds.getDefaultTestOnCreate());
179     }
180 
181     @Test
182     public void testDefaultTransactionIsolation() {
183         assertEquals(InstanceKeyDataSource.UNKNOWN_TRANSACTIONISOLATION, spds.getDefaultTransactionIsolation());
184         spds.setDefaultTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
185         assertEquals(Connection.TRANSACTION_READ_COMMITTED, spds.getDefaultTransactionIsolation());
186     }
187 
188     @Test
189     public void testDefaultTransactionIsolationInvalid() {
190         assertEquals(InstanceKeyDataSource.UNKNOWN_TRANSACTIONISOLATION, spds.getDefaultTransactionIsolation());
191         assertThrows(IllegalArgumentException.class, () -> spds.setDefaultTransactionIsolation(Integer.MAX_VALUE));
192     }
193 
194     @Test
195     public void testDescription() {
196         spds.setDescription("anything");
197         assertEquals("anything", spds.getDescription());
198     }
199 
200     /**
201      * Verify that exception on setupDefaults does not leak PooledConnection
202      *
203      * JIRA: DBCP-237
204      * @throws Exception
205      */
206     @Test
207     public void testExceptionOnSetupDefaults() throws Exception {
208         try (final ThrowOnSetupDefaultsDataSource tds = new ThrowOnSetupDefaultsDataSource()) {
209             final int numConnections = tds.getNumActive();
210             assertThrows(SQLException.class, () -> tds.getConnection(USER, PASS));
211             assertEquals(numConnections, tds.getNumActive());
212         }
213     }
214 
215     @Test
216     public void testIsWrapperFor() throws Exception {
217         assertTrue(spds.isWrapperFor(InstanceKeyDataSource.class));
218         assertTrue(spds.isWrapperFor(AutoCloseable.class));
219     }
220 
221     @Test
222     public void testJndiEnvironment() {
223         assertNull(spds.getJndiEnvironment("name"));
224         final Properties properties = new Properties();
225         properties.setProperty("name", "clarke");
226         spds.setJndiEnvironment(properties);
227         assertEquals("clarke", spds.getJndiEnvironment("name"));
228         spds.setJndiEnvironment("name", "asimov");
229         assertEquals("asimov", spds.getJndiEnvironment("name"));
230     }
231 
232     @Test
233     public void testJndiNullProperties() {
234         assertThrows(NullPointerException.class, () -> spds.setJndiEnvironment(null));
235     }
236 
237     @Test
238     public void testJndiPropertiesCleared() {
239         spds.setJndiEnvironment("name", "king");
240         assertEquals("king", spds.getJndiEnvironment("name"));
241         final Properties properties = new Properties();
242         properties.setProperty("fish", "kohi");
243         spds.setJndiEnvironment(properties);
244         assertNull(spds.getJndiEnvironment("name"));
245     }
246 
247     @Test
248     public void testJndiPropertiesNotInitialized() {
249         assertNull(spds.getJndiEnvironment("name"));
250         spds.setJndiEnvironment("name", "king");
251         assertEquals("king", spds.getJndiEnvironment("name"));
252     }
253 
254     @Test
255     public void testLoginTimeout() {
256         spds.setLoginTimeout(10);
257         assertEquals(10, spds.getLoginTimeout());
258     }
259 
260     @SuppressWarnings("resource")
261     @Test
262     public void testLogWriter() {
263         spds.setLogWriter(new PrintWriter(System.out));
264         assertNotNull(spds.getLogWriter());
265     }
266 
267     @SuppressWarnings("resource")
268     @Test
269     public void testLogWriterAutoInitialized() {
270         assertNotNull(spds.getLogWriter());
271     }
272 
273     @Test
274     public void testMaxConnLifetimeMillis() {
275         assertEquals(-1, spds.getMaxConnLifetimeMillis());
276         spds.setMaxConnLifetimeMillis(10);
277         assertEquals(10, spds.getMaxConnLifetimeMillis());
278     }
279 
280     @Test
281     public void testRollbackAfterValidation() {
282         assertFalse(spds.isRollbackAfterValidation());
283         spds.setRollbackAfterValidation(true);
284         assertTrue(spds.isRollbackAfterValidation());
285     }
286 
287     @Test
288     public void testRollbackAfterValidationWithConnectionCalled() throws SQLException {
289         try (Connection connection = spds.getConnection()) {
290             assertFalse(spds.isRollbackAfterValidation());
291             assertThrows(IllegalStateException.class, () -> spds.setRollbackAfterValidation(true));
292         }
293     }
294 
295     @SuppressWarnings("resource")
296     @Test
297     public void testUnwrap() throws Exception {
298         assertSame(spds.unwrap(InstanceKeyDataSource.class), spds);
299         assertSame(spds.unwrap(AutoCloseable.class), spds);
300     }
301 
302     @Test
303     public void testValidationQuery() {
304         assertNull(spds.getValidationQuery());
305         spds.setValidationQuery("anything");
306         assertEquals("anything", spds.getValidationQuery());
307     }
308 
309     @Test
310     public void testValidationQueryTimeout() {
311         assertEquals(-1, spds.getValidationQueryTimeout());
312         spds.setValidationQueryTimeout(10);
313         assertEquals(10, spds.getValidationQueryTimeout());
314     }
315 
316     @Test
317     public void testValidationQueryWithConnectionCalled() throws SQLException {
318         try (Connection connection = spds.getConnection()) {
319             assertNull(spds.getValidationQuery());
320             assertThrows(IllegalStateException.class, () -> spds.setValidationQuery("anything"));
321         }
322     }
323 }