001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.dbutils.wrappers;
018:
019: import java.io.ByteArrayInputStream;
020: import java.io.CharArrayReader;
021: import java.io.InputStream;
022: import java.io.OutputStream;
023: import java.io.Reader;
024: import java.io.Writer;
025: import java.lang.reflect.InvocationHandler;
026: import java.lang.reflect.Method;
027: import java.math.BigDecimal;
028: import java.net.MalformedURLException;
029: import java.net.URL;
030: import java.sql.Blob;
031: import java.sql.Clob;
032: import java.sql.Ref;
033: import java.sql.ResultSet;
034: import java.sql.SQLException;
035: import java.sql.Time;
036: import java.sql.Timestamp;
037: import java.util.Calendar;
038: import java.util.Map;
039:
040: import org.apache.commons.dbutils.BaseTestCase;
041: import org.apache.commons.dbutils.ProxyFactory;
042:
043: /**
044: * Test cases for <code>SqlNullCheckedResultSet</code> class.
045: */
046: public class SqlNullCheckedResultSetTest extends BaseTestCase {
047:
048: private ResultSet rs = null;
049:
050: private SqlNullCheckedResultSet rs2 = null;
051:
052: /**
053: * Constructs a new instance of
054: * <code>SqlNullCheckedResultSetTestCase</code>
055: * with the specified name.
056: *
057: * @param name the test case name
058: */
059: public SqlNullCheckedResultSetTest(String name) {
060: super (name);
061: }
062:
063: /**
064: * Sets up instance variables required by this test case.
065: */
066: public void setUp() throws Exception {
067: super .setUp();
068:
069: rs2 = new SqlNullCheckedResultSet(ProxyFactory.instance()
070: .createResultSet(new SqlNullUncheckedMockResultSet()));
071:
072: rs = ProxyFactory.instance().createResultSet(rs2);
073: }
074:
075: /**
076: * Tests the getAsciiStream implementation.
077: */
078: public void testGetAsciiStream() throws SQLException {
079:
080: assertNull(rs.getAsciiStream(1));
081: assertTrue(rs.wasNull());
082: assertNull(rs.getAsciiStream("column"));
083: assertTrue(rs.wasNull());
084: // Set what gets returned to something other than the default
085: InputStream stream = new ByteArrayInputStream(new byte[0]);
086: rs2.setNullAsciiStream(stream);
087: assertNotNull(rs.getAsciiStream(1));
088: assertEquals(stream, rs.getAsciiStream(1));
089: assertNotNull(rs.getAsciiStream("column"));
090: assertEquals(stream, rs.getAsciiStream("column"));
091:
092: }
093:
094: /**
095: * Tests the getBigDecimal implementation.
096: */
097: public void testGetBigDecimal() throws SQLException {
098:
099: assertNull(rs.getBigDecimal(1));
100: assertTrue(rs.wasNull());
101: assertNull(rs.getBigDecimal("column"));
102: assertTrue(rs.wasNull());
103: // Set what gets returned to something other than the default
104: BigDecimal bd = new BigDecimal(5.0);
105: rs2.setNullBigDecimal(bd);
106: assertNotNull(rs.getBigDecimal(1));
107: assertEquals(bd, rs.getBigDecimal(1));
108: assertNotNull(rs.getBigDecimal("column"));
109: assertEquals(bd, rs.getBigDecimal("column"));
110:
111: }
112:
113: /**
114: * Tests the getBinaryStream implementation.
115: */
116: public void testGetBinaryStream() throws SQLException {
117:
118: assertNull(rs.getBinaryStream(1));
119: assertTrue(rs.wasNull());
120: assertNull(rs.getBinaryStream("column"));
121: assertTrue(rs.wasNull());
122: // Set what gets returned to something other than the default
123: InputStream stream = new ByteArrayInputStream(new byte[0]);
124: rs2.setNullBinaryStream(stream);
125: assertNotNull(rs.getBinaryStream(1));
126: assertEquals(stream, rs.getBinaryStream(1));
127: assertNotNull(rs.getBinaryStream("column"));
128: assertEquals(stream, rs.getBinaryStream("column"));
129:
130: }
131:
132: /**
133: * Tests the getBlob implementation.
134: */
135: public void testGetBlob() throws SQLException {
136:
137: assertNull(rs.getBlob(1));
138: assertTrue(rs.wasNull());
139: assertNull(rs.getBlob("column"));
140: assertTrue(rs.wasNull());
141: // Set what gets returned to something other than the default
142: Blob blob = new SqlNullCheckedResultSetMockBlob();
143: rs2.setNullBlob(blob);
144: assertNotNull(rs.getBlob(1));
145: assertEquals(blob, rs.getBlob(1));
146: assertNotNull(rs.getBlob("column"));
147: assertEquals(blob, rs.getBlob("column"));
148:
149: }
150:
151: /**
152: * Tests the getBoolean implementation.
153: */
154: public void testGetBoolean() throws SQLException {
155:
156: assertEquals(false, rs.getBoolean(1));
157: assertTrue(rs.wasNull());
158: assertEquals(false, rs.getBoolean("column"));
159: assertTrue(rs.wasNull());
160: // Set what gets returned to something other than the default
161: rs2.setNullBoolean(true);
162: assertEquals(true, rs.getBoolean(1));
163: assertEquals(true, rs.getBoolean("column"));
164:
165: }
166:
167: /**
168: * Tests the getByte implementation.
169: */
170: public void testGetByte() throws SQLException {
171:
172: assertEquals((byte) 0, rs.getByte(1));
173: assertTrue(rs.wasNull());
174: assertEquals((byte) 0, rs.getByte("column"));
175: assertTrue(rs.wasNull());
176: // Set what gets returned to something other than the default
177: byte b = (byte) 10;
178: rs2.setNullByte(b);
179: assertEquals(b, rs.getByte(1));
180: assertEquals(b, rs.getByte("column"));
181:
182: }
183:
184: /**
185: * Tests the getByte implementation.
186: */
187: public void testGetBytes() throws SQLException {
188:
189: assertNull(rs.getBytes(1));
190: assertTrue(rs.wasNull());
191: assertNull(rs.getBytes("column"));
192: assertTrue(rs.wasNull());
193: // Set what gets returned to something other than the default
194: byte[] b = new byte[5];
195: for (int i = 0; i < 5; i++) {
196: b[0] = (byte) i;
197: }
198: rs2.setNullBytes(b);
199: assertNotNull(rs.getBytes(1));
200: assertEquals(b, rs.getBytes(1));
201: assertNotNull(rs.getBytes("column"));
202: assertEquals(b, rs.getBytes("column"));
203:
204: }
205:
206: /**
207: * Tests the getCharacterStream implementation.
208: */
209: public void testGetCharacterStream() throws SQLException {
210:
211: assertNull(rs.getCharacterStream(1));
212: assertTrue(rs.wasNull());
213: assertNull(rs.getCharacterStream("column"));
214: assertTrue(rs.wasNull());
215: // Set what gets returned to something other than the default
216: Reader reader = new CharArrayReader("this is a string"
217: .toCharArray());
218: rs2.setNullCharacterStream(reader);
219: assertNotNull(rs.getCharacterStream(1));
220: assertEquals(reader, rs.getCharacterStream(1));
221: assertNotNull(rs.getCharacterStream("column"));
222: assertEquals(reader, rs.getCharacterStream("column"));
223:
224: }
225:
226: /**
227: * Tests the getClob implementation.
228: */
229: public void testGetClob() throws SQLException {
230:
231: assertNull(rs.getClob(1));
232: assertTrue(rs.wasNull());
233: assertNull(rs.getClob("column"));
234: assertTrue(rs.wasNull());
235: // Set what gets returned to something other than the default
236: Clob clob = new SqlNullCheckedResultSetMockClob();
237: rs2.setNullClob(clob);
238: assertNotNull(rs.getClob(1));
239: assertEquals(clob, rs.getClob(1));
240: assertNotNull(rs.getClob("column"));
241: assertEquals(clob, rs.getClob("column"));
242:
243: }
244:
245: /**
246: * Tests the getDate implementation.
247: */
248: public void testGetDate() throws SQLException {
249:
250: assertNull(rs.getDate(1));
251: assertTrue(rs.wasNull());
252: assertNull(rs.getDate("column"));
253: assertTrue(rs.wasNull());
254: assertNull(rs.getDate(1, Calendar.getInstance()));
255: assertTrue(rs.wasNull());
256: assertNull(rs.getDate("column", Calendar.getInstance()));
257: assertTrue(rs.wasNull());
258: // Set what gets returned to something other than the default
259: java.sql.Date date = new java.sql.Date(new java.util.Date()
260: .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
463: .getInstance()));
464: }
465:
466: /**
467: * Tests the getURL and setNullURL implementations.
468: *
469: * Uses reflection to allow for building under JDK 1.3.
470: */
471: public void testURL() throws SQLException, MalformedURLException,
472: IllegalAccessException, IllegalArgumentException,
473: java.lang.reflect.InvocationTargetException {
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"
621: .toCharArray());
622: rs2.setNullCharacterStream(reader);
623: assertNotNull(rs.getCharacterStream(1));
624: assertEquals(reader, rs.getCharacterStream(1));
625: assertNotNull(rs.getCharacterStream("column"));
626: assertEquals(reader, rs.getCharacterStream("column"));
627:
628: }
629:
630: /**
631: * Tests the setNullClob implementation.
632: */
633: public void testSetNullClob() throws SQLException {
634:
635: assertNull(rs2.getNullClob());
636: // Set what gets returned to something other than the default
637: Clob clob = new SqlNullCheckedResultSetMockClob();
638: rs2.setNullClob(clob);
639: assertNotNull(rs.getClob(1));
640: assertEquals(clob, rs.getClob(1));
641: assertNotNull(rs.getClob("column"));
642: assertEquals(clob, rs.getClob("column"));
643:
644: }
645:
646: /**
647: * Tests the setNullDate implementation.
648: */
649: public void testSetNullDate() throws SQLException {
650:
651: assertNull(rs2.getNullDate());
652: // Set what gets returned to something other than the default
653: java.sql.Date date = new java.sql.Date(new java.util.Date()
654: .getTime());
655: rs2.setNullDate(date);
656: assertNotNull(rs.getDate(1));
657: assertEquals(date, rs.getDate(1));
658: assertNotNull(rs.getDate("column"));
659: assertEquals(date, rs.getDate("column"));
660: assertNotNull(rs.getDate(1, Calendar.getInstance()));
661: assertEquals(date, rs.getDate(1, Calendar.getInstance()));
662: assertNotNull(rs.getDate("column", Calendar.getInstance()));
663: assertEquals(date, rs.getDate("column", Calendar.getInstance()));
664:
665: }
666:
667: /**
668: * Tests the setNullDouble implementation.
669: */
670: public void testSetNullDouble() throws SQLException {
671: assertEquals(0.0, rs2.getNullDouble(), 0.0);
672: // Set what gets returned to something other than the default
673: double d = 10.0;
674: rs2.setNullDouble(d);
675: assertEquals(d, rs.getDouble(1), 0.0);
676: assertEquals(d, rs.getDouble("column"), 0.0);
677: }
678:
679: /**
680: * Tests the setNullFloat implementation.
681: */
682: public void testSetNullFloat() throws SQLException {
683: assertEquals((float) 0.0, rs2.getNullFloat(), 0.0);
684: // Set what gets returned to something other than the default
685: float f = (float) 10.0;
686: rs2.setNullFloat(f);
687: assertEquals(f, rs.getFloat(1), 0.0);
688: assertEquals(f, rs.getFloat("column"), 0.0);
689: }
690:
691: /**
692: * Tests the setNullInt implementation.
693: */
694: public void testSetNullInt() throws SQLException {
695: assertEquals(0, rs2.getNullInt());
696: assertEquals(0, rs.getInt(1));
697: assertTrue(rs.wasNull());
698: assertEquals(0, rs.getInt("column"));
699: assertTrue(rs.wasNull());
700: // Set what gets returned to something other than the default
701: int i = 10;
702: rs2.setNullInt(i);
703: assertEquals(i, rs.getInt(1));
704: assertEquals(i, rs.getInt("column"));
705: }
706:
707: /**
708: * Tests the setNullLong implementation.
709: */
710: public void testSetNullLong() throws SQLException {
711: assertEquals(0, rs2.getNullLong());
712: // Set what gets returned to something other than the default
713: long l = 10;
714: rs2.setNullLong(l);
715: assertEquals(l, rs.getLong(1));
716: assertEquals(l, rs.getLong("column"));
717: }
718:
719: /**
720: * Tests the setNullObject implementation.
721: */
722: public void testSetNullObject() throws SQLException {
723: assertNull(rs2.getNullObject());
724: // Set what gets returned to something other than the default
725: Object o = new Object();
726: rs2.setNullObject(o);
727: assertNotNull(rs.getObject(1));
728: assertEquals(o, rs.getObject(1));
729: assertNotNull(rs.getObject("column"));
730: assertEquals(o, rs.getObject("column"));
731: assertNotNull(rs.getObject(1, (Map) null));
732: assertEquals(o, rs.getObject(1, (Map) null));
733: assertNotNull(rs.getObject("column", (Map) null));
734: assertEquals(o, rs.getObject("column", (Map) null));
735: }
736:
737: /**
738: * Tests the setNullShort implementation.
739: */
740: public void testSetNullShort() throws SQLException {
741:
742: assertEquals((short) 0, rs2.getNullShort());
743: // Set what gets returned to something other than the default
744: short s = (short) 10;
745: rs2.setNullShort(s);
746: assertEquals(s, rs.getShort(1));
747: assertEquals(s, rs.getShort("column"));
748:
749: }
750:
751: /**
752: * Tests the setNullString implementation.
753: */
754: public void testSetNullString() throws SQLException {
755: assertEquals(null, rs2.getNullString());
756: // Set what gets returned to something other than the default
757: String s = "hello, world";
758: rs2.setNullString(s);
759: assertEquals(s, rs.getString(1));
760: assertEquals(s, rs.getString("column"));
761: }
762:
763: /**
764: * Tests the setNullRef implementation.
765: */
766: public void testSetNullRef() throws SQLException {
767: assertNull(rs2.getNullRef());
768: // Set what gets returned to something other than the default
769: Ref ref = new SqlNullCheckedResultSetMockRef();
770: rs2.setNullRef(ref);
771: assertNotNull(rs.getRef(1));
772: assertEquals(ref, rs.getRef(1));
773: assertNotNull(rs.getRef("column"));
774: assertEquals(ref, rs.getRef("column"));
775: }
776:
777: /**
778: * Tests the setNullTime implementation.
779: */
780: public void testSetNullTime() throws SQLException {
781: assertEquals(null, rs2.getNullTime());
782: // Set what gets returned to something other than the default
783: Time time = new Time(new java.util.Date().getTime());
784: rs2.setNullTime(time);
785: assertNotNull(rs.getTime(1));
786: assertEquals(time, rs.getTime(1));
787: assertNotNull(rs.getTime("column"));
788: assertEquals(time, rs.getTime("column"));
789: assertNotNull(rs.getTime(1, Calendar.getInstance()));
790: assertEquals(time, rs.getTime(1, Calendar.getInstance()));
791: assertNotNull(rs.getTime("column", Calendar.getInstance()));
792: assertEquals(time, rs.getTime("column", Calendar.getInstance()));
793: }
794:
795: /**
796: * Tests the setNullTimestamp implementation.
797: */
798: public void testSetNullTimestamp() throws SQLException {
799: assertEquals(null, rs2.getNullTimestamp());
800: // Set what gets returned to something other than the default
801: Timestamp ts = new Timestamp(new java.util.Date().getTime());
802: rs2.setNullTimestamp(ts);
803: assertNotNull(rs.getTimestamp(1));
804: assertEquals(ts, rs.getTimestamp(1));
805: assertNotNull(rs.getTimestamp("column"));
806: assertEquals(ts, rs.getTimestamp("column"));
807: assertNotNull(rs.getTimestamp(1, Calendar.getInstance()));
808: assertEquals(ts, rs.getTimestamp(1, Calendar.getInstance()));
809: assertNotNull(rs.getTimestamp("column", Calendar.getInstance()));
810: assertEquals(ts, rs.getTimestamp("column", Calendar
811: .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: public Object invoke(Object proxy, Method method, Object[] args)
823: throws Throwable {
824:
825: Class returnType = method.getReturnType();
826:
827: if (method.getName().equals("wasNull")) {
828: return Boolean.TRUE;
829:
830: } else if (returnType.equals(Boolean.TYPE)) {
831: return Boolean.FALSE;
832:
833: } else if (returnType.equals(Integer.TYPE)) {
834: return new Integer(0);
835:
836: } else if (returnType.equals(Short.TYPE)) {
837: return new Short((short) 0);
838:
839: } else if (returnType.equals(Double.TYPE)) {
840: return new Double(0);
841:
842: } else if (returnType.equals(Long.TYPE)) {
843: return new Long(0);
844:
845: } else if (returnType.equals(Byte.TYPE)) {
846: return new Byte((byte) 0);
847:
848: } else if (returnType.equals(Float.TYPE)) {
849: return new Float(0);
850:
851: } else {
852: return null;
853: }
854: }
855: }
856:
857: class SqlNullCheckedResultSetMockBlob implements Blob {
858:
859: public InputStream getBinaryStream() throws SQLException {
860: return new ByteArrayInputStream(new byte[0]);
861: }
862:
863: public byte[] getBytes(long param, int param1) throws SQLException {
864: return new byte[0];
865: }
866:
867: public long length() throws SQLException {
868: return 0;
869: }
870:
871: public long position(byte[] values, long param) throws SQLException {
872: return 0;
873: }
874:
875: public long position(Blob blob, long param) throws SQLException {
876: return 0;
877: }
878:
879: public void truncate(long len) throws SQLException {
880:
881: }
882:
883: public int setBytes(long pos, byte[] bytes) throws SQLException {
884: return 0;
885: }
886:
887: public int setBytes(long pos, byte[] bytes, int offset, int len)
888: throws SQLException {
889: return 0;
890: }
891:
892: public OutputStream setBinaryStream(long pos) throws SQLException {
893: return null;
894: }
895:
896: public void free() throws SQLException {
897:
898: }
899:
900: public InputStream getBinaryStream(long pos, long length)
901: throws SQLException {
902: return null;
903: }
904:
905: }
906:
907: class SqlNullCheckedResultSetMockClob implements Clob {
908:
909: public InputStream getAsciiStream() throws SQLException {
910: return null;
911: }
912:
913: public Reader getCharacterStream() throws SQLException {
914: return null;
915: }
916:
917: public String getSubString(long param, int param1)
918: throws SQLException {
919: return "";
920: }
921:
922: public long length() throws SQLException {
923: return 0;
924: }
925:
926: public long position(Clob clob, long param) throws SQLException {
927: return 0;
928: }
929:
930: public long position(String str, long param) throws SQLException {
931: return 0;
932: }
933:
934: public void truncate(long len) throws SQLException {
935:
936: }
937:
938: public OutputStream setAsciiStream(long pos) throws SQLException {
939: return null;
940: }
941:
942: public Writer setCharacterStream(long pos) throws SQLException {
943: return null;
944: }
945:
946: public int setString(long pos, String str) throws SQLException {
947: return 0;
948: }
949:
950: public int setString(long pos, String str, int offset, int len)
951: throws SQLException {
952: return 0;
953: }
954:
955: public void free() throws SQLException {
956:
957: }
958:
959: public Reader getCharacterStream(long pos, long length)
960: throws SQLException {
961: return null;
962: }
963:
964: }
965:
966: class SqlNullCheckedResultSetMockRef implements Ref {
967:
968: public String getBaseTypeName() throws SQLException {
969: return "";
970: }
971:
972: public Object getObject() throws SQLException {
973: return null;
974: }
975:
976: public void setObject(Object value) throws SQLException {
977:
978: }
979:
980: public Object getObject(Map map) throws SQLException {
981: return null;
982: }
983:
984: }
|