-
Notifications
You must be signed in to change notification settings - Fork 41
Verify Jsonb calls close methods #351
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
Open
marschall
wants to merge
2
commits into
jakartaee:master
Choose a base branch
from
marschall:test-call-close
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
@@ -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; | ||
|
||
|
@@ -37,6 +39,7 @@ | |
import static org.hamcrest.Matchers.is; | ||
import static org.hamcrest.Matchers.matchesPattern; | ||
|
||
|
||
/** | ||
* @test | ||
* @sources JsonbTest.java | ||
|
@@ -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()); | ||
} | ||
} | ||
|
||
|
@@ -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()); | ||
} | ||
} | ||
|
||
|
@@ -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()); | ||
} | ||
} | ||
|
||
|
@@ -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() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. missing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can't do that |
||
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); | ||
} | ||
|
||
|
||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.