Skip to content

Commit 67a0c7a

Browse files
parikshitduttafmbenhassine
authored andcommitted
Add support to build StaxEventItemReader when no Resource is provided
Resolves #3736
1 parent 51a1ecd commit 67a0c7a

File tree

2 files changed

+25
-11
lines changed

2 files changed

+25
-11
lines changed

spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/builder/StaxEventItemReaderBuilder.java

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2019 the original author or authors.
2+
* Copyright 2017-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,9 @@
2121

2222
import javax.xml.stream.XMLInputFactory;
2323

24+
import org.apache.commons.logging.Log;
25+
import org.apache.commons.logging.LogFactory;
26+
2427
import org.springframework.batch.item.xml.StaxEventItemReader;
2528
import org.springframework.batch.item.xml.StaxUtils;
2629
import org.springframework.core.io.Resource;
@@ -33,10 +36,13 @@
3336
*
3437
* @author Michael Minella
3538
* @author Glenn Renfro
39+
* @author Parikshit Dutta
3640
* @since 4.0
3741
*/
3842
public class StaxEventItemReaderBuilder<T> {
3943

44+
protected Log logger = LogFactory.getLog(getClass());
45+
4046
private boolean strict = true;
4147

4248
private Resource resource;
@@ -199,10 +205,13 @@ public StaxEventItemReaderBuilder<T> xmlInputFactory(XMLInputFactory xmlInputFac
199205
* @return a new instance of the {@link StaxEventItemReader}
200206
*/
201207
public StaxEventItemReader<T> build() {
202-
Assert.notNull(this.resource, "A resource is required.");
203-
204208
StaxEventItemReader<T> reader = new StaxEventItemReader<>();
205209

210+
if (this.resource == null) {
211+
logger.debug("The resource is null. This is only a valid scenario when " +
212+
"injecting resource later as in when using the MultiResourceItemReader");
213+
}
214+
206215
if (this.saveState) {
207216
Assert.state(StringUtils.hasText(this.name), "A name is required when saveState is set to true.");
208217
}

spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/builder/StaxEventItemReaderBuilderTests.java

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017-2019 the original author or authors.
2+
* Copyright 2017-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -32,11 +32,13 @@
3232

3333
import static org.junit.Assert.assertEquals;
3434
import static org.junit.Assert.assertNull;
35+
import static org.junit.Assert.assertNotNull;
3536
import static org.junit.Assert.fail;
3637

3738
/**
3839
* @author Michael Minella
3940
* @author Mahmoud Ben Hassine
41+
* @author Parikshit Dutta
4042
*/
4143
public class StaxEventItemReaderBuilderTests {
4244

@@ -55,13 +57,6 @@ public void setUp() {
5557

5658
@Test
5759
public void testValidation() {
58-
try {
59-
new StaxEventItemReaderBuilder<Foo>().build();
60-
fail("Validation of the missing resource failed");
61-
}
62-
catch (IllegalArgumentException ignore) {
63-
}
64-
6560
try {
6661
new StaxEventItemReaderBuilder<Foo>()
6762
.resource(this.resource)
@@ -85,6 +80,16 @@ public void testValidation() {
8580
}
8681
}
8782

83+
@Test
84+
public void testBuildWithoutProvidingResource() {
85+
StaxEventItemReader<Foo> reader = new StaxEventItemReaderBuilder<Foo>()
86+
.name("fooReader")
87+
.addFragmentRootElements("foo")
88+
.build();
89+
90+
assertNotNull(reader);
91+
}
92+
8893
@Test
8994
public void testConfiguration() throws Exception {
9095
Jaxb2Marshaller unmarshaller = new Jaxb2Marshaller();

0 commit comments

Comments
 (0)