Skip to content

Commit

Permalink
Implemented random temp file/folder creation
Browse files Browse the repository at this point in the history
  • Loading branch information
drothmaler authored and Daniel Rothmaler committed Sep 8, 2011
1 parent 84d0e8a commit ce86773
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 12 deletions.
22 changes: 19 additions & 3 deletions src/main/java/org/junit/rules/TemporaryFolder.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ protected void after() {
* for testing purposes only. Do not use.
*/
public void create() throws IOException {
folder= File.createTempFile("junit", "");
folder.delete();
folder.mkdir();
folder= newFolder();
}

/**
Expand All @@ -56,6 +54,13 @@ public File newFile(String fileName) throws IOException {
return file;
}

/**
* Returns a new fresh file with a random name under the temporary folder.
*/
public File newFile() throws IOException {
return File.createTempFile("junit", null, folder);
}

/**
* Returns a new fresh folder with the given name under the temporary folder.
*/
Expand All @@ -68,6 +73,17 @@ public File newFolder(String... folderNames) {
return file;
}

/**
* Returns a new fresh folder with a random name under the temporary
* folder.
*/
public File newFolder() throws IOException {
File createdFolder= File.createTempFile("junit", "", folder);
createdFolder.delete();
createdFolder.mkdir();
return createdFolder;
}

/**
* @return the location of this temporary folder.
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,39 @@
package org.junit.tests.experimental.rules;

import static org.hamcrest.core.IsNot.not;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;
import static org.junit.experimental.results.PrintableResult.testResult;
import static org.junit.experimental.results.ResultMatchers.isSuccessful;
import static org.junit.internal.matchers.IsCollectionContaining.hasItem;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

public class TempFolderRuleTest {
private static File createdFile;
private static File[] createdFiles= new File[20];

public static class HasTempFolder {
@Rule
public TemporaryFolder folder= new TemporaryFolder();

@Test
public void testUsingTempFolder() throws IOException {
createdFile= folder.newFile("myfile.txt");
assertTrue(createdFile.exists());
createdFiles[0]= folder.newFile("myfile.txt");
assertTrue(createdFiles[0].exists());
}
}

@Test
public void tempFolderIsDeleted() {
assertThat(testResult(HasTempFolder.class), isSuccessful());
assertFalse(createdFile.exists());
assertFalse(createdFiles[0].exists());
}

public static class CreatesSubFolder {
Expand All @@ -41,8 +44,8 @@ public static class CreatesSubFolder {
public void testUsingTempFolder() throws IOException {
String subfolder = "subfolder";
String filename = "a.txt";
createdFile= folder.newFolder(subfolder);
new File(createdFile, filename).createNewFile();
createdFiles[0]= folder.newFolder(subfolder);
new File(createdFiles[0], filename).createNewFile();

File expectedFile = new File(folder.getRoot(), join(subfolder, filename));

Expand All @@ -55,8 +58,8 @@ public void testUsingTempTreeFolders() throws IOException {
String anotherfolder = "anotherfolder";
String filename = "a.txt";

createdFile = folder.newFolder(subfolder, anotherfolder);
new File(createdFile, filename).createNewFile();
createdFiles[0] = folder.newFolder(subfolder, anotherfolder);
new File(createdFiles[0], filename).createNewFile();

File expectedFile = new File(folder.getRoot(), join(subfolder, anotherfolder, filename));

Expand All @@ -75,7 +78,54 @@ private String join(String... folderNames) {
@Test
public void subFolderIsDeleted() {
assertThat(testResult(CreatesSubFolder.class), isSuccessful());
assertFalse(createdFile.exists());
assertFalse(createdFiles[0].exists());
}

public static class CreatesRandomSubFolders {
@Rule
public TemporaryFolder folder= new TemporaryFolder();

@Test
public void testUsingRandomTempFolders() throws IOException {
for (int i= 0; i < 20; i++) {
File newFolder= folder.newFolder();
assertThat(Arrays.asList(createdFiles), not(hasItem(newFolder)));
createdFiles[i]= newFolder;
new File(newFolder, "a.txt").createNewFile();
assertTrue(newFolder.exists());
}
}
}

@Test
public void randomSubFoldersAreDeleted() {
assertThat(testResult(CreatesRandomSubFolders.class), isSuccessful());
for (File f : createdFiles) {
assertFalse(f.exists());
}
}

public static class CreatesRandomFiles {
@Rule
public TemporaryFolder folder= new TemporaryFolder();

@Test
public void testUsingRandomTempFiles() throws IOException {
for (int i= 0; i < 20; i++) {
File newFile= folder.newFile();
assertThat(Arrays.asList(createdFiles), not(hasItem(newFile)));
createdFiles[i]= newFile;
assertTrue(newFile.exists());
}
}
}

@Test
public void randomFilesAreDeleted() {
assertThat(testResult(CreatesRandomFiles.class), isSuccessful());
for (File f : createdFiles) {
assertFalse(f.exists());
}
}

@Test
Expand All @@ -88,6 +138,16 @@ public void recursiveDeleteFolderWithOneElement() throws IOException {
assertFalse(folder.getRoot().exists());
}

@Test
public void recursiveDeleteFolderWithOneRandomElement() throws IOException {
TemporaryFolder folder= new TemporaryFolder();
folder.create();
File file= folder.newFile();
folder.delete();
assertFalse(file.exists());
assertFalse(folder.getRoot().exists());
}

@Test
public void recursiveDeleteFolderWithZeroElements() throws IOException {
TemporaryFolder folder= new TemporaryFolder();
Expand Down

0 comments on commit ce86773

Please sign in to comment.