From 01d7ec2c8212ed9395877cbf77eb5139d80b74e1 Mon Sep 17 00:00:00 2001 From: Stefan Birkner Date: Fri, 26 Oct 2018 22:14:17 +0200 Subject: [PATCH 1/2] Check that newFolder fails for read only parent --- .../org/junit/rules/TemporaryFolderUsageTest.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/test/java/org/junit/rules/TemporaryFolderUsageTest.java b/src/test/java/org/junit/rules/TemporaryFolderUsageTest.java index 925408fa0818..f70c59f409fb 100644 --- a/src/test/java/org/junit/rules/TemporaryFolderUsageTest.java +++ b/src/test/java/org/junit/rules/TemporaryFolderUsageTest.java @@ -93,6 +93,17 @@ public void newFolderWithGivenFolderThrowsIOExceptionIfFileExists() throws IOExc thrown.expectMessage("could not create a folder with the path 'level1'"); tempFolder.newFolder("level1"); } + + @Test + public void newFolderWithGivenFolderThrowsIOExceptionWhenFolderCannotBeCreated() throws IOException { + tempFolder.create(); + assertTrue("Could not make folder " + tempFolder.getRoot() + " read only.", + tempFolder.getRoot().setReadOnly()); + + thrown.expect(IOException.class); + thrown.expectMessage("could not create a folder with the path 'level1'"); + tempFolder.newFolder("level1"); + } @Test public void newFolderWithPathStartingWithFileSeparatorThrowsIOException() From 10a863eee43c9e13d1647675016b086f527a0697 Mon Sep 17 00:00:00 2001 From: Stefan Birkner Date: Fri, 26 Oct 2018 22:23:36 +0200 Subject: [PATCH 2/2] Improve message if file exists instead of folder When TemporaryFolder(String path) or TemporaryFolder(String... paths) is called with a path that matches the path of an existing file then an IOException with a message like "a file with path exists" is thrown. --- src/main/java/org/junit/rules/TemporaryFolder.java | 9 +++++++-- .../java/org/junit/rules/TemporaryFolderUsageTest.java | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/junit/rules/TemporaryFolder.java b/src/main/java/org/junit/rules/TemporaryFolder.java index fe0ac38b0f90..1a6a77060805 100644 --- a/src/main/java/org/junit/rules/TemporaryFolder.java +++ b/src/main/java/org/junit/rules/TemporaryFolder.java @@ -206,8 +206,13 @@ public File newFolder(String... paths) throws IOException { lastMkdirsCallSuccessful = file.mkdirs(); if (!lastMkdirsCallSuccessful && !file.isDirectory()) { - throw new IOException( - "could not create a folder with the path \'" + relativePath.getPath() + "\'"); + if (file.exists()) { + throw new IOException( + "a file with the path \'" + relativePath.getPath() + "\' exists"); + } else { + throw new IOException( + "could not create a folder with the path \'" + relativePath.getPath() + "\'"); + } } } if (!lastMkdirsCallSuccessful) { diff --git a/src/test/java/org/junit/rules/TemporaryFolderUsageTest.java b/src/test/java/org/junit/rules/TemporaryFolderUsageTest.java index f70c59f409fb..8213e39ede49 100644 --- a/src/test/java/org/junit/rules/TemporaryFolderUsageTest.java +++ b/src/test/java/org/junit/rules/TemporaryFolderUsageTest.java @@ -90,7 +90,7 @@ public void newFolderWithGivenFolderThrowsIOExceptionIfFileExists() throws IOExc assertTrue("Could not create" + file, file.createNewFile()); thrown.expect(IOException.class); - thrown.expectMessage("could not create a folder with the path 'level1'"); + thrown.expectMessage("a file with the path 'level1' exists"); tempFolder.newFolder("level1"); }