public class TemporaryFolder extends ExternalResource
Example of usage:
public static class HasTempFolder { @Rule public TemporaryFolder folder= new TemporaryFolder(); @Test public void testUsingTempFolder() throws IOException { File createdFile= folder.newFile("myfile.txt"); File createdFolder= folder.newFolder("subfolder"); // ... } }
TemporaryFolder rule supports assured deletion mode, which
will fail the test in case deletion fails with AssertionError
.
Creating TemporaryFolder with assured deletion:
@Rule public TemporaryFolder folder= TemporaryFolder.builder().assureDeletion().build();
Modifier and Type | Class and Description |
---|---|
static class |
TemporaryFolder.Builder
Builds an instance of
TemporaryFolder . |
Modifier and Type | Field and Description |
---|---|
private boolean |
assureDeletion |
private java.io.File |
folder |
private java.io.File |
parentFolder |
private static int |
TEMP_DIR_ATTEMPTS |
private static java.lang.String |
TMP_PREFIX |
Modifier | Constructor and Description |
---|---|
|
TemporaryFolder()
Create a temporary folder which uses system default temporary-file
directory to create temporary resources.
|
|
TemporaryFolder(java.io.File parentFolder)
Create a temporary folder which uses the specified directory to create
temporary resources.
|
protected |
TemporaryFolder(TemporaryFolder.Builder builder)
Create a
TemporaryFolder initialized with
values from a builder. |
Modifier and Type | Method and Description |
---|---|
protected void |
after()
Override to tear down your specific external resource.
|
protected void |
before()
Override to set up your specific external resource.
|
static TemporaryFolder.Builder |
builder()
Returns a new builder for building an instance of
TemporaryFolder . |
void |
create()
for testing purposes only.
|
private static java.io.File |
createTemporaryFolderIn(java.io.File parentFolder) |
private static java.io.File |
createTemporaryFolderWithFileApi(java.io.File parentFolder) |
private static java.io.File |
createTemporaryFolderWithNioApi(java.io.File parentFolder) |
void |
delete()
Delete all files and folders under the temporary folder.
|
java.io.File |
getRoot() |
java.io.File |
newFile()
Returns a new fresh file with a random name under the temporary folder.
|
java.io.File |
newFile(java.lang.String fileName)
Returns a new fresh file with the given name under the temporary folder.
|
java.io.File |
newFolder()
Returns a new fresh folder with a random name under the temporary folder.
|
java.io.File |
newFolder(java.lang.String... paths)
Returns a new fresh folder with the given paths under the temporary
folder.
|
java.io.File |
newFolder(java.lang.String path)
Returns a new fresh folder with the given path under the temporary
folder.
|
private boolean |
recursiveDelete(java.io.File file) |
private boolean |
tryDelete()
Tries to delete all files and folders under the temporary folder and
returns whether deletion was successful or not.
|
apply
private final java.io.File parentFolder
private final boolean assureDeletion
private java.io.File folder
private static final int TEMP_DIR_ATTEMPTS
private static final java.lang.String TMP_PREFIX
public TemporaryFolder()
public TemporaryFolder(java.io.File parentFolder)
parentFolder
- folder where temporary resources will be created.
If null
then system default temporary-file directory is used.protected TemporaryFolder(TemporaryFolder.Builder builder)
TemporaryFolder
initialized with
values from a builder.public static TemporaryFolder.Builder builder()
TemporaryFolder
.protected void before() throws java.lang.Throwable
ExternalResource
before
in class ExternalResource
java.lang.Throwable
- if setup fails (which will disable after
protected void after()
ExternalResource
after
in class ExternalResource
public void create() throws java.io.IOException
java.io.IOException
public java.io.File newFile(java.lang.String fileName) throws java.io.IOException
java.io.IOException
public java.io.File newFile() throws java.io.IOException
java.io.IOException
public java.io.File newFolder(java.lang.String path) throws java.io.IOException
java.io.IOException
public java.io.File newFolder(java.lang.String... paths) throws java.io.IOException
"parent"
and "child"
then a directory named "parent"
will be created under the temporary folder
and a directory named "child"
will be created under the newly-created
"parent"
directory.java.io.IOException
public java.io.File newFolder() throws java.io.IOException
java.io.IOException
private static java.io.File createTemporaryFolderIn(java.io.File parentFolder) throws java.io.IOException
java.io.IOException
private static java.io.File createTemporaryFolderWithNioApi(java.io.File parentFolder) throws java.lang.ClassNotFoundException, java.lang.NoSuchMethodException, java.lang.reflect.InvocationTargetException, java.lang.IllegalAccessException
java.lang.ClassNotFoundException
java.lang.NoSuchMethodException
java.lang.reflect.InvocationTargetException
java.lang.IllegalAccessException
private static java.io.File createTemporaryFolderWithFileApi(java.io.File parentFolder) throws java.io.IOException
java.io.IOException
public java.io.File getRoot()
public void delete()
Rule
.java.lang.AssertionError
- if unable to clean up resources
and deletion of resources is assured.private boolean tryDelete()
true
if all resources are deleted successfully,
false
otherwise.private boolean recursiveDelete(java.io.File file)