From 3b544f571c2ec23a31e15ecc031a313ffa6ebcee Mon Sep 17 00:00:00 2001 From: Priya Date: Tue, 12 Aug 2014 22:24:46 +0530 Subject: [PATCH] #946 - Updated TemporaryFolder.newFolder() to give a better error message if a path contains a slash --- .../java/org/junit/rules/TemporaryFolder.java | 16 +++++++++++++++ .../rules/TemporaryFolderUsageTest.java | 20 ++++++++++++++++++- 2 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/junit/rules/TemporaryFolder.java b/src/main/java/org/junit/rules/TemporaryFolder.java index 2bf9b03d810c..14ebf6b017bb 100644 --- a/src/main/java/org/junit/rules/TemporaryFolder.java +++ b/src/main/java/org/junit/rules/TemporaryFolder.java @@ -92,6 +92,7 @@ public File newFolder(String... folderNames) throws IOException { File file = getRoot(); for (int i = 0; i < folderNames.length; i++) { String folderName = folderNames[i]; + validateFolderName(folderName); file = new File(file, folderName); if (!file.mkdir() && isLastElementInArray(i, folderNames)) { throw new IOException( @@ -100,6 +101,21 @@ public File newFolder(String... folderNames) throws IOException { } return file; } + + /** + * Validates if multiple path components were used while creating a folder. + * + * @param folderName + * Name of the folder being created + */ + private void validateFolderName(String folderName) throws IOException { + File tempFile = new File(folderName); + if (tempFile.getParent() != null) { + String errorMsg = "Folder name cannot consist of multiple path components separated by a file separator." + + " Please use newFolder('MyParentFolder','MyFolder') to create hierarchies of folders"; + throw new IOException(errorMsg); + } + } private boolean isLastElementInArray(int index, String[] array) { return index == array.length - 1; diff --git a/src/test/java/org/junit/tests/experimental/rules/TemporaryFolderUsageTest.java b/src/test/java/org/junit/tests/experimental/rules/TemporaryFolderUsageTest.java index c208aaddae58..314a14101236 100644 --- a/src/test/java/org/junit/tests/experimental/rules/TemporaryFolderUsageTest.java +++ b/src/test/java/org/junit/tests/experimental/rules/TemporaryFolderUsageTest.java @@ -84,7 +84,16 @@ public void newFolderWithGivenFolderThrowsIllegalArgumentExceptionIfFolderExists thrown.expectMessage("a folder with the name 'level1' already exists"); tempFolder.newFolder("level1"); } - + + @Test + public void newFolderWithGivenFolderThrowsIOExceptionIfFolderNameConsistsOfMultiplePathComponents() + throws IOException { + tempFolder.create(); + thrown.expect(IOException.class); + thrown.expectMessage("name cannot consist of multiple path components"); + tempFolder.newFolder("temp1/temp2"); + } + @Test public void newFolderWithGivenPathThrowsIllegalArgumentExceptionIfPathExists() throws IOException { tempFolder.create(); @@ -95,6 +104,15 @@ public void newFolderWithGivenPathThrowsIllegalArgumentExceptionIfPathExists() t tempFolder.newFolder("level1", "level2", "level3"); } + @Test + public void newFolderWithGivenPathThrowsIOExceptionIfFolderNamesConsistOfMultiplePathComponents() + throws IOException { + tempFolder.create(); + thrown.expect(IOException.class); + thrown.expectMessage("name cannot consist of multiple path components"); + tempFolder.newFolder("temp1", "temp2", "temp3/temp4"); + } + @Test public void createInitializesRootFolder() throws IOException { tempFolder.create();