13
13
import java .sql .SQLException ;
14
14
import java .sql .Statement ;
15
15
import java .util .ArrayList ;
16
+ import java .util .Scanner ;
16
17
import java .util .stream .IntStream ;
17
18
18
19
import org .junit .jupiter .api .BeforeEach ;
@@ -48,10 +49,8 @@ private String getRandomString(int length, String validCharacters) {
48
49
return saltStr ;
49
50
}
50
51
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 ) {
55
54
return s .hasNext () ? s .next () : "" ;
56
55
}
57
56
@@ -99,18 +98,21 @@ public void testLengthAfterStream() throws SQLException, IOException {
99
98
try (Connection conn = getConnection ();) {
100
99
try (Statement stmt = conn .createStatement ()) {
101
100
TestUtils .dropTableIfExists (tableName , stmt );
102
- createLobTable (stmt , tableName , Constants .LOB .CLOB );
103
101
ArrayList <String > lob_data = createRandomStringArray (Constants .LOB .CLOB );
102
+
103
+ createLobTable (stmt , tableName , Constants .LOB .CLOB );
104
104
insertData (conn , tableName , lob_data );
105
105
106
106
try (ResultSet rs = stmt .executeQuery ("SELECT * FROM [" + tableName + "] ORDER BY id ASC" )) {
107
107
while (rs .next ()) {
108
108
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
+ }
114
116
}
115
117
}
116
118
} finally {
@@ -123,61 +125,72 @@ public void testLengthAfterStream() throws SQLException, IOException {
123
125
124
126
@ Test
125
127
@ DisplayName ("testClobsVarcharASCII" )
126
- public void testClobsVarcharASCII () throws SQLException {
128
+ @ SuppressWarnings ("resource" )
129
+ public void testClobsVarcharASCII () throws SQLException , IOException {
127
130
try (Connection conn = getConnection ()) {
128
131
try (Statement stmt = conn .createStatement ()) {
129
132
TestUtils .dropTableIfExists (tableName , stmt );
130
- createLobTable ( stmt , tableName , Constants . LOB . CLOB );
133
+
131
134
ArrayList <String > lob_data = createRandomStringArray (Constants .LOB .CLOB );
135
+ ArrayList <String > recievedDataFromServer = new ArrayList <>();
136
+
137
+ createLobTable (stmt , tableName , Constants .LOB .CLOB );
132
138
insertData (conn , tableName , lob_data );
133
139
134
- ArrayList <Clob > lobsFromServer = new ArrayList <>();
135
140
try (ResultSet rs = stmt .executeQuery ("SELECT * FROM [" + tableName + "] ORDER BY id ASC" )) {
136
141
while (rs .next ()) {
137
142
int index = rs .getInt (1 );
138
143
Clob c = rs .getClob (2 );
139
144
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
143
158
}
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 ();
152
159
}
153
160
} finally {
154
161
try (Statement stmt = conn .createStatement ()) {
155
162
TestUtils .dropTableIfExists (tableName , stmt );
156
163
}
157
164
}
158
165
}
166
+
159
167
}
160
168
169
+ @ SuppressWarnings ("resource" )
161
170
@ Test
162
171
@ DisplayName ("testNClobsNVarcharASCII" )
163
172
public void testNClobsVarcharASCII () throws SQLException , IOException {
164
173
try (Connection conn = getConnection ()) {
165
174
try (Statement stmt = conn .createStatement ()) {
166
175
TestUtils .dropTableIfExists (tableName , stmt );
167
- createLobTable (stmt , tableName , Constants .LOB .NCLOB );
168
176
// Testing AsciiStream, use Clob string set or characters will be converted to '?'
169
177
ArrayList <String > lob_data = createRandomStringArray (Constants .LOB .CLOB );
178
+
179
+ createLobTable (stmt , tableName , Constants .LOB .NCLOB );
170
180
insertData (conn , tableName , lob_data );
171
181
172
182
try (ResultSet rs = stmt .executeQuery ("SELECT * FROM [" + tableName + "] ORDER BY id ASC" )) {
173
183
while (rs .next ()) {
174
184
int index = rs .getInt (1 );
175
185
NClob c = rs .getNClob (2 );
176
186
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
+ }
181
194
}
182
195
}
183
196
} finally {
@@ -194,28 +207,29 @@ public void testClobsVarcharCHARA() throws SQLException, IOException {
194
207
try (Connection conn = getConnection ()) {
195
208
try (Statement stmt = conn .createStatement ()) {
196
209
TestUtils .dropTableIfExists (tableName , stmt );
197
- createLobTable ( stmt , tableName , Constants . LOB . CLOB );
210
+
198
211
ArrayList <String > lob_data = createRandomStringArray (Constants .LOB .CLOB );
212
+ ArrayList <String > receivedDataFromServer = new ArrayList <>();
213
+
214
+ createLobTable (stmt , tableName , Constants .LOB .CLOB );
199
215
insertData (conn , tableName , lob_data );
200
216
201
- ArrayList <Clob > lobsFromServer = new ArrayList <>();
202
217
try (ResultSet rs = stmt .executeQuery ("SELECT * FROM [" + tableName + "] ORDER BY id ASC" )) {
203
218
while (rs .next ()) {
204
219
int index = rs .getInt (1 );
205
220
Clob c = rs .getClob (2 );
206
221
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
+ }
210
228
}
211
229
}
212
230
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
219
233
}
220
234
} finally {
221
235
try (Statement stmt = conn .createStatement ()) {
@@ -231,28 +245,29 @@ public void testNClobsVarcharCHARA() throws SQLException, IOException {
231
245
try (Connection conn = getConnection ()) {
232
246
try (Statement stmt = conn .createStatement ()) {
233
247
TestUtils .dropTableIfExists (tableName , stmt );
234
- createLobTable ( stmt , tableName , Constants . LOB . NCLOB );
248
+
235
249
ArrayList <String > lob_data = createRandomStringArray (Constants .LOB .NCLOB );
250
+ ArrayList <String > receivedDataFromServer = new ArrayList <>();
251
+
252
+ createLobTable (stmt , tableName , Constants .LOB .NCLOB );
236
253
insertData (conn , tableName , lob_data );
237
254
238
- ArrayList <NClob > lobsFromServer = new ArrayList <>();
239
255
try (ResultSet rs = stmt .executeQuery ("SELECT * FROM [" + tableName + "] ORDER BY id ASC" )) {
240
256
while (rs .next ()) {
241
257
int index = rs .getInt (1 );
242
258
NClob c = rs .getNClob (2 );
243
259
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
+ }
247
266
}
248
267
}
249
268
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
256
271
}
257
272
} finally {
258
273
try (Statement stmt = conn .createStatement ()) {
0 commit comments