1
1
/*
2
- * Copyright (c) 2017, 2022 Oracle and/or its affiliates. All rights reserved.
2
+ * Copyright (c) 2017, 2024 Oracle and/or its affiliates. All rights reserved.
3
3
*
4
4
* This program and the accompanying materials are made available under the
5
5
* terms of the Eclipse Public License v. 2.0, which is available at
23
23
import java .io .ByteArrayInputStream ;
24
24
import java .io .ByteArrayOutputStream ;
25
25
import java .io .IOException ;
26
+ import java .io .InputStream ;
26
27
import java .io .InputStreamReader ;
28
+ import java .io .OutputStream ;
27
29
import java .io .OutputStreamWriter ;
28
30
import java .nio .charset .StandardCharsets ;
29
31
37
39
import static org .hamcrest .Matchers .is ;
38
40
import static org .hamcrest .Matchers .matchesPattern ;
39
41
42
+
40
43
/**
41
44
* @test
42
45
* @sources JsonbTest.java
@@ -131,9 +134,11 @@ public void testFromJsonReaderType() throws IOException {
131
134
@ Test
132
135
public void testFromJsonStreamClass () throws IOException {
133
136
try (ByteArrayInputStream stream = new ByteArrayInputStream (TEST_JSON_BYTE )) {
134
- SimpleContainer unmarshalledObject = jsonb .fromJson (stream , SimpleContainer .class );
137
+ CloseRememberingInputStream rememberingStream = new CloseRememberingInputStream (stream );
138
+ SimpleContainer unmarshalledObject = jsonb .fromJson (rememberingStream , SimpleContainer .class );
135
139
assertThat ("Failed to unmarshal using Jsonb.fromJson method with InputStream and Class arguments." ,
136
140
unmarshalledObject .getInstance (), is (TEST_STRING ));
141
+ assertThat ("Failed to close stream upon a successful completion" , rememberingStream .isCloseCalled ());
137
142
}
138
143
}
139
144
@@ -148,10 +153,12 @@ public void testFromJsonStreamClass() throws IOException {
148
153
@ Test
149
154
public void testFromJsonStreamType () throws IOException {
150
155
try (ByteArrayInputStream stream = new ByteArrayInputStream (TEST_JSON_BYTE )) {
156
+ CloseRememberingInputStream rememberingStream = new CloseRememberingInputStream (stream );
151
157
SimpleContainer unmarshalledObject = jsonb
152
- .fromJson (stream , new SimpleContainer () { }.getClass ().getGenericSuperclass ());
158
+ .fromJson (rememberingStream , new SimpleContainer () { }.getClass ().getGenericSuperclass ());
153
159
assertThat ("Failed to unmarshal using Jsonb.fromJson method with InputStream and Type arguments." ,
154
160
unmarshalledObject .getInstance (), is (TEST_STRING ));
161
+ assertThat ("Failed to close stream upon a successful completion" , rememberingStream .isCloseCalled ());
155
162
}
156
163
}
157
164
@@ -234,10 +241,12 @@ public void testToJsonObjectTypeWriter() throws IOException {
234
241
@ Test
235
242
public void testToJsonObjectStream () throws IOException {
236
243
try (ByteArrayOutputStream stream = new ByteArrayOutputStream ()) {
237
- jsonb .toJson (new SimpleContainer (), stream );
238
- String jsonString = new String (stream .toByteArray (), StandardCharsets .UTF_8 );
244
+ CloseRememberingOutputStream rememberingStream = new CloseRememberingOutputStream (stream );
245
+ jsonb .toJson (new SimpleContainer (), rememberingStream );
246
+ String jsonString = new String (rememberingStream .toByteArray (), StandardCharsets .UTF_8 );
239
247
assertThat ("Failed to marshal using Jsonb.toJson method with Object and OutputStream arguments." ,
240
248
jsonString , matchesPattern (MATCHING_PATTERN ));
249
+ assertThat ("Failed to close stream upon a successful completion" , rememberingStream .isCloseCalled ());
241
250
}
242
251
}
243
252
@@ -252,10 +261,135 @@ public void testToJsonObjectStream() throws IOException {
252
261
@ Test
253
262
public void testToJsonObjectTypeStream () throws IOException {
254
263
try (ByteArrayOutputStream stream = new ByteArrayOutputStream ()) {
255
- jsonb .toJson (new SimpleContainer (), new SimpleContainer () { }.getClass ().getGenericSuperclass (), stream );
256
- String jsonString = new String (stream .toByteArray (), StandardCharsets .UTF_8 );
264
+ CloseRememberingOutputStream rememberingStream = new CloseRememberingOutputStream (stream );
265
+ jsonb .toJson (new SimpleContainer (), new SimpleContainer () { }.getClass ().getGenericSuperclass (), rememberingStream );
266
+ String jsonString = new String (rememberingStream .toByteArray (), StandardCharsets .UTF_8 );
257
267
assertThat ("Failed to marshal using Jsonb.toJson method with Object, Type and OutputStream arguments." ,
258
268
jsonString , matchesPattern (MATCHING_PATTERN ));
269
+ assertThat ("Failed to close stream upon a successful completion" , rememberingStream .isCloseCalled ());
270
+ }
271
+ }
272
+
273
+ static final class CloseRememberingOutputStream extends OutputStream {
274
+
275
+ private ByteArrayOutputStream delegate ;
276
+ private boolean closeCalled ;
277
+
278
+ CloseRememberingOutputStream (ByteArrayOutputStream delegate ) {
279
+ this .delegate = delegate ;
280
+ this .closeCalled = false ;
281
+ }
282
+
283
+ @ Override
284
+ public void close () throws IOException {
285
+ closeCalled = true ;
286
+ delegate .close ();
287
+ }
288
+
289
+ boolean isCloseCalled () {
290
+ return closeCalled ;
291
+ }
292
+
293
+ byte [] toByteArray () {
294
+ return delegate .toByteArray ();
295
+ }
296
+
297
+ @ Override
298
+ public void write (int b ) throws IOException {
299
+ delegate .write (b );
300
+ }
301
+
302
+ @ Override
303
+ public void write (byte [] b ) throws IOException {
304
+ delegate .write (b );
305
+ }
306
+
307
+ @ Override
308
+ public void write (byte [] b , int off , int len ) throws IOException {
309
+ delegate .write (b , off , len );
310
+ }
311
+
312
+ }
313
+
314
+ static final class CloseRememberingInputStream extends InputStream {
315
+
316
+ private InputStream delegate ;
317
+ private boolean closeCalled ;
318
+
319
+ CloseRememberingInputStream (InputStream delegate ) {
320
+ this .delegate = delegate ;
321
+ this .closeCalled = false ;
322
+ }
323
+
324
+ @ Override
325
+ public void close () throws IOException {
326
+ closeCalled = true ;
327
+ delegate .close ();
259
328
}
329
+
330
+ boolean isCloseCalled () {
331
+ return closeCalled ;
332
+ }
333
+
334
+ @ Override
335
+ public int read () throws IOException {
336
+ return delegate .read ();
337
+ }
338
+
339
+ public int read (byte [] b ) throws IOException {
340
+ return delegate .read (b );
341
+ }
342
+
343
+ @ Override
344
+ public int read (byte [] b , int off , int len ) throws IOException {
345
+ return delegate .read (b , off , len );
346
+ }
347
+
348
+ @ Override
349
+ public byte [] readAllBytes () throws IOException {
350
+ return delegate .readAllBytes ();
351
+ }
352
+
353
+ @ Override
354
+ public byte [] readNBytes (int len ) throws IOException {
355
+ return delegate .readNBytes (len );
356
+ }
357
+
358
+ @ Override
359
+ public int readNBytes (byte [] b , int off , int len ) throws IOException {
360
+ return delegate .readNBytes (b , off , len );
361
+ }
362
+
363
+ @ Override
364
+ public long skip (long n ) throws IOException {
365
+ return delegate .skip (n );
366
+ }
367
+
368
+ @ Override
369
+ public int available () throws IOException {
370
+ return delegate .available ();
371
+ }
372
+
373
+ @ Override
374
+ public void mark (int readlimit ) {
375
+ delegate .mark (readlimit );
376
+ }
377
+
378
+ @ Override
379
+ public void reset () throws IOException {
380
+ delegate .reset ();
381
+ }
382
+
383
+ @ Override
384
+ public boolean markSupported () {
385
+ return delegate .markSupported ();
386
+ }
387
+
388
+ @ Override
389
+ public long transferTo (OutputStream out ) throws IOException {
390
+ return delegate .transferTo (out );
391
+ }
392
+
393
+
260
394
}
261
395
}
0 commit comments