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
This commit is contained in:
Veit Hoffmann
2013-08-05 17:58:11 +02:00
committed by Michael Minella
parent 3ea8d3ed65
commit 7c9396da18
2 changed files with 26 additions and 0 deletions

View File

@@ -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()
+ "]");

View File

@@ -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(){