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
|
/*
Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.Derby2017LayerATest
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.io.UnsupportedEncodingException;
import java.math.BigInteger;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import junit.framework.Test;
import org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetReader;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.JDBC;
import org.apache.derbyTesting.junit.TestConfiguration;
/**
* Tests that inserts with streams whose lengths differs from the the length
* specified don't insert data into the database when they shouldn't.
* <p>
* The test uses various combinations of auto-commit and rollback.
*/
public class Derby2017LayerATest
extends BaseJDBCTestCase {
/** The encoding used when generating a byte array from a string. */
public static final String UTF8 = "UTF-8";
public Derby2017LayerATest(String name) {
super(name);
}
/**
* Returns a suite running most of the tests with both the client driver
* and the embedded driver, and some of the tests only with the client
* driver.
*
* @return A suite of tests.
*/
public static Test suite() {
BaseTestSuite ts = new BaseTestSuite();
ts.addTest(
TestConfiguration.defaultSuite(Derby2017LayerATest.class));
// Run the tests below with the client driver only.
BaseTestSuite clientSuite = new BaseTestSuite("Client only tests");
clientSuite.addTest(new Derby2017LayerATest(
"cs_FailedStreamInsertCharBufferBoundaries"));
clientSuite.addTest(new Derby2017LayerATest(
"cs_StreamInsertCharBufferBoundary"));
ts.addTest(TestConfiguration.clientServerDecorator(clientSuite));
return ts;
}
/**
* Tests inserts around some selected buffer boundaries. This test verifies
* that the client and server can sucessfully insert values of various
* lengths. It will work also before the fix for DERBY-2017, but will fail
* if an incorrect fix is applied.
*/
public void cs_StreamInsertCharBufferBoundary()
throws IOException, SQLException {
rollback();
Statement stmt = createStatement();
try {
stmt.executeUpdate("create table t2017_len (len int, c clob)");
} catch (SQLException sqle) {
assertSQLState("X0Y32", sqle);
stmt.executeUpdate("delete from t2017_len");
}
commit();
setAutoCommit(false);
// Note that when using layer A streaming the data is converted to
// UTF-16 on the wire.
PreparedStatement ps =
prepareStatement("insert into t2017_len values (?,?)");
// Test small values.
for (int i=0; i < 512; i++) {
ps.setInt(1, i);
ps.setCharacterStream(2, new LoopingAlphabetReader(i), i);
ps.executeUpdate();
}
commit();
// Test values at the buffer boundary. Assumes UTF-16 and a ~32 KB
// transmit buffer.
for (int i=16000; i < 18000; i++) {
ps.setInt(1, i);
ps.setCharacterStream(2, new LoopingAlphabetReader(i), i);
ps.executeUpdate();
// Commit periodically.
if (i % 1000 == 0) {
commit();
}
}
commit();
for (int i=32500; i < 33000; i++) {
ps.setInt(1, i);
ps.setCharacterStream(2, new LoopingAlphabetReader(i), i);
ps.executeUpdate();
}
commit();
// Verify the data, basically making sure the status flag isn't
// included as part of the user data.
ResultSet rs = stmt.executeQuery("select len, c from t2017_len");
int rows = 0;
while (rs.next()) {
rows++;
assertEquals(new LoopingAlphabetReader(rs.getInt(1)),
rs.getCharacterStream(2));
}
}
/**
* Runs some failing inserts around buffer boundaries.
*/
public void cs_FailedStreamInsertCharBufferBoundaries()
throws IOException, SQLException {
int[] INSERT;
for (int i=0; i < 1024; i++) {
INSERT = new int[] {
8*1000+i,
16*1000+i,
32*1000+i,
16*1000+i, // This will fail (forced length mismatch)
32*1000+i, // This will fail (forced length mismatch)
48*1000+i,
0+i,
};
// We test only one combination of auto-commit and rollback here.
doInsertTest(INSERT, false, false);
}
}
public void testFailedStreamInsertCharLong()
throws IOException, SQLException {
int[] INSERT = new int[] {
10*1024+1,
89*1024+3,
32*1024,
64*1024, // This will fail (forced length mismatch)
99*1024, // This will fail (forced length mismatch)
1,
197*1024,
};
doInsertTest(INSERT, false, false);
doInsertTest(INSERT, false, true);
doInsertTest(INSERT, true, false);
doInsertTest(INSERT, true, true);
}
/**
* Inserts data by reading from streams, where two of these will thrown
* an {@code IOException}. Data from these streams should not be committed.
*/
public void testFailedStreamInsertCharIOException()
throws IOException, SQLException {
String[] INSERT = new String[] {
"row 1", "row 2", "row 3",
"IGNORE", "IGNORE",
"row 6", "row 7"
};
String[][] MASTER = new String[][] {
{"row 1"}, {"row 2"}, {"row 3"},
{"row 6"}, {"row 7"}
};
rollback();
Statement stmt = createStatement();
try {
stmt.executeUpdate("create table t2017 (c clob)");
} catch (SQLException sqle) {
assertSQLState("X0Y32", sqle);
stmt.executeUpdate("delete from t2017");
}
commit();
setAutoCommit(true);
PreparedStatement ps = prepareStatement("insert into t2017 values (?)");
// Insert the 3 first rows.
for (int i=0; i < 3; i++) {
ps.setString(1, INSERT[i]);
assertEquals(1, ps.executeUpdate());
}
// Insert the 4th and 5th row with a stream that throws an IOException.
// Partial data shouldn't be inserted into the database.
Reader r4 = new FailingReader(518, 500);
ps.setCharacterStream(1, r4, 518);
try {
ps.executeUpdate();
fail("Insert should have failed");
} catch (SQLException sqle) {
// TODO: Check when exception handling has been settled.
// The states are different between client and embedded.
//assertSQLState(usingEmbedded() ? "XSDA4" : "XJ001", sqle);
}
Reader r5 = new FailingReader(67*1024, 42*1024);
ps.setCharacterStream(1, r5, 67*1024);
try {
ps.executeUpdate();
fail("Insert should have failed");
} catch (SQLException sqle) {
// TODO: Check when exception handling has been settled.
// The states are different between client and embedded.
//assertSQLState(usingEmbedded() ? "XSDA4" : "XJ001", sqle);
}
// The errors above should have statement severity. Insert the last two
// rows.
for (int i=5; i < INSERT.length; i++) {
ps.setString(1, INSERT[i]);
assertEquals(1, ps.executeUpdate());
}
// Select data in the table, compare to MASTER
ResultSet rs = stmt.executeQuery("select * from t2017");
JDBC.assertFullResultSet(rs, MASTER);
}
public void testFailedStreamInsertChar()
throws IOException, SQLException {
String[] INSERT = new String[] {
"This is row 1",
"This is row 2",
"This is row 3",
"This is row 4, a bit too long",
"This is row 5, a bit too short",
"This is row 6",
"This is row 7",
};
String[][] MASTER = new String[][] {
{"This is row 1"},
{"This is row 2"},
{"This is row 3"},
//{"This is row 4, a bit too long"},
//{"This is row 5, a bit too short"},
{"This is row 6"},
{"This is row 7"},
};
doInsertTest(INSERT, MASTER, false, false);
}
public void testFailedStreamInsertCharAutoCommit()
throws IOException, SQLException {
String[] INSERT = new String[] {
"This is row 1",
"This is row 2",
"This is row 3",
"This is row 4, a bit too long",
"This is row 5, a bit too short",
"This is row 6",
"This is row 7",
};
String[][] MASTER = new String[][] {
{"This is row 1"},
{"This is row 2"},
{"This is row 3"},
//{"This is row 4, a bit too long"},
//{"This is row 5, a bit too short"},
{"This is row 6"},
{"This is row 7"},
};
doInsertTest(INSERT, MASTER, true, false);
}
public void testFailedStreamInsertCharRollbackOnError()
throws IOException, SQLException {
String[] INSERT = new String[] {
"This is row 1",
"This is row 2",
"This is row 3",
"This is row 4, a bit too long",
"This is row 5, a bit too short",
"This is row 6",
"This is row 7",
};
String[][] MASTER = new String[][] {
//{"This is row 1"},
//{"This is row 2"},
//{"This is row 3"},
//{"This is row 4, a bit too long"},
//{"This is row 5, a bit too short"},
{"This is row 6"},
{"This is row 7"},
};
doInsertTest(INSERT, MASTER, false, true);
}
public void testFailedStreamInsertCharAutoCommitRollbackOnError()
throws IOException, SQLException {
String[] INSERT = new String[] {
"This is row 1",
"This is row 2",
"This is row 3",
"This is row 4, a bit too long",
"This is row 5, a bit too short",
"This is row 6",
"This is row 7",
};
String[][] MASTER = new String[][] {
{"This is row 1"},
{"This is row 2"},
{"This is row 3"},
//{"This is row 4, a bit too long"},
//{"This is row 5, a bit too short"},
{"This is row 6"},
{"This is row 7"},
};
doInsertTest(INSERT, MASTER, true, true);
}
public void testFailedStreamInsertBinary()
throws IOException, SQLException {
byte[][] INSERT = generateDefaultInsert();
String[][] MASTER = generateMaster(INSERT, new int[] {3, 4});
doInsertTest(INSERT, MASTER, false, false);
}
public void testFailedStreamInsertBinaryAutoCommit()
throws IOException, SQLException {
byte[][] INSERT = generateDefaultInsert();
String[][] MASTER = generateMaster(INSERT, new int[] {3, 4});
doInsertTest(INSERT, MASTER, true, false);
}
public void testFailedStreamInsertBinaryRollbackOnError()
throws IOException, SQLException {
byte[][] INSERT = generateDefaultInsert();
String[][] MASTER = generateMaster(INSERT, new int[] {0, 1, 2, 3, 4});
doInsertTest(INSERT, MASTER, false, true);
}
public void testFailedStreamInsertBinaryAutoCommitRollbackOnError()
throws IOException, SQLException {
byte[][] INSERT = generateDefaultInsert();
String[][] MASTER = generateMaster(INSERT, new int[] {3, 4});
doInsertTest(INSERT, MASTER, true, true);
}
/**
* Performs the base test cycle; insert 3 valid rows, try to insert 2
* invalid rows, insert 2 valid rows.
* <p>
* The outcome depends on whether auto-commit is on, and whether a rollback
* is issued when an insert fails.
*
* @param INSERT the data to insert
* @param MASTER the expected outcome
* @param autoCommit the auto-commit state to use
* @param rollbackOnError whether or not to issue a rollback if an insert
* fails
*
* @throws IOException if something goes wrong
* @throws SQLException if something goes wrong
*/
private void doInsertTest(String[] INSERT, String[][] MASTER,
boolean autoCommit, boolean rollbackOnError)
throws IOException, SQLException {
// A few sanity checks.
assertEquals("Expects 7 rows", 7, INSERT.length);
assertTrue(MASTER.length < INSERT.length);
rollback();
Statement stmt = createStatement();
try {
stmt.executeUpdate("create table t2017 (c clob)");
} catch (SQLException sqle) {
assertSQLState("X0Y32", sqle);
stmt.executeUpdate("delete from t2017");
}
commit();
setAutoCommit(autoCommit);
PreparedStatement ps = prepareStatement("insert into t2017 values (?)");
// Insert the 3 first rows.
for (int i=0; i < 3; i++) {
ps.setString(1, INSERT[i]);
assertEquals(1, ps.executeUpdate());
}
// Insert the 4th row with a stream that's longer than the specified
// length, then the 5th row that's shorter. Both should fail, and the
// data shouldn't be inserted into the database.
Reader r4 = new StringReader(INSERT[3]);
ps.setCharacterStream(1, r4, INSERT[3].length() - 5);
try {
ps.executeUpdate();
fail("Insert should have failed, stream too long");
} catch (SQLException sqle) {
// The states are different between client and embedded.
assertSQLState(usingEmbedded() ? "XSDA4" : "XN015", sqle);
if (rollbackOnError) {
rollback();
}
}
Reader r5 = new StringReader(INSERT[4]);
ps.setCharacterStream(1, r5, INSERT[4].length() + 5);
try {
ps.executeUpdate();
fail("Insert should have failed, stream too short");
} catch (SQLException sqle) {
// The states are different between client and embedded.
assertSQLState(usingEmbedded() ? "XSDA4" : "XN017", sqle);
if (rollbackOnError) {
rollback();
}
}
// The errors above should have statement severity. Insert the last
// two rows and make sure the transaction commits.
for (int i=5; i < INSERT.length; i++) {
ps.setString(1, INSERT[i]);
assertEquals(1, ps.executeUpdate());
}
if (!autoCommit) {
commit();
}
// Select data in the table, compare to MASTER
ResultSet rs = stmt.executeQuery("select * from t2017");
JDBC.assertFullResultSet(rs, MASTER);
}
/**
* Performs the base test cycle; insert 3 valid rows, try to insert 2
* invalid rows, insert 2 valid rows.
* <p>
* The outcome depends on whether auto-commit is on, and whether a rollback
* is issued when an insert fails.
*
* @param INSERT the data to insert
* @param autoCommit the auto-commit state to use
* @param rollbackOnError whether or not to issue a rollback if an insert
* fails
*
* @throws IOException if something goes wrong
* @throws SQLException if something goes wrong
*/
private void doInsertTest(int[] INSERT,
boolean autoCommit, boolean rollbackOnError)
throws IOException, SQLException {
// A few sanity checks.
assertEquals("Expects 7 rows", 7, INSERT.length);
rollback();
Statement stmt = createStatement();
try {
stmt.executeUpdate("create table t2017_id (id int unique, c clob)");
} catch (SQLException sqle) {
assertSQLState("X0Y32", sqle);
stmt.executeUpdate("delete from t2017_id");
}
commit();
setAutoCommit(autoCommit);
PreparedStatement ps =
prepareStatement("insert into t2017_id values (?, ?)");
// Insert the 3 first rows (id is 1-based).
for (int i=0; i < 3; i++) {
ps.setInt(1, i+1);
int length = INSERT[i];
ps.setCharacterStream(2, new LoopingAlphabetReader(length), length);
assertEquals(1, ps.executeUpdate());
}
// Insert the 4th row with a stream that's longer than the specified
// length, then the 5th row that's shorter. Both should fail, and the
// data shouldn't be inserted into the database.
Reader r4 = new LoopingAlphabetReader(INSERT[3]);
ps.setInt(1, 4);
ps.setCharacterStream(2, r4, INSERT[3] - 5);
try {
ps.executeUpdate();
fail("Insert should have failed, stream too long");
} catch (SQLException sqle) {
// The states are different between client and embedded.
assertSQLState(usingEmbedded() ? "XSDA4" : "XN015", sqle);
if (rollbackOnError) {
rollback();
}
}
Reader r5 = new LoopingAlphabetReader(INSERT[4]);
ps.setInt(1, 5);
ps.setCharacterStream(2, r5, INSERT[4] + 5);
try {
ps.executeUpdate();
fail("Insert should have failed, stream too short");
} catch (SQLException sqle) {
// The states are different between client and embedded.
assertSQLState(usingEmbedded() ? "XSDA4" : "XN017", sqle);
if (rollbackOnError) {
rollback();
}
}
// The errors above should have statement severity. Insert the last
// two rows and make sure the transaction commits.
for (int i=5; i < INSERT.length; i++) {
ps.setInt(1, i+1);
int length = INSERT[i];
ps.setCharacterStream(2, new LoopingAlphabetReader(length), length);
assertEquals(1, ps.executeUpdate());
}
if (!autoCommit) {
commit();
}
// Make sure we have the expected number of rows.
ResultSet rs = stmt.executeQuery("select count(*) from t2017_id");
rs.next();
assertEquals((rollbackOnError && !autoCommit ? 2 : 5), rs.getInt(1));
// Select data in the table, compare to what we expect.
rs = stmt.executeQuery( "select * from t2017_id order by id asc");
// Check rows 1-4 if rollback on error is false.
if (autoCommit || !rollbackOnError) {
for (int i=0; i < 3; i++) {
rs.next();
int id = rs.getInt(1);
assertTrue(id - 1 == i);
assertEquals(new LoopingAlphabetReader(INSERT[i]),
rs.getCharacterStream(2));
}
}
// Check rows 6 and 7.
for (int i=5; i < 7; i++) {
rs.next();
int id = rs.getInt(1);
assertTrue(id - 1 == i);
assertEquals(new LoopingAlphabetReader(INSERT[i]),
rs.getCharacterStream(2));
}
assertFalse(rs.next());
rs.close();
}
/**
* Performs the base test cycle; insert 3 valid rows, try to insert 2
* invalid rows, insert 2 valid rows.
* <p>
* The outcome depends on whether auto-commit is on, and whether a rollback
* is issued when an insert fails.
*
* @param INSERT the data to insert
* @param MASTER the expected outcome
* @param autoCommit the auto-commit state to use
* @param rollbackOnError whether or not to issue a rollback if an insert
* fails
*
* @throws IOException if something goes wrong
* @throws SQLException if something goes wrong
*/
private void doInsertTest(byte[][] INSERT, String[][] MASTER,
boolean autoCommit, boolean rollbackOnError)
throws IOException, SQLException {
// A few sanity checks.
assertEquals("Expects 7 rows", 7, INSERT.length);
assertTrue(MASTER.length < INSERT.length);
rollback();
Statement stmt = createStatement();
try {
stmt.executeUpdate("create table t2017_binary (b blob)");
} catch (SQLException sqle) {
assertSQLState("X0Y32", sqle);
stmt.executeUpdate("delete from t2017_binary");
}
commit();
setAutoCommit(autoCommit);
PreparedStatement ps = prepareStatement(
"insert into t2017_binary values (?)");
// Insert the 3 first rows.
for (int i=0; i < 3; i++) {
ps.setBytes(1, INSERT[i]);
assertEquals(1, ps.executeUpdate());
}
// Insert the 4th row with a stream that's longer than the specified
// length, then the 5th row that's shorter. Both should fail, and the
// data shouldn't be inserted into the database.
InputStream r4 = new ByteArrayInputStream(INSERT[3]);
ps.setBinaryStream(1, r4, INSERT[3].length - 5);
try {
ps.executeUpdate();
fail("Insert should have failed, stream too long");
} catch (SQLException sqle) {
// The states are different between client and embedded.
assertSQLState(usingEmbedded() ? "XSDA4" : "XN015", sqle);
if (rollbackOnError) {
rollback();
}
}
InputStream r5 = new ByteArrayInputStream(INSERT[4]);
ps.setBinaryStream(1, r5, INSERT[4].length + 5);
try {
ps.executeUpdate();
fail("Insert should have failed, stream too short");
} catch (SQLException sqle) {
// The states are different between client and embedded.
assertSQLState(usingEmbedded() ? "XSDA4" : "XN017", sqle);
if (rollbackOnError) {
rollback();
}
}
// The errors above should have statement severity. Insert the last
// two rows and make sure the transaction commits.
for (int i=5; i < INSERT.length; i++) {
ps.setBytes(1, INSERT[i]);
assertEquals(1, ps.executeUpdate());
}
if (!autoCommit) {
commit();
}
// Select data in the table, compare to MASTER
ResultSet rs = stmt.executeQuery("select * from t2017_binary");
JDBC.assertFullResultSet(rs, MASTER);
}
/**
* WARNING: This reader is not a general purpose reader!!!
* <p>
* Reader thrown an exception when a certain amount of characters has been
* returned (or is about to be returned).
*/
public static class FailingReader
extends Reader {
private final LoopingAlphabetReader in;
private final long failAtPos;
private long pos;
/**
* Creates a new failing reader.
*
* @param length the total length of the source
* @param failAtPos the position to fail at (specifying zero or a
* negative value causes an exception on the first read request)
*/
public FailingReader(long length, long failAtPos) {
this.failAtPos = failAtPos;
this.in = new LoopingAlphabetReader(length);
}
public int read()
throws IOException {
// If we failed once, just keep failing on subsequent requests.
pos++;
int ret = in.read();
if (pos >= failAtPos) {
throw new IOException("forced exception");
}
return ret;
}
public int read(char[] cbuf, int off, int len)
throws IOException {
// If we failed once, just keep failing on subsequent requests.
// Try to return some valid data before failing.
if (pos == 0 && failAtPos > 1) {
len = (int)Math.min(failAtPos -1, len);
}
int ret = in.read(cbuf, off, len);
if (ret != -1) {
pos += ret;
}
if (pos >= failAtPos) {
throw new IOException("forced exception");
}
return ret;
}
public void close() {
in.close();
}
}
/**
* Helper method returning the default bytes used for testing binary data.
*
* @return A byte array with byte arrays ("rows").
*/
public static byte[][] generateDefaultInsert() {
try {
byte[][] INSERT = new byte[][] {
"This is row 1".getBytes(UTF8),
"This is row 2".getBytes(UTF8),
"This is row 3".getBytes(UTF8),
"This is row 4, a bit too long".getBytes(UTF8),
"This is row 5, a bit too short".getBytes(UTF8),
"This is row 6".getBytes(UTF8),
"This is row 7".getBytes(UTF8),
};
return INSERT;
} catch (UnsupportedEncodingException uuee) {
// Just return null, should never happen if UTF-8 is used.
return null;
}
}
/**
* Helper method returning a string array with the expected values from the
* database (based on the values inserted).
* <p>
* The purpose of this method is to convert from byte[] to String (as
* done by {@code ResultSet.getString()} when invoked on a column with
* binary data).
*
* @param insert the data inserted
* @param excludes the rows that is expected to fail, will be ignored
* @return A string array with expected strings.
*/
public static String[][] generateMaster(byte[][] insert, int[] excludes) {
Arrays.sort(excludes);
int exIx = 0;
String[][] res = new String[insert.length - excludes.length][];
int resIx = 0;
for (int i=0; i < insert.length; i++) {
if (i == excludes[exIx]) {
if (exIx < excludes.length -1) {
exIx++;
}
continue;
}
String str = new BigInteger(1, insert[i]).toString(16);
// Pad if there are missing nulls.
while (str.length() < insert[i].length *2) {
str = "0" + str;
}
res[resIx++] = new String[] {str};
}
return res;
}
}
|