Skip to content
Open
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 @@ -97,11 +97,14 @@ public void open(Resource resource) throws Exception {
return null;
}

@SuppressWarnings("DataFlowIssue")
@Override
public void close() throws Exception {
this.inputStream.close();
this.jsonReader.close();
if (this.inputStream != null) {
this.inputStream.close();
}
if (this.jsonReader != null) {
this.jsonReader.close();
}
}

@SuppressWarnings("DataFlowIssue")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,11 +106,14 @@ public void open(Resource resource) throws Exception {
return null;
}

@SuppressWarnings("DataFlowIssue")
@Override
public void close() throws Exception {
this.inputStream.close();
this.jsonParser.close();
if (this.inputStream != null) {
this.inputStream.close();
}
if (this.jsonParser != null) {
this.jsonParser.close();
}
}

@SuppressWarnings("DataFlowIssue")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package org.springframework.batch.infrastructure.item.json;

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;

import org.junit.jupiter.api.Test;
Expand All @@ -25,9 +27,11 @@
import org.springframework.batch.infrastructure.item.ParseException;
import org.springframework.batch.infrastructure.item.json.builder.JsonItemReaderBuilder;
import org.springframework.batch.infrastructure.item.json.domain.Trade;
import org.springframework.core.io.AbstractResource;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.ClassPathResource;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
Expand Down Expand Up @@ -130,6 +134,32 @@ void testInvalidResourceContent() {
assertTrue(getJsonParsingException().isInstance(expectedException.getCause()));
}

@Test
void testCloseWhenOpenFailsShouldNotThrowNpe() {
// given
AbstractResource failingResource = new AbstractResource() {
@Override
public String getDescription() {
return "failing resource";
}

@Override
public InputStream getInputStream() throws IOException {
throw new IOException("Simulated connection failure");
}
};
JsonItemReader<Trade> itemReader = new JsonItemReaderBuilder<Trade>().jsonObjectReader(getJsonObjectReader())
.resource(failingResource)
.name("tradeJsonItemReader")
.build();

// when
assertThrows(ItemStreamException.class, () -> itemReader.open(new ExecutionContext()));

// then
assertDoesNotThrow(itemReader::close);
}

@Test
void testJumpToItem() throws Exception {
// given
Expand Down