1 21 22 package org.apache.derbyTesting.functionTests.tests.jdbc4; 23 24 import junit.framework.*; 25 26 import org.apache.derbyTesting.junit.BaseJDBCTestCase; 27 import org.apache.derbyTesting.junit.BaseJDBCTestSetup; 28 import org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream; 29 30 import java.io.*; 31 import java.sql.*; 32 import javax.sql.*; 33 34 import org.apache.derby.iapi.services.io.DerbyIOException; 35 import org.apache.derby.impl.jdbc.EmbedSQLException; 36 37 52 public class PreparedStatementTest extends BaseJDBCTestCase { 53 54 private static final String BLOBTBL = "BlobTestTable"; 55 private static final String CLOBTBL = "ClobTestTable"; 56 private static final String LONGVARCHAR = "LongVarcharTestTable"; 57 58 59 private static int globalKey = 1; 60 61 62 private static final byte[] BYTES = { 63 0x65, 0x66, 0x67, 0x68, 0x69, 64 0x69, 0x68, 0x67, 0x66, 0x65 65 }; 66 67 73 private int key; 74 75 76 private PreparedStatement ps = null; 77 78 private PreparedStatement psFetchBlob = null; 79 80 private PreparedStatement psInsertBlob = null; 81 82 private PreparedStatement psFetchClob = null; 83 84 private PreparedStatement psInsertClob = null; 85 86 private PreparedStatement psInsertLongVarchar = null; 87 private Statement s = null; 89 90 91 92 97 public PreparedStatementTest(String name) { 98 super(name); 99 } 100 101 108 public void setUp() 109 throws SQLException { 110 key = requestKey(); 111 s = createStatement(); 113 ps = prepareStatement("select count(*) from sys.systables"); 118 119 psFetchBlob = prepareStatement("SELECT dBlob FROM " + 121 BLOBTBL + " WHERE sno = ?"); 122 psInsertBlob = prepareStatement("INSERT INTO " + BLOBTBL + 123 " VALUES (?, ?)"); 124 psFetchClob = prepareStatement("SELECT dClob FROM " + 125 CLOBTBL + " WHERE sno = ?"); 126 psInsertClob = prepareStatement("INSERT INTO " + CLOBTBL + 127 " VALUES (?, ?)"); 128 psInsertLongVarchar = prepareStatement("INSERT INTO " + LONGVARCHAR + 129 " VALUES (?, ?)"); 130 } 131 132 139 public void tearDown() 140 throws Exception { 141 142 psFetchBlob.close(); 143 psFetchClob.close(); 144 psInsertBlob.close(); 145 psInsertClob.close(); 146 psInsertLongVarchar.close(); 147 148 super.tearDown(); 149 } 150 151 public static Test suite() { 152 TestSuite suite = new TestSuite(); 153 suite.addTestSuite(PreparedStatementTest.class); 154 suite.addTest(SetObjectUnsupportedTest.suite(false)); 155 return new BaseJDBCTestSetup(suite) { 156 public void setUp() 157 throws java.lang.Exception { 158 try { 159 create(); 160 } catch (SQLException sqle) { 161 if (sqle.getSQLState().equals("X0Y32")) { 162 drop(); 163 create(); 164 } else { 165 throw sqle; 166 } 167 } 168 } 169 170 public void tearDown() 171 throws java.lang.Exception { 172 drop(); 173 super.tearDown(); 174 } 175 176 private void create() 177 throws SQLException { 178 Statement stmt = getConnection().createStatement(); 179 stmt.execute("create table " + BLOBTBL + 180 " (sno int, dBlob BLOB(1M))"); 181 stmt.execute("create table " + CLOBTBL + 182 " (sno int, dClob CLOB(1M))"); 183 stmt.execute("create table " + LONGVARCHAR + 184 " (sno int, dLongVarchar LONG VARCHAR)"); 185 stmt.close(); 186 } 187 188 private void drop() 189 throws SQLException { 190 Statement stmt = getConnection().createStatement(); 191 stmt.execute("drop table " + BLOBTBL); 192 stmt.execute("drop table " + CLOBTBL); 193 stmt.execute("drop table " + LONGVARCHAR); 194 stmt.close(); 195 } 196 }; 197 } 198 199 203 209 public void testSetRowId() throws SQLException{ 210 try { 211 RowId rowid = null; 212 ps.setRowId(0,rowid); 213 fail("setRowId should not be implemented"); 214 } 215 catch(SQLFeatureNotSupportedException sqlfne) { 216 } 218 } 219 220 226 public void testSetNString() throws SQLException{ 227 try { 228 String str = null; 229 ps.setNString(0,str); 230 fail("setNString should not be implemented"); 231 } 232 catch(SQLFeatureNotSupportedException sqlfne) { 233 } 235 } 236 237 243 public void testSetNCharacterStream() throws SQLException{ 244 try { 245 Reader r = null; 246 ps.setNCharacterStream(0,r,0); 247 fail("setNCharacterStream should not be implemented"); 248 } 249 catch(SQLFeatureNotSupportedException sqlfne) { 250 } 252 } 253 254 public void testSetNCharacterStreamLengthlessNotImplemented() 255 throws SQLException { 256 try { 257 ps.setNCharacterStream(1, new StringReader("A string")); 258 fail("setNCharacterStream(int,Reader) should not be implemented"); 259 } catch (SQLFeatureNotSupportedException sfnse) { 260 } 262 } 263 264 public void testSetNClobLengthlessNotImplemented() 265 throws SQLException { 266 try { 267 ps.setNClob(1, new StringReader("A string")); 268 fail("setNClob(int,Reader) should not be implemented"); 269 } catch (SQLFeatureNotSupportedException sfnse) { 270 } 272 } 273 274 280 public void testSetNClob1() throws SQLException{ 281 try { 282 NClob nclob = null; 283 ps.setNClob(0,nclob); 284 fail("setNClob should not be implemented"); 285 } 286 catch(SQLFeatureNotSupportedException sqlfne) { 287 } 289 } 290 291 297 public void testSetNClob2() throws SQLException{ 298 try { 299 Reader reader = null; 300 ps.setNClob(0,reader,0); 301 fail("setNClob should not be implemented"); 302 } 303 catch(SQLFeatureNotSupportedException sqlfne) { 304 } 306 } 307 308 314 public void testSetSQLXML() throws SQLException{ 315 try { 316 SQLXML sqlxml = null; 317 ps.setSQLXML(0,sqlxml); 318 fail("setNClob should not be implemented"); 319 } 320 catch(SQLFeatureNotSupportedException sqlfne) { 321 } 323 } 324 325 329 public void testIsWrapperForStatement() throws SQLException { 330 assertTrue(ps.isWrapperFor(Statement.class)); 331 } 332 333 public void testIsWrapperForPreparedStatement() throws SQLException { 334 assertTrue(ps.isWrapperFor(PreparedStatement.class)); 335 } 336 337 public void testIsNotWrapperForCallableStatement() throws SQLException { 338 assertFalse(ps.isWrapperFor(CallableStatement.class)); 339 } 340 341 public void testIsNotWrapperForResultSet() throws SQLException { 342 assertFalse(ps.isWrapperFor(ResultSet.class)); 343 } 344 345 public void testUnwrapStatement() throws SQLException { 346 Statement stmt = ps.unwrap(Statement.class); 347 assertSame("Unwrap returned wrong object.", ps, stmt); 348 } 349 350 public void testUnwrapPreparedStatement() throws SQLException { 351 PreparedStatement ps2 = ps.unwrap(PreparedStatement.class); 352 assertSame("Unwrap returned wrong object.", ps, ps2); 353 } 354 355 public void testUnwrapCallableStatement() { 356 try { 357 CallableStatement cs = ps.unwrap(CallableStatement.class); 358 fail("Unwrap didn't fail."); 359 } catch (SQLException e) { 360 assertSQLState("XJ128", e); 361 } 362 } 363 364 public void testUnwrapResultSet() { 365 try { 366 ResultSet rs = ps.unwrap(ResultSet.class); 367 fail("Unwrap didn't fail."); 368 } catch (SQLException e) { 369 assertSQLState("XJ128", e); 370 } 371 } 372 373 376 393 394 401 public void testSetClob() 402 throws IOException, SQLException { 403 405 String str = "Test data for the Clob object"; 406 StringReader is = new StringReader("Test data for the Clob object"); 407 is.reset(); 408 409 psInsertClob.setInt(1, key); 411 psInsertClob.setClob(2, is, str.length()); 412 psInsertClob.executeUpdate(); 413 414 psFetchClob.setInt(1, key); 416 ResultSet rs = psFetchClob.executeQuery(); 417 rs.next(); 418 Clob clobToBeInserted = rs.getClob(1); 419 rs.close(); 420 421 int secondKey = requestKey(); 423 psInsertClob.setInt(1, secondKey); 424 psInsertClob.setClob(2, clobToBeInserted); 425 psInsertClob.execute(); 426 427 psInsertClob.close(); 428 429 psFetchClob.setInt(1, secondKey); 431 rs = psFetchClob.executeQuery(); 432 rs.next(); 433 Clob clobRetrieved = rs.getClob(1); 434 435 assertEquals(clobToBeInserted,clobRetrieved); 436 } 437 438 447 public void testSetClobLengthless() 448 throws IOException, SQLException { 449 String testString = "Test string for setCharacterStream\u1A00"; 451 Reader reader = new StringReader(testString); 452 psInsertClob.setInt(1, key); 453 psInsertClob.setCharacterStream(2, reader); 454 psInsertClob.execute(); 455 reader.close(); 456 psFetchClob.setInt(1, key); 459 ResultSet rs = psFetchClob.executeQuery(); 460 assertTrue("No results retrieved", rs.next()); 461 int secondKey = requestKey(); 462 Clob insertClob = rs.getClob(1); 463 psInsertClob.setInt(1, secondKey); 464 psInsertClob.setClob(2, insertClob); 465 psInsertClob.execute(); 466 467 psFetchClob.setInt(1, secondKey); 469 rs = psFetchClob.executeQuery(); 470 assertTrue("No results retrieved", rs.next()); 471 Clob clobRetrieved = rs.getClob(1); 472 473 assertEquals(insertClob, clobRetrieved); 475 } 476 477 484 public void testSetBlob() 485 throws IOException, SQLException { 486 488 InputStream is = new java.io.ByteArrayInputStream (BYTES); 489 is.reset(); 490 491 psInsertBlob.setInt(1, key); 493 psInsertBlob.setBlob(2, is, BYTES.length); 494 psInsertBlob.executeUpdate(); 495 496 psFetchBlob.setInt(1, key); 498 ResultSet rs = psFetchBlob.executeQuery(); 499 rs.next(); 500 Blob blobToBeInserted = rs.getBlob(1); 501 rs.close(); 502 503 int secondKey = requestKey(); 505 psInsertBlob.setInt(1, secondKey); 506 psInsertBlob.setBlob(2, blobToBeInserted); 507 psInsertBlob.execute(); 508 509 psInsertBlob.close(); 510 511 psFetchBlob.setInt(1, secondKey); 513 rs = psFetchBlob.executeQuery(); 514 rs.next(); 515 Blob blobRetrieved = rs.getBlob(1); 516 517 assertEquals(blobToBeInserted, blobRetrieved); 518 } 519 520 529 public void testSetBlobLengthless() 530 throws IOException, SQLException { 531 InputStream is = new ByteArrayInputStream(BYTES); 533 psInsertBlob.setInt(1, key); 534 psInsertBlob.setBinaryStream(2, is); 535 psInsertBlob.execute(); 536 is.close(); 537 psFetchBlob.setInt(1, key); 540 ResultSet rs = psFetchBlob.executeQuery(); 541 assertTrue("No results retrieved", rs.next()); 542 Blob insertBlob = rs.getBlob(1); 543 int secondKey = requestKey(); 544 psInsertBlob.setInt(1, secondKey); 545 psInsertBlob.setBlob(2, insertBlob); 546 psInsertBlob.execute(); 547 548 psFetchBlob.setInt(1, secondKey); 550 rs = psFetchBlob.executeQuery(); 551 assertTrue("No results retrieved", rs.next()); 552 Blob blobRetrieved = rs.getBlob(1); 553 554 assertEquals(insertBlob, blobRetrieved); 556 } 557 558 561 567 568 public void testSetPoolable() throws SQLException { 569 try { 570 ps.setPoolable(false); 572 if (ps.isPoolable()) 573 fail("Expected a non-poolable statement"); 574 ps.setPoolable(true); 576 if (!ps.isPoolable()) 577 fail("Expected a poolable statement"); 578 } catch(SQLException sqle) { 579 if (sqle.getSQLState().equals("XJ012") || 583 sqle.getSQLState().equals("XCL31")) { 584 } else { 586 fail("Unexpected SQLException " + sqle); 587 } 588 } catch(Exception e) { 589 fail("Unexpected exception thrown in method " + e); 590 } 591 } 592 593 600 601 public void testIsPoolable() throws SQLException { 602 try { 603 if (!ps.isPoolable()) 605 fail("Expected a poolable statement"); 606 } catch(SQLException sqle) { 607 if (sqle.getSQLState().equals("XJ012") || 611 sqle.getSQLState().equals("XCL31")) { 612 } else { 614 fail("Unexpected SQLException " + sqle); 615 } 616 } catch(Exception e) { 617 fail("Unexpected exception thrown in method " + e); 618 } 619 } 620 621 622 629 public void testSetCharacterStream() throws Exception { 630 String str = "Test data for the Clob object"; 631 StringReader is = new StringReader("Test data for the Clob object"); 632 633 is.reset(); 634 635 psInsertClob.setInt(1, key); 637 psInsertClob.setCharacterStream(2, is, str.length()); 638 psInsertClob.executeUpdate(); 639 640 psFetchClob.setInt(1, key); 642 ResultSet rs = psFetchClob.executeQuery(); 643 rs.next(); 644 Clob clobRetrieved = rs.getClob(1); 645 rs.close(); 646 647 String str_out = clobRetrieved.getSubString(1L,(int)clobRetrieved.length()); 648 649 assertEquals("Error in inserting data into the Clob object",str,str_out); 650 psInsertClob.close(); 651 } 652 653 public void testSetCharacterStreamLengthless() 654 throws IOException, SQLException { 655 String testString = "Test string for setCharacterStream\u1A00"; 657 Reader reader = new StringReader(testString); 658 psInsertClob.setInt(1, key); 659 psInsertClob.setCharacterStream(2, reader); 660 psInsertClob.execute(); 661 reader.close(); 662 663 psFetchClob.setInt(1, key); 665 ResultSet rs = psFetchClob.executeQuery(); 666 assertTrue("No results retrieved", rs.next()); 667 Clob clobRetrieved = rs.getClob(1); 668 669 assertEquals("Mismatch test data in/out", testString, 671 clobRetrieved.getSubString(1, testString.length())); 672 } 673 674 681 682 public void testSetAsciiStream() throws Exception { 683 685 byte [] bytes1 = new byte[10]; 686 687 InputStream is = new java.io.ByteArrayInputStream (BYTES); 688 689 is.reset(); 690 691 psInsertClob.setInt(1, key); 693 psInsertClob.setAsciiStream(2, is, BYTES.length); 694 psInsertClob.executeUpdate(); 695 696 psFetchClob.setInt(1, key); 698 ResultSet rs = psFetchClob.executeQuery(); 699 rs.next(); 700 Clob ClobRetrieved = rs.getClob(1); 701 rs.close(); 702 703 try { 704 InputStream is_ret = ClobRetrieved.getAsciiStream(); 705 is_ret.read(bytes1); 706 } catch(IOException ioe) { 707 fail("IOException while reading the Clob from the database"); 708 } 709 for(int i=0;i<BYTES.length;i++) { 710 assertEquals("Error in inserting data into the Clob",BYTES[i],bytes1[i]); 711 } 712 psInsertClob.close(); 713 } 714 715 public void testSetAsciiStreamLengthless() 716 throws IOException, SQLException { 717 InputStream is = new ByteArrayInputStream(BYTES); 719 psInsertClob.setInt(1, key); 720 psInsertClob.setAsciiStream(2, is); 721 psInsertClob.execute(); 722 is.close(); 723 724 psFetchClob.setInt(1, key); 726 ResultSet rs = psFetchClob.executeQuery(); 727 assertTrue("No results retrieved", rs.next()); 728 Clob clobRetrieved = rs.getClob(1); 729 730 byte[] dbBytes = new byte[10]; 732 InputStream isRetrieved = clobRetrieved.getAsciiStream(); 733 assertEquals("Unexpected number of bytes read", BYTES.length, 734 isRetrieved.read(dbBytes)); 735 assertEquals("Stream should be exhausted", -1, isRetrieved.read()); 736 for (int i=0; i < BYTES.length; i++) { 737 assertEquals("Byte mismatch in/out", BYTES[i], dbBytes[i]); 738 } 739 740 isRetrieved.close(); 742 psInsertClob.close(); 743 } 744 745 752 753 public void testSetBinaryStream() throws Exception { 754 756 byte [] bytes1 = new byte[10]; 757 758 InputStream is = new java.io.ByteArrayInputStream (BYTES); 759 760 is.reset(); 761 762 psInsertBlob.setInt(1, key); 764 psInsertBlob.setBinaryStream(2, is, BYTES.length); 765 psInsertBlob.executeUpdate(); 766 767 psFetchBlob.setInt(1, key); 769 ResultSet rs = psFetchBlob.executeQuery(); 770 rs.next(); 771 Blob blobRetrieved = rs.getBlob(1); 772 rs.close(); 773 774 try { 775 InputStream is_ret = blobRetrieved.getBinaryStream(); 776 is_ret.read(bytes1); 777 } catch(IOException ioe) { 778 fail("IOException while reading the Clob from the database"); 779 } 780 781 for(int i=0;i<BYTES.length;i++) { 782 assertEquals("Error in inserting data into the Blob",BYTES[i],bytes1[i]); 783 } 784 psInsertBlob.close(); 785 } 786 787 public void testSetBinaryStreamLengthless() 788 throws IOException, SQLException { 789 InputStream is = new ByteArrayInputStream(BYTES); 791 psInsertBlob.setInt(1, key); 792 psInsertBlob.setBinaryStream(2, is); 793 psInsertBlob.execute(); 794 is.close(); 795 796 psFetchBlob.setInt(1, key); 798 ResultSet rs = psFetchBlob.executeQuery(); 799 assertTrue("No results retrieved", rs.next()); 800 Blob blobRetrieved = rs.getBlob(1); 801 802 byte[] dbBytes = new byte[10]; 804 InputStream isRetrieved = blobRetrieved.getBinaryStream(); 805 assertEquals("Unexpected number of bytes read", BYTES.length, 806 isRetrieved.read(dbBytes)); 807 assertEquals("Stream should be exhausted", -1, isRetrieved.read()); 808 for (int i=0; i < BYTES.length; i++) { 809 assertEquals("Byte mismatch in/out", BYTES[i], dbBytes[i]); 810 } 811 812 isRetrieved.close(); 814 psInsertBlob.close(); 815 } 816 817 public void testSetBinaryStreamLengthLess1KOnBlob() 818 throws IOException, SQLException { 819 int length = 1*1024; 820 setBinaryStreamOnBlob(key, length, -1, 0, true); 821 psFetchBlob.setInt(1, key); 822 ResultSet rs = psFetchBlob.executeQuery(); 823 assertTrue("Empty resultset", rs.next()); 824 assertEquals(new LoopingAlphabetStream(length), 825 rs.getBinaryStream(1)); 826 assertFalse("Resultset should have been exhausted", rs.next()); 827 rs.close(); 828 } 829 830 public void testSetBinaryStreamLengthLess32KOnBlob() 831 throws IOException, SQLException { 832 int length = 32*1024; 833 setBinaryStreamOnBlob(key, length, -1, 0, true); 834 psFetchBlob.setInt(1, key); 835 ResultSet rs = psFetchBlob.executeQuery(); 836 assertTrue("Empty resultset", rs.next()); 837 assertEquals(new LoopingAlphabetStream(length), 838 rs.getBinaryStream(1)); 839 assertFalse("Resultset should have been exhausted", rs.next()); 840 rs.close(); 841 } 842 843 public void testSetBinaryStreamLengthLess65KOnBlob() 844 throws IOException, SQLException { 845 int length = 65*1024; 846 setBinaryStreamOnBlob(key, length, -1, 0, true); 847 psFetchBlob.setInt(1, key); 848 ResultSet rs = psFetchBlob.executeQuery(); 849 assertTrue("Empty resultset", rs.next()); 850 LoopingAlphabetStream s1 = new LoopingAlphabetStream(length); 851 assertEquals(new LoopingAlphabetStream(length), 852 rs.getBinaryStream(1)); 853 assertFalse("Resultset should have been exhausted", rs.next()); 854 rs.close(); 855 } 856 857 public void testSetBinaryStreamLengthLessOnBlobTooLong() { 858 int length = 1*1024*1024+512; 859 try { 860 setBinaryStreamOnBlob(key, length, -1, 0, true); 861 } catch (SQLException sqle) { 862 if (usingEmbedded()) { 863 assertSQLState("XSDA4", sqle); 864 } else { 865 assertSQLState("22001", sqle); 866 } 867 } 868 } 869 870 public void testExceptionPathOnePage_bs() 871 throws SQLException { 872 int length = 11; 873 try { 874 setBinaryStreamOnBlob(key, length -1, length, 0, false); 875 fail("Inserted a BLOB with fewer bytes than specified"); 876 } catch (SQLException sqle) { 877 if (usingEmbedded()) { 878 assertSQLState("XSDA4", sqle); 879 } else { 880 assertSQLState("XN017", sqle); 881 } 882 } 883 } 884 885 public void testExceptionPathMultiplePages_bs() 886 throws SQLException { 887 int length = 1*1024*1024; 888 try { 889 setBinaryStreamOnBlob(key, length -1, length, 0, false); 890 fail("Inserted a BLOB with fewer bytes than specified"); 891 } catch (SQLException sqle) { 892 if (usingEmbedded()) { 893 assertSQLState("XSDA4", sqle); 894 } else { 895 assertSQLState("XN017", sqle); 896 } 897 } 898 } 899 900 public void testBlobExceptionDoesNotRollbackOtherStatements() 901 throws IOException, SQLException { 902 getConnection().setAutoCommit(false); 903 int[] keys = {key, requestKey(), requestKey()}; 904 for (int i=0; i < keys.length; i++) { 905 psInsertBlob.setInt(1, keys[i]); 906 psInsertBlob.setNull(2, Types.BLOB); 907 assertEquals(1, psInsertBlob.executeUpdate()); 908 } 909 int failedKey = requestKey(); 911 int length = 1*1024*1024; 912 try { 913 setBinaryStreamOnBlob(failedKey, length -1, length, 0, false); 914 fail("Inserted a BLOB with less data than specified"); 915 } catch (SQLException sqle) { 916 if (usingEmbedded()) { 917 assertSQLState("XSDA4", sqle); 918 } else { 919 assertSQLState("XN017", sqle); 920 } 921 } 922 ResultSet rs; 925 for (int i=0; i < keys.length; i++) { 926 psFetchBlob.setInt(1, keys[i]); 927 rs = psFetchBlob.executeQuery(); 928 assertTrue(rs.next()); 929 assertFalse(rs.next()); 930 rs.close(); 931 } 932 psFetchBlob.setInt(1, failedKey); 933 rs = psFetchBlob.executeQuery(); 934 if (!usingEmbedded()) { 937 assertTrue(rs.next()); 938 InputStream is = rs.getBinaryStream(1); 939 int lastByte = -1; 940 int b = 99; while (b > -1) { 942 lastByte = b; 943 b = is.read(); 944 } 945 assertEquals("Last padded byte is not 0", 0, lastByte); 946 } 947 assertFalse(rs.next()); 948 rs.close(); 949 rollback(); 950 for (int i=0; i < keys.length; i++) { 952 psFetchBlob.setInt(1, keys[i]); 953 rs = psFetchBlob.executeQuery(); 954 assertFalse(rs.next()); 955 rs.close(); 956 } 957 psFetchBlob.setInt(1, failedKey); 959 rs = psFetchBlob.executeQuery(); 960 assertFalse(rs.next()); 961 962 } 963 964 public void testSetAsciiStreamLengthLess1KOnClob() 965 throws IOException, SQLException { 966 int length = 1*1024; 967 setAsciiStream(psInsertClob, key, length, -1, 0, true); 968 psFetchClob.setInt(1, key); 969 ResultSet rs = psFetchClob.executeQuery(); 970 assertTrue("Empty resultset", rs.next()); 971 assertEquals(new LoopingAlphabetStream(length), 972 rs.getAsciiStream(1)); 973 assertFalse("Resultset should have been exhausted", rs.next()); 974 rs.close(); 975 } 976 977 public void testSetAsciiStreamLengthLess32KOnClob() 978 throws IOException, SQLException { 979 int length = 32*1024; 980 setAsciiStream(psInsertClob, key, length, -1, 0, true); 981 psFetchClob.setInt(1, key); 982 ResultSet rs = psFetchClob.executeQuery(); 983 assertTrue("Empty resultset", rs.next()); 984 assertEquals(new LoopingAlphabetStream(length), 985 rs.getAsciiStream(1)); 986 assertFalse("Resultset should have been exhausted", rs.next()); 987 rs.close(); 988 } 989 990 public void testSetAsciiStreamLengthLess65KOnClob() 991 throws IOException, SQLException { 992 int length = 65*1024; 993 setAsciiStream(psInsertClob, key, length, -1, 0, true); 994 psFetchClob.setInt(1, key); 995 ResultSet rs = psFetchClob.executeQuery(); 996 assertTrue("Empty resultset", rs.next()); 997 assertEquals(new LoopingAlphabetStream(length), 998 rs.getAsciiStream(1)); 999 assertFalse("Resultset should have been exhausted", rs.next()); 1000 rs.close(); 1001 } 1002 1003 public void testSetAsciiStreamLengthLessOnClobTooLong() { 1004 int length = 1*1024*1024+512; 1005 try { 1006 setAsciiStream(psInsertClob, key, length, -1, 0, true); 1007 } catch (SQLException sqle) { 1008 if (usingEmbedded()) { 1009 assertSQLState("XSDA4", sqle); 1010 } else { 1011 assertSQLState("22001", sqle); 1012 } 1013 } 1014 } 1015 1016 public void testSetAsciiStreamLengthLessOnClobTooLongTruncate() 1017 throws SQLException { 1018 int trailingBlanks = 512; 1019 int length = 1*1024*1024 + trailingBlanks; 1020 setAsciiStream(psInsertClob, key, length, -1, trailingBlanks, true); 1021 } 1022 1023 public void testSetAsciiStreamLengthlessOnLongVarCharTooLong() { 1024 int length = 32700+512; 1025 try { 1026 setAsciiStream(psInsertLongVarchar, key, length, -1, 0, true); 1027 fail("Inserted a LONG VARCHAR that is too long"); 1028 } catch (SQLException sqle) { 1029 if (usingEmbedded()) { 1030 assertInternalDerbyIOExceptionState("XCL30", "22001", sqle); 1031 } else { 1032 assertSQLState("22001", sqle); 1033 } 1034 } 1035 } 1036 1037 public void testSetAsciiStreamLengthlessOnLongVarCharDontTruncate() { 1038 int trailingBlanks = 2000; 1039 int length = 32000 + trailingBlanks; 1040 try { 1041 setAsciiStream(psInsertLongVarchar, key, length, -1, 1042 trailingBlanks, true); 1043 fail("Truncation is not allowed for LONG VARCHAR"); 1044 } catch (SQLException sqle) { 1045 if (usingEmbedded()) { 1046 assertInternalDerbyIOExceptionState("XCL30", "22001", sqle); 1047 } else { 1048 assertSQLState("22001", sqle); 1049 } 1050 } 1051 } 1052 1053 1056 1057 1066 private void setBinaryStreamOnBlob(int id, 1067 int actualLength, 1068 int specifiedLength, 1069 int trailingBlanks, 1070 boolean lengthLess) 1071 throws SQLException { 1072 psInsertBlob.setInt(1, id); 1073 if (lengthLess) { 1074 psInsertBlob.setBinaryStream(2, new LoopingAlphabetStream( 1075 actualLength, 1076 trailingBlanks)); 1077 } else { 1078 psInsertBlob.setBinaryStream(2, 1079 new LoopingAlphabetStream( 1080 actualLength, 1081 trailingBlanks), 1082 specifiedLength); 1083 } 1084 assertEquals("Insert with setBinaryStream failed", 1085 1, psInsertBlob.executeUpdate()); 1086 } 1087 1088 1102 private void setAsciiStream(PreparedStatement ps, 1103 int id, 1104 int actualLength, 1105 int specifiedLength, 1106 int trailingBlanks, 1107 boolean lengthLess) 1108 throws SQLException { 1109 ps.setInt(1, id); 1110 if (lengthLess) { 1111 ps.setAsciiStream(2, 1112 new LoopingAlphabetStream( 1113 actualLength, 1114 trailingBlanks)); 1115 } else { 1116 ps.setAsciiStream(2, 1117 new LoopingAlphabetStream( 1118 actualLength, 1119 trailingBlanks), 1120 specifiedLength); 1121 } 1122 assertEquals("Insert with setAsciiStream failed", 1123 1, ps.executeUpdate()); 1124 } 1125 1126 1129 private static int requestKey() { 1130 return globalKey++; 1131 } 1132 1133 1137 private SQLException getLastSQLException(SQLException sqle) { 1138 SQLException last = sqle; 1139 SQLException next = sqle; 1140 while (next != null) { 1141 last = next; 1142 next = last.getNextException(); 1143 } 1144 return last; 1145 } 1146 1147 1154 private void assertInternalDerbyIOExceptionState( 1155 String preSQLState, 1156 String expectedInternal, 1157 SQLException sqle) { 1158 assertSQLState("Outer/public SQL state incorrect", 1159 preSQLState, sqle); 1160 Throwable cause = getLastSQLException(sqle).getCause(); 1164 assertTrue("Exception not an EmbedSQLException", 1165 cause instanceof EmbedSQLException); 1166 cause = ((EmbedSQLException)cause).getJavaException(); 1167 assertTrue("Exception not a DerbyIOException", 1168 cause instanceof DerbyIOException); 1169 DerbyIOException dioe = (DerbyIOException)cause; 1170 assertEquals("Incorrect internal SQL state", expectedInternal, 1171 dioe.getSQLState()); 1172 } 1173} 1174 | Popular Tags |