Skip to content
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

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 33 additions & 38 deletions src/main/java/org/junit/rules/TemporaryFolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Copy link
Contributor

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

getRoot() throws IllegalStateException if before() wasn't (yet?) called.

File root = getRoot();
for (String path : paths) {
if (new File(path).isAbsolute()) {
throw new IOException("folder path \'" + path + "\' is not a relative path");
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also add a check for paths.length > 0. Because in that case we currently return getRoot() which isn't a "new fresh folder".

Copy link
Member Author

Choose a reason for hiding this comment

The 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;
}

/**
Expand Down
54 changes: 45 additions & 9 deletions src/test/java/org/junit/rules/TemporaryFolderUsageTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public void newFileWithGivenNameThrowsIllegalStateExceptionIfCreateWasNotInvoked
}

@Test
public void newFileWithGivenFilenameThrowsIllegalArgumentExceptionIfFileExists() throws IOException {
public void newFileWithGivenFilenameThrowsIOExceptionIfFileExists() throws IOException {
tempFolder.create();
tempFolder.newFile("MyFile.txt");

Expand All @@ -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");
}

Expand All @@ -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);
}

Expand All @@ -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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be sufficient have a single assertion: assertFileIsDirectory(new File(tempFolder.getRoot(), "temp1/temp2/temp3/temp4")?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't 100% certain that would work on all platforms

Copy link
Member

Choose a reason for hiding this comment

The 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?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. I have no way to verify on Windows though.

}

@Test
Expand Down