From 9a1300ed1bc1036771e0f4b9a3f8c07007ee8a67 Mon Sep 17 00:00:00 2001 From: rene-ye Date: Thu, 22 Feb 2018 13:18:47 -0800 Subject: [PATCH] Additional tests added tests which specifically test blob streams and behavior after the RS is closed. --- .../sqlserver/jdbc/unit/lobs/lobsTest.java | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/unit/lobs/lobsTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/unit/lobs/lobsTest.java index af9499fa9..26a4ec266 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/unit/lobs/lobsTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/unit/lobs/lobsTest.java @@ -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 {