11/*
2- * Copyright (c) 2017, 2022 Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2017, 2023 Oracle and/or its affiliates. All rights reserved.
33 *
44 * This program and the accompanying materials are made available under the
55 * terms of the Eclipse Public License v. 2.0, which is available at
2323import java .io .ByteArrayInputStream ;
2424import java .io .ByteArrayOutputStream ;
2525import java .io .IOException ;
26+ import java .io .InputStream ;
2627import java .io .InputStreamReader ;
28+ import java .io .OutputStream ;
2729import java .io .OutputStreamWriter ;
2830import java .nio .charset .StandardCharsets ;
2931
3739import static org .hamcrest .Matchers .is ;
3840import static org .hamcrest .Matchers .matchesPattern ;
3941
42+
4043/**
4144 * @test
4245 * @sources JsonbTest.java
@@ -131,9 +134,11 @@ public void testFromJsonReaderType() throws IOException {
131134 @ Test
132135 public void testFromJsonStreamClass () throws IOException {
133136 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 );
135139 assertThat ("Failed to unmarshal using Jsonb.fromJson method with InputStream and Class arguments." ,
136140 unmarshalledObject .getInstance (), is (TEST_STRING ));
141+ assertThat ("Failed to close stream upon a successful completion" , rememberingStream .isCloseCalled ());
137142 }
138143 }
139144
@@ -148,10 +153,12 @@ public void testFromJsonStreamClass() throws IOException {
148153 @ Test
149154 public void testFromJsonStreamType () throws IOException {
150155 try (ByteArrayInputStream stream = new ByteArrayInputStream (TEST_JSON_BYTE )) {
156+ CloseRememberingInputStream rememberingStream = new CloseRememberingInputStream (stream );
151157 SimpleContainer unmarshalledObject = jsonb
152- .fromJson (stream , new SimpleContainer () { }.getClass ().getGenericSuperclass ());
158+ .fromJson (rememberingStream , new SimpleContainer () { }.getClass ().getGenericSuperclass ());
153159 assertThat ("Failed to unmarshal using Jsonb.fromJson method with InputStream and Type arguments." ,
154160 unmarshalledObject .getInstance (), is (TEST_STRING ));
161+ assertThat ("Failed to close stream upon a successful completion" , rememberingStream .isCloseCalled ());
155162 }
156163 }
157164
@@ -234,10 +241,12 @@ public void testToJsonObjectTypeWriter() throws IOException {
234241 @ Test
235242 public void testToJsonObjectStream () throws IOException {
236243 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 );
239247 assertThat ("Failed to marshal using Jsonb.toJson method with Object and OutputStream arguments." ,
240248 jsonString , matchesPattern (MATCHING_PATTERN ));
249+ assertThat ("Failed to close stream upon a successful completion" , rememberingStream .isCloseCalled ());
241250 }
242251 }
243252
@@ -252,10 +261,135 @@ public void testToJsonObjectStream() throws IOException {
252261 @ Test
253262 public void testToJsonObjectTypeStream () throws IOException {
254263 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 );
257267 assertThat ("Failed to marshal using Jsonb.toJson method with Object, Type and OutputStream arguments." ,
258268 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 ();
259328 }
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+
260394 }
261395}
0 commit comments