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.dbutils;
18  
19  import static org.junit.Assert.assertFalse;
20  import static org.junit.Assert.fail;
21  import static org.mockito.Mockito.doThrow;
22  import static org.mockito.Mockito.mock;
23  import static org.mockito.Mockito.reset;
24  import static org.mockito.Mockito.verify;
25  
26  import java.sql.Connection;
27  import java.sql.Driver;
28  import java.sql.ResultSet;
29  import java.sql.SQLException;
30  import java.sql.Statement;
31  import java.util.Properties;
32  
33  import org.junit.After;
34  import org.junit.Before;
35  import org.junit.Test;
36  
37  public class DbUtilsTest {
38  
39      public static class DriverProxyTest {
40          private static final Driver mockedDriver = mock(Driver.class);
41          private DbUtils.DriverProxy proxy;
42  
43          @Before
44          public void setUp() {
45              proxy = new DbUtils.DriverProxy(mockedDriver);
46          }
47  
48          @After
49          public void tearDown() {
50              reset(mockedDriver);
51          }
52  
53          @Test
54          public void testProxiedMethods() throws Exception {
55              proxy.getMajorVersion();
56              verify(mockedDriver).getMajorVersion();
57              proxy.getMinorVersion();
58              verify(mockedDriver).getMinorVersion();
59              proxy.jdbcCompliant();
60              verify(mockedDriver).jdbcCompliant();
61  
62              final String url = "testUrl";
63              proxy.acceptsURL(url);
64              verify(mockedDriver).acceptsURL(url);
65  
66              final Properties props = new Properties();
67              props.setProperty("test", "true");
68              proxy.connect(url, props);
69              verify(mockedDriver).connect(url, props);
70              proxy.getPropertyInfo(url, props);
71              verify(mockedDriver).getPropertyInfo(url, props);
72          }
73      }
74  
75      @Test
76      public void closeConnection() throws Exception {
77          final Connection mockCon = mock(Connection.class);
78          DbUtils.close(mockCon);
79          verify(mockCon).close();
80      }
81  
82      @Test
83      public void closeNullConnection() throws Exception {
84          DbUtils.close((Connection) null);
85      }
86  
87      @Test
88      public void closeNullResultSet() throws Exception {
89          DbUtils.close((ResultSet) null);
90      }
91  
92      @Test
93      public void closeNullStatement() throws Exception {
94          DbUtils.close((Statement) null);
95      }
96  
97      @Test
98      public void closeQuietlyConnection() throws Exception {
99          final Connection mockConnection = mock(Connection.class);
100         DbUtils.closeQuietly(mockConnection);
101         verify(mockConnection).close();
102     }
103 
104     @Test
105     public void closeQuietlyConnectionResultSetStatement() throws Exception {
106         final Connection mockConnection = mock(Connection.class);
107         final ResultSet mockResultSet = mock(ResultSet.class);
108         final Statement mockStatement = mock(Statement.class);
109         DbUtils.closeQuietly(mockConnection, mockStatement, mockResultSet);
110         verify(mockConnection).close();
111         verify(mockResultSet).close();
112         verify(mockStatement).close();
113     }
114 
115     @Test
116     public void closeQuietlyConnectionResultSetStatementThrowingException() throws Exception {
117         final Connection mockConnection = mock(Connection.class);
118         final ResultSet mockResultSet = mock(ResultSet.class);
119         final Statement mockStatement = mock(Statement.class);
120         doThrow(SQLException.class).when(mockStatement).close();
121         DbUtils.closeQuietly(mockConnection, mockStatement, mockResultSet);
122         verify(mockConnection).close();
123         verify(mockResultSet).close();
124         verify(mockStatement).close();
125     }
126 
127     @Test
128     public void closeQuietlyConnectionResultSetThrowingExceptionStatement() throws Exception {
129         final Connection mockConnection = mock(Connection.class);
130         final ResultSet mockResultSet = mock(ResultSet.class);
131         doThrow(SQLException.class).when(mockResultSet).close();
132         final Statement mockStatement = mock(Statement.class);
133         DbUtils.closeQuietly(mockConnection, mockStatement, mockResultSet);
134         verify(mockConnection).close();
135         verify(mockResultSet).close();
136         verify(mockStatement).close();
137     }
138 
139     @Test
140     public void closeQuietlyConnectionThrowingException() throws Exception {
141         final Connection mockConnection = mock(Connection.class);
142         doThrow(SQLException.class).when(mockConnection).close();
143         DbUtils.closeQuietly(mockConnection);
144     }
145 
146     @Test
147     public void closeQuietlyConnectionThrowingExceptionResultSetStatement() throws Exception {
148         final Connection mockConnection = mock(Connection.class);
149         doThrow(SQLException.class).when(mockConnection).close();
150         final ResultSet mockResultSet = mock(ResultSet.class);
151         final Statement mockStatement = mock(Statement.class);
152         DbUtils.closeQuietly(mockConnection, mockStatement, mockResultSet);
153         verify(mockConnection).close();
154         verify(mockResultSet).close();
155         verify(mockStatement).close();
156     }
157 
158     @Test
159     public void closeQuietlyNullConnection() throws Exception {
160         DbUtils.closeQuietly((Connection) null);
161     }
162 
163     @Test
164     public void closeQuietlyNullResultSet() throws Exception {
165         DbUtils.closeQuietly((ResultSet) null);
166     }
167 
168     @Test
169     public void closeQuietlyNullStatement() throws Exception {
170         DbUtils.closeQuietly((Statement) null);
171     }
172 
173     @Test
174     public void closeQuietlyResultSet() throws Exception {
175         final ResultSet mockResultSet = mock(ResultSet.class);
176         DbUtils.closeQuietly(mockResultSet);
177         verify(mockResultSet).close();
178     }
179 
180     @Test
181     public void closeQuietlyResultSetThrowingException() throws Exception {
182         final ResultSet mockResultSet = mock(ResultSet.class);
183         doThrow(SQLException.class).when(mockResultSet).close();
184         DbUtils.closeQuietly(mockResultSet);
185     }
186 
187     @Test
188     public void closeQuietlyStatement() throws Exception {
189         final Statement mockStatement = mock(Statement.class);
190         DbUtils.closeQuietly(mockStatement);
191         verify(mockStatement).close();
192     }
193 
194     @Test
195     public void closeQuietlyStatementThrowingException() throws Exception {
196         final Statement mockStatement = mock(Statement.class);
197         doThrow(SQLException.class).when(mockStatement).close();
198         DbUtils.closeQuietly(mockStatement);
199     }
200 
201     @Test
202     public void closeResultSet() throws Exception {
203         final ResultSet mockResultSet = mock(ResultSet.class);
204         DbUtils.close(mockResultSet);
205         verify(mockResultSet).close();
206     }
207 
208     @Test
209     public void closeStatement() throws Exception {
210         final Statement mockStatement = mock(Statement.class);
211         DbUtils.close(mockStatement);
212         verify(mockStatement).close();
213     }
214 
215     @Test
216     public void commitAndClose() throws Exception {
217         final Connection mockConnection = mock(Connection.class);
218         DbUtils.commitAndClose(mockConnection);
219         verify(mockConnection).commit();
220         verify(mockConnection).close();
221     }
222 
223     @Test
224     public void commitAndCloseQuietly() throws Exception {
225         final Connection mockConnection = mock(Connection.class);
226         DbUtils.commitAndClose(mockConnection);
227         verify(mockConnection).commit();
228         verify(mockConnection).close();
229     }
230 
231     @Test
232     public void commitAndCloseQuietlyWithException() throws Exception {
233         final Connection mockConnection = mock(Connection.class);
234         doThrow(SQLException.class).when(mockConnection).close();
235         DbUtils.commitAndCloseQuietly(mockConnection);
236         verify(mockConnection).commit();
237         verify(mockConnection).close();
238     }
239 
240     @Test
241     public void commitAndCloseWithException() throws Exception {
242         final Connection mockConnection = mock(Connection.class);
243         doThrow(SQLException.class).when(mockConnection).commit();
244         try {
245             DbUtils.commitAndClose(mockConnection);
246             fail("DbUtils.commitAndClose() swallowed SQLEception!");
247         } catch (final SQLException e) {
248             // we expect this exception
249         }
250         verify(mockConnection).close();
251     }
252 
253     @Test
254     public void rollback() throws Exception {
255         final Connection mockConnection = mock(Connection.class);
256         DbUtils.rollback(mockConnection);
257         verify(mockConnection).rollback();
258     }
259 
260     @Test
261     public void rollbackAndClose() throws Exception {
262         final Connection mockConnection = mock(Connection.class);
263         DbUtils.rollbackAndClose(mockConnection);
264         verify(mockConnection).rollback();
265         verify(mockConnection).close();
266     }
267 
268     @Test
269     public void rollbackAndCloseNull() throws Exception {
270         DbUtils.rollbackAndClose(null);
271     }
272 
273     @Test
274     public void rollbackAndCloseQuietly() throws Exception {
275         final Connection mockConnection = mock(Connection.class);
276         DbUtils.rollbackAndCloseQuietly(mockConnection);
277         verify(mockConnection).rollback();
278         verify(mockConnection).close();
279     }
280 
281     @Test
282     public void rollbackAndCloseQuietlyNull() throws Exception {
283         DbUtils.rollbackAndCloseQuietly(null);
284     }
285 
286     @Test
287     public void rollbackAndCloseQuietlyWithException() throws Exception {
288         final Connection mockConnection = mock(Connection.class);
289         doThrow(SQLException.class).when(mockConnection).rollback();
290         DbUtils.rollbackAndCloseQuietly(mockConnection);
291         verify(mockConnection).rollback();
292         verify(mockConnection).close();
293     }
294 
295     @Test
296     public void rollbackAndCloseWithException() throws Exception {
297         final Connection mockConnection = mock(Connection.class);
298         doThrow(SQLException.class).when(mockConnection).rollback();
299         try {
300             DbUtils.rollbackAndClose(mockConnection);
301             fail("DbUtils.rollbackAndClose() swallowed SQLException!");
302         } catch (final SQLException e) {
303             // we expect this exception
304         }
305         verify(mockConnection).rollback();
306         verify(mockConnection).close();
307     }
308 
309     @Test
310     public void rollbackNull() throws Exception {
311         DbUtils.rollback(null);
312     }
313 
314     @Test
315     public void rollbackQuietly() throws Exception {
316         final Connection mockConnection = mock(Connection.class);
317         DbUtils.rollbackQuietly(mockConnection);
318         verify(mockConnection).rollback();
319     }
320 
321     @Test
322     public void rollbackQuietlyNull() throws Exception {
323         DbUtils.rollbackQuietly(null);
324     }
325 
326     @Test
327     public void rollbackQuietlyWithException() throws Exception {
328         final Connection mockConnection = mock(Connection.class);
329         doThrow(SQLException.class).when(mockConnection).rollback();
330         DbUtils.rollbackQuietly(mockConnection);
331         verify(mockConnection).rollback();
332     }
333 
334     @Test
335     public void testCommitAndCloseQuietlyWithNullDoesNotThrowAnSQLException() {
336 
337         DbUtils.commitAndCloseQuietly(null);
338 
339     }
340 
341     @Test
342     public void testLoadDriverReturnsFalse() {
343 
344         assertFalse(DbUtils.loadDriver(""));
345 
346     }
347 }