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