-
Notifications
You must be signed in to change notification settings - Fork 3.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
TemporaryFolder.newFolder(String...) supports path separator #1406
Changes from all commits
dcae43c
fc2f040
741571f
70a86fb
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -170,56 +170,51 @@ public File newFile() throws IOException { | |
* Returns a new fresh folder with the given path under the temporary | ||
* folder. | ||
*/ | ||
public File newFolder(String folder) throws IOException { | ||
if (new File(folder).isAbsolute()) { | ||
throw new IOException("folder name must be a relative path"); | ||
} | ||
File file = new File(getRoot(), folder); | ||
if (!file.mkdirs()) { | ||
throw new IOException( | ||
"a folder with the name \'" + folder + "\' already exists"); | ||
} | ||
return file; | ||
public File newFolder(String path) throws IOException { | ||
return newFolder(new String[]{path}); | ||
} | ||
|
||
/** | ||
* Returns a new fresh folder with the given name(s) under the temporary | ||
* Returns a new fresh folder with the given paths under the temporary | ||
* folder. For example, if you pass in the strings {@code "parent"} and {@code "child"} | ||
* then a directory named {@code "parent"} will be created under the temporary folder | ||
* and a directory named {@code "child"} will be created under the newly-created | ||
* {@code "parent"} directory. | ||
*/ | ||
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)) { | ||
public File newFolder(String... paths) throws IOException { | ||
if (paths.length == 0) { | ||
throw new IllegalArgumentException("must pass at least one path"); | ||
} | ||
|
||
/* | ||
* Before checking if the paths are absolute paths, check if create() was ever called, | ||
* and if it wasn't, throw IllegalStateException. | ||
*/ | ||
File root = getRoot(); | ||
for (String path : paths) { | ||
if (new File(path).isAbsolute()) { | ||
throw new IOException("folder path \'" + path + "\' is not a relative path"); | ||
} | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should also add a check for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, I noticed that was the current behavior, too. I wasn't sure you wanted that in the same pull. I added the fix in a new commit. I also added a commit fixing some incorrect test method names. |
||
|
||
File relativePath = null; | ||
File file = root; | ||
boolean lastMkdirsCallSuccessful = true; | ||
for (int i = 0; i < paths.length; i++) { | ||
relativePath = new File(relativePath, paths[i]); | ||
file = new File(root, relativePath.getPath()); | ||
|
||
lastMkdirsCallSuccessful = file.mkdirs(); | ||
if (!lastMkdirsCallSuccessful && !file.isDirectory()) { | ||
throw new IOException( | ||
"a folder with the name \'" + folderName + "\' already exists"); | ||
"could not create a folder with the path \'" + relativePath.getPath() + "\'"); | ||
} | ||
} | ||
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); | ||
if (!lastMkdirsCallSuccessful) { | ||
throw new IOException( | ||
"a folder with the path \'" + relativePath.getPath() + "\' already exists"); | ||
} | ||
} | ||
|
||
private boolean isLastElementInArray(int index, String[] array) { | ||
return index == array.length - 1; | ||
return file; | ||
} | ||
|
||
/** | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -53,7 +53,7 @@ public void newFileWithGivenNameThrowsIllegalStateExceptionIfCreateWasNotInvoked | |
} | ||
|
||
@Test | ||
public void newFileWithGivenFilenameThrowsIllegalArgumentExceptionIfFileExists() throws IOException { | ||
public void newFileWithGivenFilenameThrowsIOExceptionIfFileExists() throws IOException { | ||
tempFolder.create(); | ||
tempFolder.newFile("MyFile.txt"); | ||
|
||
|
@@ -74,12 +74,23 @@ public void newFolderWithGivenPathThrowsIllegalStateExceptionIfCreateWasNotInvok | |
} | ||
|
||
@Test | ||
public void newFolderWithGivenFolderThrowsIllegalArgumentExceptionIfFolderExists() throws IOException { | ||
public void newFolderWithGivenFolderThrowsIOExceptionIfFolderExists() throws IOException { | ||
tempFolder.create(); | ||
tempFolder.newFolder("level1"); | ||
|
||
thrown.expect(IOException.class); | ||
thrown.expectMessage("a folder with the name 'level1' already exists"); | ||
thrown.expectMessage("a folder with the path 'level1' already exists"); | ||
tempFolder.newFolder("level1"); | ||
} | ||
|
||
@Test | ||
public void newFolderWithGivenFolderThrowsIOExceptionIfFileExists() throws IOException { | ||
tempFolder.create(); | ||
File file = new File(tempFolder.getRoot(), "level1"); | ||
assertTrue("Could not create" + file, file.createNewFile()); | ||
|
||
thrown.expect(IOException.class); | ||
thrown.expectMessage("could not create a folder with the path 'level1'"); | ||
tempFolder.newFolder("level1"); | ||
} | ||
|
||
|
@@ -95,7 +106,7 @@ public void newFolderWithPathStartingWithFileSeparatorThrowsIOException() | |
} | ||
tempFolder.create(); | ||
thrown.expect(IOException.class); | ||
thrown.expectMessage("folder name must be a relative path"); | ||
thrown.expectMessage("folder path '/temp1' is not a relative path"); | ||
tempFolder.newFolder(fileAtRoot); | ||
} | ||
|
||
|
@@ -120,22 +131,47 @@ public void newFolderWithPathContainingForwardSlashCreatesDirectories() | |
} | ||
|
||
@Test | ||
public void newFolderWithGivenPathThrowsIllegalArgumentExceptionIfPathExists() throws IOException { | ||
public void newFolderWithGivenPathThrowsIOExceptionIfFolderExists() throws IOException { | ||
tempFolder.create(); | ||
tempFolder.newFolder("level1", "level2", "level3"); | ||
|
||
thrown.expect(IOException.class); | ||
thrown.expectMessage("a folder with the name 'level3' already exists"); | ||
String path = "level1" + File.separator + "level2" + File.separator + "level3"; | ||
thrown.expectMessage("a folder with the path '" + path + "' already exists"); | ||
tempFolder.newFolder("level1", "level2", "level3"); | ||
} | ||
|
||
@Test | ||
public void newFolderWithGivenPathThrowsIOExceptionIfFolderNamesConsistOfMultiplePathComponents() | ||
public void newFolderWithGivenEmptyArrayThrowsIllegalArgumentException() throws IOException { | ||
tempFolder.create(); | ||
|
||
thrown.expect(IllegalArgumentException.class); | ||
thrown.expectMessage("must pass at least one path"); | ||
tempFolder.newFolder(new String[0]); | ||
} | ||
|
||
@Test | ||
public void newFolderWithPathsContainingForwardSlashCreatesFullPath() | ||
throws IOException { | ||
tempFolder.create(); | ||
thrown.expect(IOException.class); | ||
thrown.expectMessage("name cannot consist of multiple path components"); | ||
tempFolder.newFolder("temp1", "temp2", "temp3/temp4"); | ||
|
||
File directory = new File(tempFolder.getRoot(), "temp1"); | ||
assertFileIsDirectory(directory); | ||
directory = new File(directory, "temp2/temp3/temp4"); | ||
assertFileIsDirectory(directory); | ||
} | ||
|
||
@Test | ||
public void newFolderWithPathsContainingFileSeparatorCreatesFullPath() | ||
throws IOException { | ||
tempFolder.create(); | ||
tempFolder.newFolder("temp1", "temp2", "temp3" + File.separator + "temp4"); | ||
|
||
File directory = new File(tempFolder.getRoot(), "temp1"); | ||
assertFileIsDirectory(directory); | ||
directory = new File(directory, "temp2/temp3/temp4"); | ||
assertFileIsDirectory(directory); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wouldn't it be sufficient have a single assertion: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I wasn't 100% certain that would work on all platforms There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm pretty sure it should. Can we assume it does until proven otherwise? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure. I have no way to verify on Windows though. |
||
} | ||
|
||
@Test | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the comment is wrong because I could not find the
IllegalStateException
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getRoot()
throwsIllegalStateException
ifbefore()
wasn't (yet?) called.