1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
|
/*
*
* Derby - Class CharacterStreamsTest
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
* either express or implied. See the License for the specific
* language governing permissions and limitations under the License.
*/
package org.apache.derbyTesting.functionTests.tests.jdbcapi;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import junit.framework.Test;
import org.apache.derbyTesting.functionTests.util.streams.CharAlphabet;
import org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.TestConfiguration;
/**
* Tests the following PreparedStatement methods:
* -> setCharacterStream(int parameterIndex, InputStream x, int length)
* -> setAsciiStream(int parameterIndex, Reader reader, int length)
*/
public class CharacterStreamsTest extends BaseJDBCTestCase {
public static Test suite() {
// Run only in embedded mode until DERBY-2017 is fixed.
return TestConfiguration.embeddedSuite(CharacterStreamsTest.class);
}
/** Creates a new instance of CharacterStreamsTest */
public CharacterStreamsTest(String name) {
super(name);
}
/**
* Test PreparedStatement.setAsciiStream() with column of type CHAR
*/
public void testSetAsciiStreamIntoChar() throws Exception {
runTestSetAsciiStream(1);
}
/**
* Test PreparedStatement.setAsciiStream() with column of type VARCHAR
*/
public void testSetAsciiStreamIntoVarchar() throws Exception {
runTestSetAsciiStream(2);
}
/**
* Test PreparedStatement.setAsciiStream() with column of type LONG VARCHAR
*/
public void testSetAsciiStreamIntoLongVarchar() throws Exception {
runTestSetAsciiStream(3);
}
/**
* Test PreparedStatement.setAsciiStream() with column of type CLOB
*/
public void testSetAsciiStreamIntoClob() throws Exception {
runTestSetAsciiStream(4);
}
/**
* Test PreparedStatement.setCharacterStream() with column of type CHAR
*/
public void testSetCharacterStreamIntoChar() throws Exception {
runTestSetCharacterStream(1);
}
/**
* Test PreparedStatement.setCharacterStream() with column of type VARCHAR
*/
public void testSetCharacterStreamIntoVarchar() throws Exception {
runTestSetCharacterStream(2);
}
/**
* Test PreparedStatement.setCharacterStream() with column of type
* LONG VARCHAR
*/
public void testSetCharacterStreamIntoLongVarchar() throws Exception {
runTestSetCharacterStream(3);
}
/**
* Test PreparedStatement.setCharacterStream() with column of type CLOB
*/
public void testSetCharacterStreamIntoClob() throws Exception {
runTestSetCharacterStream(4);
}
/**
* Test PreparedStatement.setAsciiStream() with streams with sizes from
* 60characters to 32K characters
*/
public void testSetAsciiStreamLongValues() throws Exception {
runTestSetAsciiStreamLongValues();
}
/**
* Test PreparedStatement.setCharacterStream() with streams with sizes from
* 60 characters to 32K characters
*/
public void testSetCharacterStreamLongValues() throws Exception {
runTestSetCharacterStreamLongValues();
}
private void runTestSetAsciiStream(int col) throws Exception {
PreparedStatement psi = prepareStatement(
"insert into charstream(c, vc, lvc, lob) " +
"values(?,?,?,?)");
PreparedStatement psq = prepareStatement(
"select id, c, {fn length(c)} AS CLEN, " +
"cast (vc as varchar(25)) AS VC, " +
"{fn length(vc)} AS VCLEN, " +
"cast (lvc as varchar(25)) AS LVC, " +
"{fn length(lvc)} AS LVCLEN, " +
"cast (lob as varchar(25)) AS LOB, " +
"{fn length(lob)} AS LOBLEN " +
"from charstream " +
"where id > ? order by 1");
// test setAsciiStream into CHAR
println("\nTest setAsciiStream into CHAR");
psi.setString(1, null);
psi.setString(2, null);
psi.setString(3, null);
psi.setString(4, null);
int maxid = getMaxId();
setAscii(psi, col);
psq.setInt(1, maxid);
verifyAsciiStreamResults(psq.executeQuery(), col);
// Show results as various streams
PreparedStatement psStreams = prepareStatement(
"SELECT id, c, vc, lvc, lob " +
"FROM charstream where id > ? order by 1");
psStreams.setInt(1, maxid);
verifyResultsUsingAsciiStream(psStreams.executeQuery(), col);
verifyResultsUsingCharacterStream(psStreams.executeQuery(), col);
verifyResultsUsingCharacterStreamBlock(psStreams.executeQuery(), col);
psStreams.close();
psi.close();
psq.close();
}
private void runTestSetCharacterStream(int col) throws Exception {
PreparedStatement psi = prepareStatement(
"insert into charstream(c, vc, lvc, lob) " +
"values(?,?,?,?)");
PreparedStatement psq = prepareStatement(
"select id, c, {fn length(c)} AS CLEN, " +
"cast (vc as varchar(25)) AS VC, " +
"{fn length(vc)} AS VCLEN, " +
"cast (lvc as varchar(25)) AS LVC, " +
"{fn length(lvc)} AS LVCLEN, " +
"cast (lob as varchar(25)) AS LOB, " +
"{fn length(lob)} AS LOBLEN " +
"from charstream " +
"where id > ? order by 1");
// test setCharacterStream into CHAR
println("\nTest setCharacterStream into CHAR");
psi.setString(1, null);
psi.setString(2, null);
psi.setString(3, null);
psi.setString(4, null);
int maxid = getMaxId();
setCharacter(psi, col);
psq.setInt(1, maxid);
verifyCharStreamResults(psq.executeQuery(), col);
psi.close();
psq.close();
}
private void runTestSetAsciiStreamLongValues() throws Exception {
// now insert long values using streams and check them programatically.
PreparedStatement psi = prepareStatement(
"insert into charstream(c, vc, lvc, lob) " +
"values(?,?,?,?)");
PreparedStatement psDel = prepareStatement("DELETE FROM charstream");
PreparedStatement psq2 =
prepareStatement("select c, vc, lvc, lob from charstream");
// now insert long values using streams and check them programatically.
println("setAsciiStream(LONG ASCII STREAMS)");
checkAsciiStreams(psDel, psi, psq2, 18, 104, 67, 67);
checkAsciiStreams(psDel, psi, psq2, 25, 16732, 14563, 14563);
checkAsciiStreams(psDel, psi, psq2, 1, 32433, 32673, 32673);
checkAsciiStreams(psDel, psi, psq2, 0, 32532, 32700, 32700);
psi.close();
psDel.close();
psq2.close();
}
private void runTestSetCharacterStreamLongValues() throws Exception {
// now insert long values using streams and check them programatically.
PreparedStatement psi = prepareStatement(
"insert into charstream(c, vc, lvc, lob) " +
"values(?,?,?,?)");
PreparedStatement psDel = prepareStatement("DELETE FROM charstream");
PreparedStatement psqSQLLength = prepareStatement(
"select length(c), length(vc), length(lvc), length(lob) " +
"from charstream");
PreparedStatement psq2 =
prepareStatement("select c, vc, lvc, lob from charstream");
println("setCharacterStream(LONG CHARACTER STREAMS WITH UNICODE)");
checkCharacterStreams(
psDel, psi, psq2, psqSQLLength, 14, 93, 55, 55, 0);
checkCharacterStreams(
psDel, psi, psq2, psqSQLLength, 21, 10887, 10887, 10887, 3);
checkCharacterStreams(
psDel, psi, psq2, psqSQLLength, 25, 19332, 18733, 18733, 0);
checkCharacterStreams(
psDel, psi, psq2, psqSQLLength, 1, 32433, 32673, 32673, 0);
checkCharacterStreams(
psDel, psi, psq2, psqSQLLength, 0, 32532, 32700, 32700, 0);
psi.close();
psDel.close();
psq2.close();
}
private int getMaxId() throws SQLException {
Statement stmt = createStatement();
ResultSet rs = stmt.executeQuery("select max(id) from charstream");
rs.next();
int maxid = rs.getInt(1);
rs.close();
stmt.close();
return maxid;
}
private void setAscii(PreparedStatement ps, int targetCol)
throws Exception {
byte[] asciiBytes = null;
// correct byte count
println("CORRECT NUMBER OF BYTES IN STREAM");
asciiBytes = ASCII_VALUE.getBytes("US-ASCII");
ps.setAsciiStream(targetCol,
new ByteArrayInputStream(asciiBytes), LEN_ASCII_VALUE);
ps.executeUpdate();
// less bytes than stream contains. JDBC 3.0 indicates it should throw
// an exception (in Tutorial & reference book)
println("MORE BYTES IN STREAM THAN PASSED IN VALUE");
try {
asciiBytes = "against Republicans George W. Bush ".
getBytes("US-ASCII");
ps.setAsciiStream(targetCol,
new ByteArrayInputStream(asciiBytes), 19);
ps.executeUpdate();
fail("FAIL - MORE BYTES IN ASCII STREAM THAN SPECIFIED LENGTH");
} catch (SQLException sqle) {
assertSQLState("XJ001", sqle);
}
// more bytes than the stream contains JDBC 3.0 changed to indicate an
// exception should be thrown. (in Tutorial & reference book)
println("LESS BYTES IN STREAM THAN PASSED IN VALUE");
try {
asciiBytes = "and Dick Cheney.".getBytes("US-ASCII");
ps.setAsciiStream(targetCol,
new ByteArrayInputStream(asciiBytes), 17);
ps.executeUpdate();
fail("FAIL - LESS BYTES IN ASCII STREAM THAN SPECIFIED LENGTH");
} catch (SQLException sqle) {
assertSQLState("XJ001", sqle);
}
// null
println("NULL ASCII STREAM");
ps.setAsciiStream(targetCol, null, 1);
ps.executeUpdate();
}
private void setCharacter(PreparedStatement ps, int targetCol)
throws Exception {
Reader reader = null;
// correct character count
reader = new StringReader(CHAR_VALUE1);
ps.setCharacterStream(targetCol, reader, LEN_CHAR_VALUE1);
ps.executeUpdate();
reader = new StringReader(CHAR_VALUE2);
ps.setCharacterStream(targetCol, reader, LEN_CHAR_VALUE2);
ps.executeUpdate();
// less bytes than stream contains.
try {
reader = new StringReader("for comments he made at");
ps.setCharacterStream(targetCol, reader, 20);
ps.executeUpdate();
fail("FAIL - MORE CHARACTERS IN READER THAN SPECIFIED LENGTH");
} catch (SQLException sqle) {
assertSQLState("XJ001", sqle);
}
// more bytes than the stream contains,
// JDBC 3.0 changed to indicate an exception should be thrown.
try {
reader = new StringReader("a birthday party");
ps.setCharacterStream(targetCol, reader, 17);
ps.executeUpdate();
fail("FAIL - LESS CHARACTERS IN READER THAN SPECIFIED LENGTH");
} catch (SQLException sqle) {
assertSQLState("XJ001", sqle);
}
// null
ps.setCharacterStream(targetCol, null, 1);
ps.executeUpdate();
}
private void verifyAsciiStreamResults(ResultSet rs, int col)
throws Exception
{
String value;
int length;
// First row
assertTrue("FAIL - row not found", rs.next());
value = rs.getString(col * 2);
assertFalse("FAIL - value should not be null", rs.wasNull());
length = rs.getInt((col * 2) + 1);
assertFalse("FAIL - length should not be null", rs.wasNull());
assertEquals("FAIL - wrong value on column " + col,
ASCII_VALUE, value.trim());
assertEquals("FAIL - wrong length " + col, LEN_ASCII_VALUE, length);
// null row
assertTrue("FAIL - row not found", rs.next());
value = rs.getString(col * 2);
assertTrue("FAIL - value should be null", rs.wasNull());
length = rs.getInt((col * 2) + 1);
assertTrue("FAIL - length should be null", rs.wasNull());
assertEquals("FAIL - wrong value on column " + col, null, value);
assertEquals("FAIL - wrong length " + col, 0, length);
assertFalse("FAIL - more rows than expected", rs.next());
}
private void verifyCharStreamResults(ResultSet rs, int col)
throws Exception {
String value;
int length;
// First row
assertTrue("FAIL - row not found", rs.next());
value = rs.getString(col * 2);
assertFalse("FAIL - value should not be null", rs.wasNull());
length = rs.getInt((col * 2) + 1);
assertFalse("FAIL - length should not be null", rs.wasNull());
assertEquals("FAIL - wrong value on column " + col,
CHAR_VALUE1, value.trim());
assertEquals("FAIL - wrong length " + col, LEN_CHAR_VALUE1, length);
// Second row
assertTrue("FAIL - row not found", rs.next());
value = rs.getString(col * 2);
assertFalse("FAIL - value should not be null", rs.wasNull());
length = rs.getInt((col * 2) + 1);
assertFalse("FAIL - length should not be null", rs.wasNull());
assertEquals("FAIL - wrong value on column " + col,
CHAR_VALUE2, value.trim());
assertEquals("FAIL - wrong length " + col, LEN_CHAR_VALUE2, length);
// null row
assertTrue("FAIL - row not found", rs.next());
value = rs.getString(col * 2);
assertTrue("FAIL - value should be null", rs.wasNull());
length = rs.getInt((col * 2) + 1);
assertTrue("FAIL - length should be null", rs.wasNull());
assertEquals("FAIL - wrong value on column " + col, null, value);
assertEquals("FAIL - wrong length " + col, 0, length);
assertFalse("FAIL - more rows than expected", rs.next());
rs.close();
}
private void verifyResultsUsingAsciiStream(ResultSet rs, int col)
throws Exception
{
InputStream valueStream;
String value;
// First row
assertTrue("FAIL - row not found", rs.next());
valueStream = rs.getAsciiStream(col + 1);
assertFalse("FAIL - value should not be null", rs.wasNull());
byte[] valueBytes = new byte[LEN_ASCII_VALUE];
assertEquals("FAIL - wrong length read from stream", LEN_ASCII_VALUE,
valueStream.read(valueBytes));
assertEquals("FAIL - wrong value on column " + col,
ASCII_VALUE, new String(valueBytes, "US-ASCII"));
// null row
assertTrue("FAIL - row not found", rs.next());
value = rs.getString(col + 1);
assertTrue("FAIL - value should be null", rs.wasNull());
assertEquals("FAIL - wrong value on column " + col, null, value);
assertFalse("FAIL - more rows than expected", rs.next());
rs.close();
}
private void verifyResultsUsingCharacterStream(ResultSet rs, int col)
throws Exception
{
Reader valueReader;
String value;
// First row
assertTrue("FAIL - row not found", rs.next());
// Read characters one by one
valueReader = rs.getCharacterStream(col + 1);
StringBuffer sb = new StringBuffer();
int c = 0;
while ((c = valueReader.read()) != -1) {
sb.append((char)c);
}
value = sb.toString().trim();
assertEquals("FAIL - wrong length read from stream", LEN_ASCII_VALUE,
value.length());
assertEquals("FAIL - wrong value on column " + col,
ASCII_VALUE, value);
// null row
assertTrue("FAIL - row not found", rs.next());
value = rs.getString(col + 1);
assertTrue("FAIL - value should be null", rs.wasNull());
assertEquals("FAIL - wrong value on column " + col, null, value);
assertFalse("FAIL - more rows than expected", rs.next());
rs.close();
}
private void verifyResultsUsingCharacterStreamBlock(ResultSet rs, int col)
throws Exception
{
Reader valueReader;
String value;
// First row
assertTrue("FAIL - row not found", rs.next());
valueReader = rs.getCharacterStream(col + 1);
assertFalse("FAIL - value should not be null", rs.wasNull());
// Read all characters in a block
char[] valueChars = new char[LEN_ASCII_VALUE];
assertEquals("FAIL - wrong length read from stream", LEN_ASCII_VALUE,
valueReader.read(valueChars));
assertEquals("FAIL - wrong value on column " + col,
ASCII_VALUE, new String(valueChars));
// null row
assertTrue("FAIL - row not found", rs.next());
value = rs.getString(col + 1);
assertTrue("FAIL - value should be null", rs.wasNull());
assertEquals("FAIL - wrong value on column " + col, null, value);
assertFalse("FAIL - more rows than expected", rs.next());
rs.close();
}
private void checkAsciiStreams(
PreparedStatement psDel,
PreparedStatement psi,
PreparedStatement psq2,
int cl, int vcl, int lvcl, int lob)
throws SQLException, IOException {
psDel.executeUpdate();
// now insert long values using streams and check them programatically.
psi.setAsciiStream(1, new c3AsciiStream(cl), cl);
psi.setAsciiStream(2, new c3AsciiStream(vcl), vcl);
psi.setAsciiStream(3, new c3AsciiStream(lvcl), lvcl);
psi.setAsciiStream(4, new c3AsciiStream(lob), lob);
psi.executeUpdate();
ResultSet rs = psq2.executeQuery();
rs.next();
InputStream is = rs.getAsciiStream(1);
checkAsciiStream(is, cl, 25);
is = rs.getAsciiStream(2);
checkAsciiStream(is, vcl, -1);
is = rs.getAsciiStream(3);
checkAsciiStream(is, lvcl, -1);
is = rs.getAsciiStream(4);
checkAsciiStream(is, lob, -1);
rs.close();
rs = psq2.executeQuery();
rs.next();
Reader r = rs.getCharacterStream(1);
checkAsciiStream(r, cl, 25);
r = rs.getCharacterStream(2);
checkAsciiStream(r, vcl, -1);
r = rs.getCharacterStream(3);
checkAsciiStream(r, lvcl, -1);
r = rs.getCharacterStream(4);
checkAsciiStream(r, lob, -1);
rs.close();
// and check as Strings
rs = psq2.executeQuery();
rs.next();
r = new StringReader(rs.getString(1));
checkAsciiStream(r, cl, 25);
r = new StringReader(rs.getString(2));
checkAsciiStream(r, vcl, -1);
r = new StringReader(rs.getString(3));
checkAsciiStream(r, lvcl, -1);
r = new StringReader(rs.getString(4));
checkAsciiStream(r, lob, -1);
rs.close();
}
private Reader getSourceStream(int length, int bytesPerChar) {
switch (bytesPerChar) {
case 0:
return new c3Reader(length);
case 1:
return new LoopingAlphabetReader(length,
CharAlphabet.modernLatinLowercase());
case 2:
return new LoopingAlphabetReader(length,
CharAlphabet.tamil());
case 3:
return new LoopingAlphabetReader(length,
CharAlphabet.cjkSubset());
default:
fail("Illegal value of bytesPerChar: " + bytesPerChar);
return null;
}
}
private void checkCharacterStreams(
PreparedStatement psDel,
PreparedStatement psi,
PreparedStatement psq2,
PreparedStatement psqSQLLength,
int cl, int vcl, int lvcl, int lob,
int bytesPerChar)
throws SQLException, IOException {
psDel.executeUpdate();
psi.setCharacterStream(1, getSourceStream(cl, bytesPerChar), cl);
psi.setCharacterStream(2, getSourceStream(vcl, bytesPerChar), vcl);
psi.setCharacterStream(3, getSourceStream(lvcl, bytesPerChar), lvcl);
psi.setCharacterStream(4, getSourceStream(lob, bytesPerChar), lob);
psi.executeUpdate();
ResultSet rsLength = psqSQLLength.executeQuery();
assertTrue(rsLength.next());
assertEquals(25, rsLength.getInt(1)); // CHAR
assertEquals(vcl, rsLength.getInt(2)); // VARCHAR
assertEquals(lvcl, rsLength.getInt(3)); // LONG VARCHAR
assertEquals(lob, rsLength.getInt(4)); //CLOB
assertFalse(rsLength.next());
rsLength.close();
ResultSet rs = psq2.executeQuery();
rs.next();
InputStream is = rs.getAsciiStream(1);
checkCharStream(is, cl, 25, bytesPerChar);
is = rs.getAsciiStream(2);
checkCharStream(is, vcl, -1, bytesPerChar);
is = rs.getAsciiStream(3);
checkCharStream(is, lvcl, -1, bytesPerChar);
is = rs.getAsciiStream(4);
checkCharStream(is, lob, -1, bytesPerChar);
rs.close();
rs = psq2.executeQuery();
rs.next();
Reader r = rs.getCharacterStream(1);
checkCharStream(r, cl, 25, bytesPerChar);
r = rs.getCharacterStream(2);
checkCharStream(r, vcl, -1, bytesPerChar);
r = rs.getCharacterStream(3);
checkCharStream(r, lvcl, -1, bytesPerChar);
r = rs.getCharacterStream(4);
checkCharStream(r, lob, -1, bytesPerChar);
rs.close();
// check converting them into Strings work
rs = psq2.executeQuery();
rs.next();
String suv = rs.getString(1);
r = new StringReader(suv);
checkCharStream(r, cl, 25, bytesPerChar);
suv = rs.getString(2);
r = new StringReader(suv);
checkCharStream(r, vcl, -1, bytesPerChar);
suv = rs.getString(3);
r = new StringReader(suv);
checkCharStream(r, lvcl, -1, bytesPerChar);
suv = rs.getString(4);
r = new StringReader(suv);
checkCharStream(r, lob, -1, bytesPerChar);
rs.close();
}
private void checkAsciiStream(InputStream is, int length, int fixedLen)
throws IOException
{
InputStream orig = new c3AsciiStream(length);
int count = 0;
for (;;) {
int o = orig == null ?
(count == fixedLen ? -2 : 0x20) : orig.read();
int c = is.read();
if (o == -1) {
orig = null;
if (fixedLen != -1 && fixedLen != length)
o = ' ';
}
if (o == -2)
o = -1;
assertEquals("FAIL - wrong value at position " + count, o, c);
if (orig == null) {
if (fixedLen == -1)
break;
}
if (c == -1 && fixedLen != -1)
break;
count++;
}
if (fixedLen != -1)
length = fixedLen;
assertEquals("FAIL - wrong length", length, count);
is.close();
}
private void checkAsciiStream(Reader r, int length, int fixedLen)
throws IOException
{
InputStream orig = new c3AsciiStream(length);
int count = 0;
for (;;) {
int o = orig == null ?
(count == fixedLen ? -2 : 0x20) : orig.read();
int c = r.read();
if (o == -1) {
orig = null;
if (fixedLen != -1 && fixedLen != length)
o = ' ';
}
if (o == -2)
o = -1;
assertEquals("FAIL - wrong value", o, c);
if (orig == null) {
if (fixedLen == -1)
break;
}
if (c == -1 && fixedLen != -1)
break;
count++;
}
if (fixedLen != -1)
length = fixedLen;
assertEquals("FAIL - wrong length", length, count);
r.close();
}
private void checkCharStream(InputStream is, int length, int fixedLen,
int bytesPerChar)
throws IOException
{
Reader orig = getSourceStream(length, bytesPerChar);
int count = 0;
for (;;) {
int o = orig == null ?
(count == fixedLen ? -2 : 0x20) : orig.read();
int c = is.read();
if (o == -1) {
orig = null;
if (fixedLen != -1 && fixedLen != length)
o = ' ';
}
if (o == -2)
o = -1;
if (o != -1) {
if (o <= 255)
o = o & 0xFF; // convert to single byte extended ASCII
else
o = '?'; // out of range character.
}
assertEquals("FAIL - wrong value", o, c);
if (orig == null) {
if (fixedLen == -1)
break;
}
if (c == -1 && fixedLen != -1)
break;
count++;
}
if (fixedLen != -1)
length = fixedLen;
assertEquals("FAIL - wrong length", length, count);
is.close();
}
private void checkCharStream(Reader r, int length, int fixedLen,
int bytesPerChar)
throws IOException
{
Reader orig = getSourceStream(length, bytesPerChar);
int count = 0;
for (;;) {
int o = (orig == null) ?
(count == fixedLen ? -2 : 0x20) : orig.read();
int c = r.read();
if (o == -1) {
orig = null;
if (fixedLen != -1 && fixedLen != length)
o = ' ';
}
if (o == -2)
o = -1;
assertEquals("FAIL - wrong value", o, c);
if (orig == null) {
if (fixedLen == -1)
break;
}
if (c == -1 && fixedLen != -1)
break;
count++;
}
if (fixedLen != -1)
length = fixedLen;
assertEquals("FAIL - wrong length", length, count);
r.close();
}
protected void setUp() throws Exception {
createStatement().executeUpdate(
"create table charstream(" +
"id int GENERATED ALWAYS AS IDENTITY primary key, " +
"c char(25), " +
"vc varchar(32532), " +
"lvc long varchar, " +
"lob clob(300K))");
}
protected void tearDown() throws Exception {
rollback();
createStatement().executeUpdate("DROP TABLE charstream");
commit();
super.tearDown();
}
private final static String ASCII_VALUE = "Lieberman ran with Gore";
private final static int LEN_ASCII_VALUE = 23;
private final static String CHAR_VALUE1 = "A Mississippi Republican";
private final static int LEN_CHAR_VALUE1 = 24;
private final static String CHAR_VALUE2 = "Lott has apologized";
private final static int LEN_CHAR_VALUE2 = 19;
}
class c3AsciiStream extends InputStream {
private final int size;
private int count;
c3AsciiStream(int size) {
this.size = size;
}
public int read(byte[] buf, int off, int length) {
if (count >= size)
return -1;
if (length > (size - count))
length = (size - count);
// ensure the readers don't always get a full buffer,
// makes sure they are not assuming the buffer will be filled.
if (length > 20)
length -= 17;
for (int i = 0; i < length ; i++) {
buf[off + i] = (byte) count++;
}
return length;
}
private byte[] rd = new byte[1];
public int read() {
int read = read(rd, 0, 1);
if (read == -1)
return -1;
return rd[0] & 0xFF;
}
public void close() {
}
}
class c3Reader extends Reader {
private final int size;
private int count;
c3Reader(int size) {
this.size = size;
}
public int read(char[] buf, int off, int length) {
if (count >= size)
return -1;
if (length > (size - count))
length = (size - count);
// ensure the readers don't always get a full buffer,
// makes sure they are not assuming the buffer will be filled.
if (length > 20)
length -= 17;
for (int i = 0; i < length ; i++) {
char c;
switch (count % 3) {
case 0:
c = (char) (count & 0x7F); // one byte UTF8
break;
case 1:
c = (char) ((count + 0x7F) & 0x07FF); // two byte UTF8
break;
default:
case 2:
c = (char) (count + 0x07FF); // three byte UTF8
break;
}
buf[off + i] = c;
count++;
}
return length;
}
public void close() {
}
}
|