Skip to content

Commit caddd83

Browse files
Tests | Fix LobsStreamingTest to close all streams and resources appropriately (#1019)
1 parent a64e479 commit caddd83

File tree

1 file changed

+66
-51
lines changed

1 file changed

+66
-51
lines changed

src/test/java/com/microsoft/sqlserver/jdbc/unit/lobs/LobsStreamingTest.java

+66-51
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import java.sql.SQLException;
1414
import java.sql.Statement;
1515
import java.util.ArrayList;
16+
import java.util.Scanner;
1617
import java.util.stream.IntStream;
1718

1819
import org.junit.jupiter.api.BeforeEach;
@@ -48,10 +49,8 @@ private String getRandomString(int length, String validCharacters) {
4849
return saltStr;
4950
}
5051

51-
// closing the scanner closes the Inputstream, and the driver needs the stream to fill LoBs
52-
@SuppressWarnings("resource")
53-
private String getStringFromInputStream(InputStream is) {
54-
java.util.Scanner s = new java.util.Scanner(is, java.nio.charset.StandardCharsets.US_ASCII).useDelimiter("\\A");
52+
// closing the scanner closes the InputStream, and the driver needs the stream to fill LoBs
53+
private String getStringFromInputStream(InputStream is, Scanner s) {
5554
return s.hasNext() ? s.next() : "";
5655
}
5756

@@ -99,18 +98,21 @@ public void testLengthAfterStream() throws SQLException, IOException {
9998
try (Connection conn = getConnection();) {
10099
try (Statement stmt = conn.createStatement()) {
101100
TestUtils.dropTableIfExists(tableName, stmt);
102-
createLobTable(stmt, tableName, Constants.LOB.CLOB);
103101
ArrayList<String> lob_data = createRandomStringArray(Constants.LOB.CLOB);
102+
103+
createLobTable(stmt, tableName, Constants.LOB.CLOB);
104104
insertData(conn, tableName, lob_data);
105105

106106
try (ResultSet rs = stmt.executeQuery("SELECT * FROM [" + tableName + "] ORDER BY id ASC")) {
107107
while (rs.next()) {
108108
Clob c = rs.getClob(2);
109-
Reader r = c.getCharacterStream();
110-
long clobLength = c.length();
111-
String received = getStringFromReader(r, clobLength);// streaming string
112-
c.free();
113-
assertEquals(lob_data.get(rs.getInt(1)), received);// compare streamed string to initial string
109+
try (Reader r = c.getCharacterStream()) {
110+
long clobLength = c.length();
111+
String received = getStringFromReader(r, clobLength);// streaming string
112+
c.free();
113+
assertEquals(lob_data.get(rs.getInt(1)), received);// compare streamed string to initial
114+
// string
115+
}
114116
}
115117
}
116118
} finally {
@@ -123,61 +125,72 @@ public void testLengthAfterStream() throws SQLException, IOException {
123125

124126
@Test
125127
@DisplayName("testClobsVarcharASCII")
126-
public void testClobsVarcharASCII() throws SQLException {
128+
@SuppressWarnings("resource")
129+
public void testClobsVarcharASCII() throws SQLException, IOException {
127130
try (Connection conn = getConnection()) {
128131
try (Statement stmt = conn.createStatement()) {
129132
TestUtils.dropTableIfExists(tableName, stmt);
130-
createLobTable(stmt, tableName, Constants.LOB.CLOB);
133+
131134
ArrayList<String> lob_data = createRandomStringArray(Constants.LOB.CLOB);
135+
ArrayList<String> recievedDataFromServer = new ArrayList<>();
136+
137+
createLobTable(stmt, tableName, Constants.LOB.CLOB);
132138
insertData(conn, tableName, lob_data);
133139

134-
ArrayList<Clob> lobsFromServer = new ArrayList<>();
135140
try (ResultSet rs = stmt.executeQuery("SELECT * FROM [" + tableName + "] ORDER BY id ASC")) {
136141
while (rs.next()) {
137142
int index = rs.getInt(1);
138143
Clob c = rs.getClob(2);
139144
assertEquals(c.length(), lob_data.get(index).length());
140-
lobsFromServer.add(c);
141-
String received = getStringFromInputStream(c.getAsciiStream());// streaming string
142-
assertEquals(lob_data.get(index), received);// compare streamed string to initial string
145+
try (InputStream is = c.getAsciiStream();
146+
Scanner s = new Scanner(is, java.nio.charset.StandardCharsets.US_ASCII)
147+
.useDelimiter("\\A")) {
148+
String received = getStringFromInputStream(is, s);// streaming string
149+
assertEquals(lob_data.get(index), received);// compare streamed string to initial string
150+
c.free();
151+
recievedDataFromServer.add(received);
152+
}
153+
}
154+
for (int i = 0; i < lob_data.size(); i++) {
155+
assertEquals(recievedDataFromServer.get(i), lob_data.get(i));// compare static string to
156+
// streamed
157+
// string
143158
}
144-
}
145-
for (int i = 0; i < lob_data.size(); i++) {
146-
String received = getStringFromInputStream(lobsFromServer.get(i).getAsciiStream());// non-streaming
147-
// string
148-
assertEquals(received, lob_data.get(i));// compare static string to streamed string
149-
}
150-
for (Clob c : lobsFromServer) {
151-
c.free();
152159
}
153160
} finally {
154161
try (Statement stmt = conn.createStatement()) {
155162
TestUtils.dropTableIfExists(tableName, stmt);
156163
}
157164
}
158165
}
166+
159167
}
160168

169+
@SuppressWarnings("resource")
161170
@Test
162171
@DisplayName("testNClobsNVarcharASCII")
163172
public void testNClobsVarcharASCII() throws SQLException, IOException {
164173
try (Connection conn = getConnection()) {
165174
try (Statement stmt = conn.createStatement()) {
166175
TestUtils.dropTableIfExists(tableName, stmt);
167-
createLobTable(stmt, tableName, Constants.LOB.NCLOB);
168176
// Testing AsciiStream, use Clob string set or characters will be converted to '?'
169177
ArrayList<String> lob_data = createRandomStringArray(Constants.LOB.CLOB);
178+
179+
createLobTable(stmt, tableName, Constants.LOB.NCLOB);
170180
insertData(conn, tableName, lob_data);
171181

172182
try (ResultSet rs = stmt.executeQuery("SELECT * FROM [" + tableName + "] ORDER BY id ASC")) {
173183
while (rs.next()) {
174184
int index = rs.getInt(1);
175185
NClob c = rs.getNClob(2);
176186
assertEquals(c.length(), lob_data.get(index).length());
177-
String received = getStringFromInputStream(c.getAsciiStream());// NClob AsciiStream is never
178-
// streamed
179-
c.free();
180-
assertEquals(lob_data.get(index), received);// compare string to initial string
187+
try (InputStream is = c.getAsciiStream();
188+
Scanner s = new Scanner(is, java.nio.charset.StandardCharsets.US_ASCII)
189+
.useDelimiter("\\A")) {
190+
String received = getStringFromInputStream(is, s);// NClob AsciiStream is never streamed
191+
c.free();
192+
assertEquals(lob_data.get(index), received);// compare string to initial string
193+
}
181194
}
182195
}
183196
} finally {
@@ -194,28 +207,29 @@ public void testClobsVarcharCHARA() throws SQLException, IOException {
194207
try (Connection conn = getConnection()) {
195208
try (Statement stmt = conn.createStatement()) {
196209
TestUtils.dropTableIfExists(tableName, stmt);
197-
createLobTable(stmt, tableName, Constants.LOB.CLOB);
210+
198211
ArrayList<String> lob_data = createRandomStringArray(Constants.LOB.CLOB);
212+
ArrayList<String> receivedDataFromServer = new ArrayList<>();
213+
214+
createLobTable(stmt, tableName, Constants.LOB.CLOB);
199215
insertData(conn, tableName, lob_data);
200216

201-
ArrayList<Clob> lobsFromServer = new ArrayList<>();
202217
try (ResultSet rs = stmt.executeQuery("SELECT * FROM [" + tableName + "] ORDER BY id ASC")) {
203218
while (rs.next()) {
204219
int index = rs.getInt(1);
205220
Clob c = rs.getClob(2);
206221
assertEquals(c.length(), lob_data.get(index).length());
207-
lobsFromServer.add(c);
208-
String received = getStringFromReader(c.getCharacterStream(), c.length());// streaming string
209-
assertEquals(lob_data.get(index), received);// compare streamed string to initial string
222+
try (Reader reader = c.getCharacterStream()) {
223+
String received = getStringFromReader(reader, c.length());// streaming string
224+
receivedDataFromServer.add(received);
225+
assertEquals(lob_data.get(index), received);// compare streamed string to initial string
226+
c.free();
227+
}
210228
}
211229
}
212230
for (int i = 0; i < lob_data.size(); i++) {
213-
String received = getStringFromReader(lobsFromServer.get(i).getCharacterStream(),
214-
lobsFromServer.get(i).length());// non-streaming string
215-
assertEquals(received, lob_data.get(i));// compare static string to streamed string
216-
}
217-
for (Clob c : lobsFromServer) {
218-
c.free();
231+
assertEquals(receivedDataFromServer.get(i), lob_data.get(i));// compare static string to streamed
232+
// string
219233
}
220234
} finally {
221235
try (Statement stmt = conn.createStatement()) {
@@ -231,28 +245,29 @@ public void testNClobsVarcharCHARA() throws SQLException, IOException {
231245
try (Connection conn = getConnection()) {
232246
try (Statement stmt = conn.createStatement()) {
233247
TestUtils.dropTableIfExists(tableName, stmt);
234-
createLobTable(stmt, tableName, Constants.LOB.NCLOB);
248+
235249
ArrayList<String> lob_data = createRandomStringArray(Constants.LOB.NCLOB);
250+
ArrayList<String> receivedDataFromServer = new ArrayList<>();
251+
252+
createLobTable(stmt, tableName, Constants.LOB.NCLOB);
236253
insertData(conn, tableName, lob_data);
237254

238-
ArrayList<NClob> lobsFromServer = new ArrayList<>();
239255
try (ResultSet rs = stmt.executeQuery("SELECT * FROM [" + tableName + "] ORDER BY id ASC")) {
240256
while (rs.next()) {
241257
int index = rs.getInt(1);
242258
NClob c = rs.getNClob(2);
243259
assertEquals(c.length(), lob_data.get(index).length());
244-
lobsFromServer.add(c);
245-
String received = getStringFromReader(c.getCharacterStream(), c.length());// streaming string
246-
assertEquals(lob_data.get(index), received);// compare streamed string to initial string
260+
try (Reader reader = c.getCharacterStream()) {
261+
String received = getStringFromReader(reader, c.length());// streaming string
262+
receivedDataFromServer.add(received);
263+
assertEquals(lob_data.get(index), received);// compare streamed string to initial string
264+
c.free();
265+
}
247266
}
248267
}
249268
for (int i = 0; i < lob_data.size(); i++) {
250-
String received = getStringFromReader(lobsFromServer.get(i).getCharacterStream(),
251-
lobsFromServer.get(i).length());// non-streaming string
252-
assertEquals(received, lob_data.get(i));// compare static string to streamed string
253-
}
254-
for (Clob c : lobsFromServer) {
255-
c.free();
269+
assertEquals(receivedDataFromServer.get(i), lob_data.get(i));// compare static string to streamed
270+
// string
256271
}
257272
} finally {
258273
try (Statement stmt = conn.createStatement()) {

0 commit comments

Comments
 (0)