From 45a1982dfeffbff2acbcb6dd34e556feee86cc62 Mon Sep 17 00:00:00 2001 From: dsyer Date: Tue, 7 Sep 2010 11:05:37 +0000 Subject: [PATCH] BATCH-1615: Add unit test for XML fatal error --- .../batch/item/xml/StaxEventItemReader.java | 10 +++--- .../item/xml/StaxEventItemReaderTests.java | 32 +++++++++++-------- 2 files changed, 24 insertions(+), 18 deletions(-) diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemReader.java index 81c404f00..047bd983b 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemReader.java @@ -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 extends AbstractItemCountingItemStreamItemRe * @return true if next fragment was found, false * 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 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 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; diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/StaxEventItemReaderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/StaxEventItemReaderTests.java index 39460ca81..02e0b3682 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/StaxEventItemReaderTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/StaxEventItemReaderTests.java @@ -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 = " testString "; - private String invalidXml = " testString "; + private String invalidXml = " "; 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(); }