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  
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.assertNull;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  import static org.junit.jupiter.api.Assertions.fail;
25  import static org.mockito.Mockito.mock;
26  import static org.mockito.Mockito.times;
27  import static org.mockito.Mockito.verify;
28  
29  import java.lang.reflect.Proxy;
30  import java.sql.Connection;
31  import java.sql.SQLException;
32  import java.sql.Statement;
33  
34  import org.junit.jupiter.api.Assertions;
35  import org.junit.jupiter.api.BeforeEach;
36  import org.junit.jupiter.api.Test;
37  
38  public class TestDelegatingStatement {
39  
40      private static final class TesterStatementNonWrapping extends TesterStatement {
41  
42          public TesterStatementNonWrapping(final Connection conn) {
43              super(conn);
44          }
45  
46          @Override
47          public boolean isWrapperFor(final Class<?> iface) throws SQLException {
48              return false;
49          }
50      }
51  
52      private DelegatingConnection<Connection> delegatingConnection;
53      private TesterConnection testerConnection;
54      private Statement mockedStatement;
55      private DelegatingStatement delegatingStatement;
56      private DelegatingStatement delegatingTesterStatement;
57      private TesterResultSet testerResultSet;
58      private TesterStatement testerStatement;
59  
60      @BeforeEach
61      public void setUp() throws Exception {
62          testerConnection = new TesterConnection("test", "test");
63          delegatingConnection = new DelegatingConnection<>(testerConnection);
64          mockedStatement = mock(Statement.class);
65          testerStatement = new TesterStatement(testerConnection);
66          delegatingStatement = new DelegatingStatement(delegatingConnection, mockedStatement);
67          delegatingTesterStatement = new DelegatingStatement(delegatingConnection, testerStatement);
68          testerResultSet = new TesterResultSet(mockedStatement);
69      }
70  
71      @Test
72      public void testAddBatchString() throws Exception {
73          try {
74              delegatingStatement.addBatch("foo");
75          } catch (final SQLException e) {
76          }
77          verify(mockedStatement, times(1)).addBatch("foo");
78      }
79  
80      @Test
81      public void testCancel() throws Exception {
82          try {
83              delegatingStatement.cancel();
84          } catch (final SQLException e) {
85          }
86          verify(mockedStatement, times(1)).cancel();
87      }
88  
89      @Test
90      public void testCheckOpen() throws Exception {
91          delegatingStatement.checkOpen();
92          delegatingStatement.close();
93          try {
94              delegatingStatement.checkOpen();
95              fail("Expecting SQLException");
96          } catch (final SQLException ex) {
97              // expected
98          }
99      }
100 
101     @Test
102     public void testClearBatch() throws Exception {
103         try {
104             delegatingStatement.clearBatch();
105         } catch (final SQLException e) {
106         }
107         verify(mockedStatement, times(1)).clearBatch();
108     }
109 
110     @Test
111     public void testClearWarnings() throws Exception {
112         try {
113             delegatingStatement.clearWarnings();
114         } catch (final SQLException e) {
115         }
116         verify(mockedStatement, times(1)).clearWarnings();
117     }
118 
119     @Test
120     public void testClose() throws Exception {
121         try {
122             delegatingStatement.close();
123         } catch (final SQLException e) {
124         }
125         verify(mockedStatement, times(1)).close();
126     }
127 
128     @Test
129     public void testCloseOnCompletion() throws Exception {
130         try {
131             delegatingStatement.closeOnCompletion();
132         } catch (final SQLException e) {
133         }
134         verify(mockedStatement, times(1)).closeOnCompletion();
135     }
136 
137     @Test
138     public void testCloseWithResultSetCloseException() throws Exception {
139         try {
140             testerResultSet.setSqlExceptionOnClose(true);
141             delegatingStatement.addTrace(testerResultSet);
142             delegatingStatement.close();
143             Assertions.fail("Excpected a SQLExceptionList");
144         } catch (final SQLException e) {
145             Assertions.assertTrue(e instanceof SQLExceptionList);
146         } finally {
147             testerResultSet.setSqlExceptionOnClose(false);
148         }
149         verify(mockedStatement, times(1)).close();
150     }
151 
152     @Test
153     public void testCloseWithStatementCloseException() throws Exception {
154         try {
155             testerStatement.setSqlExceptionOnClose(true);
156             delegatingTesterStatement.close();
157             Assertions.fail("Excpected a SQLExceptionList");
158         } catch (final SQLException e) {
159             Assertions.assertTrue(e instanceof SQLExceptionList);
160         } finally {
161             testerStatement.setSqlExceptionOnClose(false);
162         }
163     }
164 
165     @Test
166     public void testExecuteBatch() throws Exception {
167         try {
168             delegatingStatement.executeBatch();
169         } catch (final SQLException e) {
170         }
171         verify(mockedStatement, times(1)).executeBatch();
172     }
173 
174     @Test
175     public void testExecuteLargeBatch() throws Exception {
176         try {
177             delegatingStatement.executeLargeBatch();
178         } catch (final SQLException e) {
179         }
180         verify(mockedStatement, times(1)).executeLargeBatch();
181     }
182 
183     @Test
184     public void testExecuteLargeUpdateString() throws Exception {
185         try {
186             delegatingStatement.executeLargeUpdate("foo");
187         } catch (final SQLException e) {
188         }
189         verify(mockedStatement, times(1)).executeLargeUpdate("foo");
190     }
191 
192     @Test
193     public void testExecuteLargeUpdateStringInteger() throws Exception {
194         try {
195             delegatingStatement.executeLargeUpdate("foo", 1);
196         } catch (final SQLException e) {
197         }
198         verify(mockedStatement, times(1)).executeLargeUpdate("foo", 1);
199     }
200 
201     @Test
202     public void testExecuteLargeUpdateStringIntegerArray() throws Exception {
203         try {
204             delegatingStatement.executeLargeUpdate("foo", (int[]) null);
205         } catch (final SQLException e) {
206         }
207         verify(mockedStatement, times(1)).executeLargeUpdate("foo", (int[]) null);
208     }
209 
210     @Test
211     public void testExecuteLargeUpdateStringStringArray() throws Exception {
212         try {
213             delegatingStatement.executeLargeUpdate("foo", (String[]) null);
214         } catch (final SQLException e) {
215         }
216         verify(mockedStatement, times(1)).executeLargeUpdate("foo", (String[]) null);
217     }
218 
219     @Test
220     public void testExecuteQueryReturnsNull() throws Exception {
221         assertNull(delegatingStatement.executeQuery("null"));
222     }
223 
224     @Test
225     public void testExecuteQueryString() throws Exception {
226         try {
227             delegatingStatement.executeQuery("foo");
228         } catch (final SQLException e) {
229         }
230         verify(mockedStatement, times(1)).executeQuery("foo");
231     }
232 
233     @Test
234     public void testExecuteString() throws Exception {
235         try {
236             delegatingStatement.execute("foo");
237         } catch (final SQLException e) {
238         }
239         verify(mockedStatement, times(1)).execute("foo");
240     }
241 
242     @Test
243     public void testExecuteStringInteger() throws Exception {
244         try {
245             delegatingStatement.execute("foo", 1);
246         } catch (final SQLException e) {
247         }
248         verify(mockedStatement, times(1)).execute("foo", 1);
249     }
250 
251     @Test
252     public void testExecuteStringIntegerArray() throws Exception {
253         try {
254             delegatingStatement.execute("foo", (int[]) null);
255         } catch (final SQLException e) {
256         }
257         verify(mockedStatement, times(1)).execute("foo", (int[]) null);
258     }
259 
260     @Test
261     public void testExecuteStringStringArray() throws Exception {
262         try {
263             delegatingStatement.execute("foo", (String[]) null);
264         } catch (final SQLException e) {
265         }
266         verify(mockedStatement, times(1)).execute("foo", (String[]) null);
267     }
268 
269     @Test
270     public void testExecuteUpdateString() throws Exception {
271         try {
272             delegatingStatement.executeUpdate("foo");
273         } catch (final SQLException e) {
274         }
275         verify(mockedStatement, times(1)).executeUpdate("foo");
276     }
277 
278     @Test
279     public void testExecuteUpdateStringInteger() throws Exception {
280         try {
281             delegatingStatement.executeUpdate("foo", 1);
282         } catch (final SQLException e) {
283         }
284         verify(mockedStatement, times(1)).executeUpdate("foo", 1);
285     }
286 
287     @Test
288     public void testExecuteUpdateStringIntegerArray() throws Exception {
289         try {
290             delegatingStatement.executeUpdate("foo", (int[]) null);
291         } catch (final SQLException e) {
292         }
293         verify(mockedStatement, times(1)).executeUpdate("foo", (int[]) null);
294     }
295 
296     @Test
297     public void testExecuteUpdateStringStringArray() throws Exception {
298         try {
299             delegatingStatement.executeUpdate("foo", (String[]) null);
300         } catch (final SQLException e) {
301         }
302         verify(mockedStatement, times(1)).executeUpdate("foo", (String[]) null);
303     }
304 
305     /**
306      * This method is a bit special, and return the delegate connection, not the
307      * wrapped statement's connection.
308      *
309      * @throws Exception
310      */
311     @Test
312     public void testGetConnection() throws Exception {
313         try {
314             delegatingStatement.getConnection();
315         } catch (final SQLException e) {
316         }
317         verify(mockedStatement, times(0)).getConnection();
318     }
319 
320     @Test
321     public void testGetDelegate() throws Exception {
322         assertEquals(mockedStatement, delegatingStatement.getDelegate());
323     }
324 
325     @Test
326     public void testGetFetchDirection() throws Exception {
327         try {
328             delegatingStatement.getFetchDirection();
329         } catch (final SQLException e) {
330         }
331         verify(mockedStatement, times(1)).getFetchDirection();
332     }
333 
334     @Test
335     public void testGetFetchSize() throws Exception {
336         try {
337             delegatingStatement.getFetchSize();
338         } catch (final SQLException e) {
339         }
340         verify(mockedStatement, times(1)).getFetchSize();
341     }
342 
343     @Test
344     public void testGetGeneratedKeys() throws Exception {
345         try {
346             delegatingStatement.getGeneratedKeys();
347         } catch (final SQLException e) {
348         }
349         verify(mockedStatement, times(1)).getGeneratedKeys();
350     }
351 
352     @Test
353     public void testGetLargeMaxRows() throws Exception {
354         try {
355             delegatingStatement.getLargeMaxRows();
356         } catch (final SQLException e) {
357         }
358         verify(mockedStatement, times(1)).getLargeMaxRows();
359     }
360 
361     @Test
362     public void testGetLargeUpdateCount() throws Exception {
363         try {
364             delegatingStatement.getLargeUpdateCount();
365         } catch (final SQLException e) {
366         }
367         verify(mockedStatement, times(1)).getLargeUpdateCount();
368     }
369 
370     @Test
371     public void testGetMaxFieldSize() throws Exception {
372         try {
373             delegatingStatement.getMaxFieldSize();
374         } catch (final SQLException e) {
375         }
376         verify(mockedStatement, times(1)).getMaxFieldSize();
377     }
378 
379     @Test
380     public void testGetMaxRows() throws Exception {
381         try {
382             delegatingStatement.getMaxRows();
383         } catch (final SQLException e) {
384         }
385         verify(mockedStatement, times(1)).getMaxRows();
386     }
387 
388     @Test
389     public void testGetMoreResults() throws Exception {
390         try {
391             delegatingStatement.getMoreResults();
392         } catch (final SQLException e) {
393         }
394         verify(mockedStatement, times(1)).getMoreResults();
395     }
396 
397     @Test
398     public void testGetMoreResultsInteger() throws Exception {
399         try {
400             delegatingStatement.getMoreResults(1);
401         } catch (final SQLException e) {
402         }
403         verify(mockedStatement, times(1)).getMoreResults(1);
404     }
405 
406     @Test
407     public void testGetQueryTimeout() throws Exception {
408         try {
409             delegatingStatement.getQueryTimeout();
410         } catch (final SQLException e) {
411         }
412         verify(mockedStatement, times(1)).getQueryTimeout();
413     }
414 
415     @Test
416     public void testGetResultSet() throws Exception {
417         try {
418             delegatingStatement.getResultSet();
419         } catch (final SQLException e) {
420         }
421         verify(mockedStatement, times(1)).getResultSet();
422     }
423 
424     @Test
425     public void testGetResultSetConcurrency() throws Exception {
426         try {
427             delegatingStatement.getResultSetConcurrency();
428         } catch (final SQLException e) {
429         }
430         verify(mockedStatement, times(1)).getResultSetConcurrency();
431     }
432 
433     @Test
434     public void testGetResultSetHoldability() throws Exception {
435         try {
436             delegatingStatement.getResultSetHoldability();
437         } catch (final SQLException e) {
438         }
439         verify(mockedStatement, times(1)).getResultSetHoldability();
440     }
441 
442     @Test
443     public void testGetResultSetType() throws Exception {
444         try {
445             delegatingStatement.getResultSetType();
446         } catch (final SQLException e) {
447         }
448         verify(mockedStatement, times(1)).getResultSetType();
449     }
450 
451     @Test
452     public void testGetUpdateCount() throws Exception {
453         try {
454             delegatingStatement.getUpdateCount();
455         } catch (final SQLException e) {
456         }
457         verify(mockedStatement, times(1)).getUpdateCount();
458     }
459 
460     @Test
461     public void testGetWarnings() throws Exception {
462         try {
463             delegatingStatement.getWarnings();
464         } catch (final SQLException e) {
465         }
466         verify(mockedStatement, times(1)).getWarnings();
467     }
468 
469     /**
470      * This method is a bit special, and call isClosed in the delegate object
471      * itself, not in the wrapped statement.
472      *
473      * @throws Exception
474      */
475     @Test
476     public void testIsClosed() throws Exception {
477         try {
478             delegatingStatement.isClosed();
479         } catch (final SQLException e) {
480         }
481         verify(mockedStatement, times(0)).isClosed();
482     }
483 
484     @Test
485     public void testIsCloseOnCompletion() throws Exception {
486         try {
487             delegatingStatement.isCloseOnCompletion();
488         } catch (final SQLException e) {
489         }
490         verify(mockedStatement, times(1)).isCloseOnCompletion();
491     }
492 
493     @Test
494     public void testIsPoolable() throws Exception {
495         try {
496             delegatingStatement.isPoolable();
497         } catch (final SQLException e) {
498         }
499         verify(mockedStatement, times(1)).isPoolable();
500     }
501 
502     @Test
503     public void testIsWrapperFor() throws Exception {
504         final TesterConnection tstConn = new TesterConnection("test", "test");
505         final TesterStatement tstStmt = new TesterStatementNonWrapping(tstConn);
506         final DelegatingConnection<TesterConnection> dconn = new DelegatingConnection<>(tstConn);
507         final DelegatingStatement stamt = new DelegatingStatement(dconn, tstStmt);
508 
509         final Class<?> stmtProxyClass = Proxy.getProxyClass(
510                 this.getClass().getClassLoader(),
511                 Statement.class);
512 
513         assertTrue(stamt.isWrapperFor(DelegatingStatement.class));
514         assertTrue(stamt.isWrapperFor(TesterStatement.class));
515         assertFalse(stamt.isWrapperFor(stmtProxyClass));
516 
517         stamt.close();
518     }
519 
520     @Test
521     public void testSetCursorNameString() throws Exception {
522         try {
523             delegatingStatement.setCursorName("foo");
524         } catch (final SQLException e) {
525         }
526         verify(mockedStatement, times(1)).setCursorName("foo");
527     }
528 
529     @Test
530     public void testSetEscapeProcessingBoolean() throws Exception {
531         try {
532             delegatingStatement.setEscapeProcessing(Boolean.TRUE);
533         } catch (final SQLException e) {
534         }
535         verify(mockedStatement, times(1)).setEscapeProcessing(Boolean.TRUE);
536     }
537 
538     @Test
539     public void testSetFetchDirectionInteger() throws Exception {
540         try {
541             delegatingStatement.setFetchDirection(1);
542         } catch (final SQLException e) {
543         }
544         verify(mockedStatement, times(1)).setFetchDirection(1);
545     }
546 
547     @Test
548     public void testSetFetchSizeInteger() throws Exception {
549         try {
550             delegatingStatement.setFetchSize(1);
551         } catch (final SQLException e) {
552         }
553         verify(mockedStatement, times(1)).setFetchSize(1);
554     }
555 
556     @Test
557     public void testSetLargeMaxRowsLong() throws Exception {
558         try {
559             delegatingStatement.setLargeMaxRows(1L);
560         } catch (final SQLException e) {
561         }
562         verify(mockedStatement, times(1)).setLargeMaxRows(1L);
563     }
564 
565     @Test
566     public void testSetMaxFieldSizeInteger() throws Exception {
567         try {
568             delegatingStatement.setMaxFieldSize(1);
569         } catch (final SQLException e) {
570         }
571         verify(mockedStatement, times(1)).setMaxFieldSize(1);
572     }
573 
574     @Test
575     public void testSetMaxRowsInteger() throws Exception {
576         try {
577             delegatingStatement.setMaxRows(1);
578         } catch (final SQLException e) {
579         }
580         verify(mockedStatement, times(1)).setMaxRows(1);
581     }
582 
583     @Test
584     public void testSetPoolableBoolean() throws Exception {
585         try {
586             delegatingStatement.setPoolable(Boolean.TRUE);
587         } catch (final SQLException e) {
588         }
589         verify(mockedStatement, times(1)).setPoolable(Boolean.TRUE);
590     }
591 
592     @Test
593     public void testSetQueryTimeoutInteger() throws Exception {
594         try {
595             delegatingStatement.setQueryTimeout(1);
596         } catch (final SQLException e) {
597         }
598         verify(mockedStatement, times(1)).setQueryTimeout(1);
599     }
600 
601     @Test
602     public void testWrap() throws SQLException {
603         assertEquals(delegatingStatement, delegatingStatement.unwrap(Statement.class));
604         assertEquals(delegatingStatement, delegatingStatement.unwrap(DelegatingStatement.class));
605         assertEquals(mockedStatement, delegatingStatement.unwrap(mockedStatement.getClass()));
606         assertNull(delegatingStatement.unwrap(String.class));
607         assertTrue(delegatingStatement.isWrapperFor(Statement.class));
608         assertTrue(delegatingStatement.isWrapperFor(DelegatingStatement.class));
609         assertTrue(delegatingStatement.isWrapperFor(mockedStatement.getClass()));
610         assertFalse(delegatingStatement.isWrapperFor(String.class));
611     }
612 }