Skip to content

Commit

Permalink
Additional tests
Browse files Browse the repository at this point in the history
added tests which specifically test blob streams and behavior after the RS is closed.
  • Loading branch information
rene-ye committed Feb 22, 2018
1 parent 3ba5f1b commit 9a1300e
Showing 1 changed file with 86 additions and 0 deletions.
86 changes: 86 additions & 0 deletions src/test/java/com/microsoft/sqlserver/jdbc/unit/lobs/lobsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,92 @@ public void testUpdatorClob() throws Exception {
String types[] = {"varchar(max)"};
testUpdateLobs(types, Clob.class);
}

@Test
@DisplayName("readBlobStreamAfterClosingRS")
public void readBlobStreamAfterClosingRS() throws Exception {
String types[] = {"varbinary(max)"};
table = createTable(table, types, false); // create empty table
int size = 10000;

byte[] data = new byte[size];
ThreadLocalRandom.current().nextBytes(data);

Blob blob = null;
InputStream stream = null;
PreparedStatement ps = conn.prepareStatement("INSERT INTO " + table.getEscapedTableName() + " VALUES(?)");
blob = conn.createBlob();
blob.setBytes(1, data);
ps.setBlob(1, blob);
ps.executeUpdate();

byte[] chunk = new byte[size];
ResultSet rs = stmt.executeQuery("select * from " + table.getEscapedTableName());
rs.next();

blob = rs.getBlob(1);
stream = blob.getBinaryStream();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int read = 0;
while ((read = stream.read(chunk)) > 0)
buffer.write(chunk, 0, read);
assertEquals(chunk.length, size);
rs.close();
stream = blob.getBinaryStream();
buffer = new ByteArrayOutputStream();
read = 0;
while ((read = stream.read(chunk)) > 0)
buffer.write(chunk, 0, read);
assertEquals(chunk.length, size);

if (null != blob)
blob.free();
dropTables(table);
}

@Test
@DisplayName("readMultipleBlobStreamsThenCloseRS")
public void readMultipleBlobStreamsThenCloseRS() throws Exception {
String types[] = {"varbinary(max)"};
table = createTable(table, types, false);
int size = 10000;

byte[] data = new byte[size];
Blob[] blobs = {null, null, null};
InputStream stream = null;
for (int i = 0; i < 3; i++)
{
PreparedStatement ps = conn.prepareStatement("INSERT INTO " + table.getEscapedTableName() + " VALUES(?)");
blobs[i] = conn.createBlob();
ThreadLocalRandom.current().nextBytes(data);
blobs[i].setBytes(1, data);
ps.setBlob(1, blobs[i]);
ps.executeUpdate();
}
byte[] chunk = new byte[size];
ResultSet rs = stmt.executeQuery("select * from " + table.getEscapedTableName());
for (int i = 0; i < 3; i++)
{
rs.next();
blobs[i] = rs.getBlob(1);
stream = blobs[i].getBinaryStream();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int read = 0;
while ((read = stream.read(chunk)) > 0)
buffer.write(chunk, 0, read);
assertEquals(chunk.length, size);
}
rs.close();
for (int i = 0; i < 3; i++)
{
stream = blobs[i].getBinaryStream();
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
int read = 0;
while ((read = stream.read(chunk)) > 0)
buffer.write(chunk, 0, read);
assertEquals(chunk.length, size);
}
}

private void testUpdateLobs(String types[],
Class lobClass) throws Exception {
Expand Down

0 comments on commit 9a1300e

Please sign in to comment.