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