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.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertNotNull;
23  import static org.junit.jupiter.api.Assertions.assertNull;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import java.math.BigDecimal;
27  import java.sql.Connection;
28  import java.sql.DriverManager;
29  import java.sql.ResultSet;
30  import java.sql.SQLException;
31  import java.sql.Statement;
32  
33  import org.h2.jdbcx.JdbcDataSource;
34  import org.junit.jupiter.api.Test;
35  import org.mockito.Mockito;
36  
37  /**
38   * Tests {@link Jdbc41Bridge}.
39   */
40  class Jdbc41BridgeTest {
41  
42      private Connection getConnection() throws SQLException {
43          return DriverManager.getConnection("jdbc:h2:mem:test");
44      }
45  
46      @SuppressWarnings("resource")
47      @Test
48      void testAbort() throws SQLException {
49          // Normal
50          try (Connection conn = getConnection()) {
51              Jdbc41Bridge.abort(conn, r -> {
52                  // empty for now
53              });
54          }
55          // Force AbstractMethodError
56          try (Connection conn = getConnection()) {
57              final Connection spy = Mockito.spy(conn);
58              Mockito.doThrow(new AbstractMethodError()).when(spy).abort(r -> {
59                  // empty for now
60              });
61              Jdbc41Bridge.abort(spy, r -> {
62                  // empty for now
63              });
64          }
65      }
66  
67      @SuppressWarnings("resource")
68      @Test
69      void testCloseOnCompletion() throws SQLException {
70          // Normal
71          try (Connection conn = getConnection();
72                  Statement stmt = conn.createStatement()) {
73              Jdbc41Bridge.closeOnCompletion(stmt);
74          }
75          // Force AbstractMethodError
76          try (Connection conn = getConnection();
77                  Statement stmt = conn.createStatement()) {
78              final Statement spy = Mockito.spy(stmt);
79              Mockito.doThrow(new AbstractMethodError()).when(spy).closeOnCompletion();
80              Jdbc41Bridge.closeOnCompletion(spy);
81          }
82      }
83  
84      @SuppressWarnings("resource")
85      @Test
86      void testGeneratedKeyAlwaysReturned() throws SQLException {
87          // Normal
88          try (Connection conn = getConnection()) {
89              assertTrue(Jdbc41Bridge.generatedKeyAlwaysReturned(conn.getMetaData()));
90          }
91          // Cannot mock a final class
92          // Force AbstractMethodError
93  //        try (Connection conn = getConnection()) {
94  //            final DatabaseMetaData spy = Mockito.spy(conn.getMetaData());
95  //            Mockito.when(spy.generatedKeyAlwaysReturned()).thenThrow(AbstractMethodError.class);
96  //            assertTrue(Jdbc41Bridge.generatedKeyAlwaysReturned(spy));
97  //        }
98      }
99  
100     @Test
101     void testGetNetworkTimeout() throws SQLException {
102         // Normal
103         try (Connection conn = getConnection()) {
104             Jdbc41Bridge.setNetworkTimeout(conn, r -> {
105             }, 30_000);
106             // noop in H2
107             assertEquals(0, Jdbc41Bridge.getNetworkTimeout(conn));
108         }
109     }
110 
111     @Test
112     void testGetObjectIndex() throws SQLException {
113         // Normal
114         try (Connection conn = getConnection();
115                 ResultSet rs = conn.getMetaData().getTypeInfo()) {
116             rs.next();
117             assertNotNull(Jdbc41Bridge.getObject(rs, 1, String.class));
118             //
119             assertNotNull(Jdbc41Bridge.getObject(rs, 2, Integer.class));
120             assertNotNull(Jdbc41Bridge.getObject(rs, 2, Long.class));
121             assertNotNull(Jdbc41Bridge.getObject(rs, 2, Double.class));
122             assertNotNull(Jdbc41Bridge.getObject(rs, 2, Float.class));
123             assertNotNull(Jdbc41Bridge.getObject(rs, 2, Byte.class));
124             assertNotNull(Jdbc41Bridge.getObject(rs, 2, BigDecimal.class));
125             //
126             assertNotNull(Jdbc41Bridge.getObject(rs, 7, Short.class));
127             assertNotNull(Jdbc41Bridge.getObject(rs, 8, Boolean.class));
128         }
129     }
130 
131     @Test
132     void testGetObjectName() throws SQLException {
133         // Normal
134         try (Connection conn = getConnection();
135                 ResultSet rs = conn.getMetaData().getTypeInfo()) {
136             rs.next();
137             assertNotNull(Jdbc41Bridge.getObject(rs, "TYPE_NAME", String.class));
138             //
139             assertNotNull(Jdbc41Bridge.getObject(rs, "DATA_TYPE", Integer.class));
140             assertNotNull(Jdbc41Bridge.getObject(rs, "DATA_TYPE", Long.class));
141             assertNotNull(Jdbc41Bridge.getObject(rs, "DATA_TYPE", Double.class));
142             assertNotNull(Jdbc41Bridge.getObject(rs, "DATA_TYPE", Float.class));
143             assertNotNull(Jdbc41Bridge.getObject(rs, "DATA_TYPE", Byte.class));
144             assertNotNull(Jdbc41Bridge.getObject(rs, "DATA_TYPE", BigDecimal.class));
145             //
146             assertNotNull(Jdbc41Bridge.getObject(rs, "NULLABLE", Short.class));
147             assertNotNull(Jdbc41Bridge.getObject(rs, "CASE_SENSITIVE", Boolean.class));
148         }
149     }
150 
151     @Test
152     void testGetParentLogger() throws SQLException {
153         // Normal
154         try (Connection conn = getConnection();
155                 Statement stmt = conn.createStatement()) {
156             // returns null for H2 (not supported).
157             Jdbc41Bridge.getParentLogger(new JdbcDataSource());
158         }
159     }
160 
161     @SuppressWarnings("resource")
162     @Test
163     void testGetSchema() throws SQLException {
164         // Normal
165         try (Connection conn = getConnection()) {
166             assertNotNull(Jdbc41Bridge.getSchema(conn));
167             final Connection spy = Mockito.spy(conn);
168             Mockito.when(spy.getSchema()).thenThrow(AbstractMethodError.class);
169             assertNull(Jdbc41Bridge.getSchema(spy));
170         }
171     }
172 
173     @Test
174     void testIsCloseOnCompletion() throws SQLException {
175         // Normal
176         try (Connection conn = getConnection();
177                 Statement stmt = conn.createStatement()) {
178             assertFalse(Jdbc41Bridge.isCloseOnCompletion(stmt));
179         }
180     }
181 
182     @Test
183     void testSetNetworkTimeout() throws SQLException {
184         // Normal
185         try (Connection conn = getConnection();
186                 Statement stmt = conn.createStatement()) {
187             // noop in H2
188             Jdbc41Bridge.setNetworkTimeout(conn, r -> {
189                 // empty for now
190             }, 30_0000);
191             assertEquals(0, Jdbc41Bridge.getNetworkTimeout(conn));
192         }
193     }
194 
195     @Test
196     void testSetSchema() throws SQLException {
197         // Normal
198         try (Connection conn = getConnection();
199                 Statement stmt = conn.createStatement()) {
200             Jdbc41Bridge.setSchema(conn, Jdbc41Bridge.getSchema(conn));
201             final String expected = "PUBLIC";
202             Jdbc41Bridge.setSchema(conn, expected);
203             assertEquals(expected, Jdbc41Bridge.getSchema(conn));
204         }
205     }
206 }