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 aab7aba96..5773950aa 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 @@ -260,6 +260,15 @@ public class FlatFileItemReader extends AbstractItemCountingItemStreamItemRea return; } + if (!resource.isReadable()) { + if (strict) { + throw new IllegalStateException("Input resource must be readable (reader is in 'strict' mode): " + resource); + } + noInput = true; + logger.warn("Input resource is not readable " + resource.getDescription()); + return; + } + reader = bufferedReaderFactory.create(resource, encoding); for (int i = 0; i < linesToSkip; i++) { String line = readLine(); 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 93925747d..23cef8cdd 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 @@ -182,6 +182,14 @@ public class StaxEventItemReader extends AbstractItemCountingItemStreamItemRe logger.warn("Input resource does not exist " + resource.getDescription()); return; } + if (!resource.isReadable()) { + if (strict) { + throw new IllegalStateException("Input resource must be readable (reader is in 'strict' mode)"); + } + noInput = true; + logger.warn("Input resource is not readable " + resource.getDescription()); + return; + } inputStream = resource.getInputStream(); eventReader = XMLInputFactory.newInstance().createXMLEventReader(inputStream); 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 e4153568a..22fc47e07 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 @@ -3,6 +3,7 @@ package org.springframework.batch.item.file; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.IOException; @@ -16,6 +17,7 @@ import org.springframework.batch.item.file.mapping.PassThroughLineMapper; import org.springframework.batch.item.file.separator.RecordSeparatorPolicy; import org.springframework.core.io.AbstractResource; import org.springframework.core.io.ByteArrayResource; +import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; @@ -300,6 +302,21 @@ public class FlatFileItemReaderTests { reader.close(); } + @Test + public void testDirectoryResource() throws Exception { + + FileSystemResource resource = new FileSystemResource("target/data"); + resource.getFile().mkdirs(); + assertTrue(resource.getFile().isDirectory()); + reader.setResource(resource); + reader.afterPropertiesSet(); + + reader.setStrict(false); + reader.open(executionContext); + assertNull(reader.read()); + + } + @Test public void testRuntimeFileCreation() throws Exception { diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/MultiResourceItemReaderIntegrationTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/MultiResourceItemReaderIntegrationTests.java index 7c4857ad6..a4d855133 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/MultiResourceItemReaderIntegrationTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/file/MultiResourceItemReaderIntegrationTests.java @@ -3,6 +3,7 @@ package org.springframework.batch.item.file; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; import java.io.IOException; @@ -269,6 +270,23 @@ public class MultiResourceItemReaderIntegrationTests { tested.close(); } + /** + * Directory resource behaves as if it was empty. + */ + @Test + public void testDirectoryResources() throws Exception { + FileSystemResource resource = new FileSystemResource("target/data"); + resource.getFile().mkdirs(); + assertTrue(resource.getFile().isDirectory()); + tested.setResources(new Resource[] {resource}); + itemReader.setStrict(false); + tested.open(new ExecutionContext()); + + assertNull(tested.read()); + + tested.close(); + } + @Test public void testMiddleResourceThrowsException() throws Exception{ 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 269521b95..0a69df2ce 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 @@ -21,6 +21,7 @@ import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.item.ItemStreamException; import org.springframework.core.io.AbstractResource; import org.springframework.core.io.ByteArrayResource; +import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.oxm.Unmarshaller; import org.springframework.oxm.XmlMappingException; @@ -237,6 +238,21 @@ public class StaxEventItemReaderTests { } + @Test + public void testDirectoryResource() throws Exception { + + FileSystemResource resource = new FileSystemResource("target/data"); + resource.getFile().mkdirs(); + assertTrue(resource.getFile().isDirectory()); + source.setResource(resource); + source.afterPropertiesSet(); + + source.setStrict(false); + source.open(executionContext); + assertNull(source.read()); + + } + @Test public void testRuntimeFileCreation() throws Exception {