BATCH-1462: ignore directories if reader has strict=false

This commit is contained in:
dsyer
2009-12-30 17:29:18 +00:00
parent 187246d4f0
commit 56a3e54954
5 changed files with 68 additions and 0 deletions

View File

@@ -260,6 +260,15 @@ public class FlatFileItemReader<T> 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();

View File

@@ -182,6 +182,14 @@ public class StaxEventItemReader<T> 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);

View File

@@ -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 {

View File

@@ -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{

View File

@@ -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 {