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.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertNotEquals;
21  import static org.junit.jupiter.api.Assertions.assertNull;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.sql.ResultSet;
25  import java.sql.Statement;
26  import java.util.Arrays;
27  
28  import org.apache.commons.dbcp2.PoolingConnection.StatementType;
29  import org.junit.jupiter.api.Assertions;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Tests {@link PStmtKey}.
34   */
35  public class TestPStmtKey {
36  
37      /**
38       * Tests constructors with different catalog.
39       */
40      @Test
41      public void testCtorDifferentCatalog() {
42          Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1"), new PStmtKey("sql", "catalog2", "schema1"));
43          Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0),
44                  new PStmtKey("sql", "catalog2", "schema1", 0));
45          Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0),
46                  new PStmtKey("sql", "catalog2", "schema1", 0, 0));
47          Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0),
48                  new PStmtKey("sql", "catalog2", "schema1", 0, 0, 0));
49          //
50          Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0, StatementType.CALLABLE_STATEMENT),
51                  new PStmtKey("sql", "catalog2", "schema1", 0, 0, 0, StatementType.CALLABLE_STATEMENT));
52          Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0, StatementType.PREPARED_STATEMENT),
53                  new PStmtKey("sql", "catalog2", "schema1", 0, 0, 0, StatementType.PREPARED_STATEMENT));
54          //
55          Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, StatementType.CALLABLE_STATEMENT),
56                  new PStmtKey("sql", "catalog2", "schema1", 0, 0, StatementType.CALLABLE_STATEMENT));
57          Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, StatementType.PREPARED_STATEMENT),
58                  new PStmtKey("sql", "catalog2", "schema1", 0, 0, StatementType.PREPARED_STATEMENT));
59          //
60          Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", (int[]) null),
61                  new PStmtKey("sql", "catalog2", "schema1", (int[]) null));
62          Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", new int[1]),
63                  new PStmtKey("sql", "catalog2", "schema1", new int[1]));
64          Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", (String[]) null),
65                  new PStmtKey("sql", "catalog2", "schema1", (String[]) null));
66          Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", new String[] {"A" }),
67                  new PStmtKey("sql", "catalog2", "schema1", new String[] {"A" }));
68          Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", StatementType.PREPARED_STATEMENT),
69                  new PStmtKey("sql", "catalog2", "schema1", StatementType.PREPARED_STATEMENT));
70          Assertions.assertNotEquals(
71                  new PStmtKey("sql", "catalog1", "schema1", StatementType.PREPARED_STATEMENT, Integer.MAX_VALUE),
72                  new PStmtKey("sql", "catalog2", "schema1", StatementType.PREPARED_STATEMENT, Integer.MAX_VALUE));
73      }
74  
75      /**
76       * Tests constructors with different schemas.
77       */
78      @Test
79      public void testCtorDifferentSchema() {
80          Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1"), new PStmtKey("sql", "catalog1", "schema2"));
81          Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0),
82                  new PStmtKey("sql", "catalog1", "schema2", 0));
83          Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0),
84                  new PStmtKey("sql", "catalog1", "schema2", 0, 0));
85          Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0),
86                  new PStmtKey("sql", "catalog1", "schema2", 0, 0, 0));
87          //
88          Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0, StatementType.CALLABLE_STATEMENT),
89                  new PStmtKey("sql", "catalog1", "schema2", 0, 0, 0, StatementType.CALLABLE_STATEMENT));
90          Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0, StatementType.PREPARED_STATEMENT),
91                  new PStmtKey("sql", "catalog1", "schema2", 0, 0, 0, StatementType.PREPARED_STATEMENT));
92          //
93          Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, StatementType.CALLABLE_STATEMENT),
94                  new PStmtKey("sql", "catalog1", "schema2", 0, 0, StatementType.CALLABLE_STATEMENT));
95          Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, StatementType.PREPARED_STATEMENT),
96                  new PStmtKey("sql", "catalog1", "schema2", 0, 0, StatementType.PREPARED_STATEMENT));
97          //
98          Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", (int[]) null),
99                  new PStmtKey("sql", "catalog1", "schema2", (int[]) null));
100         Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", new int[1]),
101                 new PStmtKey("sql", "catalog1", "schema2", new int[1]));
102         Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", (String[]) null),
103                 new PStmtKey("sql", "catalog1", "schema2", (String[]) null));
104         Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", new String[] {"A" }),
105                 new PStmtKey("sql", "catalog1", "schema2", new String[] {"A" }));
106         Assertions.assertNotEquals(new PStmtKey("sql", "catalog1", "schema1", StatementType.PREPARED_STATEMENT),
107                 new PStmtKey("sql", "catalog1", "schema2", StatementType.PREPARED_STATEMENT));
108         Assertions.assertNotEquals(
109                 new PStmtKey("sql", "catalog1", "schema1", StatementType.PREPARED_STATEMENT, Integer.MAX_VALUE),
110                 new PStmtKey("sql", "catalog1", "schema2", StatementType.PREPARED_STATEMENT, Integer.MAX_VALUE));
111     }
112 
113     /**
114      * Tests constructors with different catalog.
115      */
116     @Test
117     public void testCtorEquals() {
118         Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1"), new PStmtKey("sql", "catalog1", "schema1"));
119         Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", 0),
120                 new PStmtKey("sql", "catalog1", "schema1", 0));
121         Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0),
122                 new PStmtKey("sql", "catalog1", "schema1", 0, 0));
123         Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0),
124                 new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0));
125         //
126         Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0, StatementType.CALLABLE_STATEMENT),
127                 new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0, StatementType.CALLABLE_STATEMENT));
128         Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0, StatementType.PREPARED_STATEMENT),
129                 new PStmtKey("sql", "catalog1", "schema1", 0, 0, 0, StatementType.PREPARED_STATEMENT));
130         //
131         Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, StatementType.CALLABLE_STATEMENT),
132                 new PStmtKey("sql", "catalog1", "schema1", 0, 0, StatementType.CALLABLE_STATEMENT));
133         Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", 0, 0, StatementType.PREPARED_STATEMENT),
134                 new PStmtKey("sql", "catalog1", "schema1", 0, 0, StatementType.PREPARED_STATEMENT));
135         //
136         Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", (int[]) null),
137                 new PStmtKey("sql", "catalog1", "schema1", (int[]) null));
138         Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", new int[1]),
139                 new PStmtKey("sql", "catalog1", "schema1", new int[1]));
140         Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", (String[]) null),
141                 new PStmtKey("sql", "catalog1", "schema1", (String[]) null));
142         Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", new String[] {"A" }),
143                 new PStmtKey("sql", "catalog1", "schema1", new String[] {"A" }));
144         Assertions.assertEquals(new PStmtKey("sql", "catalog1", "schema1", StatementType.PREPARED_STATEMENT),
145                 new PStmtKey("sql", "catalog1", "schema1", StatementType.PREPARED_STATEMENT));
146         Assertions.assertEquals(
147                 new PStmtKey("sql", "catalog1", "schema1", StatementType.PREPARED_STATEMENT, Integer.MAX_VALUE),
148                 new PStmtKey("sql", "catalog1", "schema1", StatementType.PREPARED_STATEMENT, Integer.MAX_VALUE));
149     }
150 
151     /**
152      * Tests {@link org.apache.commons.dbcp2.PStmtKey#PStmtKey(String, String, String, int[])}.
153      *
154      * See https://issues.apache.org/jira/browse/DBCP-494
155      */
156     @Test
157     public void testCtorStringStringArrayOfInts() {
158         final int[] input = {0, 0};
159         final PStmtKey pStmtKey = new PStmtKey("", "", "", input);
160         Assertions.assertArrayEquals(input, pStmtKey.getColumnIndexes());
161         input[0] = 1;
162         input[1] = 1;
163         Assertions.assertFalse(Arrays.equals(input, pStmtKey.getColumnIndexes()));
164     }
165 
166     /**
167      * Tests {@link org.apache.commons.dbcp2.PStmtKey#PStmtKey(String, String, String, int[])}.
168      *
169      * See https://issues.apache.org/jira/browse/DBCP-494
170      */
171     @Test
172     public void testCtorStringStringArrayOfNullInts() {
173         final int[] input = null;
174         final PStmtKey pStmtKey = new PStmtKey("", "", "", input);
175         Assertions.assertArrayEquals(input, pStmtKey.getColumnIndexes());
176     }
177 
178     /**
179      * Tests {@link org.apache.commons.dbcp2.PStmtKey#PStmtKey(String, String, String, String[])}.
180      *
181      * See https://issues.apache.org/jira/browse/DBCP-494
182      */
183     @Test
184     public void testCtorStringStringArrayOfNullStrings() {
185         final String[] input = null;
186         final PStmtKey pStmtKey = new PStmtKey("", "", "", input);
187         Assertions.assertArrayEquals(input, pStmtKey.getColumnNames());
188     }
189 
190     /**
191      * Tests {@link org.apache.commons.dbcp2.PStmtKey#PStmtKey(String, String, String, String[])}.
192      *
193      * See https://issues.apache.org/jira/browse/DBCP-494
194      */
195     @Test
196     public void testCtorStringStringArrayOfStrings() {
197         final String[] input = {"A", "B"};
198         final PStmtKey pStmtKey = new PStmtKey("", "", "", input);
199         Assertions.assertArrayEquals(input, pStmtKey.getColumnNames());
200         input[0] = "C";
201         input[1] = "D";
202         Assertions.assertFalse(Arrays.equals(input, pStmtKey.getColumnNames()));
203     }
204 
205     @Test
206     public void testEquals() {
207         final PStmtKey pStmtKey = new PStmtKey("SELECT 1", "catalog", "public",
208                 java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY,
209                 StatementType.CALLABLE_STATEMENT);
210         assertEquals(pStmtKey, pStmtKey);
211         assertNotEquals(null, pStmtKey);
212         assertNotEquals(pStmtKey, new Object());
213         assertNotEquals(pStmtKey, new PStmtKey("SELECT 2", "catalog", "public",
214                 ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY,
215                 StatementType.CALLABLE_STATEMENT));
216         assertNotEquals(pStmtKey, new PStmtKey("SELECT 1", "anothercatalog", "public",
217                 ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY,
218                 StatementType.CALLABLE_STATEMENT));
219         assertNotEquals(pStmtKey, new PStmtKey("SELECT 1", "catalog", "private",
220                 ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY,
221                 StatementType.CALLABLE_STATEMENT));
222         assertNotEquals(pStmtKey, new PStmtKey("SELECT 1", "catalog", "public",
223                 ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_READ_ONLY,
224                 StatementType.CALLABLE_STATEMENT));
225         assertNotEquals(pStmtKey, new PStmtKey("SELECT 1", "catalog", "public",
226                 ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE,
227                 StatementType.CALLABLE_STATEMENT));
228         assertNotEquals(pStmtKey, new PStmtKey("SELECT 1", "catalog", "public",
229                 ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY,
230                 StatementType.PREPARED_STATEMENT));
231         assertEquals(pStmtKey, new PStmtKey("SELECT 1", "catalog", "public",
232                 ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY,
233                 StatementType.CALLABLE_STATEMENT));
234         assertEquals(pStmtKey.hashCode(), new PStmtKey("SELECT 1", "catalog", "public",
235                 java.sql.ResultSet.TYPE_SCROLL_INSENSITIVE, java.sql.ResultSet.CONCUR_READ_ONLY,
236                 StatementType.CALLABLE_STATEMENT).hashCode());
237     }
238 
239     @Test
240     public void testGettersSetters() {
241         final PStmtKey pStmtKey = new PStmtKey("SELECT 1", "catalog", "public");
242         assertEquals("SELECT 1", pStmtKey.getSql());
243         assertEquals("public", pStmtKey.getSchema());
244         assertEquals("catalog", pStmtKey.getCatalog());
245         assertNull(pStmtKey.getAutoGeneratedKeys());
246         assertNull(pStmtKey.getResultSetConcurrency());
247         assertNull(pStmtKey.getResultSetHoldability());
248         assertNull(pStmtKey.getResultSetType());
249         assertEquals(StatementType.PREPARED_STATEMENT, pStmtKey.getStmtType());
250     }
251 
252     @Test
253     public void testToString() {
254         final PStmtKey pStmtKey = new PStmtKey("SELECT 1", "catalog", "public",
255                 StatementType.CALLABLE_STATEMENT, Statement.RETURN_GENERATED_KEYS);
256         assertTrue(pStmtKey.toString().contains("sql=SELECT 1"));
257         assertTrue(pStmtKey.toString().contains("schema=public"));
258         assertTrue(pStmtKey.toString().contains("autoGeneratedKeys=1"));
259         assertTrue(pStmtKey.toString().contains("statementType=CALLABLE_STATEMENT"));
260     }
261 }