From fa2076594d4c844e5cb0a2ab52796bb2897b226c Mon Sep 17 00:00:00 2001 From: robokaso Date: Mon, 23 Feb 2009 23:49:19 +0000 Subject: [PATCH] RESOLVED - BATCH-1091: Add strict flag to file readers (flat and XML). added strict flag - when true reader throws exception on open if input resource doesn't exist --- .../batch/item/file/FlatFileItemReader.java | 24 +++++++++++++++---- .../batch/item/xml/StaxEventItemReader.java | 15 ++++++++++++ .../item/file/FlatFileItemReaderTests.java | 17 +++++++++++++ .../item/xml/StaxEventItemReaderTests.java | 14 +++++++++-- 4 files changed, 63 insertions(+), 7 deletions(-) diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemReader.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemReader.java index d7ebf43ad..706904c1d 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemReader.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/file/FlatFileItemReader.java @@ -70,13 +70,25 @@ public class FlatFileItemReader extends AbstractItemCountingItemStreamItemRea private LineCallbackHandler skippedLinesCallback; + private boolean strict = false; + public FlatFileItemReader() { setName(ClassUtils.getShortName(FlatFileItemReader.class)); } /** - * @param skippedLinesCallback will be called for each one of the initial skipped - * lines before any items are read. + * In strict mode the reader will throw an exception on + * {@link #open(org.springframework.batch.item.ExecutionContext)} if the + * input resource does not exist. + * @param strict false by default + */ + public void setStrict(boolean strict) { + this.strict = strict; + } + + /** + * @param skippedLinesCallback will be called for each one of the initial + * skipped lines before any items are read. */ public void setSkippedLinesCallback(LineCallbackHandler skippedLinesCallback) { this.skippedLinesCallback = skippedLinesCallback; @@ -194,7 +206,7 @@ public class FlatFileItemReader extends AbstractItemCountingItemStreamItemRea } } catch (IOException e) { - throw new FlatFileParseException("Unable to read from resource: [" + resource + "]", e, line, lineCount ); + throw new FlatFileParseException("Unable to read from resource: [" + resource + "]", e, line, lineCount); } return line; } @@ -211,8 +223,7 @@ public class FlatFileItemReader extends AbstractItemCountingItemStreamItemRea @Override protected void doClose() throws Exception { lineCount = 0; - if(resource.exists()) - { + if (resource.exists()) { reader.close(); } } @@ -224,6 +235,9 @@ public class FlatFileItemReader extends AbstractItemCountingItemStreamItemRea noInput = false; if (!resource.exists()) { + if (strict) { + throw new IllegalStateException("Input resource must exist (reader is in 'strict' mode)"); + } noInput = true; logger.warn("Input resource does not exist " + resource.getDescription()); return; 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 e5c9ffb79..b963c5b2f 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 @@ -69,10 +69,22 @@ public class StaxEventItemReader extends AbstractItemCountingItemStreamItemRe private boolean noInput; + private boolean strict = false; + public StaxEventItemReader() { setName(ClassUtils.getShortName(StaxEventItemReader.class)); } + /** + * In strict mode the reader will throw an exception on + * {@link #open(org.springframework.batch.item.ExecutionContext)} if the + * input resource does not exist. + * @param strict false by default + */ + public void setStrict(boolean strict) { + this.strict = strict; + } + public void setResource(Resource resource) { this.resource = resource; } @@ -162,6 +174,9 @@ public class StaxEventItemReader extends AbstractItemCountingItemStreamItemRe noInput = false; if (!resource.exists()) { + if (strict) { + throw new IllegalStateException("Input resource must exist (reader is in 'strict' mode)"); + } noInput = true; logger.warn("Input resource does not exist " + resource.getDescription()); return; diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/FlatFileItemReaderTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/FlatFileItemReaderTests.java index 590abb653..9943773ce 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/FlatFileItemReaderTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/FlatFileItemReaderTests.java @@ -8,6 +8,7 @@ import java.io.InputStream; 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.file.mapping.PassThroughLineMapper; import org.springframework.batch.item.file.separator.RecordSeparatorPolicy; import org.springframework.core.io.AbstractResource; @@ -135,6 +136,22 @@ public class FlatFileItemReaderTests { assertEquals(TEST_STRING, reader.read()); } + /** + * In strict mode, resource must exist at the time reader is opened. + */ + @Test(expected = ItemStreamException.class) + public void testStrictness() throws Exception { + + Resource resource = new NonExistentResource(); + + reader.setResource(resource); + reader.setStrict(true); + + reader.afterPropertiesSet(); + + reader.open(executionContext); + } + private Resource getInputResource(String input) { return new ByteArrayResource(input.getBytes()); } 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 9519e65fb..2dfdd99d1 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 @@ -231,10 +231,9 @@ public class StaxEventItemReaderTests { source.setResource(new NonExistentResource()); source.afterPropertiesSet(); - source.open(executionContext); assertNull(source.read()); - + } @Test @@ -248,6 +247,17 @@ public class StaxEventItemReaderTests { source.read(); } + @Test(expected = ItemStreamException.class) + public void testStrictness() throws Exception { + + source.setResource(new NonExistentResource()); + source.setStrict(true); + source.afterPropertiesSet(); + + source.open(executionContext); + + } + private StaxEventItemReader> createNewInputSouce() { Resource resource = new ByteArrayResource(xml.getBytes());