diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/util/FileUtils.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/util/FileUtils.java index e2703af89..240d934ff 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/util/FileUtils.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/util/FileUtils.java @@ -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( diff --git a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java index 292486136..7e5cb47fd 100644 --- a/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java +++ b/spring-batch-infrastructure/src/main/java/org/springframework/batch/item/xml/StaxEventItemWriter.java @@ -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); diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/util/FileUtilsTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/util/FileUtilsTests.java index cd8651232..c2f02f1f4 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/util/FileUtilsTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/util/FileUtilsTests.java @@ -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()); diff --git a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/StaxEventItemWriterTests.java b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/StaxEventItemWriterTests.java index 6637f0693..e5d99a5a1 100644 --- a/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/StaxEventItemWriterTests.java +++ b/spring-batch-infrastructure/src/test/java/org/springframework/batch/item/xml/StaxEventItemWriterTests.java @@ -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; }