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    *      https://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;
19  
20  import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
21  import static org.junit.jupiter.api.Assertions.assertEquals;
22  import static org.junit.jupiter.api.Assertions.assertFalse;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import java.util.Arrays;
27  import java.util.Collection;
28  import java.util.Collections;
29  import java.util.HashSet;
30  
31  import org.junit.jupiter.api.Test;
32  
33  public class TestUtils {
34  
35      public static PStmtKey getPStmtKey(final PoolablePreparedStatement<PStmtKey> poolablePreparedStatement) {
36          return poolablePreparedStatement.getKey();
37      }
38  
39      @Test
40      void testCheckForConflictsBothCollectionsNull() {
41          assertDoesNotThrow(() -> Utils.checkSqlCodes(null, null));
42      }
43  
44      @Test
45      void testCheckForConflictsEmptyCollections() {
46          final Collection<String> codes1 = Collections.emptySet();
47          final Collection<String> codes2 = Collections.emptySet();
48          assertDoesNotThrow(() -> Utils.checkSqlCodes(codes1, codes2));
49      }
50  
51      @Test
52      void testCheckForConflictsFirstCollectionNull() {
53          final Collection<String> codes1 = null;
54          final Collection<String> codes2 = new HashSet<>(Arrays.asList("08005", "08007"));
55          assertDoesNotThrow(() -> Utils.checkSqlCodes(codes1, codes2));
56      }
57  
58      @Test
59      void testCheckForConflictsNoOverlap() {
60          final Collection<String> codes1 = new HashSet<>(Arrays.asList("08003", "08006"));
61          final Collection<String> codes2 = new HashSet<>(Arrays.asList("08005", "08007"));
62          assertDoesNotThrow(() -> Utils.checkSqlCodes(codes1, codes2));
63      }
64  
65      @Test
66      void testCheckForConflictsSecondCollectionNull() {
67          final Collection<String> codes1 = new HashSet<>(Arrays.asList("08003", "08006"));
68          final Collection<String> codes2 = null;
69          assertDoesNotThrow(() -> Utils.checkSqlCodes(codes1, codes2));
70      }
71  
72      @Test
73      void testCheckForConflictsWith1Overlap() {
74          final Collection<String> codes1 = new HashSet<>(Arrays.asList("08003", "08006"));
75          final Collection<String> codes2 = new HashSet<>(Arrays.asList("08005", "08006"));
76          final IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> Utils.checkSqlCodes(codes1, codes2));
77          assertEquals("[08006] cannot be in both disconnectionSqlCodes and disconnectionIgnoreSqlCodes.", exception.getMessage());
78      }
79  
80      @Test
81      void testCheckForConflictsWith2Overlap() {
82          final Collection<String> codes1 = new HashSet<>(Arrays.asList("08003", "08006", "08007"));
83          final Collection<String> codes2 = new HashSet<>(Arrays.asList("08005", "08006", "08007"));
84          final IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> Utils.checkSqlCodes(codes1, codes2));
85          assertEquals("[08006, 08007] cannot be in both disconnectionSqlCodes and disconnectionIgnoreSqlCodes.", exception.getMessage());
86      }
87  
88      @Test
89      void testClassLoads() {
90          Utils.closeQuietly((AutoCloseable) null);
91      }
92  
93      @Test
94      void testIsDisconnectionSqlCode() {
95          assertTrue(Utils.isDisconnectionSqlCode("57P01"), "57P01 should be recognised as a disconnection SQL code.");
96          assertTrue(Utils.isDisconnectionSqlCode("01002"), "01002 should be recognised as a disconnection SQL code.");
97          assertTrue(Utils.isDisconnectionSqlCode("JZ0C0"), "JZ0C0 should be recognised as a disconnection SQL code.");
98  
99          assertFalse(Utils.isDisconnectionSqlCode("INVALID"), "INVALID should not be recognised as a disconnection SQL code.");
100         assertFalse(Utils.isDisconnectionSqlCode("00000"), "00000 should not be recognised as a disconnection SQL code.");
101     }
102 }