Skip to content

Commit 493196c

Browse files
committed
BATCH-2691: use constructor injection for required dependencies
1 parent 53262e6 commit 493196c

File tree

4 files changed

+57
-40
lines changed

4 files changed

+57
-40
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/json/JsonItemReader.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.springframework.batch.item.ItemStreamReader;
2323
import org.springframework.batch.item.file.ResourceAwareItemReaderItemStream;
2424
import org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader;
25-
import org.springframework.beans.factory.InitializingBean;
2625
import org.springframework.core.io.Resource;
2726
import org.springframework.util.Assert;
2827

@@ -50,7 +49,7 @@
5049
* @since 4.1
5150
*/
5251
public class JsonItemReader<T> extends AbstractItemCountingItemStreamItemReader<T> implements
53-
ResourceAwareItemReaderItemStream<T>, InitializingBean {
52+
ResourceAwareItemReaderItemStream<T> {
5453

5554
private static final Log LOGGER = LogFactory.getLog(JsonItemReader.class);
5655

@@ -60,6 +59,18 @@ public class JsonItemReader<T> extends AbstractItemCountingItemStreamItemReader<
6059

6160
private boolean strict = true;
6261

62+
/**
63+
* Create a new {@link JsonItemReader} instance.
64+
* @param resource the input json resource
65+
* @param jsonObjectReader the json object reader to use
66+
*/
67+
public JsonItemReader(Resource resource, JsonObjectReader<T> jsonObjectReader) {
68+
Assert.notNull(resource, "The resource must not be null.");
69+
Assert.notNull(jsonObjectReader, "The json object reader must not be null.");
70+
this.resource = resource;
71+
this.jsonObjectReader = jsonObjectReader;
72+
}
73+
6374
/**
6475
* Set the {@link JsonObjectReader} to use to read and map Json fragments to domain objects.
6576
* @param jsonObjectReader the json object reader to use
@@ -112,10 +123,4 @@ protected void doClose() throws Exception {
112123
this.jsonObjectReader.close();
113124
}
114125

115-
@Override
116-
public void afterPropertiesSet() throws Exception {
117-
Assert.notNull(this.jsonObjectReader, "The json object reader must not be null.");
118-
Assert.notNull(this.resource, "The resource must not be null.");
119-
}
120-
121126
}

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/json/builder/JsonItemReaderBuilder.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,9 +145,7 @@ public JsonItemReader<T> build() {
145145
Assert.state(StringUtils.hasText(this.name), "A name is required when saveState is set to true.");
146146
}
147147

148-
JsonItemReader<T> reader = new JsonItemReader<>();
149-
reader.setJsonObjectReader(this.jsonObjectReader);
150-
reader.setResource(this.resource);
148+
JsonItemReader<T> reader = new JsonItemReader<>(this.resource, this.jsonObjectReader);
151149
reader.setName(this.name);
152150
reader.setStrict(this.strict);
153151
reader.setSaveState(this.saveState);

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/json/JsonItemReaderCommonTests.java

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,18 @@ public abstract class JsonItemReaderCommonTests extends AbstractItemStreamItemRe
3838
protected abstract JsonObjectReader<Foo> getJsonObjectReader();
3939

4040
@Override
41-
protected ItemReader<Foo> getItemReader() throws Exception {
42-
JsonItemReader<Foo> itemReader = new JsonItemReader<>();
43-
itemReader.setResource(new ByteArrayResource(FOOS.getBytes()));
41+
protected ItemReader<Foo> getItemReader() {
42+
ByteArrayResource resource = new ByteArrayResource(FOOS.getBytes());
43+
JsonObjectReader<Foo> jsonObjectReader = getJsonObjectReader();
44+
JsonItemReader<Foo> itemReader = new JsonItemReader<>(resource, jsonObjectReader);
4445
itemReader.setName("fooJsonItemReader");
45-
itemReader.setSaveState(true);
46-
itemReader.setJsonObjectReader(getJsonObjectReader());
47-
itemReader.afterPropertiesSet();
4846
return itemReader;
4947
}
5048

5149
@Override
52-
protected void pointToEmptyInput(ItemReader<Foo> tested) throws Exception {
50+
protected void pointToEmptyInput(ItemReader<Foo> tested) {
5351
JsonItemReader<Foo> reader = (JsonItemReader<Foo>) tested;
5452
reader.setResource(new ByteArrayResource("[]".getBytes()));
55-
reader.afterPropertiesSet();
5653

5754
reader.open(new ExecutionContext());
5855
}

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/json/JsonItemReaderTests.java

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,60 +22,77 @@
2222
import org.junit.Rule;
2323
import org.junit.Test;
2424
import org.junit.rules.ExpectedException;
25+
import org.junit.runner.RunWith;
26+
import org.mockito.Mock;
27+
import org.mockito.junit.MockitoJUnitRunner;
2528

2629
import org.springframework.batch.item.ExecutionContext;
2730
import org.springframework.batch.item.ItemStreamException;
2831
import org.springframework.core.io.AbstractResource;
32+
import org.springframework.core.io.ByteArrayResource;
33+
34+
import static org.junit.Assert.assertEquals;
35+
import static org.junit.Assert.fail;
2936

3037
/**
3138
* @author Mahmoud Ben Hassine
3239
*/
40+
@RunWith(MockitoJUnitRunner.class)
3341
public class JsonItemReaderTests {
3442

3543
@Rule
3644
public ExpectedException expectedException = ExpectedException.none();
3745

38-
private JsonItemReader<String> itemReader = new JsonItemReader<>();
39-
40-
@Test
41-
public void testMandatoryJsonObjectReader() throws Exception {
42-
this.expectedException.expect(IllegalArgumentException.class);
43-
this.expectedException.expectMessage("The json object reader must not be null.");
46+
@Mock
47+
private JsonObjectReader<String> jsonObjectReader;
4448

45-
this.itemReader.afterPropertiesSet();
46-
}
49+
private JsonItemReader<String> itemReader;
4750

4851
@Test
49-
public void testMandatoryResource() throws Exception {
50-
this.expectedException.expect(IllegalArgumentException.class);
51-
this.expectedException.expectMessage("The resource must not be null.");
52-
this.itemReader.setJsonObjectReader(() -> null);
52+
public void testValidation() {
53+
try {
54+
new JsonItemReader<>(null, this.jsonObjectReader);
55+
fail("A resource is required.");
56+
} catch (IllegalArgumentException iae) {
57+
assertEquals("The resource must not be null.", iae.getMessage());
58+
}
5359

54-
this.itemReader.afterPropertiesSet();
60+
try {
61+
new JsonItemReader<>(new ByteArrayResource("[{}]".getBytes()), null);
62+
fail("A json object reader is required.");
63+
} catch (IllegalArgumentException iae) {
64+
assertEquals("The json object reader must not be null.", iae.getMessage());
65+
}
5566
}
5667

5768
@Test
58-
public void testNonExistentResource() throws Exception {
69+
public void testNonExistentResource() {
70+
// given
5971
this.expectedException.expect(ItemStreamException.class);
6072
this.expectedException.expectMessage("Failed to initialize the reader");
6173
this.expectedException.expectCause(Matchers.instanceOf(IllegalStateException.class));
62-
this.itemReader.setJsonObjectReader(() -> null);
63-
this.itemReader.setResource(new NonExistentResource());
64-
this.itemReader.afterPropertiesSet();
74+
this.itemReader = new JsonItemReader<>(new NonExistentResource(), this.jsonObjectReader);
6575

76+
// when
6677
this.itemReader.open(new ExecutionContext());
78+
79+
// then
80+
// expected exception
6781
}
6882

6983
@Test
70-
public void testNonReadableResource() throws Exception {
84+
public void testNonReadableResource() {
85+
// given
7186
this.expectedException.expect(ItemStreamException.class);
7287
this.expectedException.expectMessage("Failed to initialize the reader");
7388
this.expectedException.expectCause(Matchers.instanceOf(IllegalStateException.class));
74-
this.itemReader.setJsonObjectReader(() -> null);
75-
this.itemReader.setResource(new NonReadableResource());
76-
this.itemReader.afterPropertiesSet();
89+
this.itemReader = new JsonItemReader<>(new NonReadableResource(), this.jsonObjectReader);
7790

91+
// when
7892
this.itemReader.open(new ExecutionContext());
93+
94+
// then
95+
// expected exception
7996
}
8097

8198
private static class NonExistentResource extends AbstractResource {

0 commit comments

Comments
 (0)