RESOLVED - issue BATCH-560: StepExecutionResourceProxy can not be used with FlatFileItemWriter

This commit is contained in:
dsyer
2008-04-07 07:21:18 +00:00
parent 7dcda664c3
commit afb9508e4f
3 changed files with 28 additions and 6 deletions

View File

@@ -108,9 +108,7 @@ public class FlatFileItemWriter extends ExecutionContextUserSupport implements I
*/
public void afterPropertiesSet() throws Exception {
Assert.notNull(resource, "The resource must be set");
Assert.notNull(fieldSetCreator, "A FieldSetUnmapper must be provided.");
File file = resource.getFile();
Assert.state(!file.exists() || file.canWrite(), "Resource is not writable: [" + resource + "]");
Assert.notNull(fieldSetCreator, "A FieldSetCreator must be provided.");
}
/**
@@ -232,6 +230,13 @@ public class FlatFileItemWriter extends ExecutionContextUserSupport implements I
// Returns object representing state.
private OutputState getOutputState() {
if (state == null) {
try {
File file = resource.getFile();
Assert.state(!file.exists() || file.canWrite(), "Resource is not writable: [" + resource + "]");
}
catch (IOException e) {
throw new ItemStreamException("Could not test resource for writable status.", e);
}
state = new OutputState();
state.setDeleteIfExists(shouldDeleteIfExists);
state.setBufferSize(bufferSize);

View File

@@ -3,6 +3,7 @@ package org.springframework.batch.item.util;
import java.io.File;
import java.io.IOException;
import org.springframework.batch.item.ItemStreamException;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.util.Assert;
@@ -41,7 +42,7 @@ public class FileUtils {
if (!restarted) {
if (file.exists()) {
if(!overwriteOutputFile){
throw new DataAccessResourceFailureException("File already exists: ["
throw new ItemStreamException("File already exists: ["
+ file.getAbsolutePath() + "]");
}
file.delete();
@@ -53,13 +54,13 @@ public class FileUtils {
file.createNewFile();
}
} catch (IOException ioe) {
throw new DataAccessResourceFailureException(
throw new ItemStreamException(
"Unable to create file: [" + file.getAbsolutePath() + "]",
ioe);
}
if (!file.canWrite()) {
throw new DataAccessResourceFailureException(
throw new ItemStreamException(
"File is not writable: [" + file.getAbsolutePath() + "]");
}
}

View File

@@ -261,6 +261,22 @@ public class FlatFileItemWriterTests extends TestCase {
assertEquals(3, executionContext.getLong(ClassUtils.getShortName(FlatFileItemWriter.class) + ".written"));
}
public void testOpenWithNonexistentFile() throws Exception {
writer = new FlatFileItemWriter();
writer.setFieldSetCreator(new PassThroughFieldSetMapper());
FileSystemResource file = new FileSystemResource("no-such-file.foo");
file.getFile().setReadOnly();
writer.setResource(file);
writer.afterPropertiesSet();
try {
writer.open(executionContext);
fail("Expected IllegalStateException");
} catch (IllegalStateException e) {
String message = e.getMessage();
assertTrue("Message does not contain 'writable': "+message, message.indexOf("writable")>=0);
}
}
public void testAfterPropertiesSetChecksMandatory() throws Exception {
writer = new FlatFileItemWriter();