From 7c9396da18b82d0cd090322bdfa64e09c031e13f Mon Sep 17 00:00:00 2001 From: Veit Hoffmann Date: Mon, 5 Aug 2013 17:58:11 +0200 Subject: [PATCH] Fixed error in FileUtils when creating a new File with append = true. In this case FileUtils throws a FileCreationException if the path for the new file doesn't already exist, because it doesn't try to create the path. TestCase appendend to FileUtilsTests testCreateDirectoryStructureAppendMode checks that a path is creaded if append is true --- .../batch/item/util/FileUtils.java | 3 +++ .../batch/item/util/FileUtilsTests.java | 23 +++++++++++++++++++ 2 files changed, 26 insertions(+) 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 4a16dd1dd..77679f88c 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 @@ -74,6 +74,9 @@ public final class FileUtils { } else { if (!file.exists()) { + if (file.getParent() != null) { + new File(file.getParent()).mkdirs(); + } if (!createNewFile(file)) { throw new ItemStreamException("Output file was not created: [" + file.getAbsolutePath() + "]"); 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 d0adae092..b4091f0c7 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 @@ -109,6 +109,29 @@ public class FileUtilsTests { dir1.delete(); } } + + /** + * If the directories on the file path do not exist, they should be created + * This must be true also in append mode + */ + @Test + public void testCreateDirectoryStructureAppendMode() { + File file = new File("testDirectory/testDirectory2/testFile.tmp"); + File dir1 = new File("testDirectory"); + File dir2 = new File("testDirectory/testDirectory2"); + + try { + FileUtils.setUpOutputFile(file, false, true, false); + assertTrue(file.exists()); + assertTrue(dir1.exists()); + assertTrue(dir2.exists()); + } + finally { + file.delete(); + dir2.delete(); + dir1.delete(); + } + } @Test public void testBadFile(){