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.Arrays;
038 import java.util.Calendar;
039 import java.util.Map;
040
041 import org.apache.commons.dbutils.BaseTestCase;
042 import org.apache.commons.dbutils.ProxyFactory;
043
044 /**
045 * Test cases for <code>SqlNullCheckedResultSet</code> class.
046 */
047 public class SqlNullCheckedResultSetTest extends BaseTestCase {
048
049 private SqlNullCheckedResultSet rs2 = null;
050
051 /**
052 * Sets up instance variables required by this test case.
053 */
054 @Override
055 public void setUp() throws Exception {
056 super.setUp();
057
058 rs2 =
059 new SqlNullCheckedResultSet(
060 ProxyFactory.instance().createResultSet(
061 new SqlNullUncheckedMockResultSet()));
062
063 rs = ProxyFactory.instance().createResultSet(rs2); // Override superclass field
064 }
065
066 /**
067 * Tests the getAsciiStream implementation.
068 */
069 public void testGetAsciiStream() throws SQLException {
070
071 assertNull(rs.getAsciiStream(1));
072 assertTrue(rs.wasNull());
073 assertNull(rs.getAsciiStream("column"));
074 assertTrue(rs.wasNull());
075 // Set what gets returned to something other than the default
076 InputStream stream = new ByteArrayInputStream(new byte[0]);
077 rs2.setNullAsciiStream(stream);
078 assertNotNull(rs.getAsciiStream(1));
079 assertEquals(stream, rs.getAsciiStream(1));
080 assertNotNull(rs.getAsciiStream("column"));
081 assertEquals(stream, rs.getAsciiStream("column"));
082
083 }
084
085 /**
086 * Tests the getBigDecimal implementation.
087 */
088 public void testGetBigDecimal() throws SQLException {
089
090 assertNull(rs.getBigDecimal(1));
091 assertTrue(rs.wasNull());
092 assertNull(rs.getBigDecimal("column"));
093 assertTrue(rs.wasNull());
094 // Set what gets returned to something other than the default
095 BigDecimal bd = new BigDecimal(5.0);
096 rs2.setNullBigDecimal(bd);
097 assertNotNull(rs.getBigDecimal(1));
098 assertEquals(bd, rs.getBigDecimal(1));
099 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) return;
199 if (expected.length != actual.length) {
200 failNotEquals(null, Arrays.toString(expected), Arrays.toString(actual));
201 }
202 for (int i = 0; i < expected.length; i++) {
203 byte expectedItem = expected[i];
204 byte actualItem = actual[i];
205 assertEquals("Array not equal at index " + i, expectedItem, actualItem);
206 }
207 }
208
209 /**
210 * Tests the getCharacterStream implementation.
211 */
212 public void testGetCharacterStream() throws SQLException {
213
214 assertNull(rs.getCharacterStream(1));
215 assertTrue(rs.wasNull());
216 assertNull(rs.getCharacterStream("column"));
217 assertTrue(rs.wasNull());
218 // Set what gets returned to something other than the default
219 Reader reader = new CharArrayReader("this is a string".toCharArray());
220 rs2.setNullCharacterStream(reader);
221 assertNotNull(rs.getCharacterStream(1));
222 assertEquals(reader, rs.getCharacterStream(1));
223 assertNotNull(rs.getCharacterStream("column"));
224 assertEquals(reader, rs.getCharacterStream("column"));
225
226 }
227
228 /**
229 * Tests the getClob implementation.
230 */
231 public void testGetClob() throws SQLException {
232
233 assertNull(rs.getClob(1));
234 assertTrue(rs.wasNull());
235 assertNull(rs.getClob("column"));
236 assertTrue(rs.wasNull());
237 // Set what gets returned to something other than the default
238 Clob clob = new SqlNullCheckedResultSetMockClob();
239 rs2.setNullClob(clob);
240 assertNotNull(rs.getClob(1));
241 assertEquals(clob, rs.getClob(1));
242 assertNotNull(rs.getClob("column"));
243 assertEquals(clob, rs.getClob("column"));
244
245 }
246
247 /**
248 * Tests the getDate implementation.
249 */
250 public void testGetDate() throws SQLException {
251
252 assertNull(rs.getDate(1));
253 assertTrue(rs.wasNull());
254 assertNull(rs.getDate("column"));
255 assertTrue(rs.wasNull());
256 assertNull(rs.getDate(1, Calendar.getInstance()));
257 assertTrue(rs.wasNull());
258 assertNull(rs.getDate("column", Calendar.getInstance()));
259 assertTrue(rs.wasNull());
260 // Set what gets returned to something other than the default
261 java.sql.Date date = new java.sql.Date(new java.util.Date().getTime());
262 rs2.setNullDate(date);
263 assertNotNull(rs.getDate(1));
264 assertEquals(date, rs.getDate(1));
265 assertNotNull(rs.getDate("column"));
266 assertEquals(date, rs.getDate("column"));
267 assertNotNull(rs.getDate(1, Calendar.getInstance()));
268 assertEquals(date, rs.getDate(1, Calendar.getInstance()));
269 assertNotNull(rs.getDate("column", Calendar.getInstance()));
270 assertEquals(date, rs.getDate("column", Calendar.getInstance()));
271
272 }
273
274 /**
275 * Tests the getDouble implementation.
276 */
277 public void testGetDouble() throws SQLException {
278
279 assertEquals(0.0, rs.getDouble(1), 0.0);
280 assertTrue(rs.wasNull());
281 assertEquals(0.0, rs.getDouble("column"), 0.0);
282 assertTrue(rs.wasNull());
283 // Set what gets returned to something other than the default
284 double d = 10.0;
285 rs2.setNullDouble(d);
286 assertEquals(d, rs.getDouble(1), 0.0);
287 assertEquals(d, rs.getDouble("column"), 0.0);
288
289 }
290
291 /**
292 * Tests the getFloat implementation.
293 */
294 public void testGetFloat() throws SQLException {
295 assertEquals(0, rs.getFloat(1), 0.0);
296 assertTrue(rs.wasNull());
297 assertEquals(0, rs.getFloat("column"), 0.0);
298 assertTrue(rs.wasNull());
299 // Set what gets returned to something other than the default
300 float f = 10;
301 rs2.setNullFloat(f);
302 assertEquals(f, rs.getFloat(1), 0.0);
303 assertEquals(f, rs.getFloat("column"), 0.0);
304 }
305
306 /**
307 * Tests the getInt implementation.
308 */
309 public void testGetInt() throws SQLException {
310 assertEquals(0, rs.getInt(1));
311 assertTrue(rs.wasNull());
312 assertEquals(0, rs.getInt("column"));
313 assertTrue(rs.wasNull());
314 // Set what gets returned to something other than the default
315 int i = 10;
316 rs2.setNullInt(i);
317 assertEquals(i, rs.getInt(1));
318 assertEquals(i, rs.getInt("column"));
319 }
320
321 /**
322 * Tests the getLong implementation.
323 */
324 public void testGetLong() throws SQLException {
325 assertEquals(0, rs.getLong(1));
326 assertTrue(rs.wasNull());
327 assertEquals(0, rs.getLong("column"));
328 assertTrue(rs.wasNull());
329 // Set what gets returned to something other than the default
330 long l = 10;
331 rs2.setNullLong(l);
332 assertEquals(l, rs.getLong(1));
333 assertEquals(l, rs.getLong("column"));
334 }
335
336 /**
337 * Tests the getObject implementation.
338 */
339 public void testGetObject() throws SQLException {
340
341 assertNull(rs.getObject(1));
342 assertTrue(rs.wasNull());
343 assertNull(rs.getObject("column"));
344 assertTrue(rs.wasNull());
345 assertNull(rs.getObject(1, (Map<String, Class<?>>) null));
346 assertTrue(rs.wasNull());
347 assertNull(rs.getObject("column", (Map<String, Class<?>>) null));
348 assertTrue(rs.wasNull());
349 // Set what gets returned to something other than the default
350 Object o = new Object();
351 rs2.setNullObject(o);
352 assertNotNull(rs.getObject(1));
353 assertEquals(o, rs.getObject(1));
354 assertNotNull(rs.getObject("column"));
355 assertEquals(o, rs.getObject("column"));
356 assertNotNull(rs.getObject(1, (Map<String, Class<?>>) null));
357 assertEquals(o, rs.getObject(1, (Map<String, Class<?>>) null));
358 assertNotNull(rs.getObject("column", (Map<String, Class<?>>) null));
359 assertEquals(o, rs.getObject("column", (Map<String, Class<?>>) null));
360
361 }
362
363 /**
364 * Tests the getRef implementation.
365 */
366 public void testGetRef() throws SQLException {
367
368 assertNull(rs.getRef(1));
369 assertTrue(rs.wasNull());
370 assertNull(rs.getRef("column"));
371 assertTrue(rs.wasNull());
372 // Set what gets returned to something other than the default
373 Ref ref = new SqlNullCheckedResultSetMockRef();
374 rs2.setNullRef(ref);
375 assertNotNull(rs.getRef(1));
376 assertEquals(ref, rs.getRef(1));
377 assertNotNull(rs.getRef("column"));
378 assertEquals(ref, rs.getRef("column"));
379
380 }
381
382 /**
383 * Tests the getShort implementation.
384 */
385 public void testGetShort() throws SQLException {
386
387 assertEquals((short) 0, rs.getShort(1));
388 assertTrue(rs.wasNull());
389 assertEquals((short) 0, rs.getShort("column"));
390 assertTrue(rs.wasNull());
391 // Set what gets returned to something other than the default
392 short s = (short) 10;
393 rs2.setNullShort(s);
394 assertEquals(s, rs.getShort(1));
395 assertEquals(s, rs.getShort("column"));
396 }
397
398 /**
399 * Tests the getString implementation.
400 */
401 public void testGetString() throws SQLException {
402 assertEquals(null, rs.getString(1));
403 assertTrue(rs.wasNull());
404 assertEquals(null, rs.getString("column"));
405 assertTrue(rs.wasNull());
406 // Set what gets returned to something other than the default
407 String s = "hello, world";
408 rs2.setNullString(s);
409 assertEquals(s, rs.getString(1));
410 assertEquals(s, rs.getString("column"));
411 }
412
413 /**
414 * Tests the getTime implementation.
415 */
416 public void testGetTime() throws SQLException {
417
418 assertNull(rs.getTime(1));
419 assertTrue(rs.wasNull());
420 assertNull(rs.getTime("column"));
421 assertTrue(rs.wasNull());
422 assertNull(rs.getTime(1, Calendar.getInstance()));
423 assertTrue(rs.wasNull());
424 assertNull(rs.getTime("column", Calendar.getInstance()));
425 assertTrue(rs.wasNull());
426 // Set what gets returned to something other than the default
427 Time time = new Time(new java.util.Date().getTime());
428 rs2.setNullTime(time);
429 assertNotNull(rs.getTime(1));
430 assertEquals(time, rs.getTime(1));
431 assertNotNull(rs.getTime("column"));
432 assertEquals(time, rs.getTime("column"));
433 assertNotNull(rs.getTime(1, Calendar.getInstance()));
434 assertEquals(time, rs.getTime(1, Calendar.getInstance()));
435 assertNotNull(rs.getTime("column", Calendar.getInstance()));
436 assertEquals(time, rs.getTime("column", Calendar.getInstance()));
437
438 }
439
440 /**
441 * Tests the getTimestamp implementation.
442 */
443 public void testGetTimestamp() throws SQLException {
444
445 assertNull(rs.getTimestamp(1));
446 assertTrue(rs.wasNull());
447 assertNull(rs.getTimestamp("column"));
448 assertTrue(rs.wasNull());
449 assertNull(rs.getTimestamp(1, Calendar.getInstance()));
450 assertTrue(rs.wasNull());
451 assertNull(rs.getTimestamp("column", Calendar.getInstance()));
452 assertTrue(rs.wasNull());
453 // Set what gets returned to something other than the default
454 Timestamp ts = new Timestamp(new java.util.Date().getTime());
455 rs2.setNullTimestamp(ts);
456 assertNotNull(rs.getTimestamp(1));
457 assertEquals(ts, rs.getTimestamp(1));
458 assertNotNull(rs.getTimestamp("column"));
459 assertEquals(ts, rs.getTimestamp("column"));
460 assertNotNull(rs.getTimestamp(1, Calendar.getInstance()));
461 assertEquals(ts, rs.getTimestamp(1, Calendar.getInstance()));
462 assertNotNull(rs.getTimestamp("column", Calendar.getInstance()));
463 assertEquals(ts, rs.getTimestamp("column", Calendar.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 {
475 Method getUrlInt = null;
476 Method getUrlString = null;
477 try {
478 getUrlInt = ResultSet.class.getMethod("getURL",
479 new Class[] { Integer.TYPE } );
480 getUrlString = ResultSet.class.getMethod("getURL",
481 new Class[] { String.class } );
482 } catch(NoSuchMethodException e) {
483 // ignore
484 } catch(SecurityException e) {
485 // ignore
486 }
487 if (getUrlInt != null && getUrlString != null) {
488 assertEquals(null, getUrlInt.invoke(rs,
489 new Object[] { Integer.valueOf(1) } ) );
490 assertTrue(rs.wasNull());
491 assertEquals(null, getUrlString.invoke(rs,
492 new Object[] { "column" } ) );
493 assertTrue(rs.wasNull());
494 // Set what gets returned to something other than the default
495 URL u = new URL("http://www.apache.org");
496 rs2.setNullURL(u);
497 assertEquals(u, getUrlInt.invoke(rs,
498 new Object[] { Integer.valueOf(1) } ) );
499 assertEquals(u, getUrlString.invoke(rs,
500 new Object[] { "column" } ) );
501 }
502 }
503
504 /**
505 * Tests the setNullAsciiStream implementation.
506 */
507 public void testSetNullAsciiStream() throws SQLException {
508
509 assertNull(rs2.getNullAsciiStream());
510 // Set what gets returned to something other than the default
511 InputStream stream = new ByteArrayInputStream(new byte[0]);
512 rs2.setNullAsciiStream(stream);
513 assertNotNull(rs.getAsciiStream(1));
514 assertEquals(stream, rs.getAsciiStream(1));
515 assertNotNull(rs.getAsciiStream("column"));
516 assertEquals(stream, rs.getAsciiStream("column"));
517
518 }
519
520 /**
521 * Tests the setNullBigDecimal implementation.
522 */
523 public void testSetNullBigDecimal() throws SQLException {
524
525 assertNull(rs2.getNullBigDecimal());
526 // Set what gets returned to something other than the default
527 BigDecimal bd = new BigDecimal(5.0);
528 rs2.setNullBigDecimal(bd);
529 assertNotNull(rs.getBigDecimal(1));
530 assertEquals(bd, rs.getBigDecimal(1));
531 assertNotNull(rs.getBigDecimal("column"));
532 assertEquals(bd, rs.getBigDecimal("column"));
533
534 }
535
536 /**
537 * Tests the setNullBinaryStream implementation.
538 */
539 public void testSetNullBinaryStream() throws SQLException {
540
541 assertNull(rs2.getNullBinaryStream());
542 // Set what gets returned to something other than the default
543 InputStream stream = new ByteArrayInputStream(new byte[0]);
544 rs2.setNullBinaryStream(stream);
545 assertNotNull(rs.getBinaryStream(1));
546 assertEquals(stream, rs.getBinaryStream(1));
547 assertNotNull(rs.getBinaryStream("column"));
548 assertEquals(stream, rs.getBinaryStream("column"));
549
550 }
551
552 /**
553 * Tests the setNullBlob implementation.
554 */
555 public void testSetNullBlob() throws SQLException {
556
557 assertNull(rs2.getNullBlob());
558 // Set what gets returned to something other than the default
559 Blob blob = new SqlNullCheckedResultSetMockBlob();
560 rs2.setNullBlob(blob);
561 assertNotNull(rs.getBlob(1));
562 assertEquals(blob, rs.getBlob(1));
563 assertNotNull(rs.getBlob("column"));
564 assertEquals(blob, rs.getBlob("column"));
565
566 }
567
568 /**
569 * Tests the setNullBoolean implementation.
570 */
571 public void testSetNullBoolean() throws SQLException {
572
573 assertEquals(false, rs2.getNullBoolean());
574 // Set what gets returned to something other than the default
575 rs2.setNullBoolean(true);
576 assertEquals(true, rs.getBoolean(1));
577 assertEquals(true, rs.getBoolean("column"));
578
579 }
580
581 /**
582 * Tests the setNullByte implementation.
583 */
584 public void testSetNullByte() throws SQLException {
585
586 assertEquals((byte) 0, rs2.getNullByte());
587 // Set what gets returned to something other than the default
588 byte b = (byte) 10;
589 rs2.setNullByte(b);
590 assertEquals(b, rs.getByte(1));
591 assertEquals(b, rs.getByte("column"));
592
593 }
594
595 /**
596 * Tests the setNullByte implementation.
597 */
598 public void testSetNullBytes() throws SQLException {
599
600 assertNull(rs2.getNullBytes());
601 // Set what gets returned to something other than the default
602 byte[] b = new byte[5];
603 for (int i = 0; i < 5; i++) {
604 b[0] = (byte) i;
605 }
606 rs2.setNullBytes(b);
607 assertNotNull(rs.getBytes(1));
608 assertArrayEquals(b, rs.getBytes(1));
609 assertNotNull(rs.getBytes("column"));
610 assertArrayEquals(b, rs.getBytes("column"));
611
612 }
613
614 /**
615 * Tests the setNullCharacterStream implementation.
616 */
617 public void testSetNullCharacterStream() throws SQLException {
618
619 assertNull(rs2.getNullCharacterStream());
620 // Set what gets returned to something other than the default
621 Reader reader = new CharArrayReader("this is a string".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().getTime());
654 rs2.setNullDate(date);
655 assertNotNull(rs.getDate(1));
656 assertEquals(date, rs.getDate(1));
657 assertNotNull(rs.getDate("column"));
658 assertEquals(date, rs.getDate("column"));
659 assertNotNull(rs.getDate(1, Calendar.getInstance()));
660 assertEquals(date, rs.getDate(1, Calendar.getInstance()));
661 assertNotNull(rs.getDate("column", Calendar.getInstance()));
662 assertEquals(date, rs.getDate("column", Calendar.getInstance()));
663
664 }
665
666 /**
667 * Tests the setNullDouble implementation.
668 */
669 public void testSetNullDouble() throws SQLException {
670 assertEquals(0.0, rs2.getNullDouble(), 0.0);
671 // Set what gets returned to something other than the default
672 double d = 10.0;
673 rs2.setNullDouble(d);
674 assertEquals(d, rs.getDouble(1), 0.0);
675 assertEquals(d, rs.getDouble("column"), 0.0);
676 }
677
678 /**
679 * Tests the setNullFloat implementation.
680 */
681 public void testSetNullFloat() throws SQLException {
682 assertEquals((float) 0.0, rs2.getNullFloat(), 0.0);
683 // Set what gets returned to something other than the default
684 float f = (float) 10.0;
685 rs2.setNullFloat(f);
686 assertEquals(f, rs.getFloat(1), 0.0);
687 assertEquals(f, rs.getFloat("column"), 0.0);
688 }
689
690 /**
691 * Tests the setNullInt implementation.
692 */
693 public void testSetNullInt() throws SQLException {
694 assertEquals(0, rs2.getNullInt());
695 assertEquals(0, rs.getInt(1));
696 assertTrue(rs.wasNull());
697 assertEquals(0, rs.getInt("column"));
698 assertTrue(rs.wasNull());
699 // Set what gets returned to something other than the default
700 int i = 10;
701 rs2.setNullInt(i);
702 assertEquals(i, rs.getInt(1));
703 assertEquals(i, rs.getInt("column"));
704 }
705
706 /**
707 * Tests the setNullLong implementation.
708 */
709 public void testSetNullLong() throws SQLException {
710 assertEquals(0, rs2.getNullLong());
711 // Set what gets returned to something other than the default
712 long l = 10;
713 rs2.setNullLong(l);
714 assertEquals(l, rs.getLong(1));
715 assertEquals(l, rs.getLong("column"));
716 }
717
718 /**
719 * Tests the setNullObject implementation.
720 */
721 public void testSetNullObject() throws SQLException {
722 assertNull(rs2.getNullObject());
723 // Set what gets returned to something other than the default
724 Object o = new Object();
725 rs2.setNullObject(o);
726 assertNotNull(rs.getObject(1));
727 assertEquals(o, rs.getObject(1));
728 assertNotNull(rs.getObject("column"));
729 assertEquals(o, rs.getObject("column"));
730 assertNotNull(rs.getObject(1, (Map<String, Class<?>>) null));
731 assertEquals(o, rs.getObject(1, (Map<String, Class<?>>) null));
732 assertNotNull(rs.getObject("column", (Map<String, Class<?>>) null));
733 assertEquals(o, rs.getObject("column", (Map<String, Class<?>>) null));
734 }
735
736 /**
737 * Tests the setNullShort implementation.
738 */
739 public void testSetNullShort() throws SQLException {
740
741 assertEquals((short) 0, rs2.getNullShort());
742 // Set what gets returned to something other than the default
743 short s = (short) 10;
744 rs2.setNullShort(s);
745 assertEquals(s, rs.getShort(1));
746 assertEquals(s, rs.getShort("column"));
747
748 }
749
750 /**
751 * Tests the setNullString implementation.
752 */
753 public void testSetNullString() throws SQLException {
754 assertEquals(null, rs2.getNullString());
755 // Set what gets returned to something other than the default
756 String s = "hello, world";
757 rs2.setNullString(s);
758 assertEquals(s, rs.getString(1));
759 assertEquals(s, rs.getString("column"));
760 }
761
762 /**
763 * Tests the setNullRef implementation.
764 */
765 public void testSetNullRef() throws SQLException {
766 assertNull(rs2.getNullRef());
767 // Set what gets returned to something other than the default
768 Ref ref = new SqlNullCheckedResultSetMockRef();
769 rs2.setNullRef(ref);
770 assertNotNull(rs.getRef(1));
771 assertEquals(ref, rs.getRef(1));
772 assertNotNull(rs.getRef("column"));
773 assertEquals(ref, rs.getRef("column"));
774 }
775
776 /**
777 * Tests the setNullTime implementation.
778 */
779 public void testSetNullTime() throws SQLException {
780 assertEquals(null, rs2.getNullTime());
781 // Set what gets returned to something other than the default
782 Time time = new Time(new java.util.Date().getTime());
783 rs2.setNullTime(time);
784 assertNotNull(rs.getTime(1));
785 assertEquals(time, rs.getTime(1));
786 assertNotNull(rs.getTime("column"));
787 assertEquals(time, rs.getTime("column"));
788 assertNotNull(rs.getTime(1, Calendar.getInstance()));
789 assertEquals(time, rs.getTime(1, Calendar.getInstance()));
790 assertNotNull(rs.getTime("column", Calendar.getInstance()));
791 assertEquals(time, rs.getTime("column", Calendar.getInstance()));
792 }
793
794 /**
795 * Tests the setNullTimestamp implementation.
796 */
797 public void testSetNullTimestamp() throws SQLException {
798 assertEquals(null, rs2.getNullTimestamp());
799 // Set what gets returned to something other than the default
800 Timestamp ts = new Timestamp(new java.util.Date().getTime());
801 rs2.setNullTimestamp(ts);
802 assertNotNull(rs.getTimestamp(1));
803 assertEquals(ts, rs.getTimestamp(1));
804 assertNotNull(rs.getTimestamp("column"));
805 assertEquals(ts, rs.getTimestamp("column"));
806 assertNotNull(rs.getTimestamp(1, Calendar.getInstance()));
807 assertEquals(ts, rs.getTimestamp(1, Calendar.getInstance()));
808 assertNotNull(rs.getTimestamp("column", Calendar.getInstance()));
809 assertEquals(ts, rs.getTimestamp("column", Calendar.getInstance()));
810 }
811
812 }
813
814 class SqlNullUncheckedMockResultSet implements InvocationHandler {
815
816 /**
817 * Always return false for booleans, 0 for numerics, and null for Objects.
818 * @see java.lang.reflect.InvocationHandler#invoke(java.lang.Object, java.lang.reflect.Method, java.lang.Object[])
819 */
820 @Override
821 public Object invoke(Object proxy, Method method, Object[] args)
822 throws Throwable {
823
824 Class<?> returnType = method.getReturnType();
825
826 if (method.getName().equals("wasNull")) {
827 return Boolean.TRUE;
828
829 } else if (returnType.equals(Boolean.TYPE)) {
830 return Boolean.FALSE;
831
832 } else if (returnType.equals(Integer.TYPE)) {
833 return Integer.valueOf(0);
834
835 } else if (returnType.equals(Short.TYPE)) {
836 return Short.valueOf((short) 0);
837
838 } else if (returnType.equals(Double.TYPE)) {
839 return new Double(0);
840
841 } else if (returnType.equals(Long.TYPE)) {
842 return Long.valueOf(0);
843
844 } else if (returnType.equals(Byte.TYPE)) {
845 return Byte.valueOf((byte) 0);
846
847 } else if (returnType.equals(Float.TYPE)) {
848 return new Float(0);
849
850 } else {
851 return null;
852 }
853 }
854 }
855
856 class SqlNullCheckedResultSetMockBlob implements Blob {
857
858 @Override
859 public InputStream getBinaryStream() throws SQLException {
860 return new ByteArrayInputStream(new byte[0]);
861 }
862
863 @Override
864 public byte[] getBytes(long param, int param1) throws SQLException {
865 return new byte[0];
866 }
867
868 @Override
869 public long length() throws SQLException {
870 return 0;
871 }
872
873 @Override
874 public long position(byte[] values, long param) throws SQLException {
875 return 0;
876 }
877
878 @Override
879 public long position(Blob blob, long param) throws SQLException {
880 return 0;
881 }
882
883 @Override
884 public void truncate(long len) throws SQLException {
885
886 }
887
888 @Override
889 public int setBytes(long pos, byte[] bytes) throws SQLException {
890 return 0;
891 }
892
893 @Override
894 public int setBytes(long pos, byte[] bytes, int offset, int len)
895 throws SQLException {
896 return 0;
897 }
898
899 @Override
900 public OutputStream setBinaryStream(long pos) throws SQLException {
901 return null;
902 }
903
904 /**
905 * @throws SQLException
906 */
907 @Override
908 public void free() throws SQLException {
909
910 }
911
912 /**
913 * @throws SQLException
914 */
915 @Override
916 public InputStream getBinaryStream(long pos, long length) throws SQLException {
917 return null;
918 }
919
920 }
921
922 class SqlNullCheckedResultSetMockClob implements Clob {
923
924 @Override
925 public InputStream getAsciiStream() throws SQLException {
926 return null;
927 }
928
929 @Override
930 public Reader getCharacterStream() throws SQLException {
931 return null;
932 }
933
934 @Override
935 public String getSubString(long param, int param1) throws SQLException {
936 return "";
937 }
938
939 @Override
940 public long length() throws SQLException {
941 return 0;
942 }
943
944 @Override
945 public long position(Clob clob, long param) throws SQLException {
946 return 0;
947 }
948
949 @Override
950 public long position(String str, long param) throws SQLException {
951 return 0;
952 }
953
954 @Override
955 public void truncate(long len) throws SQLException {
956
957 }
958
959 @Override
960 public OutputStream setAsciiStream(long pos) throws SQLException {
961 return null;
962 }
963
964 @Override
965 public Writer setCharacterStream(long pos) throws SQLException {
966 return null;
967 }
968
969 @Override
970 public int setString(long pos, String str) throws SQLException {
971 return 0;
972 }
973
974 @Override
975 public int setString(long pos, String str, int offset, int len)
976 throws SQLException {
977 return 0;
978 }
979
980 /**
981 * @throws SQLException
982 */
983 @Override
984 public void free() throws SQLException {
985
986 }
987
988 /**
989 * @throws SQLException
990 */
991 @Override
992 public Reader getCharacterStream(long pos, long length) throws SQLException {
993 return null;
994 }
995
996 }
997
998 class SqlNullCheckedResultSetMockRef implements Ref {
999
1000 @Override
1001 public String getBaseTypeName() throws SQLException {
1002 return "";
1003 }
1004
1005 @Override
1006 public Object getObject() throws SQLException {
1007 return null;
1008 }
1009
1010 @Override
1011 public void setObject(Object value) throws SQLException {
1012
1013 }
1014
1015 @Override
1016 public Object getObject(Map<String,Class<?>> map) throws SQLException {
1017 return null;
1018 }
1019
1020 }