Add support to build StaxEventItemReader when no Resource is provided

Resolves #3736
This commit is contained in:
Parikshit Dutta
2020-07-26 01:33:37 +05:30
committed by Mahmoud Ben Hassine
parent 51a1ecddad
commit 67a0c7a372
2 changed files with 25 additions and 11 deletions

View File

@@ -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<T> {
protected Log logger = LogFactory.getLog(getClass());
private boolean strict = true;
private Resource resource;
@@ -199,10 +205,13 @@ public class StaxEventItemReaderBuilder<T> {
* @return a new instance of the {@link StaxEventItemReader}
*/
public StaxEventItemReader<T> build() {
Assert.notNull(this.resource, "A resource is required.");
StaxEventItemReader<T> 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.");
}

View File

@@ -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<Foo>().build();
fail("Validation of the missing resource failed");
}
catch (IllegalArgumentException ignore) {
}
try {
new StaxEventItemReaderBuilder<Foo>()
.resource(this.resource)
@@ -85,6 +80,16 @@ public class StaxEventItemReaderBuilderTests {
}
}
@Test
public void testBuildWithoutProvidingResource() {
StaxEventItemReader<Foo> reader = new StaxEventItemReaderBuilder<Foo>()
.name("fooReader")
.addFragmentRootElements("foo")
.build();
assertNotNull(reader);
}
@Test
public void testConfiguration() throws Exception {
Jaxb2Marshaller unmarshaller = new Jaxb2Marshaller();