1 21 22 package org.apache.derbyTesting.functionTests.tests.jdbcapi; 23 24 25 import java.io.File ; 26 import java.io.FileInputStream ; 27 import java.io.FileNotFoundException ; 28 import java.io.FileReader ; 29 import java.io.InputStream ; 30 import java.io.OutputStream ; 31 import java.io.Reader ; 32 import java.io.Writer ; 33 import java.sql.Blob ; 34 import java.sql.Clob ; 35 import java.sql.Connection ; 36 import java.sql.PreparedStatement ; 37 import java.sql.ResultSet ; 38 import java.sql.SQLException ; 39 import java.sql.Statement ; 40 41 import org.apache.derby.tools.ij; 42 import org.apache.derbyTesting.functionTests.util.TestUtil; 43 44 public class lobStreams { 45 46 static String [] fileName = new String [2]; 47 static String sep; 48 static long fileLength; 49 50 static boolean debug = true; 51 private static final String START = "\nSTART: "; 52 53 private static final String unicodeTestString = "This is a test string containing a few " + 54 "non-ascii characters:\nÆØÅ and æøå are used in norwegian: 'Blåbærsyltetøy' means" + 55 "'blueberry jam', and tastes great on pancakes. =)"; 56 57 static 58 { 59 fileName[0] = "aclob.utf"; 62 fileName[1] = "littleclob.utf"; 63 } 64 65 public static void main(String [] args) 66 { 67 System.out.println("Test lob stream with multiple writes starting"); 68 69 boolean exists = (new File ("extin", fileName[0])).exists(); 71 String sep = System.getProperty("file.separator"); 72 if (!exists) 73 { 74 String userdir = System.getProperty("user.dir"); 76 fileName[0] = userdir + sep + ".." + sep + "extin" + sep + fileName[0]; 77 fileName[1] = userdir + sep + ".." + sep + "extin" + sep + fileName[1]; 78 } 79 else 80 { 81 fileName[0] = "extin" + sep + fileName[0]; 83 fileName[1] = "extin" + sep + fileName[1]; 84 } 85 86 87 try 88 { 89 ij.getPropertyArg(args); 92 Connection conn = ij.startJBMS(); 93 conn.setAutoCommit(false); 96 97 prepareTable(conn); 98 testBlobWrite3Param(conn); 99 resetBlobClob(conn); 100 testBlobWrite1Param(conn); 101 resetBlobClob(conn); 102 testClobAsciiWrite3Param(conn); 103 resetBlobClob(conn); 104 testClobAsciiWrite1Param(conn); 105 resetBlobClob(conn); 106 testClobCharacterWrite3ParamChar(conn); 107 resetBlobClob(conn); 108 testClobCharacterWrite3ParamString(conn); 109 resetBlobClob(conn); 110 testClobCharacterWrite1ParamString(conn); 111 resetBlobClob(conn); 112 testClobCharacterWrite1Char(conn); 113 114 conn.commit(); 116 cleanUp(conn); 117 conn.commit(); 118 conn.close(); 119 120 } 121 catch (Throwable e) 122 { 123 System.out.println("FAIL -- unexpected exception:" + e.toString()); 124 if (debug) e.printStackTrace(); 125 } 126 System.out.println("Test lob stream with multiple writes finished\n"); 127 } 128 129 private static void prepareTable(Connection conn) { 130 try { 131 Statement stmt1 = conn.createStatement(); 132 stmt1.execute("create table testBlobX1 (a integer, b blob(300K), c clob(300K))"); 133 stmt1.close(); 134 135 byte[] b2 = new byte[1]; 136 b2[0] = (byte)64; 137 String c2 = "c"; 138 PreparedStatement stmt2 = conn.prepareStatement( 139 "INSERT INTO testBlobX1(a, b, c) VALUES (?, ?, ?)"); 140 stmt2.setInt(1, 1); 141 stmt2.setBytes(2, b2); 142 stmt2.setString(3, c2); 143 stmt2.execute(); 144 stmt2.close(); 145 146 } catch (SQLException e) { 147 TestUtil.dumpSQLExceptions(e); 148 } catch (Throwable e) { 149 if (debug) e.printStackTrace(); 150 } 151 152 } 153 154 private static void resetBlobClob(Connection conn) { 155 try { 156 byte[] b2 = new byte[1]; 157 b2[0] = (byte)64; 158 String c2 = "a"; 159 PreparedStatement stmt = conn.prepareStatement( 160 "UPDATE testBlobX1 SET b = ?, c = ? WHERE a = 1"); 161 stmt.setBytes(1, b2); 162 stmt.setString(2, c2); 163 stmt.execute(); 164 stmt.close(); 165 166 } catch (SQLException e) { 167 TestUtil.dumpSQLExceptions(e); 168 } catch (Throwable e) { 169 if (debug) e.printStackTrace(); 170 } 171 172 } 173 174 177 private static void testBlobWrite3Param(Connection conn) 178 { 179 try { 180 System.out.println(START + "testBlobWrite3Param"); 181 182 PreparedStatement stmt3 = conn.prepareStatement( 183 "SELECT b FROM testBlobX1 WHERE a = 1"); 184 185 ResultSet rs3 = stmt3.executeQuery(); 186 187 rs3.next(); 188 189 Blob blob = rs3.getBlob(1); 190 191 File file = new File (fileName[0]); 192 fileLength = file.length(); 193 InputStream fileIn = new FileInputStream (file); 194 195 if (blob != null) { 196 int count = 0; 197 byte[] buffer = new byte[1024]; 198 OutputStream outstream = blob.setBinaryStream(1L); 199 while ((count = fileIn.read(buffer)) != -1) { 200 outstream.write(buffer, 0, count); 201 } 202 outstream.close(); 203 fileIn.close(); 204 205 PreparedStatement stmt4 = conn.prepareStatement( 206 "UPDATE testBlobX1 SET b = ? WHERE a = 1"); 207 stmt4.setBlob(1, blob); 208 stmt4.executeUpdate(); 209 stmt4.close(); 210 211 } else { 212 System.out.println("FAIL -- blob is NULL"); 213 } 214 215 rs3.close(); 216 rs3 = stmt3.executeQuery(); 217 218 if (rs3.next()) { 219 long new_length = rs3.getBlob(1).length(); 220 if (new_length != fileLength) { 221 System.out.println( 222 "FAIL -- wrong blob length; original: " + 223 fileLength + " blob length: " + new_length); 224 } else { 225 InputStream fStream = new FileInputStream (file); 227 InputStream lStream = rs3.getBlob(1).getBinaryStream(); 228 229 if (!compareLob2File(fStream, lStream)) 230 System.out.println("FAIL - Blob and file contents do not match"); 231 232 fStream.close(); 233 lStream.close(); 234 235 } 236 } else { 237 System.out.println("FAIL -- blob not found"); 238 } 239 rs3.close(); 240 stmt3.close(); 241 242 System.out.println("testBlobWrite3Param finished"); 243 } catch (SQLException e) { 244 TestUtil.dumpSQLExceptions(e); 245 } catch (Throwable e) { 246 if (debug) e.printStackTrace(); 247 } 248 } 249 250 253 private static void testBlobWrite1Param(Connection conn) 254 { 255 try { 256 System.out.println(START + "testBlobWrite1Param"); 257 258 PreparedStatement stmt3 = conn.prepareStatement( 259 "SELECT b FROM testBlobX1 WHERE a = 1"); 260 261 ResultSet rs3 = stmt3.executeQuery(); 262 263 rs3.next(); 264 265 Blob blob = rs3.getBlob(1); 266 267 File file = new File (fileName[1]); 268 fileLength = file.length(); 269 InputStream fileIn = new FileInputStream (file); 270 271 if (blob != null) { 272 int buffer; 273 OutputStream outstream = blob.setBinaryStream(1L); 274 while ((buffer = fileIn.read()) != -1) { 275 outstream.write(buffer); 276 } 277 outstream.close(); 278 fileIn.close(); 279 280 PreparedStatement stmt4 = conn.prepareStatement( 281 "UPDATE testBlobX1 SET b = ? WHERE a = 1"); 282 stmt4.setBlob(1, blob); 283 stmt4.executeUpdate(); 284 stmt4.close(); 285 286 } else { 287 System.out.println("FAIL -- blob is NULL"); 288 } 289 290 rs3.close(); 291 rs3 = stmt3.executeQuery(); 292 293 if (rs3.next()) { 294 long new_length = rs3.getBlob(1).length(); 295 if (new_length != fileLength) { 296 System.out.println( 297 "FAIL -- wrong blob length; original: " + 298 fileLength + " blob length: " + new_length); 299 } else { 300 InputStream fStream = new FileInputStream (file); 302 InputStream lStream = rs3.getBlob(1).getBinaryStream(); 303 304 if (!compareLob2File(fStream, lStream)) 305 System.out.println("FAIL - Blob and file contents do not match"); 306 307 fStream.close(); 308 lStream.close(); 309 310 } 311 } else { 312 System.out.println("FAIL -- blob not found"); 313 } 314 rs3.close(); 315 stmt3.close(); 316 317 System.out.println("testBlobWrite1Param finished"); 318 } catch (SQLException e) { 319 TestUtil.dumpSQLExceptions(e); 320 } catch (Throwable e) { 321 if (debug) e.printStackTrace(); 322 } 323 } 324 325 328 private static void testClobAsciiWrite3Param(Connection conn) 329 { 330 try { 331 System.out.println(START + "testClobAsciiWrite3Param"); 332 333 PreparedStatement stmt3 = conn.prepareStatement( 334 "SELECT c FROM testBlobX1 WHERE a = 1"); 335 336 ResultSet rs3 = stmt3.executeQuery(); 337 338 rs3.next(); 339 340 Clob clob = rs3.getClob(1); 341 342 File file = new File (fileName[0]); 343 fileLength = file.length(); 344 InputStream fileIn = new FileInputStream (file); 345 346 if (clob != null) { 347 int count = 0; 348 byte[] buffer = new byte[1024]; 349 OutputStream outstream = clob.setAsciiStream(1L); 350 while ((count = fileIn.read(buffer)) != -1) { 351 outstream.write(buffer, 0, count); 352 } 353 outstream.close(); 354 fileIn.close(); 355 356 PreparedStatement stmt4 = conn.prepareStatement( 357 "UPDATE testBlobX1 SET c = ? WHERE a = 1"); 358 stmt4.setClob(1, clob); 359 stmt4.executeUpdate(); 360 stmt4.close(); 361 } else { 362 System.out.println("FAIL -- clob is NULL"); 363 } 364 365 rs3.close(); 366 rs3 = stmt3.executeQuery(); 367 368 if (rs3.next()) { 369 long new_length = rs3.getClob(1).length(); 370 if (new_length != fileLength) { 371 System.out.println( 372 "FAIL -- wrong clob length; original: " + 373 fileLength + " clob length: " + new_length); 374 } else { 375 InputStream fStream = new FileInputStream (file); 377 InputStream lStream = rs3.getClob(1).getAsciiStream(); 378 379 if (!compareLob2File(fStream, lStream)) 380 System.out.println("FAIL - Clob and file contents do not match"); 381 382 fStream.close(); 383 lStream.close(); 384 385 } 386 } else { 387 System.out.println("FAIL -- clob not found"); 388 } 389 rs3.close(); 390 stmt3.close(); 391 392 System.out.println("testClobAsciiWrite3Param finished"); 393 } catch (SQLException e) { 394 TestUtil.dumpSQLExceptions(e); 395 } catch (Throwable e) { 396 if (debug) e.printStackTrace(); 397 } 398 } 399 400 403 private static void testClobAsciiWrite1Param(Connection conn) 404 { 405 try { 406 System.out.println(START + "testClobAsciiWrite1Param"); 407 408 PreparedStatement stmt3 = conn.prepareStatement( 409 "SELECT c FROM testBlobX1 WHERE a = 1"); 410 411 ResultSet rs3 = stmt3.executeQuery(); 412 413 rs3.next(); 414 415 Clob clob = rs3.getClob(1); 416 417 File file = new File (fileName[1]); 418 fileLength = file.length(); 419 InputStream fileIn = new FileInputStream (file); 420 421 if (clob != null) { 422 int buffer; 423 OutputStream outstream = clob.setAsciiStream(1L); 424 while ((buffer = fileIn.read()) != -1) { 425 outstream.write(buffer); 426 } 427 outstream.close(); 428 fileIn.close(); 429 430 PreparedStatement stmt4 = conn.prepareStatement( 431 "UPDATE testBlobX1 SET c = ? WHERE a = 1"); 432 stmt4.setClob(1, clob); 433 stmt4.executeUpdate(); 434 stmt4.close(); 435 436 } else { 437 System.out.println("FAIL -- clob is NULL"); 438 } 439 440 rs3.close(); 441 rs3 = stmt3.executeQuery(); 442 443 if (rs3.next()) { 444 long new_length = rs3.getClob(1).length(); 445 if (new_length != fileLength) { 446 System.out.println( 447 "FAIL -- wrong clob length; original: " + 448 fileLength + " clob length: " + new_length); 449 } else { 450 InputStream fStream = new FileInputStream (file); 452 InputStream lStream = rs3.getClob(1).getAsciiStream(); 453 454 if (!compareLob2File(fStream, lStream)) 455 System.out.println("FAIL - Clob and file contents do not match"); 456 457 fStream.close(); 458 lStream.close(); 459 460 } 461 } else { 462 System.out.println("FAIL -- clob not found"); 463 } 464 rs3.close(); 465 stmt3.close(); 466 467 System.out.println("testClobAsciiWrite1Param finished"); 468 } catch (SQLException e) { 469 TestUtil.dumpSQLExceptions(e); 470 } catch (Throwable e) { 471 if (debug) e.printStackTrace(); 472 } 473 } 474 475 478 private static void testClobCharacterWrite3ParamChar(Connection conn) 479 { 480 try { 481 System.out.println(START + "testClobCharacterWrite3ParamChar"); 482 483 PreparedStatement stmt3 = conn.prepareStatement( 484 "SELECT c FROM testBlobX1 WHERE a = 1"); 485 486 ResultSet rs3 = stmt3.executeQuery(); 487 488 rs3.next(); 489 490 Clob clob = rs3.getClob(1); 491 char[] testdata = unicodeTestString.toCharArray(); 492 493 494 if (clob != null) { 495 Writer clobWriter = clob.setCharacterStream(1L); 496 clobWriter.write(testdata, 0, testdata.length); 497 clobWriter.close(); 498 499 PreparedStatement stmt4 = conn.prepareStatement( 500 "UPDATE testBlobX1 SET c = ? WHERE a = 1"); 501 stmt4.setClob(1, clob); 502 stmt4.executeUpdate(); 503 stmt4.close(); 504 } else { 505 System.out.println("FAIL -- clob is NULL"); 506 } 507 508 rs3.close(); 509 rs3 = stmt3.executeQuery(); 510 511 if (rs3.next()) { 512 long new_length = rs3.getClob(1).length(); 513 if (new_length != testdata.length) { 514 System.out.println( 515 "FAIL -- wrong clob length; original: " + 516 testdata.length + " clob length: " + new_length); 517 } else { 518 Reader lStream = rs3.getClob(1).getCharacterStream(); 520 521 if (!compareClobReader2CharArray(testdata, lStream)) 522 System.out.println("FAIL - Clob and buffer contents do not match"); 523 524 lStream.close(); 525 526 } 527 } else { 528 System.out.println("FAIL -- clob not found"); 529 } 530 rs3.close(); 531 stmt3.close(); 532 533 System.out.println("testClobCharacterWrite3ParamChar finished"); 534 } catch (SQLException e) { 535 TestUtil.dumpSQLExceptions(e); 536 } catch (Throwable e) { 537 if (debug) e.printStackTrace(); 538 } 539 } 540 541 544 private static void testClobCharacterWrite3ParamString(Connection conn) 545 { 546 try { 547 System.out.println(START + "testClobCharacterWrite3ParamString"); 548 549 PreparedStatement stmt3 = conn.prepareStatement( 550 "SELECT c FROM testBlobX1 WHERE a = 1"); 551 552 ResultSet rs3 = stmt3.executeQuery(); 553 554 rs3.next(); 555 556 Clob clob = rs3.getClob(1); 557 558 559 if (clob != null) { 560 Writer clobWriter = clob.setCharacterStream(1L); 561 clobWriter.write(unicodeTestString, 0, unicodeTestString.length()); 562 clobWriter.close(); 563 564 PreparedStatement stmt4 = conn.prepareStatement( 565 "UPDATE testBlobX1 SET c = ? WHERE a = 1"); 566 stmt4.setClob(1, clob); 567 stmt4.executeUpdate(); 568 stmt4.close(); 569 } else { 570 System.out.println("FAIL -- clob is NULL"); 571 } 572 573 rs3.close(); 574 rs3 = stmt3.executeQuery(); 575 576 if (rs3.next()) { 577 long new_length = rs3.getClob(1).length(); 578 if (new_length != unicodeTestString.length()) { 579 System.out.println( 580 "FAIL -- wrong clob length; original: " + 581 unicodeTestString.length() + " clob length: " + new_length); 582 } else { 583 Reader lStream = rs3.getClob(1).getCharacterStream(); 585 586 if (!compareClobReader2CharArray(unicodeTestString.toCharArray(), lStream)) 587 System.out.println("FAIL - Clob and buffer contents do not match"); 588 589 lStream.close(); 590 591 } 592 } else { 593 System.out.println("FAIL -- clob not found"); 594 } 595 rs3.close(); 596 stmt3.close(); 597 598 System.out.println("testClobCharacterWrite3ParamString finished"); 599 } catch (SQLException e) { 600 TestUtil.dumpSQLExceptions(e); 601 } catch (Throwable e) { 602 if (debug) e.printStackTrace(); 603 } 604 } 605 606 609 private static void testClobCharacterWrite1ParamString(Connection conn) 610 { 611 try { 612 System.out.println(START + "testClobCharacterWrite1ParamString"); 613 614 PreparedStatement stmt3 = conn.prepareStatement( 615 "SELECT c FROM testBlobX1 WHERE a = 1"); 616 617 ResultSet rs3 = stmt3.executeQuery(); 618 619 rs3.next(); 620 621 Clob clob = rs3.getClob(1); 622 623 624 if (clob != null) { 625 Writer clobWriter = clob.setCharacterStream(1L); 626 clobWriter.write(unicodeTestString); 627 clobWriter.close(); 628 629 PreparedStatement stmt4 = conn.prepareStatement( 630 "UPDATE testBlobX1 SET c = ? WHERE a = 1"); 631 stmt4.setClob(1, clob); 632 stmt4.executeUpdate(); 633 stmt4.close(); 634 } else { 635 System.out.println("FAIL -- clob is NULL"); 636 } 637 638 rs3.close(); 639 rs3 = stmt3.executeQuery(); 640 641 if (rs3.next()) { 642 long new_length = rs3.getClob(1).length(); 643 if (new_length != unicodeTestString.length()) { 644 System.out.println( 645 "FAIL -- wrong clob length; original: " + 646 unicodeTestString.length() + " clob length: " + new_length); 647 } else { 648 Reader lStream = rs3.getClob(1).getCharacterStream(); 650 651 if (!compareClobReader2CharArray(unicodeTestString.toCharArray(), lStream)) 652 System.out.println("FAIL - Clob and buffer contents do not match"); 653 654 lStream.close(); 655 656 } 657 } else { 658 System.out.println("FAIL -- clob not found"); 659 } 660 rs3.close(); 661 stmt3.close(); 662 663 System.out.println("testClobCharacterWrite1ParamString finished"); 664 } catch (SQLException e) { 665 TestUtil.dumpSQLExceptions(e); 666 } catch (Throwable e) { 667 if (debug) e.printStackTrace(); 668 } 669 } 670 673 private static void testClobCharacterWrite1Char(Connection conn) 674 { 675 try { 676 System.out.println(START + "testClobCharacterWrite1Char"); 677 678 PreparedStatement stmt3 = conn.prepareStatement( 679 "SELECT c FROM testBlobX1 WHERE a = 1"); 680 681 ResultSet rs3 = stmt3.executeQuery(); 682 683 rs3.next(); 684 685 Clob clob = rs3.getClob(1); 686 687 char testchar = 'a'; 688 689 if (clob != null) { 690 Writer clobWriter = clob.setCharacterStream(1L); 691 clobWriter.write(testchar); 692 clobWriter.close(); 693 694 PreparedStatement stmt4 = conn.prepareStatement( 695 "UPDATE testBlobX1 SET c = ? WHERE a = 1"); 696 stmt4.setClob(1, clob); 697 stmt4.executeUpdate(); 698 stmt4.close(); 699 } else { 700 System.out.println("FAIL -- clob is NULL"); 701 } 702 703 rs3.close(); 704 rs3 = stmt3.executeQuery(); 705 706 if (rs3.next()) { 707 long new_length = rs3.getClob(1).length(); 708 Clob fish = rs3.getClob(1); 709 if (new_length != 1) { 710 System.out.println( 711 "FAIL -- wrong clob length; original: " + 712 1 + " clob length: " + new_length); 713 } else { 714 Reader lStream = rs3.getClob(1).getCharacterStream(); 716 char clobchar = (char) lStream.read(); 717 718 if (clobchar != testchar) 719 System.out.println("FAIL - fetched Clob and original contents do not match"); 720 721 lStream.close(); 722 723 } 724 } else { 725 System.out.println("FAIL -- clob not found"); 726 } 727 rs3.close(); 728 stmt3.close(); 729 730 System.out.println("testClobCharacterWrite1Char finished"); 731 } catch (SQLException e) { 732 TestUtil.dumpSQLExceptions(e); 733 } catch (Throwable e) { 734 if (debug) e.printStackTrace(); 735 } 736 } 737 738 739 private static boolean compareLob2File(InputStream fStream, InputStream lStream) { 740 byte[] fByte = new byte[1024]; 741 byte[] lByte = new byte[1024]; 742 int lLength = 0, fLength = 0; 743 String fString, lString; 744 745 try { 746 do { 747 fLength = fStream.read(fByte, 0, 1024); 748 lLength = lStream.read(lByte, 0, 1024); 749 if (!java.util.Arrays.equals(fByte, lByte)) 750 return false; 751 } while (fLength > 0 && lLength > 0); 752 753 fStream.close(); 754 lStream.close(); 755 } catch (Throwable e) { 756 if (debug) e.printStackTrace(); 757 } 758 return true; 759 } 760 761 private static boolean compareClobReader2CharArray(char[] cArray, Reader charReader) { 762 char[] clobChars = new char[cArray.length]; 763 764 int readChars = 0; 765 int totalCharsRead = 0; 766 767 try { 768 do { 769 readChars = charReader.read(clobChars, totalCharsRead, cArray.length - totalCharsRead); 770 if (readChars != -1) 771 totalCharsRead += readChars; 772 } while (readChars != -1 && totalCharsRead < cArray.length); 773 charReader.close(); 774 if (!java.util.Arrays.equals(cArray, clobChars)) 775 return false; 776 777 } catch (Throwable e) { 778 if (debug) e.printStackTrace(); 779 } 780 return true; 781 } 782 783 784 private static void cleanUp(Connection conn) throws SQLException { 785 String [] testObjects = {"table testBlobX1"}; 786 Statement cleanupStmt = conn.createStatement(); 787 TestUtil.cleanUpTest(cleanupStmt, testObjects); 788 } 789 790 } 791 | Popular Tags |