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 ad37a4d45..fedff633b 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 @@ -256,8 +256,6 @@ public class StaxEventItemWriter extends ExecutionContextUserSupport implemen Assert.notNull(resource, "The resource must be set"); - Assert.state(resource.exists(), "Output resource must exist"); - long startAtPosition = 0; // if restart data is provided, restart from provided offset @@ -274,6 +272,7 @@ public class StaxEventItemWriter extends ExecutionContextUserSupport implemen write(iterator.next()); } } + } /** @@ -287,6 +286,7 @@ public class StaxEventItemWriter extends ExecutionContextUserSupport implemen 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..3b0d0e5c1 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 @@ -5,8 +5,11 @@ import java.io.File; import java.io.FileWriter; import java.io.IOException; -import junit.framework.TestCase; +import static org.junit.Assert.*; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; import org.springframework.batch.item.ItemStreamException; import org.springframework.util.Assert; @@ -15,7 +18,7 @@ import org.springframework.util.Assert; * * @author Robert Kasanicky */ -public class FileUtilsTests extends TestCase { +public class FileUtilsTests { private File file = new File("FileUtilsTests.tmp"); @@ -23,6 +26,7 @@ public class FileUtilsTests extends TestCase { * No restart + file should not be overwritten => file is created if it does * not exist, exception is thrown if it already exists */ + @Test public void testNoRestart() throws Exception { FileUtils.setUpOutputFile(file, false, false); assertTrue(file.exists()); @@ -58,6 +62,7 @@ public class FileUtilsTests extends TestCase { * In case of restart, the file is supposed to exist and exception is thrown * if it does not. */ + @Test public void testRestart() throws Exception { try { FileUtils.setUpOutputFile(file, true, false); @@ -86,6 +91,7 @@ public class FileUtilsTests extends TestCase { /** * If the directories on the file path do not exist, they should be created */ + @Test public void testCreateDirectoryStructure() { File file = new File("testDirectory/testDirectory2/testFile.tmp"); File dir1 = new File("testDirectory"); @@ -104,6 +110,7 @@ public class FileUtilsTests extends TestCase { } } + @Test public void testBadFile(){ File file = new File("new file"){ @@ -120,12 +127,35 @@ public class FileUtilsTests extends TestCase { file.delete(); } } + + @Test + public void testCouldntCreateFile(){ - protected void setUp() throws Exception { + File file = new File("new file"){ + + @Override + 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(); + } + } + + @Before + public void setUp() throws Exception { Assert.state(!file.exists()); } - protected void tearDown() throws Exception { + @After + public void tearDown() throws Exception { file.delete(); } 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 53c223c47..61efbe6b4 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 @@ -1,5 +1,14 @@ package org.springframework.batch.item.xml; +import static org.easymock.EasyMock.createMock; +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.replay; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + import java.io.File; import java.io.IOException; import java.util.HashMap; @@ -8,14 +17,11 @@ import javax.xml.stream.XMLEventFactory; import javax.xml.stream.XMLStreamException; import javax.xml.transform.Result; -import static org.junit.Assert.*; - import org.apache.commons.io.FileUtils; import org.junit.Before; import org.junit.Test; import org.springframework.batch.item.ExecutionContext; import org.springframework.batch.item.xml.oxm.MarshallingEventWriterSerializer; -import org.springframework.core.io.DescriptiveResource; import org.springframework.core.io.FileSystemResource; import org.springframework.core.io.Resource; import org.springframework.oxm.Marshaller; @@ -239,13 +245,11 @@ public class StaxEventItemWriterTests { @Test public void testNonExistantResource() throws Exception { - Resource doesntExist = new DescriptiveResource("") { - - public boolean exists() { - return false; - } - - }; + Resource doesntExist = createMock(Resource.class); + expect(doesntExist.getFile()).andReturn(new File("does not exist")); + expect(doesntExist.exists()).andReturn(false); + replay(doesntExist); + writer.setResource(doesntExist); try {