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.wrappers;
18  
19  import static org.mockito.Mockito.mock;
20  import static org.mockito.Mockito.verify;
21  
22  import java.io.ByteArrayInputStream;
23  import java.io.CharArrayReader;
24  import java.io.InputStream;
25  import java.io.OutputStream;
26  import java.io.Reader;
27  import java.io.Writer;
28  import java.lang.reflect.InvocationHandler;
29  import java.lang.reflect.InvocationTargetException;
30  import java.lang.reflect.Method;
31  import java.math.BigDecimal;
32  import java.net.MalformedURLException;
33  import java.net.URL;
34  import java.sql.Blob;
35  import java.sql.Clob;
36  import java.sql.Ref;
37  import java.sql.ResultSet;
38  import java.sql.SQLException;
39  import java.sql.Time;
40  import java.sql.Timestamp;
41  import java.util.Arrays;
42  import java.util.Calendar;
43  import java.util.Map;
44  
45  import org.apache.commons.dbutils.BaseTestCase;
46  import org.apache.commons.dbutils.ProxyFactory;
47  
48  class SqlNullCheckedResultSetMockBlob implements Blob {
49  
50      /**
51       * @throws SQLException
52       */
53      @Override
54      public void free() throws SQLException {
55  
56      }
57  
58      @Override
59      public InputStream getBinaryStream() throws SQLException {
60          return new ByteArrayInputStream(new byte[0]);
61      }
62  
63      /**
64       * @throws SQLException
65       */
66      @Override
67      public InputStream getBinaryStream(final long pos, final long length) throws SQLException {
68        return null;
69      }
70  
71      @Override
72      public byte[] getBytes(final long param, final int param1) throws SQLException {
73          return new byte[0];
74      }
75  
76      @Override
77      public long length() throws SQLException {
78          return 0;
79      }
80  
81      @Override
82      public long position(final Blob blob, final long param) throws SQLException {
83          return 0;
84      }
85  
86      @Override
87      public long position(final byte[] values, final long param) throws SQLException {
88          return 0;
89      }
90  
91      @Override
92      public OutputStream setBinaryStream(final long pos) throws SQLException {
93          return null;
94      }
95  
96      @Override
97      public int setBytes(final long pos, final byte[] bytes) throws SQLException {
98          return 0;
99      }
100 
101     @Override
102     public int setBytes(final long pos, final byte[] bytes, final int offset, final int len)
103         throws SQLException {
104         return 0;
105     }
106 
107     @Override
108     public void truncate(final long len) throws SQLException {
109 
110     }
111 
112 }
113 
114 class SqlNullCheckedResultSetMockClob implements Clob {
115 
116     /**
117      * @throws SQLException
118      */
119     @Override
120     public void free() throws SQLException {
121 
122     }
123 
124     @Override
125     public InputStream getAsciiStream() throws SQLException {
126         return null;
127     }
128 
129     @Override
130     public Reader getCharacterStream() throws SQLException {
131         return null;
132     }
133 
134     /**
135      * @throws SQLException
136      */
137     @Override
138     public Reader getCharacterStream(final long pos, final long length) throws SQLException {
139       return null;
140     }
141 
142     @Override
143     public String getSubString(final long param, final int param1) throws SQLException {
144         return "";
145     }
146 
147     @Override
148     public long length() throws SQLException {
149         return 0;
150     }
151 
152     @Override
153     public long position(final Clob clob, final long param) throws SQLException {
154         return 0;
155     }
156 
157     @Override
158     public long position(final String str, final long param) throws SQLException {
159         return 0;
160     }
161 
162     @Override
163     public OutputStream setAsciiStream(final long pos) throws SQLException {
164         return null;
165     }
166 
167     @Override
168     public Writer setCharacterStream(final long pos) throws SQLException {
169         return null;
170     }
171 
172     @Override
173     public int setString(final long pos, final String str) throws SQLException {
174         return 0;
175     }
176 
177     @Override
178     public int setString(final long pos, final String str, final int offset, final int len)
179         throws SQLException {
180         return 0;
181     }
182 
183     @Override
184     public void truncate(final long len) throws SQLException {
185 
186     }
187 
188 }
189 
190 class SqlNullCheckedResultSetMockRef implements Ref {
191 
192     @Override
193     public String getBaseTypeName() throws SQLException {
194         return "";
195     }
196 
197     @Override
198     public Object getObject() throws SQLException {
199         return null;
200     }
201 
202     @Override
203     public Object getObject(final Map<String,Class<?>> map) throws SQLException {
204         return null;
205     }
206 
207     @Override
208     public void setObject(final Object value) throws SQLException {
209 
210     }
211 
212 }
213 
214 /**
215  * Test cases for {@code SqlNullCheckedResultSet} class.
216  */
217 public class SqlNullCheckedResultSetTest extends BaseTestCase {
218 
219     private static void assertArrayEquals(final byte[] expected, final byte[] actual) {
220         if (expected == actual) {
221             return;
222         }
223         if (expected.length != actual.length) {
224             failNotEquals(null, Arrays.toString(expected), Arrays.toString(actual));
225         }
226         for (int i = 0; i < expected.length; i++) {
227             final byte expectedItem = expected[i];
228             final byte actualItem = actual[i];
229             assertEquals("Array not equal at index " + i, expectedItem, actualItem);
230         }
231     }
232 
233     private SqlNullCheckedResultSet rs2 = null;
234 
235     /**
236      * Sets up instance variables required by this test case.
237      */
238     @Override
239     public void setUp() throws Exception {
240         super.setUp();
241 
242         rs2 =
243             new SqlNullCheckedResultSet(
244                 ProxyFactory.instance().createResultSet(
245                     new SqlNullUncheckedMockResultSet()));
246 
247         rs = ProxyFactory.instance().createResultSet(rs2); // Override superclass field
248     }
249 
250     /**
251      * Tests the getAsciiStream implementation.
252      */
253     public void testGetAsciiStream() throws SQLException {
254 
255         assertNull(rs.getAsciiStream(1));
256         assertTrue(rs.wasNull());
257         assertNull(rs.getAsciiStream("column"));
258         assertTrue(rs.wasNull());
259         // Set what gets returned to something other than the default
260         final InputStream stream = new ByteArrayInputStream(new byte[0]);
261         rs2.setNullAsciiStream(stream);
262         assertNotNull(rs.getAsciiStream(1));
263         assertEquals(stream, rs.getAsciiStream(1));
264         assertNotNull(rs.getAsciiStream("column"));
265         assertEquals(stream, rs.getAsciiStream("column"));
266 
267     }
268 
269     /**
270      * Tests the getBigDecimal implementation.
271      */
272     public void testGetBigDecimal() throws SQLException {
273 
274         assertNull(rs.getBigDecimal(1));
275         assertTrue(rs.wasNull());
276         assertNull(rs.getBigDecimal("column"));
277         assertTrue(rs.wasNull());
278         // Set what gets returned to something other than the default
279         final BigDecimal bd = new BigDecimal(5.0);
280         rs2.setNullBigDecimal(bd);
281         assertNotNull(rs.getBigDecimal(1));
282         assertEquals(bd, rs.getBigDecimal(1));
283         assertNotNull(rs.getBigDecimal("column"));
284         assertEquals(bd, rs.getBigDecimal("column"));
285 
286     }
287 
288     /**
289      * Tests the getBinaryStream implementation.
290      */
291     public void testGetBinaryStream() throws SQLException {
292 
293         assertNull(rs.getBinaryStream(1));
294         assertTrue(rs.wasNull());
295         assertNull(rs.getBinaryStream("column"));
296         assertTrue(rs.wasNull());
297         // Set what gets returned to something other than the default
298         final InputStream stream = new ByteArrayInputStream(new byte[0]);
299         rs2.setNullBinaryStream(stream);
300         assertNotNull(rs.getBinaryStream(1));
301         assertEquals(stream, rs.getBinaryStream(1));
302         assertNotNull(rs.getBinaryStream("column"));
303         assertEquals(stream, rs.getBinaryStream("column"));
304 
305     }
306 
307     /**
308      * Tests the getBlob implementation.
309      */
310     public void testGetBlob() throws SQLException {
311 
312         assertNull(rs.getBlob(1));
313         assertTrue(rs.wasNull());
314         assertNull(rs.getBlob("column"));
315         assertTrue(rs.wasNull());
316         // Set what gets returned to something other than the default
317         final Blob blob = new SqlNullCheckedResultSetMockBlob();
318         rs2.setNullBlob(blob);
319         assertNotNull(rs.getBlob(1));
320         assertEquals(blob, rs.getBlob(1));
321         assertNotNull(rs.getBlob("column"));
322         assertEquals(blob, rs.getBlob("column"));
323 
324     }
325 
326     /**
327      * Tests the getBoolean implementation.
328      */
329     public void testGetBoolean() throws SQLException {
330 
331         assertEquals(false, rs.getBoolean(1));
332         assertTrue(rs.wasNull());
333         assertEquals(false, rs.getBoolean("column"));
334         assertTrue(rs.wasNull());
335         // Set what gets returned to something other than the default
336         rs2.setNullBoolean(true);
337         assertEquals(true, rs.getBoolean(1));
338         assertEquals(true, rs.getBoolean("column"));
339 
340     }
341 
342     /**
343      * Tests the getByte implementation.
344      */
345     public void testGetByte() throws SQLException {
346 
347         assertEquals((byte) 0, rs.getByte(1));
348         assertTrue(rs.wasNull());
349         assertEquals((byte) 0, rs.getByte("column"));
350         assertTrue(rs.wasNull());
351         // Set what gets returned to something other than the default
352         final byte b = (byte) 10;
353         rs2.setNullByte(b);
354         assertEquals(b, rs.getByte(1));
355         assertEquals(b, rs.getByte("column"));
356 
357     }
358 
359     /**
360      * Tests the getByte implementation.
361      */
362     public void testGetBytes() throws SQLException {
363 
364         assertNull(rs.getBytes(1));
365         assertTrue(rs.wasNull());
366         assertNull(rs.getBytes("column"));
367         assertTrue(rs.wasNull());
368         // Set what gets returned to something other than the default
369         final byte[] b = new byte[5];
370         for (int i = 0; i < 5; i++) {
371             b[0] = (byte) i;
372         }
373         rs2.setNullBytes(b);
374         assertNotNull(rs.getBytes(1));
375         assertArrayEquals(b, rs.getBytes(1));
376         assertNotNull(rs.getBytes("column"));
377         assertArrayEquals(b, rs.getBytes("column"));
378 
379     }
380 
381     /**
382      * Tests the getCharacterStream implementation.
383      */
384     public void testGetCharacterStream() throws SQLException {
385 
386         assertNull(rs.getCharacterStream(1));
387         assertTrue(rs.wasNull());
388         assertNull(rs.getCharacterStream("column"));
389         assertTrue(rs.wasNull());
390         // Set what gets returned to something other than the default
391         final Reader reader = new CharArrayReader("this is a string".toCharArray());
392         rs2.setNullCharacterStream(reader);
393         assertNotNull(rs.getCharacterStream(1));
394         assertEquals(reader, rs.getCharacterStream(1));
395         assertNotNull(rs.getCharacterStream("column"));
396         assertEquals(reader, rs.getCharacterStream("column"));
397 
398     }
399 
400     /**
401      * Tests the getClob implementation.
402      */
403     public void testGetClob() throws SQLException {
404 
405         assertNull(rs.getClob(1));
406         assertTrue(rs.wasNull());
407         assertNull(rs.getClob("column"));
408         assertTrue(rs.wasNull());
409         // Set what gets returned to something other than the default
410         final Clob clob = new SqlNullCheckedResultSetMockClob();
411         rs2.setNullClob(clob);
412         assertNotNull(rs.getClob(1));
413         assertEquals(clob, rs.getClob(1));
414         assertNotNull(rs.getClob("column"));
415         assertEquals(clob, rs.getClob("column"));
416 
417     }
418 
419     /**
420      * Tests the getDate implementation.
421      */
422     public void testGetDate() throws SQLException {
423 
424         assertNull(rs.getDate(1));
425         assertTrue(rs.wasNull());
426         assertNull(rs.getDate("column"));
427         assertTrue(rs.wasNull());
428         assertNull(rs.getDate(1, Calendar.getInstance()));
429         assertTrue(rs.wasNull());
430         assertNull(rs.getDate("column", Calendar.getInstance()));
431         assertTrue(rs.wasNull());
432         // Set what gets returned to something other than the default
433         final java.sql.Date date = new java.sql.Date(new java.util.Date().getTime());
434         rs2.setNullDate(date);
435         assertNotNull(rs.getDate(1));
436         assertEquals(date, rs.getDate(1));
437         assertNotNull(rs.getDate("column"));
438         assertEquals(date, rs.getDate("column"));
439         assertNotNull(rs.getDate(1, Calendar.getInstance()));
440         assertEquals(date, rs.getDate(1, Calendar.getInstance()));
441         assertNotNull(rs.getDate("column", Calendar.getInstance()));
442         assertEquals(date, rs.getDate("column", Calendar.getInstance()));
443 
444     }
445 
446     /**
447      * Tests the getDouble implementation.
448      */
449     public void testGetDouble() throws SQLException {
450 
451         assertEquals(0.0, rs.getDouble(1), 0.0);
452         assertTrue(rs.wasNull());
453         assertEquals(0.0, rs.getDouble("column"), 0.0);
454         assertTrue(rs.wasNull());
455         // Set what gets returned to something other than the default
456         final double d = 10.0;
457         rs2.setNullDouble(d);
458         assertEquals(d, rs.getDouble(1), 0.0);
459         assertEquals(d, rs.getDouble("column"), 0.0);
460 
461     }
462 
463     /**
464      * Tests the getFloat implementation.
465      */
466     public void testGetFloat() throws SQLException {
467         assertEquals(0, rs.getFloat(1), 0.0);
468         assertTrue(rs.wasNull());
469         assertEquals(0, rs.getFloat("column"), 0.0);
470         assertTrue(rs.wasNull());
471         // Set what gets returned to something other than the default
472         final float f = 10;
473         rs2.setNullFloat(f);
474         assertEquals(f, rs.getFloat(1), 0.0);
475         assertEquals(f, rs.getFloat("column"), 0.0);
476     }
477 
478     /**
479      * Tests the getInt implementation.
480      */
481     public void testGetInt() throws SQLException {
482         assertEquals(0, rs.getInt(1));
483         assertTrue(rs.wasNull());
484         assertEquals(0, rs.getInt("column"));
485         assertTrue(rs.wasNull());
486         // Set what gets returned to something other than the default
487         final int i = 10;
488         rs2.setNullInt(i);
489         assertEquals(i, rs.getInt(1));
490         assertEquals(i, rs.getInt("column"));
491     }
492 
493     /**
494      * Tests the getLong implementation.
495      */
496     public void testGetLong() throws SQLException {
497         assertEquals(0, rs.getLong(1));
498         assertTrue(rs.wasNull());
499         assertEquals(0, rs.getLong("column"));
500         assertTrue(rs.wasNull());
501         // Set what gets returned to something other than the default
502         final long l = 10;
503         rs2.setNullLong(l);
504         assertEquals(l, rs.getLong(1));
505         assertEquals(l, rs.getLong("column"));
506     }
507 
508     /**
509      * Tests the getObject implementation.
510      */
511     public void testGetObject() throws SQLException {
512 
513         assertNull(rs.getObject(1));
514         assertTrue(rs.wasNull());
515         assertNull(rs.getObject("column"));
516         assertTrue(rs.wasNull());
517         assertNull(rs.getObject(1, (Map<String, Class<?>>) null));
518         assertTrue(rs.wasNull());
519         assertNull(rs.getObject("column", (Map<String, Class<?>>) null));
520         assertTrue(rs.wasNull());
521         // Set what gets returned to something other than the default
522         final Object o = new Object();
523         rs2.setNullObject(o);
524         assertNotNull(rs.getObject(1));
525         assertEquals(o, rs.getObject(1));
526         assertNotNull(rs.getObject("column"));
527         assertEquals(o, rs.getObject("column"));
528         assertNotNull(rs.getObject(1, (Map<String, Class<?>>) null));
529         assertEquals(o, rs.getObject(1, (Map<String, Class<?>>) null));
530         assertNotNull(rs.getObject("column", (Map<String, Class<?>>) null));
531         assertEquals(o, rs.getObject("column", (Map<String, Class<?>>) null));
532 
533     }
534 
535     /**
536      * Tests the getRef implementation.
537      */
538     public void testGetRef() throws SQLException {
539 
540         assertNull(rs.getRef(1));
541         assertTrue(rs.wasNull());
542         assertNull(rs.getRef("column"));
543         assertTrue(rs.wasNull());
544         // Set what gets returned to something other than the default
545         final Ref ref = new SqlNullCheckedResultSetMockRef();
546         rs2.setNullRef(ref);
547         assertNotNull(rs.getRef(1));
548         assertEquals(ref, rs.getRef(1));
549         assertNotNull(rs.getRef("column"));
550         assertEquals(ref, rs.getRef("column"));
551 
552     }
553 
554     /**
555      * Tests the getShort implementation.
556      */
557     public void testGetShort() throws SQLException {
558 
559         assertEquals((short) 0, rs.getShort(1));
560         assertTrue(rs.wasNull());
561         assertEquals((short) 0, rs.getShort("column"));
562         assertTrue(rs.wasNull());
563         // Set what gets returned to something other than the default
564         final short s = (short) 10;
565         rs2.setNullShort(s);
566         assertEquals(s, rs.getShort(1));
567         assertEquals(s, rs.getShort("column"));
568     }
569 
570     /**
571      * Tests the getString implementation.
572      */
573     public void testGetString() throws SQLException {
574         assertEquals(null, rs.getString(1));
575         assertTrue(rs.wasNull());
576         assertEquals(null, rs.getString("column"));
577         assertTrue(rs.wasNull());
578         // Set what gets returned to something other than the default
579         final String s = "hello, world";
580         rs2.setNullString(s);
581         assertEquals(s, rs.getString(1));
582         assertEquals(s, rs.getString("column"));
583     }
584 
585     /**
586      * Tests the getTime implementation.
587      */
588     public void testGetTime() throws SQLException {
589 
590         assertNull(rs.getTime(1));
591         assertTrue(rs.wasNull());
592         assertNull(rs.getTime("column"));
593         assertTrue(rs.wasNull());
594         assertNull(rs.getTime(1, Calendar.getInstance()));
595         assertTrue(rs.wasNull());
596         assertNull(rs.getTime("column", Calendar.getInstance()));
597         assertTrue(rs.wasNull());
598         // Set what gets returned to something other than the default
599         final Time time = new Time(new java.util.Date().getTime());
600         rs2.setNullTime(time);
601         assertNotNull(rs.getTime(1));
602         assertEquals(time, rs.getTime(1));
603         assertNotNull(rs.getTime("column"));
604         assertEquals(time, rs.getTime("column"));
605         assertNotNull(rs.getTime(1, Calendar.getInstance()));
606         assertEquals(time, rs.getTime(1, Calendar.getInstance()));
607         assertNotNull(rs.getTime("column", Calendar.getInstance()));
608         assertEquals(time, rs.getTime("column", Calendar.getInstance()));
609 
610     }
611 
612     /**
613      * Tests the getTimestamp implementation.
614      */
615     public void testGetTimestamp() throws SQLException {
616 
617         assertNull(rs.getTimestamp(1));
618         assertTrue(rs.wasNull());
619         assertNull(rs.getTimestamp("column"));
620         assertTrue(rs.wasNull());
621         assertNull(rs.getTimestamp(1, Calendar.getInstance()));
622         assertTrue(rs.wasNull());
623         assertNull(rs.getTimestamp("column", Calendar.getInstance()));
624         assertTrue(rs.wasNull());
625         // Set what gets returned to something other than the default
626         final Timestamp ts = new Timestamp(new java.util.Date().getTime());
627         rs2.setNullTimestamp(ts);
628         assertNotNull(rs.getTimestamp(1));
629         assertEquals(ts, rs.getTimestamp(1));
630         assertNotNull(rs.getTimestamp("column"));
631         assertEquals(ts, rs.getTimestamp("column"));
632         assertNotNull(rs.getTimestamp(1, Calendar.getInstance()));
633         assertEquals(ts, rs.getTimestamp(1, Calendar.getInstance()));
634         assertNotNull(rs.getTimestamp("column", Calendar.getInstance()));
635         assertEquals(ts, rs.getTimestamp("column", Calendar.getInstance()));
636     }
637 
638     /**
639      * Tests the setNullAsciiStream implementation.
640      */
641     public void testSetNullAsciiStream() throws SQLException {
642 
643         assertNull(rs2.getNullAsciiStream());
644         // Set what gets returned to something other than the default
645         final InputStream stream = new ByteArrayInputStream(new byte[0]);
646         rs2.setNullAsciiStream(stream);
647         assertNotNull(rs.getAsciiStream(1));
648         assertEquals(stream, rs.getAsciiStream(1));
649         assertNotNull(rs.getAsciiStream("column"));
650         assertEquals(stream, rs.getAsciiStream("column"));
651 
652     }
653 
654     /**
655      * Tests the setNullBigDecimal implementation.
656      */
657     public void testSetNullBigDecimal() throws SQLException {
658 
659         assertNull(rs2.getNullBigDecimal());
660         // Set what gets returned to something other than the default
661         final BigDecimal bd = new BigDecimal(5.0);
662         rs2.setNullBigDecimal(bd);
663         assertNotNull(rs.getBigDecimal(1));
664         assertEquals(bd, rs.getBigDecimal(1));
665         assertNotNull(rs.getBigDecimal("column"));
666         assertEquals(bd, rs.getBigDecimal("column"));
667 
668     }
669 
670     /**
671      * Tests the setNullBinaryStream implementation.
672      */
673     public void testSetNullBinaryStream() throws SQLException {
674 
675         assertNull(rs2.getNullBinaryStream());
676         // Set what gets returned to something other than the default
677         final InputStream stream = new ByteArrayInputStream(new byte[0]);
678         rs2.setNullBinaryStream(stream);
679         assertNotNull(rs.getBinaryStream(1));
680         assertEquals(stream, rs.getBinaryStream(1));
681         assertNotNull(rs.getBinaryStream("column"));
682         assertEquals(stream, rs.getBinaryStream("column"));
683 
684     }
685 
686     /**
687      * Tests the setNullBlob implementation.
688      */
689     public void testSetNullBlob() throws SQLException {
690 
691         assertNull(rs2.getNullBlob());
692         // Set what gets returned to something other than the default
693         final Blob blob = new SqlNullCheckedResultSetMockBlob();
694         rs2.setNullBlob(blob);
695         assertNotNull(rs.getBlob(1));
696         assertEquals(blob, rs.getBlob(1));
697         assertNotNull(rs.getBlob("column"));
698         assertEquals(blob, rs.getBlob("column"));
699 
700     }
701 
702     /**
703      * Tests the setNullBoolean implementation.
704      */
705     public void testSetNullBoolean() throws SQLException {
706 
707         assertEquals(false, rs2.getNullBoolean());
708         // Set what gets returned to something other than the default
709         rs2.setNullBoolean(true);
710         assertEquals(true, rs.getBoolean(1));
711         assertEquals(true, rs.getBoolean("column"));
712 
713     }
714 
715     /**
716      * Tests the setNullByte implementation.
717      */
718     public void testSetNullByte() throws SQLException {
719 
720         assertEquals((byte) 0, rs2.getNullByte());
721         // Set what gets returned to something other than the default
722         final byte b = (byte) 10;
723         rs2.setNullByte(b);
724         assertEquals(b, rs.getByte(1));
725         assertEquals(b, rs.getByte("column"));
726 
727     }
728 
729     /**
730      * Tests the setNullByte implementation.
731      */
732     public void testSetNullBytes() throws SQLException {
733         // test the default, unset value
734         assertEquals(null, rs2.getNullBytes());
735 
736         // test that setting null is safe
737         rs2.setNullBytes(null);
738         assertEquals(null, rs2.getNullBytes());
739 
740         // Set what gets returned to something other than the default
741         final byte[] b = new byte[5];
742         for (int i = 0; i < 5; i++) {
743             b[0] = (byte) i;
744         }
745         rs2.setNullBytes(b);
746         assertNotNull(rs.getBytes(1));
747         assertArrayEquals(b, rs.getBytes(1));
748         assertNotNull(rs.getBytes("column"));
749         assertArrayEquals(b, rs.getBytes("column"));
750 
751     }
752 
753     /**
754      * Tests the setNullCharacterStream implementation.
755      */
756     public void testSetNullCharacterStream() throws SQLException {
757 
758         assertNull(rs2.getNullCharacterStream());
759         // Set what gets returned to something other than the default
760         final Reader reader = new CharArrayReader("this is a string".toCharArray());
761         rs2.setNullCharacterStream(reader);
762         assertNotNull(rs.getCharacterStream(1));
763         assertEquals(reader, rs.getCharacterStream(1));
764         assertNotNull(rs.getCharacterStream("column"));
765         assertEquals(reader, rs.getCharacterStream("column"));
766 
767     }
768 
769     /**
770      * Tests the setNullClob implementation.
771      */
772     public void testSetNullClob() throws SQLException {
773 
774         assertNull(rs2.getNullClob());
775         // Set what gets returned to something other than the default
776         final Clob clob = new SqlNullCheckedResultSetMockClob();
777         rs2.setNullClob(clob);
778         assertNotNull(rs.getClob(1));
779         assertEquals(clob, rs.getClob(1));
780         assertNotNull(rs.getClob("column"));
781         assertEquals(clob, rs.getClob("column"));
782 
783     }
784 
785     /**
786      * Tests the setNullDate implementation.
787      */
788     public void testSetNullDate() throws SQLException {
789         // test the default, unset value
790         assertEquals(null, rs2.getNullDate());
791 
792         // test that setting null is safe
793         rs2.setNullDate(null);
794         assertEquals(null, rs2.getNullDate());
795 
796         // Set what gets returned to something other than the default
797         final java.sql.Date date = new java.sql.Date(new java.util.Date().getTime());
798         rs2.setNullDate(date);
799         assertNotNull(rs.getDate(1));
800         assertEquals(date, rs.getDate(1));
801         assertNotNull(rs.getDate("column"));
802         assertEquals(date, rs.getDate("column"));
803         assertNotNull(rs.getDate(1, Calendar.getInstance()));
804         assertEquals(date, rs.getDate(1, Calendar.getInstance()));
805         assertNotNull(rs.getDate("column", Calendar.getInstance()));
806         assertEquals(date, rs.getDate("column", Calendar.getInstance()));
807 
808     }
809 
810     /**
811      * Tests the setNullDouble implementation.
812      */
813     public void testSetNullDouble() throws SQLException {
814         assertEquals(0.0, rs2.getNullDouble(), 0.0);
815         // Set what gets returned to something other than the default
816         final double d = 10.0;
817         rs2.setNullDouble(d);
818         assertEquals(d, rs.getDouble(1), 0.0);
819         assertEquals(d, rs.getDouble("column"), 0.0);
820     }
821 
822     /**
823      * Tests the setNullFloat implementation.
824      */
825     public void testSetNullFloat() throws SQLException {
826         assertEquals((float) 0.0, rs2.getNullFloat(), 0.0);
827         // Set what gets returned to something other than the default
828         final float f = (float) 10.0;
829         rs2.setNullFloat(f);
830         assertEquals(f, rs.getFloat(1), 0.0);
831         assertEquals(f, rs.getFloat("column"), 0.0);
832     }
833 
834     /**
835      * Tests the setNullInt implementation.
836      */
837     public void testSetNullInt() throws SQLException {
838         assertEquals(0, rs2.getNullInt());
839         assertEquals(0, rs.getInt(1));
840         assertTrue(rs.wasNull());
841         assertEquals(0, rs.getInt("column"));
842         assertTrue(rs.wasNull());
843         // Set what gets returned to something other than the default
844         final int i = 10;
845         rs2.setNullInt(i);
846         assertEquals(i, rs.getInt(1));
847         assertEquals(i, rs.getInt("column"));
848     }
849 
850     /**
851      * Tests the setNullLong implementation.
852      */
853     public void testSetNullLong() throws SQLException {
854         assertEquals(0, rs2.getNullLong());
855         // Set what gets returned to something other than the default
856         final long l = 10;
857         rs2.setNullLong(l);
858         assertEquals(l, rs.getLong(1));
859         assertEquals(l, rs.getLong("column"));
860     }
861 
862     /**
863      * Tests the setNullObject implementation.
864      */
865     public void testSetNullObject() throws SQLException {
866         assertNull(rs2.getNullObject());
867         // Set what gets returned to something other than the default
868         final Object o = new Object();
869         rs2.setNullObject(o);
870         assertNotNull(rs.getObject(1));
871         assertEquals(o, rs.getObject(1));
872         assertNotNull(rs.getObject("column"));
873         assertEquals(o, rs.getObject("column"));
874         assertNotNull(rs.getObject(1, (Map<String, Class<?>>) null));
875         assertEquals(o, rs.getObject(1, (Map<String, Class<?>>) null));
876         assertNotNull(rs.getObject("column", (Map<String, Class<?>>) null));
877         assertEquals(o, rs.getObject("column", (Map<String, Class<?>>) null));
878     }
879 
880     /**
881      * Tests the setNullRef implementation.
882      */
883     public void testSetNullRef() throws SQLException {
884         assertNull(rs2.getNullRef());
885         // Set what gets returned to something other than the default
886         final Ref ref = new SqlNullCheckedResultSetMockRef();
887         rs2.setNullRef(ref);
888         assertNotNull(rs.getRef(1));
889         assertEquals(ref, rs.getRef(1));
890         assertNotNull(rs.getRef("column"));
891         assertEquals(ref, rs.getRef("column"));
892     }
893 
894     /**
895      * Tests the setNullShort implementation.
896      */
897     public void testSetNullShort() throws SQLException {
898 
899         assertEquals((short) 0, rs2.getNullShort());
900         // Set what gets returned to something other than the default
901         final short s = (short) 10;
902         rs2.setNullShort(s);
903         assertEquals(s, rs.getShort(1));
904         assertEquals(s, rs.getShort("column"));
905 
906     }
907 
908     /**
909      * Tests the setNullString implementation.
910      */
911     public void testSetNullString() throws SQLException {
912         assertEquals(null, rs2.getNullString());
913         // Set what gets returned to something other than the default
914         final String s = "hello, world";
915         rs2.setNullString(s);
916         assertEquals(s, rs.getString(1));
917         assertEquals(s, rs.getString("column"));
918     }
919 
920     /**
921      * Tests the setNullTime implementation.
922      */
923     public void testSetNullTime() throws SQLException {
924         // test the default, unset value
925         assertEquals(null, rs2.getNullTime());
926 
927         // test that setting null is safe
928         rs2.setNullTime(null);
929         assertEquals(null, rs2.getNullTime());
930 
931         // Set what gets returned to something other than the default
932         final Time time = new Time(new java.util.Date().getTime());
933         rs2.setNullTime(time);
934         assertNotNull(rs.getTime(1));
935         assertEquals(time, rs.getTime(1));
936         assertNotNull(rs.getTime("column"));
937         assertEquals(time, rs.getTime("column"));
938         assertNotNull(rs.getTime(1, Calendar.getInstance()));
939         assertEquals(time, rs.getTime(1, Calendar.getInstance()));
940         assertNotNull(rs.getTime("column", Calendar.getInstance()));
941         assertEquals(time, rs.getTime("column", Calendar.getInstance()));
942     }
943 
944     /**
945      * Tests the setNullTimestamp implementation.
946      */
947     public void testSetNullTimestamp() throws SQLException {
948         // test the default, unset value
949         assertEquals(null, rs2.getNullTimestamp());
950 
951         // test that setting null is safe
952         rs2.setNullTimestamp(null);
953         assertEquals(null, rs2.getNullTimestamp());
954 
955         // Set what gets returned to something other than the default
956         final Timestamp ts = new Timestamp(new java.util.Date().getTime());
957         rs2.setNullTimestamp(ts);
958         assertNotNull(rs.getTimestamp(1));
959         assertEquals(ts, rs.getTimestamp(1));
960         assertNotNull(rs.getTimestamp("column"));
961         assertEquals(ts, rs.getTimestamp("column"));
962         assertNotNull(rs.getTimestamp(1, Calendar.getInstance()));
963         assertEquals(ts, rs.getTimestamp(1, Calendar.getInstance()));
964         assertNotNull(rs.getTimestamp("column", Calendar.getInstance()));
965         assertEquals(ts, rs.getTimestamp("column", Calendar.getInstance()));
966     }
967 
968     /**
969      * Tests the getURL and setNullURL implementations.
970      */
971     public void testURL() throws SQLException, MalformedURLException, IllegalAccessException, IllegalArgumentException, InvocationTargetException {
972         assertEquals(null, rs.getURL(1));
973         assertTrue(rs.wasNull());
974         assertEquals(null, rs.getURL("column"));
975         assertTrue(rs.wasNull());
976         // Set what gets returned to something other than the default
977         final URL u = new URL("http://www.apache.org");
978         rs2.setNullURL(u);
979         assertEquals(u, rs.getURL(1));
980         assertEquals(u, rs.getURL("column"));
981     }
982 
983     public void testWrapResultSet() throws SQLException {
984         final ResultSet wrappedRs = mock(ResultSet.class);
985         final ResultSet rs = SqlNullCheckedResultSet.wrap(wrappedRs);
986         rs.beforeFirst();
987         verify(wrappedRs).beforeFirst();
988         rs.next();
989         verify(wrappedRs).next();
990     }
991 }
992 
993 class SqlNullUncheckedMockResultSet implements InvocationHandler {
994 
995     /**
996      * Always return false for booleans, 0 for numerics, and null for Objects.
997      * @see java.lang.reflect.InvocationHandler#invoke(Object, java.lang.reflect.Method, Object[])
998      */
999     @Override
1000     public Object invoke(final Object proxy, final Method method, final Object[] args)
1001         throws Throwable {
1002 
1003         final Class<?> returnType = method.getReturnType();
1004 
1005         if (method.getName().equals("wasNull")) {
1006             return Boolean.TRUE;
1007 
1008         }
1009         if (returnType.equals(Boolean.TYPE)) {
1010             return Boolean.FALSE;
1011 
1012         }
1013         if (returnType.equals(Integer.TYPE)) {
1014             return Integer.valueOf(0);
1015 
1016         }
1017         if (returnType.equals(Short.TYPE)) {
1018             return Short.valueOf((short) 0);
1019 
1020         }
1021         if (returnType.equals(Double.TYPE)) {
1022             return Double.valueOf(0);
1023 
1024         }
1025         if (returnType.equals(Long.TYPE)) {
1026             return Long.valueOf(0);
1027 
1028         }
1029         if (returnType.equals(Byte.TYPE)) {
1030             return Byte.valueOf((byte) 0);
1031 
1032         }
1033         if (returnType.equals(Float.TYPE)) {
1034             return Float.valueOf(0);
1035 
1036         }
1037         return null;
1038     }
1039 }