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 d5e09beaf..374f893f6 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 @@ -1,5 +1,5 @@ /* - * Copyright 2017-2019 the original author or authors. + * Copyright 2017-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -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.batch.item.xml.StaxUtils; import org.springframework.core.io.Resource; @@ -33,10 +36,13 @@ import org.springframework.util.StringUtils; * * @author Michael Minella * @author Glenn Renfro + * @author Parikshit Dutta * @since 4.0 */ public class StaxEventItemReaderBuilder { + protected Log logger = LogFactory.getLog(getClass()); + private boolean strict = true; private Resource resource; @@ -199,10 +205,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 7d2355140..b5bf35022 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 @@ -1,5 +1,5 @@ /* - * Copyright 2017-2019 the original author or authors. + * Copyright 2017-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -32,11 +32,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 { @@ -55,13 +57,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) @@ -85,6 +80,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();