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  package org.apache.commons.dbcp2;
18  
19  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  
22  import java.time.Duration;
23  
24  import org.apache.commons.pool2.impl.GenericKeyedObjectPool;
25  import org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig;
26  import org.junit.jupiter.api.AfterEach;
27  import org.junit.jupiter.api.BeforeEach;
28  import org.junit.jupiter.api.Test;
29  
30  public class TestPoolingConnection {
31  
32      private PoolingConnection connection;
33  
34      @SuppressWarnings("resource") // Resources closed in @AfterEach method.
35      @BeforeEach
36      public void setUp() throws Exception {
37          connection = new PoolingConnection(new TesterConnection("test", "test"));
38          final GenericKeyedObjectPoolConfig<DelegatingPreparedStatement> config = new GenericKeyedObjectPoolConfig<>();
39          config.setMaxTotalPerKey(-1);
40          config.setBlockWhenExhausted(false);
41          config.setMaxWait(Duration.ZERO);
42          config.setMaxIdlePerKey(1);
43          config.setMaxTotal(1);
44          connection.setStatementPool(new GenericKeyedObjectPool<>(connection, config));
45      }
46  
47      @AfterEach
48      public void tearDown() throws Exception {
49          connection.close();
50          connection = null;
51      }
52  
53      @Test
54      public void testPrepareCall() throws Exception {
55          final String sql = "select 'a' from dual";
56          try (final DelegatingCallableStatement statement = (DelegatingCallableStatement) connection.prepareCall(sql)) {
57              final TesterCallableStatement testStatement = (TesterCallableStatement) statement.getInnermostDelegate();
58              // assert
59              assertEquals(sql, testStatement.getSql());
60          }
61      }
62  
63      @Test
64      public void testPrepareCallWithResultSetConcurrency() throws Exception {
65          final String sql = "select 'a' from dual";
66          final int resultSetType = 0;
67          final int resultSetConcurrency = 0;
68          try (final DelegatingCallableStatement statement = (DelegatingCallableStatement) connection.prepareCall(sql, resultSetType, resultSetConcurrency)) {
69              final TesterCallableStatement testStatement = (TesterCallableStatement) statement.getInnermostDelegate();
70              // assert
71              assertEquals(sql, testStatement.getSql());
72              assertEquals(resultSetType, testStatement.getResultSetType());
73              assertEquals(resultSetConcurrency, testStatement.getResultSetConcurrency());
74          }
75      }
76  
77      @Test
78      public void testPrepareCallWithResultSetHoldability() throws Exception {
79          final String sql = "select 'a' from dual";
80          final int resultSetType = 0;
81          final int resultSetConcurrency = 0;
82          final int resultSetHoldability = 0;
83          try (final DelegatingCallableStatement statement = (DelegatingCallableStatement) connection.prepareCall(sql, resultSetType, resultSetConcurrency,
84              resultSetHoldability)) {
85              final TesterCallableStatement testStatement = (TesterCallableStatement) statement.getInnermostDelegate();
86              // assert
87              assertEquals(sql, testStatement.getSql());
88              assertEquals(resultSetType, testStatement.getResultSetType());
89              assertEquals(resultSetConcurrency, testStatement.getResultSetConcurrency());
90              assertEquals(resultSetHoldability, testStatement.getResultSetHoldability());
91          }
92      }
93  
94      @Test
95      public void testPrepareStatement() throws Exception {
96          final String sql = "select 'a' from dual";
97          try (final DelegatingPreparedStatement statement = (DelegatingPreparedStatement) connection.prepareStatement(sql)) {
98              final TesterPreparedStatement testStatement = (TesterPreparedStatement) statement.getInnermostDelegate();
99              // assert
100             assertEquals(sql, testStatement.getSql());
101         }
102     }
103 
104     @Test
105     public void testPrepareStatementWithAutoGeneratedKeys() throws Exception {
106         final String sql = "select 'a' from dual";
107         final int autoGeneratedKeys = 0;
108         try (final DelegatingPreparedStatement statement = (DelegatingPreparedStatement) connection.prepareStatement(sql, autoGeneratedKeys)) {
109             final TesterPreparedStatement testStatement = (TesterPreparedStatement) statement.getInnermostDelegate();
110             // assert
111             assertEquals(sql, testStatement.getSql());
112             assertEquals(autoGeneratedKeys, testStatement.getAutoGeneratedKeys());
113         }
114     }
115 
116     @Test
117     public void testPrepareStatementWithColumnIndexes() throws Exception {
118         final String sql = "select 'a' from dual";
119         final int[] columnIndexes = {1};
120         try (final DelegatingPreparedStatement statement = (DelegatingPreparedStatement) connection.prepareStatement(sql, columnIndexes)) {
121             final TesterPreparedStatement testStatement = (TesterPreparedStatement) statement.getInnermostDelegate();
122             // assert
123             assertEquals(sql, testStatement.getSql());
124             assertArrayEquals(columnIndexes, testStatement.getColumnIndexes());
125         }
126     }
127 
128     @Test
129     public void testPrepareStatementWithColumnNames() throws Exception {
130         final String sql = "select 'a' from dual";
131         final String[] columnNames = {"columnName1"};
132         try (final DelegatingPreparedStatement statement = (DelegatingPreparedStatement) connection.prepareStatement(sql, columnNames)) {
133             final TesterPreparedStatement testStatement = (TesterPreparedStatement) statement.getInnermostDelegate();
134             // assert
135             assertEquals(sql, testStatement.getSql());
136             assertArrayEquals(columnNames, testStatement.getColumnNames());
137         }
138     }
139 
140     @Test
141     public void testPrepareStatementWithResultSetConcurrency() throws Exception {
142         final String sql = "select 'a' from dual";
143         final int resultSetType = 0;
144         final int resultSetConcurrency = 0;
145         try (
146             final DelegatingPreparedStatement statement = (DelegatingPreparedStatement) connection.prepareStatement(sql, resultSetType, resultSetConcurrency)) {
147             final TesterPreparedStatement testStatement = (TesterPreparedStatement) statement.getInnermostDelegate();
148             // assert
149             assertEquals(sql, testStatement.getSql());
150             assertEquals(resultSetType, testStatement.getResultSetType());
151             assertEquals(resultSetConcurrency, testStatement.getResultSetConcurrency());
152         }
153     }
154 
155     @Test
156     public void testPrepareStatementWithResultSetHoldability() throws Exception {
157         final String sql = "select 'a' from dual";
158         final int resultSetType = 0;
159         final int resultSetConcurrency = 0;
160         final int resultSetHoldability = 0;
161         try (final DelegatingPreparedStatement statement = (DelegatingPreparedStatement) connection.prepareStatement(sql, resultSetType, resultSetConcurrency,
162             resultSetHoldability)) {
163             final TesterPreparedStatement testStatement = (TesterPreparedStatement) statement.getInnermostDelegate();
164             // assert
165             assertEquals(sql, testStatement.getSql());
166             assertEquals(resultSetType, testStatement.getResultSetType());
167             assertEquals(resultSetConcurrency, testStatement.getResultSetConcurrency());
168             assertEquals(resultSetHoldability, testStatement.getResultSetHoldability());
169         }
170     }
171 
172     /**
173      * Tests DBCP-596 PoolingConnection.toString() causes StackOverflowError.
174      */
175     @Test
176     public void testToStringStackOverflow() {
177         final PoolingConnection conn = new PoolingConnection(null);
178         final GenericKeyedObjectPoolConfig<DelegatingPreparedStatement> config = new GenericKeyedObjectPoolConfig<>();
179         final GenericKeyedObjectPool stmtPool = new GenericKeyedObjectPool<>(conn, config);
180         conn.setStatementPool(stmtPool);
181         conn.toString();
182     }
183 }