Skip to content

Commit

Permalink
Verify Jsonb calls close methods
Browse files Browse the repository at this point in the history
The stream based methods in Jsonb specify close being called upon
successful completion. Add tests that verify an implementation calls
these methods.
  • Loading branch information
marschall committed Dec 27, 2024
1 parent f3a2332 commit a3c85ec
Showing 1 changed file with 141 additions and 7 deletions.
148 changes: 141 additions & 7 deletions tck/src/main/java/ee/jakarta/tck/json/bind/api/jsonb/JsonbTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2017, 2022 Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2017, 2024 Oracle and/or its affiliates. All rights reserved.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License v. 2.0, which is available at
Expand All @@ -23,7 +23,9 @@
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.StandardCharsets;

Expand All @@ -37,6 +39,7 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.matchesPattern;


/**
* @test
* @sources JsonbTest.java
Expand Down Expand Up @@ -131,9 +134,11 @@ public void testFromJsonReaderType() throws IOException {
@Test
public void testFromJsonStreamClass() throws IOException {
try (ByteArrayInputStream stream = new ByteArrayInputStream(TEST_JSON_BYTE)) {
SimpleContainer unmarshalledObject = jsonb.fromJson(stream, SimpleContainer.class);
CloseRememberingInputStream rememberingStream = new CloseRememberingInputStream(stream);
SimpleContainer unmarshalledObject = jsonb.fromJson(rememberingStream, SimpleContainer.class);
assertThat("Failed to unmarshal using Jsonb.fromJson method with InputStream and Class arguments.",
unmarshalledObject.getInstance(), is(TEST_STRING));
assertThat("Failed to close stream upon a successful completion", rememberingStream.isCloseCalled());
}
}

Expand All @@ -148,10 +153,12 @@ public void testFromJsonStreamClass() throws IOException {
@Test
public void testFromJsonStreamType() throws IOException {
try (ByteArrayInputStream stream = new ByteArrayInputStream(TEST_JSON_BYTE)) {
CloseRememberingInputStream rememberingStream = new CloseRememberingInputStream(stream);
SimpleContainer unmarshalledObject = jsonb
.fromJson(stream, new SimpleContainer() { }.getClass().getGenericSuperclass());
.fromJson(rememberingStream, new SimpleContainer() { }.getClass().getGenericSuperclass());
assertThat("Failed to unmarshal using Jsonb.fromJson method with InputStream and Type arguments.",
unmarshalledObject.getInstance(), is(TEST_STRING));
assertThat("Failed to close stream upon a successful completion", rememberingStream.isCloseCalled());
}
}

Expand Down Expand Up @@ -234,10 +241,12 @@ public void testToJsonObjectTypeWriter() throws IOException {
@Test
public void testToJsonObjectStream() throws IOException {
try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
jsonb.toJson(new SimpleContainer(), stream);
String jsonString = new String(stream.toByteArray(), StandardCharsets.UTF_8);
CloseRememberingOutputStream rememberingStream = new CloseRememberingOutputStream(stream);
jsonb.toJson(new SimpleContainer(), rememberingStream);
String jsonString = new String(rememberingStream.toByteArray(), StandardCharsets.UTF_8);
assertThat("Failed to marshal using Jsonb.toJson method with Object and OutputStream arguments.",
jsonString, matchesPattern(MATCHING_PATTERN));
assertThat("Failed to close stream upon a successful completion", rememberingStream.isCloseCalled());
}
}

Expand All @@ -252,10 +261,135 @@ public void testToJsonObjectStream() throws IOException {
@Test
public void testToJsonObjectTypeStream() throws IOException {
try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
jsonb.toJson(new SimpleContainer(), new SimpleContainer() { }.getClass().getGenericSuperclass(), stream);
String jsonString = new String(stream.toByteArray(), StandardCharsets.UTF_8);
CloseRememberingOutputStream rememberingStream = new CloseRememberingOutputStream(stream);
jsonb.toJson(new SimpleContainer(), new SimpleContainer() { }.getClass().getGenericSuperclass(), rememberingStream);
String jsonString = new String(rememberingStream.toByteArray(), StandardCharsets.UTF_8);
assertThat("Failed to marshal using Jsonb.toJson method with Object, Type and OutputStream arguments.",
jsonString, matchesPattern(MATCHING_PATTERN));
assertThat("Failed to close stream upon a successful completion", rememberingStream.isCloseCalled());
}
}

static final class CloseRememberingOutputStream extends OutputStream {

private ByteArrayOutputStream delegate;
private boolean closeCalled;

CloseRememberingOutputStream(ByteArrayOutputStream delegate) {
this.delegate = delegate;
this.closeCalled = false;
}

@Override
public void close() throws IOException {
closeCalled = true;
delegate.close();
}

boolean isCloseCalled() {
return closeCalled;
}

byte[] toByteArray() {
return delegate.toByteArray();
}

@Override
public void write(int b) throws IOException {
delegate.write(b);
}

@Override
public void write(byte[] b) throws IOException {
delegate.write(b);
}

@Override
public void write(byte[] b, int off, int len) throws IOException {
delegate.write(b, off, len);
}

}

static final class CloseRememberingInputStream extends InputStream {

private InputStream delegate;
private boolean closeCalled;

CloseRememberingInputStream(InputStream delegate) {
this.delegate = delegate;
this.closeCalled = false;
}

@Override
public void close() throws IOException {
closeCalled = true;
delegate.close();
}

boolean isCloseCalled() {
return closeCalled;
}

@Override
public int read() throws IOException {
return delegate.read();
}

public int read(byte[] b) throws IOException {
return delegate.read(b);
}

@Override
public int read(byte[] b, int off, int len) throws IOException {
return delegate.read(b, off, len);
}

@Override
public byte[] readAllBytes() throws IOException {
return delegate.readAllBytes();
}

@Override
public byte[] readNBytes(int len) throws IOException {
return delegate.readNBytes(len);
}

@Override
public int readNBytes(byte[] b, int off, int len) throws IOException {
return delegate.readNBytes(b, off, len);
}

@Override
public long skip(long n) throws IOException {
return delegate.skip(n);
}

@Override
public int available() throws IOException {
return delegate.available();
}

@Override
public void mark(int readlimit) {
delegate.mark(readlimit);
}

@Override
public void reset() throws IOException {
delegate.reset();
}

@Override
public boolean markSupported() {
return delegate.markSupported();
}

@Override
public long transferTo(OutputStream out) throws IOException {
return delegate.transferTo(out);
}


}
}

0 comments on commit a3c85ec

Please sign in to comment.