diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/builder/StaxEventItemReaderBuilder.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/builder/StaxEventItemReaderBuilder.java index 8d2819ab5..bb29f623f 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/builder/StaxEventItemReaderBuilder.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/builder/StaxEventItemReaderBuilder.java @@ -21,6 +21,9 @@ import java.util.List; import javax.xml.stream.XMLInputFactory; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + import org.springframework.batch.item.xml.StaxEventItemReader; import org.springframework.core.io.Resource; import org.springframework.oxm.Unmarshaller; @@ -34,10 +37,13 @@ import org.springframework.util.xml.StaxUtils; * @author Michael Minella * @author Glenn Renfro * @author Mahmoud Ben Hassine + * @author Parikshit Dutta * @since 4.0 */ public class StaxEventItemReaderBuilder { + protected Log logger = LogFactory.getLog(getClass()); + private boolean strict = true; private Resource resource; @@ -215,10 +221,13 @@ public class StaxEventItemReaderBuilder { * @return a new instance of the {@link StaxEventItemReader} */ public StaxEventItemReader build() { - Assert.notNull(this.resource, "A resource is required."); - StaxEventItemReader reader = new StaxEventItemReader<>(); + if (this.resource == null) { + logger.debug("The resource is null. This is only a valid scenario when " + + "injecting resource later as in when using the MultiResourceItemReader"); + } + if (this.saveState) { Assert.state(StringUtils.hasText(this.name), "A name is required when saveState is set to true."); } diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/builder/StaxEventItemReaderBuilderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/builder/StaxEventItemReaderBuilderTests.java index 3e466df00..690217038 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/builder/StaxEventItemReaderBuilderTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/builder/StaxEventItemReaderBuilderTests.java @@ -35,11 +35,13 @@ import org.springframework.oxm.jaxb.Jaxb2Marshaller; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertNotNull; import static org.junit.Assert.fail; /** * @author Michael Minella * @author Mahmoud Ben Hassine + * @author Parikshit Dutta */ public class StaxEventItemReaderBuilderTests { @@ -58,13 +60,6 @@ public class StaxEventItemReaderBuilderTests { @Test public void testValidation() { - try { - new StaxEventItemReaderBuilder().build(); - fail("Validation of the missing resource failed"); - } - catch (IllegalArgumentException ignore) { - } - try { new StaxEventItemReaderBuilder() .resource(this.resource) @@ -88,6 +83,16 @@ public class StaxEventItemReaderBuilderTests { } } + @Test + public void testBuildWithoutProvidingResource() { + StaxEventItemReader reader = new StaxEventItemReaderBuilder() + .name("fooReader") + .addFragmentRootElements("foo") + .build(); + + assertNotNull(reader); + } + @Test public void testConfiguration() throws Exception { Jaxb2Marshaller unmarshaller = new Jaxb2Marshaller();