1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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")
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
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
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
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
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
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
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
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
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
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
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 }