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