BATCH-1615: Add unit test for XML fatal error
This commit is contained in:
@@ -28,13 +28,13 @@ import javax.xml.stream.events.XMLEvent;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.batch.item.NonTransientResourceException;
|
||||
import org.springframework.batch.item.file.ResourceAwareItemReaderItemStream;
|
||||
import org.springframework.batch.item.support.AbstractItemCountingItemStreamItemReader;
|
||||
import org.springframework.batch.item.xml.stax.DefaultFragmentEventReader;
|
||||
import org.springframework.batch.item.xml.stax.FragmentEventReader;
|
||||
import org.springframework.beans.factory.InitializingBean;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.dao.DataAccessResourceFailureException;
|
||||
import org.springframework.oxm.Unmarshaller;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.ClassUtils;
|
||||
@@ -137,11 +137,11 @@ public class StaxEventItemReader<T> extends AbstractItemCountingItemStreamItemRe
|
||||
* @return <code>true</code> if next fragment was found, <code>false</code>
|
||||
* otherwise.
|
||||
*
|
||||
* @throws DataAccessResourceFailureException if the cursor could not be
|
||||
* @throws NonTransientResourceException if the cursor could not be
|
||||
* moved. This will be treated as fatal and subsequent calls to read will
|
||||
* return null.
|
||||
*/
|
||||
protected boolean moveCursorToNextFragment(XMLEventReader reader) throws DataAccessResourceFailureException {
|
||||
protected boolean moveCursorToNextFragment(XMLEventReader reader) throws NonTransientResourceException {
|
||||
try {
|
||||
while (true) {
|
||||
while (reader.peek() != null && !reader.peek().isStartElement()) {
|
||||
@@ -162,7 +162,7 @@ public class StaxEventItemReader<T> extends AbstractItemCountingItemStreamItemRe
|
||||
}
|
||||
}
|
||||
catch (XMLStreamException e) {
|
||||
throw new DataAccessResourceFailureException("Error while reading from event reader", e);
|
||||
throw new NonTransientResourceException("Error while reading from event reader", e);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -223,7 +223,7 @@ public class StaxEventItemReader<T> extends AbstractItemCountingItemStreamItemRe
|
||||
try {
|
||||
success = moveCursorToNextFragment(fragmentReader);
|
||||
}
|
||||
catch (DataAccessResourceFailureException e) {
|
||||
catch (NonTransientResourceException e) {
|
||||
// Prevent caller from retrying indefinitely since this is fatal
|
||||
noInput = true;
|
||||
throw e;
|
||||
|
||||
@@ -19,6 +19,7 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.batch.item.ExecutionContext;
|
||||
import org.springframework.batch.item.ItemStreamException;
|
||||
import org.springframework.batch.item.NonTransientResourceException;
|
||||
import org.springframework.core.io.AbstractResource;
|
||||
import org.springframework.core.io.ByteArrayResource;
|
||||
import org.springframework.core.io.FileSystemResource;
|
||||
@@ -45,7 +46,7 @@ public class StaxEventItemReaderTests {
|
||||
|
||||
private String mixedXml = "<fragment xmlns=\"urn:org.test.foo\"> <fragment xmlns=\"urn:org.test.bar\"> <misc1/> </fragment> <misc2/> <fragment xmlns=\"urn:org.test.bar\"> testString </fragment> </fragment>";
|
||||
|
||||
private String invalidXml = "<root> <fragment> <misc1/> <fragment> <misc2/> <fragment> testString </fragment> </root>";
|
||||
private String invalidXml = "<root> </fragment> <misc1/> </root>";
|
||||
|
||||
private Unmarshaller unmarshaller = new MockFragmentUnmarshaller();
|
||||
|
||||
@@ -106,7 +107,7 @@ public class StaxEventItemReaderTests {
|
||||
|
||||
@Test
|
||||
public void testFragmentNamespace() throws Exception {
|
||||
|
||||
|
||||
source.setResource(new ByteArrayResource(fooXml.getBytes()));
|
||||
source.afterPropertiesSet();
|
||||
source.open(executionContext);
|
||||
@@ -120,9 +121,9 @@ public class StaxEventItemReaderTests {
|
||||
|
||||
@Test
|
||||
public void testFragmentMixedNamespace() throws Exception {
|
||||
|
||||
|
||||
source.setResource(new ByteArrayResource(mixedXml.getBytes()));
|
||||
source.setFragmentRootElementName("{urn:org.test.bar}"+FRAGMENT_ROOT_ELEMENT);
|
||||
source.setFragmentRootElementName("{urn:org.test.bar}" + FRAGMENT_ROOT_ELEMENT);
|
||||
source.afterPropertiesSet();
|
||||
source.open(executionContext);
|
||||
// see asserts in the mock unmarshaller
|
||||
@@ -135,15 +136,20 @@ public class StaxEventItemReaderTests {
|
||||
|
||||
@Test
|
||||
public void testFragmentInvalid() throws Exception {
|
||||
|
||||
|
||||
source.setResource(new ByteArrayResource(invalidXml.getBytes()));
|
||||
source.setFragmentRootElementName(FRAGMENT_ROOT_ELEMENT);
|
||||
source.afterPropertiesSet();
|
||||
source.open(executionContext);
|
||||
// see asserts in the mock unmarshaller
|
||||
assertNotNull(source.read());
|
||||
assertNotNull(source.read());
|
||||
assertNull(source.read()); // there are only two fragments
|
||||
// Should fail before it gets to the marshaller
|
||||
try {
|
||||
assertNotNull(source.read());
|
||||
fail("Expected NonTransientResourceException");
|
||||
}
|
||||
catch (NonTransientResourceException e) {
|
||||
// expected
|
||||
}
|
||||
assertNull(source.read()); // after an error there is no more output
|
||||
|
||||
source.close();
|
||||
}
|
||||
@@ -274,10 +280,10 @@ public class StaxEventItemReaderTests {
|
||||
catch (ItemStreamException ex) {
|
||||
// expected
|
||||
}
|
||||
|
||||
// read() should then return a null
|
||||
assertNull(source.read());
|
||||
source.close();
|
||||
|
||||
// read() should then return a null
|
||||
assertNull(source.read());
|
||||
source.close();
|
||||
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user