Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify Jsonb calls close methods #351

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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() {
marschall marked this conversation as resolved.
Show resolved Hide resolved
return closeCalled;
}

byte[] toByteArray() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing @Override

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't do that #toByteArray() is defined on ByteArrayOutputStream not on OutputStream.

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 {
marschall marked this conversation as resolved.
Show resolved Hide resolved

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 {
marschall marked this conversation as resolved.
Show resolved Hide resolved
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);
}


}
}