KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > dbutils > wrappers > SqlNullCheckedResultSetTest


1 /*
2  * $Header: /home/cvs/jakarta-commons/dbutils/src/test/org/apache/commons/dbutils/wrappers/SqlNullCheckedResultSetTest.java,v 1.2 2003/11/09 18:18:04 dgraham Exp $
3  * $Revision: 1.2 $
4  * $Date: 2003/11/09 18:18:04 $
5  *
6  * ====================================================================
7  *
8  * The Apache Software License, Version 1.1
9  *
10  * Copyright (c) 2003 The Apache Software Foundation. All rights
11  * reserved.
12  *
13  * Redistribution and use in source and binary forms, with or without
14  * modification, are permitted provided that the following conditions
15  * are met:
16  *
17  * 1. Redistributions of source code must retain the above copyright
18  * notice, this list of conditions and the following disclaimer.
19  *
20  * 2. Redistributions in binary form must reproduce the above copyright
21  * notice, this list of conditions and the following disclaimer in
22  * the documentation and/or other materials provided with the
23  * distribution.
24  *
25  * 3. The end-user documentation included with the redistribution, if
26  * any, must include the following acknowledgement:
27  * "This product includes software developed by the
28  * Apache Software Foundation (http://www.apache.org/)."
29  * Alternately, this acknowledgement may appear in the software itself,
30  * if and wherever such third-party acknowledgements normally appear.
31  *
32  * 4. The names "The Jakarta Project", "Commons", and "Apache Software
33  * Foundation" must not be used to endorse or promote products derived
34  * from this software without prior written permission. For written
35  * permission, please contact apache@apache.org.
36  *
37  * 5. Products derived from this software may not be called "Apache"
38  * nor may "Apache" appear in their names without prior written
39  * permission of the Apache Software Foundation.
40  *
41  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
42  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
43  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
44  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
45  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
47  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
48  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
49  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
50  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
51  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
52  * SUCH DAMAGE.
53  * ====================================================================
54  *
55  * This software consists of voluntary contributions made by many
56  * individuals on behalf of the Apache Software Foundation. For more
57  * information on the Apache Software Foundation, please see
58  * <http://www.apache.org/>.
59  *
60  */

61
62 package org.apache.commons.dbutils.wrappers;
63
64 import java.io.ByteArrayInputStream JavaDoc;
65 import java.io.CharArrayReader JavaDoc;
66 import java.io.InputStream JavaDoc;
67 import java.io.OutputStream JavaDoc;
68 import java.io.Reader JavaDoc;
69 import java.io.Writer JavaDoc;
70 import java.lang.reflect.InvocationHandler JavaDoc;
71 import java.lang.reflect.Method JavaDoc;
72 import java.math.BigDecimal JavaDoc;
73 import java.net.MalformedURLException JavaDoc;
74 import java.net.URL JavaDoc;
75 import java.sql.Blob JavaDoc;
76 import java.sql.Clob JavaDoc;
77 import java.sql.Ref JavaDoc;
78 import java.sql.ResultSet JavaDoc;
79 import java.sql.SQLException JavaDoc;
80 import java.sql.Time JavaDoc;
81 import java.sql.Timestamp JavaDoc;
82 import java.util.Calendar JavaDoc;
83 import java.util.Map JavaDoc;
84
85 import org.apache.commons.dbutils.BaseTestCase;
86 import org.apache.commons.dbutils.ProxyFactory;
87
88 /**
89  * Test cases for <code>SqlNullCheckedResultSet</code> class.
90  *
91  * @author <a HREF="stevencaswell@apache.org">Steven Caswell</a>
92  * @author David Graham
93  * @version $Id: SqlNullCheckedResultSetTest.java,v 1.2 2003/11/09 18:18:04 dgraham Exp $
94  */

95 public class SqlNullCheckedResultSetTest extends BaseTestCase {
96
97     private ResultSet JavaDoc rs = null;
98
99     private SqlNullCheckedResultSet rs2 = null;
100
101     /**
102      * Constructs a new instance of
103      * <code>SqlNullCheckedResultSetTestCase</code>
104      * with the specified name.
105      *
106      * @param name the test case name
107      */

108     public SqlNullCheckedResultSetTest(String JavaDoc name) {
109         super(name);
110     }
111
112     /**
113      * Sets up instance variables required by this test case.
114      */

115     public void setUp() throws Exception JavaDoc {
116         super.setUp();
117
118         rs2 =
119             new SqlNullCheckedResultSet(
120                 ProxyFactory.instance().createResultSet(
121                     new SqlNullUncheckedMockResultSet()));
122
123         rs = ProxyFactory.instance().createResultSet(rs2);
124     }
125
126     /**
127      * Tests the getAsciiStream implementation.
128      */

129     public void testGetAsciiStream() throws SQLException JavaDoc {
130
131         assertNull(rs.getAsciiStream(1));
132         assertTrue(rs.wasNull());
133         assertNull(rs.getAsciiStream("column"));
134         assertTrue(rs.wasNull());
135         // Set what gets returned to something other than the default
136
InputStream JavaDoc stream = new ByteArrayInputStream JavaDoc(new byte[0]);
137         rs2.setNullAsciiStream(stream);
138         assertNotNull(rs.getAsciiStream(1));
139         assertEquals(stream, rs.getAsciiStream(1));
140         assertNotNull(rs.getAsciiStream("column"));
141         assertEquals(stream, rs.getAsciiStream("column"));
142
143     }
144
145     /**
146      * Tests the getBigDecimal implementation.
147      */

148     public void testGetBigDecimal() throws SQLException JavaDoc {
149
150         assertNull(rs.getBigDecimal(1));
151         assertTrue(rs.wasNull());
152         assertNull(rs.getBigDecimal("column"));
153         assertTrue(rs.wasNull());
154         // Set what gets returned to something other than the default
155
BigDecimal JavaDoc bd = new BigDecimal JavaDoc(5.0);
156         rs2.setNullBigDecimal(bd);
157         assertNotNull(rs.getBigDecimal(1));
158         assertEquals(bd, rs.getBigDecimal(1));
159         assertNotNull(rs.getBigDecimal("column"));
160         assertEquals(bd, rs.getBigDecimal("column"));
161
162     }
163
164     /**
165      * Tests the getBinaryStream implementation.
166      */

167     public void testGetBinaryStream() throws SQLException JavaDoc {
168
169         assertNull(rs.getBinaryStream(1));
170         assertTrue(rs.wasNull());
171         assertNull(rs.getBinaryStream("column"));
172         assertTrue(rs.wasNull());
173         // Set what gets returned to something other than the default
174
InputStream JavaDoc stream = new ByteArrayInputStream JavaDoc(new byte[0]);
175         rs2.setNullBinaryStream(stream);
176         assertNotNull(rs.getBinaryStream(1));
177         assertEquals(stream, rs.getBinaryStream(1));
178         assertNotNull(rs.getBinaryStream("column"));
179         assertEquals(stream, rs.getBinaryStream("column"));
180
181     }
182
183     /**
184      * Tests the getBlob implementation.
185      */

186     public void testGetBlob() throws SQLException JavaDoc {
187
188         assertNull(rs.getBlob(1));
189         assertTrue(rs.wasNull());
190         assertNull(rs.getBlob("column"));
191         assertTrue(rs.wasNull());
192         // Set what gets returned to something other than the default
193
Blob JavaDoc blob = new SqlNullCheckedResultSetMockBlob();
194         rs2.setNullBlob(blob);
195         assertNotNull(rs.getBlob(1));
196         assertEquals(blob, rs.getBlob(1));
197         assertNotNull(rs.getBlob("column"));
198         assertEquals(blob, rs.getBlob("column"));
199
200     }
201
202     /**
203      * Tests the getBoolean implementation.
204      */

205     public void testGetBoolean() throws SQLException JavaDoc {
206
207         assertEquals(false, rs.getBoolean(1));
208         assertTrue(rs.wasNull());
209         assertEquals(false, rs.getBoolean("column"));
210         assertTrue(rs.wasNull());
211         // Set what gets returned to something other than the default
212
rs2.setNullBoolean(true);
213         assertEquals(true, rs.getBoolean(1));
214         assertEquals(true, rs.getBoolean("column"));
215
216     }
217
218     /**
219      * Tests the getByte implementation.
220      */

221     public void testGetByte() throws SQLException JavaDoc {
222
223         assertEquals((byte) 0, rs.getByte(1));
224         assertTrue(rs.wasNull());
225         assertEquals((byte) 0, rs.getByte("column"));
226         assertTrue(rs.wasNull());
227         // Set what gets returned to something other than the default
228
byte b = (byte) 10;
229         rs2.setNullByte(b);
230         assertEquals(b, rs.getByte(1));
231         assertEquals(b, rs.getByte("column"));
232
233     }
234
235     /**
236      * Tests the getByte implementation.
237      */

238     public void testGetBytes() throws SQLException JavaDoc {
239
240         assertNull(rs.getBytes(1));
241         assertTrue(rs.wasNull());
242         assertNull(rs.getBytes("column"));
243         assertTrue(rs.wasNull());
244         // Set what gets returned to something other than the default
245
byte[] b = new byte[5];
246         for (int i = 0; i < 5; i++) {
247             b[0] = (byte) i;
248         }
249         rs2.setNullBytes(b);
250         assertNotNull(rs.getBytes(1));
251         assertEquals(b, rs.getBytes(1));
252         assertNotNull(rs.getBytes("column"));
253         assertEquals(b, rs.getBytes("column"));
254
255     }
256
257     /**
258      * Tests the getCharacterStream implementation.
259      */

260     public void testGetCharacterStream() throws SQLException JavaDoc {
261
262         assertNull(rs.getCharacterStream(1));
263         assertTrue(rs.wasNull());
264         assertNull(rs.getCharacterStream("column"));
265         assertTrue(rs.wasNull());
266         // Set what gets returned to something other than the default
267
Reader JavaDoc reader = new CharArrayReader JavaDoc("this is a string".toCharArray());
268         rs2.setNullCharacterStream(reader);
269         assertNotNull(rs.getCharacterStream(1));
270         assertEquals(reader, rs.getCharacterStream(1));
271         assertNotNull(rs.getCharacterStream("column"));
272         assertEquals(reader, rs.getCharacterStream("column"));
273
274     }
275
276     /**
277      * Tests the getClob implementation.
278      */

279     public void testGetClob() throws SQLException JavaDoc {
280
281         assertNull(rs.getClob(1));
282         assertTrue(rs.wasNull());
283         assertNull(rs.getClob("column"));
284         assertTrue(rs.wasNull());
285         // Set what gets returned to something other than the default
286
Clob JavaDoc clob = new SqlNullCheckedResultSetMockClob();
287         rs2.setNullClob(clob);
288         assertNotNull(rs.getClob(1));
289         assertEquals(clob, rs.getClob(1));
290         assertNotNull(rs.getClob("column"));
291         assertEquals(clob, rs.getClob("column"));
292
293     }
294
295     /**
296      * Tests the getDate implementation.
297      */

298     public void testGetDate() throws SQLException JavaDoc {
299
300         assertNull(rs.getDate(1));
301         assertTrue(rs.wasNull());
302         assertNull(rs.getDate("column"));
303         assertTrue(rs.wasNull());
304         assertNull(rs.getDate(1, Calendar.getInstance()));
305         assertTrue(rs.wasNull());
306         assertNull(rs.getDate("column", Calendar.getInstance()));
307         assertTrue(rs.wasNull());
308         // Set what gets returned to something other than the default
309
java.sql.Date JavaDoc date = new java.sql.Date JavaDoc(new java.util.Date JavaDoc().getTime());
310         rs2.setNullDate(date);
311         assertNotNull(rs.getDate(1));
312         assertEquals(date, rs.getDate(1));
313         assertNotNull(rs.getDate("column"));
314         assertEquals(date, rs.getDate("column"));
315         assertNotNull(rs.getDate(1, Calendar.getInstance()));
316         assertEquals(date, rs.getDate(1, Calendar.getInstance()));
317         assertNotNull(rs.getDate("column", Calendar.getInstance()));
318         assertEquals(date, rs.getDate("column", Calendar.getInstance()));
319
320     }
321
322     /**
323      * Tests the getDouble implementation.
324      */

325     public void testGetDouble() throws SQLException JavaDoc {
326
327         assertEquals(0.0, rs.getDouble(1), 0.0);
328         assertTrue(rs.wasNull());
329         assertEquals(0.0, rs.getDouble("column"), 0.0);
330         assertTrue(rs.wasNull());
331         // Set what gets returned to something other than the default
332
double d = 10.0;
333         rs2.setNullDouble(d);
334         assertEquals(d, rs.getDouble(1), 0.0);
335         assertEquals(d, rs.getDouble("column"), 0.0);
336
337     }
338
339     /**
340      * Tests the getFloat implementation.
341      */

342     public void testGetFloat() throws SQLException JavaDoc {
343
344         assertEquals((float) 0, rs.getFloat(1), 0.0);
345         assertTrue(rs.wasNull());
346         assertEquals((float) 0, rs.getFloat("column"), 0.0);
347         assertTrue(rs.wasNull());
348         // Set what gets returned to something other than the default
349
float f = (float) 10.0;
350         rs2.setNullFloat(f);
351         assertEquals(f, rs.getFloat(1), 0.0);
352         assertEquals(f, rs.getFloat("column"), 0.0);
353
354     }
355
356     /**
357      * Tests the getInt implementation.
358      */

359     public void testGetInt() throws SQLException JavaDoc {
360
361         assertEquals(0, rs.getInt(1));
362         assertTrue(rs.wasNull());
363         assertEquals(0, rs.getInt("column"));
364         assertTrue(rs.wasNull());
365         // Set what gets returned to something other than the default
366
int i = 10;
367         rs2.setNullInt(i);
368         assertEquals(i, rs.getInt(1));
369         assertEquals(i, rs.getInt("column"));
370
371     }
372
373     /**
374      * Tests the getLong implementation.
375      */

376     public void testGetLong() throws SQLException JavaDoc {
377
378         assertEquals((long) 0, rs.getLong(1));
379         assertTrue(rs.wasNull());
380         assertEquals((long) 0, rs.getLong("column"));
381         assertTrue(rs.wasNull());
382         // Set what gets returned to something other than the default
383
long l = (long) 10;
384         rs2.setNullLong(l);
385         assertEquals(l, rs.getLong(1));
386         assertEquals(l, rs.getLong("column"));
387
388     }
389
390     /**
391      * Tests the getObject implementation.
392      */

393     public void testGetObject() throws SQLException JavaDoc {
394
395         assertNull(rs.getObject(1));
396         assertTrue(rs.wasNull());
397         assertNull(rs.getObject("column"));
398         assertTrue(rs.wasNull());
399         assertNull(rs.getObject(1, (Map JavaDoc) null));
400         assertTrue(rs.wasNull());
401         assertNull(rs.getObject("column", (Map JavaDoc) null));
402         assertTrue(rs.wasNull());
403         // Set what gets returned to something other than the default
404
Object JavaDoc o = new Object JavaDoc();
405         rs2.setNullObject(o);
406         assertNotNull(rs.getObject(1));
407         assertEquals(o, rs.getObject(1));
408         assertNotNull(rs.getObject("column"));
409         assertEquals(o, rs.getObject("column"));
410         assertNotNull(rs.getObject(1, (Map JavaDoc) null));
411         assertEquals(o, rs.getObject(1, (Map JavaDoc) null));
412         assertNotNull(rs.getObject("column", (Map JavaDoc) null));
413         assertEquals(o, rs.getObject("column", (Map JavaDoc) null));
414
415     }
416
417     /**
418      * Tests the getRef implementation.
419      */

420     public void testGetRef() throws SQLException JavaDoc {
421
422         assertNull(rs.getRef(1));
423         assertTrue(rs.wasNull());
424         assertNull(rs.getRef("column"));
425         assertTrue(rs.wasNull());
426         // Set what gets returned to something other than the default
427
Ref JavaDoc ref = new SqlNullCheckedResultSetMockRef();
428         rs2.setNullRef(ref);
429         assertNotNull(rs.getRef(1));
430         assertEquals(ref, rs.getRef(1));
431         assertNotNull(rs.getRef("column"));
432         assertEquals(ref, rs.getRef("column"));
433
434     }
435
436     /**
437      * Tests the getShort implementation.
438      */

439     public void testGetShort() throws SQLException JavaDoc {
440
441         assertEquals((short) 0, rs.getShort(1));
442         assertTrue(rs.wasNull());
443         assertEquals((short) 0, rs.getShort("column"));
444         assertTrue(rs.wasNull());
445         // Set what gets returned to something other than the default
446
short s = (short) 10;
447         rs2.setNullShort(s);
448         assertEquals(s, rs.getShort(1));
449         assertEquals(s, rs.getShort("column"));
450     }
451
452     /**
453      * Tests the getString implementation.
454      */

455     public void testGetString() throws SQLException JavaDoc {
456         assertEquals(null, rs.getString(1));
457         assertTrue(rs.wasNull());
458         assertEquals(null, rs.getString("column"));
459         assertTrue(rs.wasNull());
460         // Set what gets returned to something other than the default
461
String JavaDoc s = "hello, world";
462         rs2.setNullString(s);
463         assertEquals(s, rs.getString(1));
464         assertEquals(s, rs.getString("column"));
465     }
466
467     /**
468      * Tests the getTime implementation.
469      */

470     public void testGetTime() throws SQLException JavaDoc {
471
472         assertNull(rs.getTime(1));
473         assertTrue(rs.wasNull());
474         assertNull(rs.getTime("column"));
475         assertTrue(rs.wasNull());
476         assertNull(rs.getTime(1, Calendar.getInstance()));
477         assertTrue(rs.wasNull());
478         assertNull(rs.getTime("column", Calendar.getInstance()));
479         assertTrue(rs.wasNull());
480         // Set what gets returned to something other than the default
481
Time JavaDoc time = new Time JavaDoc(new java.util.Date JavaDoc().getTime());
482         rs2.setNullTime(time);
483         assertNotNull(rs.getTime(1));
484         assertEquals(time, rs.getTime(1));
485         assertNotNull(rs.getTime("column"));
486         assertEquals(time, rs.getTime("column"));
487         assertNotNull(rs.getTime(1, Calendar.getInstance()));
488         assertEquals(time, rs.getTime(1, Calendar.getInstance()));
489         assertNotNull(rs.getTime("column", Calendar.getInstance()));
490         assertEquals(time, rs.getTime("column", Calendar.getInstance()));
491
492     }
493
494     /**
495      * Tests the getTimestamp implementation.
496      */

497     public void testGetTimestamp() throws SQLException JavaDoc {
498
499         assertNull(rs.getTimestamp(1));
500         assertTrue(rs.wasNull());
501         assertNull(rs.getTimestamp("column"));
502         assertTrue(rs.wasNull());
503         assertNull(rs.getTimestamp(1, Calendar.getInstance()));
504         assertTrue(rs.wasNull());
505         assertNull(rs.getTimestamp("column", Calendar.getInstance()));
506         assertTrue(rs.wasNull());
507         // Set what gets returned to something other than the default
508
Timestamp JavaDoc ts = new Timestamp JavaDoc(new java.util.Date JavaDoc().getTime());
509         rs2.setNullTimestamp(ts);
510         assertNotNull(rs.getTimestamp(1));
511         assertEquals(ts, rs.getTimestamp(1));
512         assertNotNull(rs.getTimestamp("column"));
513         assertEquals(ts, rs.getTimestamp("column"));
514         assertNotNull(rs.getTimestamp(1, Calendar.getInstance()));
515         assertEquals(ts, rs.getTimestamp(1, Calendar.getInstance()));
516         assertNotNull(rs.getTimestamp("column", Calendar.getInstance()));
517         assertEquals(ts, rs.getTimestamp("column", Calendar.getInstance()));
518     }
519     
520     /**
521      * Tests the getURL implementation.
522      */

523     public void testGetURL() throws SQLException JavaDoc, MalformedURLException JavaDoc {
524         assertEquals(null, rs.getURL(1));
525         assertTrue(rs.wasNull());
526         assertEquals(null, rs.getURL("column"));
527         assertTrue(rs.wasNull());
528         // Set what gets returned to something other than the default
529
URL JavaDoc u = new URL JavaDoc("http://www.apache.org");
530         rs2.setNullURL(u);
531         assertEquals(u, rs.getURL(1));
532         assertEquals(u, rs.getURL("column"));
533     }
534
535     /**
536      * Tests the setNullAsciiStream implementation.
537      */

538     public void testSetNullAsciiStream() throws SQLException JavaDoc {
539
540         assertNull(rs2.getNullAsciiStream());
541         // Set what gets returned to something other than the default
542
InputStream JavaDoc stream = new ByteArrayInputStream JavaDoc(new byte[0]);
543         rs2.setNullAsciiStream(stream);
544         assertNotNull(rs.getAsciiStream(1));
545         assertEquals(stream, rs.getAsciiStream(1));
546         assertNotNull(rs.getAsciiStream("column"));
547         assertEquals(stream, rs.getAsciiStream("column"));
548
549     }
550
551     /**
552      * Tests the setNullBigDecimal implementation.
553      */

554     public void testSetNullBigDecimal() throws SQLException JavaDoc {
555
556         assertNull(rs2.getNullBigDecimal());
557         // Set what gets returned to something other than the default
558
BigDecimal JavaDoc bd = new BigDecimal JavaDoc(5.0);
559         rs2.setNullBigDecimal(bd);
560         assertNotNull(rs.getBigDecimal(1));
561         assertEquals(bd, rs.getBigDecimal(1));
562         assertNotNull(rs.getBigDecimal("column"));
563         assertEquals(bd, rs.getBigDecimal("column"));
564
565     }
566
567     /**
568      * Tests the setNullBinaryStream implementation.
569      */

570     public void testSetNullBinaryStream() throws SQLException JavaDoc {
571
572         assertNull(rs2.getNullBinaryStream());
573         // Set what gets returned to something other than the default
574
InputStream JavaDoc stream = new ByteArrayInputStream JavaDoc(new byte[0]);
575         rs2.setNullBinaryStream(stream);
576         assertNotNull(rs.getBinaryStream(1));
577         assertEquals(stream, rs.getBinaryStream(1));
578         assertNotNull(rs.getBinaryStream("column"));
579         assertEquals(stream, rs.getBinaryStream("column"));
580
581     }
582
583     /**
584      * Tests the setNullBlob implementation.
585      */

586     public void testSetNullBlob() throws SQLException JavaDoc {
587
588         assertNull(rs2.getNullBlob());
589         // Set what gets returned to something other than the default
590
Blob JavaDoc blob = new SqlNullCheckedResultSetMockBlob();
591         rs2.setNullBlob(blob);
592         assertNotNull(rs.getBlob(1));
593         assertEquals(blob, rs.getBlob(1));
594         assertNotNull(rs.getBlob("column"));
595         assertEquals(blob, rs.getBlob("column"));
596
597     }
598
599     /**
600      * Tests the setNullBoolean implementation.
601      */

602     public void testSetNullBoolean() throws SQLException JavaDoc {
603
604         assertEquals(false, rs2.getNullBoolean());
605         // Set what gets returned to something other than the default
606
rs2.setNullBoolean(true);
607         assertEquals(true, rs.getBoolean(1));
608         assertEquals(true, rs.getBoolean("column"));
609
610     }
611
612     /**
613      * Tests the setNullByte implementation.
614      */

615     public void testSetNullByte() throws SQLException JavaDoc {
616
617         assertEquals((byte) 0, rs2.getNullByte());
618         // Set what gets returned to something other than the default
619
byte b = (byte) 10;
620         rs2.setNullByte(b);
621         assertEquals(b, rs.getByte(1));
622         assertEquals(b, rs.getByte("column"));
623
624     }
625
626     /**
627      * Tests the setNullByte implementation.
628      */

629     public void testSetNullBytes() throws SQLException JavaDoc {
630
631         assertNull(rs2.getNullBytes());
632         // Set what gets returned to something other than the default
633
byte[] b = new byte[5];
634         for (int i = 0; i < 5; i++) {
635             b[0] = (byte) i;
636         }
637         rs2.setNullBytes(b);
638         assertNotNull(rs.getBytes(1));
639         assertEquals(b, rs.getBytes(1));
640         assertNotNull(rs.getBytes("column"));
641         assertEquals(b, rs.getBytes("column"));
642
643     }
644
645     /**
646      * Tests the setNullCharacterStream implementation.
647      */

648     public void testSetNullCharacterStream() throws SQLException JavaDoc {
649
650         assertNull(rs2.getNullCharacterStream());
651         // Set what gets returned to something other than the default
652
Reader JavaDoc reader = new CharArrayReader JavaDoc("this is a string".toCharArray());
653         rs2.setNullCharacterStream(reader);
654         assertNotNull(rs.getCharacterStream(1));
655         assertEquals(reader, rs.getCharacterStream(1));
656         assertNotNull(rs.getCharacterStream("column"));
657         assertEquals(reader, rs.getCharacterStream("column"));
658
659     }
660
661     /**
662      * Tests the setNullClob implementation.
663      */

664     public void testSetNullClob() throws SQLException JavaDoc {
665
666         assertNull(rs2.getNullClob());
667         // Set what gets returned to something other than the default
668
Clob JavaDoc clob = new SqlNullCheckedResultSetMockClob();
669         rs2.setNullClob(clob);
670         assertNotNull(rs.getClob(1));
671         assertEquals(clob, rs.getClob(1));
672         assertNotNull(rs.getClob("column"));
673         assertEquals(clob, rs.getClob("column"));
674
675     }
676
677     /**
678      * Tests the setNullDate implementation.
679      */

680     public void testSetNullDate() throws SQLException JavaDoc {
681
682         assertNull(rs2.getNullDate());
683         // Set what gets returned to something other than the default
684
java.sql.Date JavaDoc date = new java.sql.Date JavaDoc(new java.util.Date JavaDoc().getTime());
685         rs2.setNullDate(date);
686         assertNotNull(rs.getDate(1));
687         assertEquals(date, rs.getDate(1));
688         assertNotNull(rs.getDate("column"));
689         assertEquals(date, rs.getDate("column"));
690         assertNotNull(rs.getDate(1, Calendar.getInstance()));
691         assertEquals(date, rs.getDate(1, Calendar.getInstance()));
692         assertNotNull(rs.getDate("column", Calendar.getInstance()));
693         assertEquals(date, rs.getDate("column", Calendar.getInstance()));
694
695     }
696
697     /**
698      * Tests the setNullDouble implementation.
699      */

700     public void testSetNullDouble() throws SQLException JavaDoc {
701
702         assertEquals((double) 0.0, rs2.getNullDouble(), 0.0);
703         // Set what gets returned to something other than the default
704
double d = (double) 10.0;
705         rs2.setNullDouble(d);
706         assertEquals(d, rs.getDouble(1), 0.0);
707         assertEquals(d, rs.getDouble("column"), 0.0);
708
709     }
710
711     /**
712      * Tests the setNullFloat implementation.
713      */

714     public void testSetNullFloat() throws SQLException JavaDoc {
715
716         assertEquals((float) 0.0, rs2.getNullFloat(), 0.0);
717         // Set what gets returned to something other than the default
718
float f = (float) 10.0;
719         rs2.setNullFloat(f);
720         assertEquals(f, rs.getFloat(1), 0.0);
721         assertEquals(f, rs.getFloat("column"), 0.0);
722
723     }
724
725     /**
726      * Tests the setNullInt implementation.
727      */

728     public void testSetNullInt() throws SQLException JavaDoc {
729
730         assertEquals(0, rs2.getNullInt());
731         assertEquals((int) 0, rs.getInt(1));
732         assertTrue(rs.wasNull());
733         assertEquals((int) 0, rs.getInt("column"));
734         assertTrue(rs.wasNull());
735         // Set what gets returned to something other than the default
736
int i = (int) 10;
737         rs2.setNullInt(i);
738         assertEquals(i, rs.getInt(1));
739         assertEquals(i, rs.getInt("column"));
740
741     }
742
743     /**
744      * Tests the setNullLong implementation.
745      */

746     public void testSetNullLong() throws SQLException JavaDoc {
747
748         assertEquals((long) 0, rs2.getNullLong());
749         // Set what gets returned to something other than the default
750
long l = (long) 10;
751         rs2.setNullLong(l);
752         assertEquals(l, rs.getLong(1));
753         assertEquals(l, rs.getLong("column"));
754
755     }
756
757     /**
758      * Tests the setNullObject implementation.
759      */

760     public void testSetNullObject() throws SQLException JavaDoc {
761
762         assertNull(rs2.getNullObject());
763         // Set what gets returned to something other than the default
764
Object JavaDoc o = new Object JavaDoc();
765         rs2.setNullObject(o);
766         assertNotNull(rs.getObject(1));
767         assertEquals(o, rs.getObject(1));
768         assertNotNull(rs.getObject("column"));
769         assertEquals(o, rs.getObject("column"));
770         assertNotNull(rs.getObject(1, (Map JavaDoc) null));
771         assertEquals(o, rs.getObject(1, (Map JavaDoc) null));
772         assertNotNull(rs.getObject("column", (Map JavaDoc) null));
773         assertEquals(o, rs.getObject("column", (Map JavaDoc) null));
774
775     }
776
777     /**
778      * Tests the setNullShort implementation.
779      */

780     public void testSetNullShort() throws SQLException JavaDoc {
781
782         assertEquals((short) 0, rs2.getNullShort());
783         // Set what gets returned to something other than the default
784
short s = (short) 10;
785         rs2.setNullShort(s);
786         assertEquals(s, rs.getShort(1));
787         assertEquals(s, rs.getShort("column"));
788
789     }
790
791     /**
792      * Tests the setNullString implementation.
793      */

794     public void testSetNullString() throws SQLException JavaDoc {
795         assertEquals(null, rs2.getNullString());
796         // Set what gets returned to something other than the default
797
String JavaDoc s = "hello, world";
798         rs2.setNullString(s);
799         assertEquals(s, rs.getString(1));
800         assertEquals(s, rs.getString("column"));
801     }
802
803     /**
804      * Tests the setNullRef implementation.
805      */

806     public void testSetNullRef() throws SQLException JavaDoc {
807         assertNull(rs2.getNullRef());
808         // Set what gets returned to something other than the default
809
Ref JavaDoc ref = new SqlNullCheckedResultSetMockRef();
810         rs2.setNullRef(ref);
811         assertNotNull(rs.getRef(1));
812         assertEquals(ref, rs.getRef(1));
813         assertNotNull(rs.getRef("column"));
814         assertEquals(ref, rs.getRef("column"));
815     }
816
817     /**
818      * Tests the setNullTime implementation.
819      */

820     public void testSetNullTime() throws SQLException JavaDoc {
821         assertEquals(null, rs2.getNullTime());
822         // Set what gets returned to something other than the default
823
Time JavaDoc time = new Time JavaDoc(new java.util.Date JavaDoc().getTime());
824         rs2.setNullTime(time);
825         assertNotNull(rs.getTime(1));
826         assertEquals(time, rs.getTime(1));
827         assertNotNull(rs.getTime("column"));
828         assertEquals(time, rs.getTime("column"));
829         assertNotNull(rs.getTime(1, Calendar.getInstance()));
830         assertEquals(time, rs.getTime(1, Calendar.getInstance()));
831         assertNotNull(rs.getTime("column", Calendar.getInstance()));
832         assertEquals(time, rs.getTime("column", Calendar.getInstance()));
833     }
834
835     /**
836      * Tests the setNullTimestamp implementation.
837      */

838     public void testSetNullTimestamp() throws SQLException JavaDoc {
839         assertEquals(null, rs2.getNullTimestamp());
840         // Set what gets returned to something other than the default
841
Timestamp JavaDoc ts = new Timestamp JavaDoc(new java.util.Date JavaDoc().getTime());
842         rs2.setNullTimestamp(ts);
843         assertNotNull(rs.getTimestamp(1));
844         assertEquals(ts, rs.getTimestamp(1));
845         assertNotNull(rs.getTimestamp("column"));
846         assertEquals(ts, rs.getTimestamp("column"));
847         assertNotNull(rs.getTimestamp(1, Calendar.getInstance()));
848         assertEquals(ts, rs.getTimestamp(1, Calendar.getInstance()));
849         assertNotNull(rs.getTimestamp("column", Calendar.getInstance()));
850         assertEquals(ts, rs.getTimestamp("column", Calendar.getInstance()));
851     }
852     
853     /**
854      * Tests the setNullString implementation.
855      */

856     public void testSetNullURL() throws SQLException JavaDoc, MalformedURLException JavaDoc {
857         assertEquals(null, rs2.getNullURL());
858         // Set what gets returned to something other than the default
859
URL JavaDoc u = new URL JavaDoc("http://jakarta.apache.org");
860         rs2.setNullURL(u);
861         assertEquals(u, rs.getURL(1));
862         assertEquals(u, rs.getURL("column"));
863     }
864 }
865
866 class SqlNullUncheckedMockResultSet implements InvocationHandler JavaDoc {
867
868     /**
869      * Always return false for booleans, 0 for numerics, and null for Objects.
870      * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
871      */

872     public Object JavaDoc invoke(Object JavaDoc proxy, Method JavaDoc method, Object JavaDoc[] args)
873         throws Throwable JavaDoc {
874
875         Class JavaDoc returnType = method.getReturnType();
876
877         if (method.getName().equals("wasNull")) {
878             return Boolean.TRUE;
879
880         } else if (returnType.equals(Boolean.TYPE)) {
881             return Boolean.FALSE;
882
883         } else if (returnType.equals(Integer.TYPE)) {
884             return new Integer JavaDoc(0);
885
886         } else if (returnType.equals(Short.TYPE)) {
887             return new Short JavaDoc((short) 0);
888
889         } else if (returnType.equals(Double.TYPE)) {
890             return new Double JavaDoc(0);
891
892         } else if (returnType.equals(Long.TYPE)) {
893             return new Long JavaDoc(0);
894
895         } else if (returnType.equals(Byte.TYPE)) {
896             return new Byte JavaDoc((byte) 0);
897
898         } else if (returnType.equals(Float.TYPE)) {
899             return new Float JavaDoc(0);
900
901         } else {
902             return null;
903         }
904     }
905 }
906
907 class SqlNullCheckedResultSetMockBlob implements Blob JavaDoc {
908
909     public InputStream JavaDoc getBinaryStream() throws SQLException JavaDoc {
910         return new ByteArrayInputStream JavaDoc(new byte[0]);
911     }
912
913     public byte[] getBytes(long param, int param1) throws SQLException JavaDoc {
914         return new byte[0];
915     }
916
917     public long length() throws SQLException JavaDoc {
918         return 0;
919     }
920
921     public long position(byte[] values, long param) throws SQLException JavaDoc {
922         return 0;
923     }
924
925     public long position(Blob JavaDoc blob, long param) throws SQLException JavaDoc {
926         return 0;
927     }
928
929     public void truncate(long len) throws SQLException JavaDoc {
930
931     }
932
933     public int setBytes(long pos, byte[] bytes) throws SQLException JavaDoc {
934         return 0;
935     }
936
937     public int setBytes(long pos, byte[] bytes, int offset, int len)
938         throws SQLException JavaDoc {
939         return 0;
940     }
941
942     public OutputStream JavaDoc setBinaryStream(long pos) throws SQLException JavaDoc {
943         return null;
944     }
945
946 }
947
948 class SqlNullCheckedResultSetMockClob implements Clob JavaDoc {
949
950     public InputStream JavaDoc getAsciiStream() throws SQLException JavaDoc {
951         return null;
952     }
953
954     public Reader JavaDoc getCharacterStream() throws SQLException JavaDoc {
955         return null;
956     }
957
958     public String JavaDoc getSubString(long param, int param1) throws SQLException JavaDoc {
959         return "";
960     }
961
962     public long length() throws SQLException JavaDoc {
963         return 0;
964     }
965
966     public long position(Clob JavaDoc clob, long param) throws SQLException JavaDoc {
967         return 0;
968     }
969
970     public long position(String JavaDoc str, long param) throws SQLException JavaDoc {
971         return 0;
972     }
973
974     public void truncate(long len) throws SQLException JavaDoc {
975
976     }
977
978     public OutputStream JavaDoc setAsciiStream(long pos) throws SQLException JavaDoc {
979         return null;
980     }
981
982     public Writer JavaDoc setCharacterStream(long pos) throws SQLException JavaDoc {
983         return null;
984     }
985
986     public int setString(long pos, String JavaDoc str) throws SQLException JavaDoc {
987         return 0;
988     }
989
990     public int setString(long pos, String JavaDoc str, int offset, int len)
991         throws SQLException JavaDoc {
992         return 0;
993     }
994
995 }
996
997 class SqlNullCheckedResultSetMockRef implements Ref JavaDoc {
998
999     public String JavaDoc getBaseTypeName() throws SQLException JavaDoc {
1000        return "";
1001    }
1002
1003    public Object JavaDoc getObject() throws SQLException JavaDoc {
1004        return null;
1005    }
1006
1007    public void setObject(Object JavaDoc value) throws SQLException JavaDoc {
1008
1009    }
1010
1011    public Object JavaDoc getObject(Map JavaDoc map) throws SQLException JavaDoc {
1012        return null;
1013    }
1014
1015}
1016
Popular Tags