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