Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -297,9 +297,13 @@ public ObjectContent<T> read(Resource resource) throws IOException {
verify();
Assert.notNull(resource, "'resource' must not be null");
InputStream inputStream = resource.getInputStream();
T object = readObject(inputStream, getTypeNotNull());
closeQuietly(inputStream);
return new ObjectContent<>(this.type, object);
try {
T object = readObject(inputStream, getTypeNotNull());
return new ObjectContent<>(this.type, object);
}
finally {
closeQuietly(inputStream);
}
}

/**
Expand All @@ -322,9 +326,13 @@ public T readObject(Reader reader) throws IOException {
public ObjectContent<T> read(Reader reader) throws IOException {
verify();
Assert.notNull(reader, "'reader' must not be null");
T object = readObject(reader, getTypeNotNull());
closeQuietly(reader);
return new ObjectContent<>(this.type, object);
try {
T object = readObject(reader, getTypeNotNull());
return new ObjectContent<>(this.type, object);
}
finally {
closeQuietly(reader);
}
}

private void closeQuietly(Closeable closeable) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
Expand All @@ -40,7 +41,13 @@
import org.springframework.util.ReflectionUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.then;
import static org.mockito.Mockito.atLeastOnce;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;

/**
* Tests for {@link AbstractJsonMarshalTester}.
Expand All @@ -55,6 +62,8 @@ abstract class AbstractJsonMarshalTesterTests {

private static final String ARRAY_JSON = "[" + JSON + "]";

private static final String TRUNCATED_JSON = "{\"name\":\"Spring\",";

private static final ExampleObject OBJECT = createExampleObject("Spring", 123);

private static final ResolvableType TYPE = ResolvableType.forClass(ExampleObject.class);
Expand Down Expand Up @@ -145,13 +154,31 @@ void readResourceShouldReturnObject() throws Exception {
assertThat(tester.read(resource)).isEqualTo(OBJECT);
}

@Test
void readResourceWhenReadFailsShouldCloseInputStream() throws IOException {
Resource resource = mock();
InputStream inputStream = spy(new ByteArrayInputStream(TRUNCATED_JSON.getBytes()));
given(resource.getInputStream()).willReturn(inputStream);
AbstractJsonMarshalTester<Object> tester = createTester(TYPE);
assertThatException().isThrownBy(() -> tester.read(resource));
then(inputStream).should(atLeastOnce()).close();
}

@Test
void readReaderShouldReturnObject() throws Exception {
Reader reader = new StringReader(JSON);
AbstractJsonMarshalTester<Object> tester = createTester(TYPE);
assertThat(tester.read(reader)).isEqualTo(OBJECT);
}

@Test
void readReaderWhenReadFailsShouldCloseReader() throws IOException {
Reader reader = spy(new StringReader(TRUNCATED_JSON));
AbstractJsonMarshalTester<Object> tester = createTester(TYPE);
assertThatException().isThrownBy(() -> tester.read(reader));
then(reader).should(atLeastOnce()).close();
}

@Test
void parseListShouldReturnContent() throws Exception {
ResolvableType type = ResolvableTypes.get("listOfExampleObject");
Expand Down