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