RESOLVED - BATCH-766: Insufficient error handling in case of a missing resource for a org.springframework.batch.item.xml.StaxEventItemWriter

moved the 'exists' check after possible output file creation
This commit is contained in:
robokaso
2008-08-11 12:11:01 +00:00
parent f5268ced57
commit f49b5c5e15
4 changed files with 23 additions and 2 deletions

View File

@@ -52,6 +52,7 @@ public class FileUtils {
new File(file.getParent()).mkdirs();
}
file.createNewFile();
Assert.state(file.exists(), "Output file must exist");
}
} catch (IOException ioe) {
throw new ItemStreamException(

View File

@@ -255,8 +255,6 @@ public class StaxEventItemWriter extends ExecutionContextUserSupport implements
*/
public void open(ExecutionContext executionContext) {
Assert.state(resource.exists(), "Output resource must exist");
long startAtPosition = 0;
// if restart data is provided, restart from provided offset
@@ -287,6 +285,7 @@ public class StaxEventItemWriter extends ExecutionContextUserSupport implements
try {
file = resource.getFile();
FileUtils.setUpOutputFile(file, restarted, overwriteOutput);
Assert.state(resource.exists(), "Output resource must exist");
os = new FileOutputStream(file, true);
channel = os.getChannel();
setPosition(position);

View File

@@ -120,6 +120,23 @@ public class FileUtilsTests extends TestCase {
file.delete();
}
}
public void testCouldntCreateFile(){
File file = new File("new file"){
public boolean exists() {
return false;
}
};
try{
FileUtils.setUpOutputFile(file, false, false);
fail();
}catch(IllegalStateException ex){
assertEquals("Output file must exist", ex.getMessage());
}finally{
file.delete();
}
}
protected void setUp() throws Exception {
Assert.state(!file.exists());

View File

@@ -227,6 +227,10 @@ public class StaxEventItemWriterTests extends TestCase {
public void testNonExistantResource() throws Exception {
Resource doesntExist = new DescriptiveResource("") {
public File getFile() throws IOException {
return new File("does not exist");
}
public boolean exists() {
return false;
}