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
|
/*
*
* Derby - Class LobStreamsTest
*
* 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.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
import java.sql.Blob;
import java.sql.Clob;
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.LoopingAlphabetReader;
import org.apache.derbyTesting.functionTests.util.streams.LoopingAlphabetStream;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.Decorator;
import org.apache.derbyTesting.junit.JDBC;
import org.apache.derbyTesting.junit.TestConfiguration;
public class LobStreamsTest extends BaseJDBCTestCase {
int[] streamSize = new int[2];
private final String unicodeTestString =
"This is a test string containing a few non-ascii characters:\n " +
"\u00E6\u00F8\u00E5 and \u00C6\u00D8\u00C5 are used in " +
"norwegian: 'Bl\u00E5b\u00E6rsyltet\u00F8y' means 'blueberry jam'" +
", and tastes great on pancakes. =)";
{
streamSize[0] = 300000;
streamSize[1] = 10000;
}
/** Creates a new instance of LobStreamsTest */
public LobStreamsTest(String name) {
super(name);
}
/**
* Set up the connection to the database.
*/
public void setUp() throws Exception {
getConnection().setAutoCommit(false);
Statement stmt1 = createStatement();
stmt1.execute("create table testBlobX1 (" +
"a integer, " +
"b blob(300K), " +
"c clob(300K))");
stmt1.close();
byte[] b2 = new byte[1];
b2[0] = (byte)64;
String c2 = "c";
PreparedStatement stmt2 = prepareStatement(
"INSERT INTO testBlobX1(a, b, c) " +
"VALUES (?, ?, ?)");
stmt2.setInt(1, 1);
stmt2.setBytes(2, b2);
stmt2.setString(3, c2);
stmt2.execute();
stmt2.close();
}
/**
* Originally tested that the usage pattern {@code rs.getBlob().method()}
* didn't cause the underlying source stream to be closed too early. This
* behavior was forbidden, the test now checks that an exception is thrown.
* <p>
* Test description: Select from a BLOB column, access the BLOB using the
* pattern rs.getBlob(1).blobMethod() (note that we do not keep a reference
* to the Blob-object), provoke/invoke GC and finalization, and finally try
* to access the same BLOB again (through a different/another call to
* rs.getBlob(1)).
* <p>
* Note that the BLOB must be of a certain size (i.e. multiple pages), such
* that it is stored/accessed as a stream in store.
* <p>
* See DERBY-3844 and DERBY-4440.
*
* @throws Exception if something goes wrong
*/
public void testGettingBlobTwice()
throws Exception {
setAutoCommit(false);
// We need a Blob represented as a stream in store.
int length = 71*1024+7;
PreparedStatement ps =
prepareStatement("insert into testBlobX1(a,b) values (?,?)");
ps.setInt(1, 2);
ps.setBinaryStream(2, new LoopingAlphabetStream(length), length);
ps.executeUpdate();
ps.close();
// Get a result set with the Blob.
ps = prepareStatement("select b from testBlobX1 where a = ?");
ps.setInt(1, 2);
ResultSet rs = ps.executeQuery();
assertTrue(rs.next());
Blob b = rs.getBlob(1);
try {
// Get the length, but don't keep a reference to the Blob.
assertEquals(length, rs.getBlob(1).length());
fail("Getting the Blob the second time should have failed");
} catch (SQLException sqle) {
assertSQLState("XCL18", sqle);
}
// Increase the likelyhood of getting the finalizer run.
// Create some junk to fill up the heap, hopefully not JIT'ed away...
int size = 10*1024; // 10 K
byte[] bytes = null;
for (int i=0; i < 50; i++) {
bytes = new byte[size *(i +1)];
}
// For good measure...
System.gc();
System.runFinalization();
try {
Thread.sleep(100L);
} catch (InterruptedException ie) {
// No need to reset the interrupted flag here in the test.
}
// This will fail if the finalizer caused the source stream to be
// closed and the source page to be unlatched.
InputStream is = b.getBinaryStream();
while (is.read() != -1) {
// Keep on reading...
}
assertNotNull(bytes);
}
/**
* Tests that accessing the same Clob multiple times on a row results in
* an exception being thrown.
*
* @throws Exception if something goes wrong
*/
public void testGettingClobTwice()
throws SQLException {
// We need a few Clobs.
int length = 71*1024+7;
PreparedStatement ps =
prepareStatement("insert into testBlobX1(a,c) values (?,?)");
ps.setInt(1, 3);
ps.setCharacterStream(2, new LoopingAlphabetReader(length), length);
ps.executeUpdate();
ps.setInt(1, 4);
ps.setString(2, "short clob");
ps.executeUpdate();
ps.close();
// Get a result set with the Clobs.
final int clobCount = 2;
int count = 0;
ps = prepareStatement(
"select c from testBlobX1 where a >= ? and a <= ?");
ps.setInt(1, 3);
ps.setInt(2, 4);
ResultSet rs = ps.executeQuery();
assertTrue(rs.next());
do {
count++;
// First get a Clob.
Clob c = rs.getClob(1);
// Get a second Clob, which should fail.
try {
rs.getClob(1);
fail("Getting the Clob the second time should have failed");
} catch (SQLException sqle) {
assertSQLState("XCL18", sqle);
}
// Finally try to access the column as a stream.
try {
rs.getCharacterStream(1);
fail("Getting the Clob the third time should have failed");
} catch (SQLException sqle) {
assertSQLState("XCL18", sqle);
}
} while (rs.next());
rs.close();
assertEquals(clobCount, count);
}
/**
* Tests the BlobOutputStream.write(byte b[], int off, int len) method
**/
public void testBlobWrite3Param() throws Exception {
InputStream streamIn = new LoopingAlphabetStream(streamSize[0]);
assertTrue("FAIL -- file not found", streamIn != null);
PreparedStatement stmt3 = prepareStatement(
"SELECT b FROM testBlobX1 WHERE a = 1");
ResultSet rs3 = stmt3.executeQuery();
rs3.next();
Blob blob = rs3.getBlob(1);
assertTrue ("FAIL -- blob is NULL", (blob != null));
int count = 0;
byte[] buffer = new byte[1024];
OutputStream outstream = blob.setBinaryStream(1L);
while ((count = streamIn.read(buffer)) != -1) {
outstream.write(buffer, 0, count);
}
outstream.close();
streamIn.close();
PreparedStatement stmt4 = prepareStatement(
"UPDATE testBlobX1 SET b = ? WHERE a = 1");
stmt4.setBlob(1, blob);
stmt4.executeUpdate();
stmt4.close();
rs3.close();
rs3 = stmt3.executeQuery();
assertTrue("FAIL -- blob not found", rs3.next());
blob = rs3.getBlob(1);
long new_length = blob.length();
assertEquals("FAIL -- wrong blob length;",
streamSize[0], new_length);
// Check contents ...
InputStream fStream = new LoopingAlphabetStream(streamSize[0]);
InputStream lStream = blob.getBinaryStream();
assertTrue("FAIL - Blob and file contents do not match",
compareLob2File(fStream, lStream));
fStream.close();
lStream.close();
rs3.close();
stmt3.close();
}
/**
* Tests the BlobOutputStream.write(int b) method
**/
public void testBlobWrite1Param() throws Exception {
InputStream streamIn = new LoopingAlphabetStream(streamSize[1]);
PreparedStatement stmt3 = prepareStatement(
"SELECT b FROM testBlobX1 WHERE a = 1");
ResultSet rs3 = stmt3.executeQuery();
rs3.next();
Blob blob = rs3.getBlob(1);
assertTrue("FAIL -- blob is NULL", blob != null);
int buffer;
OutputStream outstream = blob.setBinaryStream(1L);
while ((buffer = streamIn.read()) != -1) {
outstream.write(buffer);
}
outstream.close();
streamIn.close();
PreparedStatement stmt4 = prepareStatement(
"UPDATE testBlobX1 SET b = ? WHERE a = 1");
stmt4.setBlob(1, blob);
stmt4.executeUpdate();
stmt4.close();
rs3.close();
rs3 = stmt3.executeQuery();
assertTrue("FAIL -- blob not found", rs3.next());
blob = rs3.getBlob(1);
long new_length = blob.length();
assertEquals("FAIL -- wrong blob length", streamSize[1], new_length);
// Check contents ...
InputStream fStream = new LoopingAlphabetStream(streamSize[1]);
InputStream lStream = blob.getBinaryStream();
assertTrue("FAIL - Blob and file contents do not match",
compareLob2File(fStream, lStream));
fStream.close();
lStream.close();
rs3.close();
stmt3.close();
}
/**
* Tests the ClobOutputStream.write(int b) method
**/
public void testClobAsciiWrite1Param() throws Exception
{
InputStream streamIn = new LoopingAlphabetStream(streamSize[1]);
PreparedStatement stmt3 = prepareStatement(
"SELECT c FROM testBlobX1 WHERE a = 1");
ResultSet rs3 = stmt3.executeQuery();
rs3.next();
Clob clob = rs3.getClob(1);
assertTrue("FAIL -- clob is NULL", clob != null);
int buffer;
OutputStream outstream = clob.setAsciiStream(1L);
while ((buffer = streamIn.read()) != -1) {
outstream.write(buffer);
}
outstream.close();
streamIn.close();
PreparedStatement stmt4 = prepareStatement(
"UPDATE testBlobX1 SET c = ? WHERE a = 1");
stmt4.setClob(1, clob);
stmt4.executeUpdate();
stmt4.close();
rs3.close();
rs3 = stmt3.executeQuery();
assertTrue("FAIL -- clob not found", rs3.next());
clob = rs3.getClob(1);
long new_length = clob.length();
assertEquals("FAIL -- wrong clob length", streamSize[1], new_length);
// Check contents ...
InputStream fStream = new LoopingAlphabetStream(streamSize[1]);
InputStream lStream = clob.getAsciiStream();
assertTrue("FAIL - Clob and file contents do not match", compareLob2File(fStream, lStream));
fStream.close();
lStream.close();
rs3.close();
stmt3.close();
}
/**
* Tests the ClobOutputStream.write(byte b[], int off, int len) method
**/
public void testClobAsciiWrite3Param() throws Exception {
InputStream streamIn = new LoopingAlphabetStream(streamSize[0]);
assertTrue("FAIL -- file not found", streamIn != null);
PreparedStatement stmt3 = prepareStatement(
"SELECT c FROM testBlobX1 WHERE a = 1");
ResultSet rs3 = stmt3.executeQuery();
rs3.next();
Clob clob = rs3.getClob(1);
assertTrue("FAIL -- clob is NULL", clob != null);
int count = 0;
byte[] buffer = new byte[1024];
OutputStream outstream = clob.setAsciiStream(1L);
while ((count = streamIn.read(buffer)) != -1) {
outstream.write(buffer, 0, count);
}
outstream.close();
streamIn.close();
PreparedStatement stmt4 = prepareStatement(
"UPDATE testBlobX1 SET c = ? WHERE a = 1");
stmt4.setClob(1, clob);
stmt4.executeUpdate();
stmt4.close();
rs3.close();
rs3 = stmt3.executeQuery();
assertTrue("FAIL -- clob not found", rs3.next());
clob = rs3.getClob(1);
long new_length = clob.length();
assertEquals("FAIL -- wrong clob length",
streamSize[0], new_length);
// Check contents ...
InputStream fStream = new LoopingAlphabetStream(streamSize[0]);
InputStream lStream = clob.getAsciiStream();
assertTrue("FAIL - Clob and file contents do not match",
compareLob2File(fStream, lStream));
fStream.close();
lStream.close();
rs3.close();
stmt3.close();
}
/**
* Tests the ClobWriter.write(char cbuf[], int off, int len) method
**/
public void testClobCharacterWrite3ParamChar() throws Exception
{
char[] testdata = unicodeTestString.toCharArray();
PreparedStatement stmt3 = prepareStatement(
"SELECT c FROM testBlobX1 WHERE a = 1");
ResultSet rs3 = stmt3.executeQuery();
rs3.next();
Clob clob = rs3.getClob(1);
assertTrue("FAIL -- clob is NULL", clob != null);
Writer clobWriter = clob.setCharacterStream(1L);
clobWriter.write(testdata, 0, testdata.length);
clobWriter.close();
PreparedStatement stmt4 = prepareStatement(
"UPDATE testBlobX1 SET c = ? WHERE a = 1");
stmt4.setClob(1, clob);
stmt4.executeUpdate();
stmt4.close();
rs3.close();
rs3 = stmt3.executeQuery();
assertTrue("FAIL -- clob not found", rs3.next());
clob = rs3.getClob(1);
long new_length = clob.length();
assertEquals("FAIL -- wrong clob length",
testdata.length, new_length);
// Check contents ...
Reader lStream = clob.getCharacterStream();
assertTrue("FAIL - Clob and buffer contents do not match",
compareClobReader2CharArray(testdata, lStream));
lStream.close();
rs3.close();
stmt3.close();
}
/**
* Tests the ClobWriter.write(String str, int off, int len) method
**/
public void testClobCharacterWrite3ParamString() throws Exception
{
PreparedStatement stmt3 = prepareStatement(
"SELECT c FROM testBlobX1 WHERE a = 1");
ResultSet rs3 = stmt3.executeQuery();
rs3.next();
Clob clob = rs3.getClob(1);
assertTrue("FAIL -- clob is NULL", clob != null);
Writer clobWriter = clob.setCharacterStream(1L);
clobWriter.write(unicodeTestString, 0, unicodeTestString.length());
clobWriter.close();
PreparedStatement stmt4 = prepareStatement(
"UPDATE testBlobX1 SET c = ? WHERE a = 1");
stmt4.setClob(1, clob);
stmt4.executeUpdate();
stmt4.close();
rs3.close();
rs3 = stmt3.executeQuery();
assertTrue("FAIL -- clob not found", rs3.next());
clob = rs3.getClob(1);
long new_length = clob.length();
assertEquals("FAIL -- wrong clob length", unicodeTestString.length(), new_length);
// Check contents ...
Reader lStream = clob.getCharacterStream();
assertTrue("FAIL - Clob and buffer contents do not match",
compareClobReader2CharArray(
unicodeTestString.toCharArray(),
lStream));
lStream.close();
rs3.close();
stmt3.close();
}
/**
* Tests the ClobWriter.write(String str) method
**/
public void testClobCharacterWrite1ParamString() throws Exception
{
PreparedStatement stmt3 = prepareStatement(
"SELECT c FROM testBlobX1 WHERE a = 1");
ResultSet rs3 = stmt3.executeQuery();
rs3.next();
Clob clob = rs3.getClob(1);
assertTrue("FAIL -- clob is NULL", clob != null);
Writer clobWriter = clob.setCharacterStream(1L);
clobWriter.write(unicodeTestString);
clobWriter.close();
PreparedStatement stmt4 = prepareStatement(
"UPDATE testBlobX1 SET c = ? WHERE a = 1");
stmt4.setClob(1, clob);
stmt4.executeUpdate();
stmt4.close();
rs3.close();
rs3 = stmt3.executeQuery();
assertTrue("FAIL -- clob not found", rs3.next());
clob = rs3.getClob(1);
long new_length = clob.length();
assertEquals("FAIL -- wrong clob length", unicodeTestString.length(), new_length);
// Check contents ...
Reader lStream = clob.getCharacterStream();
assertTrue("FAIL - Clob and buffer contents do not match",
compareClobReader2CharArray(
unicodeTestString.toCharArray(),
lStream));
lStream.close();
rs3.close();
stmt3.close();
}
/**
* Tests the ClobWriter.write(int c) method
**/
public void testClobCharacterWrite1Char() throws Exception
{
char testchar = 'a';
PreparedStatement stmt3 = prepareStatement(
"SELECT c FROM testBlobX1 WHERE a = 1");
ResultSet rs3 = stmt3.executeQuery();
rs3.next();
Clob clob = rs3.getClob(1);
assertTrue ("FAIL -- clob is NULL", clob != null);
Writer clobWriter = clob.setCharacterStream(1L);
clobWriter.write(testchar);
clobWriter.close();
PreparedStatement stmt4 = prepareStatement(
"UPDATE testBlobX1 SET c = ? WHERE a = 1");
stmt4.setClob(1, clob);
stmt4.executeUpdate();
stmt4.close();
rs3.close();
rs3 = stmt3.executeQuery();
assertTrue("FAIL -- clob not found", rs3.next());
clob = rs3.getClob(1);
long new_length = clob.length();
assertEquals("FAIL -- wrong clob length", 1, new_length);
// Check contents ...
Reader lStream = clob.getCharacterStream();
char clobchar = (char) lStream.read();
assertEquals("FAIL - fetched Clob and original contents do " +
"not match", testchar, clobchar);
lStream.close();
rs3.close();
stmt3.close();
}
/**
* Run with DerbyNetClient only.
* Embedded Clob/Blob.setXXXStream() methods are not implemented.
*/
public static Test suite() {
BaseTestSuite ts = new BaseTestSuite ("LobStreamsTest");
ts.addTest(TestConfiguration.defaultSuite (LobStreamsTest.class));
// JSR169 does not have support for encryption
if (JDBC.vmSupportsJDBC3()) {
BaseTestSuite encSuite =
new BaseTestSuite ("LobStreamsTest:encrypted");
encSuite.addTestSuite (LobStreamsTest.class);
ts.addTest(Decorator.encryptedDatabase (encSuite));
}
return ts;
}
//method to ensure that buffer is filled if there is any data in stream
private int readBytesFromStream (byte [] b, InputStream is)
throws IOException {
int read = 0;
while (read < b.length) {
int ret = is.read (b, read, b.length - read);
if (ret < 0) {
if (read == 0) {
return ret;
}
else {
break;
}
}
read += ret;
}
return read;
}
private boolean compareLob2File(
InputStream fStream,
InputStream lStream) throws Exception
{
byte[] fByte = new byte[1024];
byte[] lByte = new byte[1024];
int lLength = 0, fLength = 0;
String fString, lString;
do {
fLength = readBytesFromStream (fByte, fStream);
lLength = readBytesFromStream (lByte, lStream);
if (!java.util.Arrays.equals(fByte, lByte))
return false;
} while (fLength > 0 && lLength > 0);
fStream.close();
lStream.close();
return true;
}
private boolean compareClobReader2CharArray(
char[] cArray,
Reader charReader) throws Exception
{
char[] clobChars = new char[cArray.length];
int readChars = 0;
int totalCharsRead = 0;
do {
readChars = charReader.read(clobChars, totalCharsRead, cArray.length - totalCharsRead);
if (readChars != -1)
totalCharsRead += readChars;
} while (readChars != -1 && totalCharsRead < cArray.length);
charReader.close();
if (!java.util.Arrays.equals(cArray, clobChars))
return false;
return true;
}
}
|