Package org.junit.rules
Class TemporaryFolder
- java.lang.Object
-
- org.junit.rules.ExternalResource
-
- org.junit.rules.TemporaryFolder
-
- All Implemented Interfaces:
TestRule
public class TemporaryFolder extends ExternalResource
The TemporaryFolder Rule allows creation of files and folders that should be deleted when the test method finishes (whether it passes or fails). By default no exception will be thrown in case the deletion fails.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();
- Since:
- 4.7
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classTemporaryFolder.BuilderBuilds an instance ofTemporaryFolder.
-
Field Summary
Fields Modifier and Type Field Description private booleanassureDeletionprivate java.io.Filefolderprivate java.io.FileparentFolderprivate static intTEMP_DIR_ATTEMPTSprivate static java.lang.StringTMP_PREFIX
-
Constructor Summary
Constructors Modifier Constructor 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.protectedTemporaryFolder(TemporaryFolder.Builder builder)Create aTemporaryFolderinitialized with values from a builder.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description protected voidafter()Override to tear down your specific external resource.protected voidbefore()Override to set up your specific external resource.static TemporaryFolder.Builderbuilder()Returns a new builder for building an instance ofTemporaryFolder.voidcreate()for testing purposes only.private static java.io.FilecreateTemporaryFolderIn(java.io.File parentFolder)private static java.io.FilecreateTemporaryFolderWithFileApi(java.io.File parentFolder)private static java.io.FilecreateTemporaryFolderWithNioApi(java.io.File parentFolder)voiddelete()Delete all files and folders under the temporary folder.java.io.FilegetRoot()java.io.FilenewFile()Returns a new fresh file with a random name under the temporary folder.java.io.FilenewFile(java.lang.String fileName)Returns a new fresh file with the given name under the temporary folder.java.io.FilenewFolder()Returns a new fresh folder with a random name under the temporary folder.java.io.FilenewFolder(java.lang.String path)Returns a new fresh folder with the given path under the temporary folder.java.io.FilenewFolder(java.lang.String... paths)Returns a new fresh folder with the given paths under the temporary folder.private booleanrecursiveDelete(java.io.File file)private booleantryDelete()Tries to delete all files and folders under the temporary folder and returns whether deletion was successful or not.-
Methods inherited from class org.junit.rules.ExternalResource
apply
-
-
-
-
Field Detail
-
parentFolder
private final java.io.File parentFolder
-
assureDeletion
private final boolean assureDeletion
-
folder
private java.io.File folder
-
TEMP_DIR_ATTEMPTS
private static final int TEMP_DIR_ATTEMPTS
- See Also:
- Constant Field Values
-
TMP_PREFIX
private static final java.lang.String TMP_PREFIX
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
TemporaryFolder
public TemporaryFolder()
Create a temporary folder which uses system default temporary-file directory to create temporary resources.
-
TemporaryFolder
public TemporaryFolder(java.io.File parentFolder)
Create a temporary folder which uses the specified directory to create temporary resources.- Parameters:
parentFolder- folder where temporary resources will be created. Ifnullthen system default temporary-file directory is used.
-
TemporaryFolder
protected TemporaryFolder(TemporaryFolder.Builder builder)
Create aTemporaryFolderinitialized with values from a builder.
-
-
Method Detail
-
builder
public static TemporaryFolder.Builder builder()
Returns a new builder for building an instance ofTemporaryFolder.- Since:
- 4.13
-
before
protected void before() throws java.lang.ThrowableDescription copied from class:ExternalResourceOverride to set up your specific external resource.- Overrides:
beforein classExternalResource- Throws:
java.lang.Throwable- if setup fails (which will disableafter
-
after
protected void after()
Description copied from class:ExternalResourceOverride to tear down your specific external resource.- Overrides:
afterin classExternalResource
-
create
public void create() throws java.io.IOExceptionfor testing purposes only. Do not use.- Throws:
java.io.IOException
-
newFile
public java.io.File newFile(java.lang.String fileName) throws java.io.IOExceptionReturns a new fresh file with the given name under the temporary folder.- Throws:
java.io.IOException
-
newFile
public java.io.File newFile() throws java.io.IOExceptionReturns a new fresh file with a random name under the temporary folder.- Throws:
java.io.IOException
-
newFolder
public java.io.File newFolder(java.lang.String path) throws java.io.IOExceptionReturns a new fresh folder with the given path under the temporary folder.- Throws:
java.io.IOException
-
newFolder
public java.io.File newFolder(java.lang.String... paths) throws java.io.IOExceptionReturns a new fresh folder with the given paths under the temporary folder. For example, if you pass in the strings"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.- Throws:
java.io.IOException
-
newFolder
public java.io.File newFolder() throws java.io.IOExceptionReturns a new fresh folder with a random name under the temporary folder.- Throws:
java.io.IOException
-
createTemporaryFolderIn
private static java.io.File createTemporaryFolderIn(java.io.File parentFolder) throws java.io.IOException- Throws:
java.io.IOException
-
createTemporaryFolderWithNioApi
private static java.io.File createTemporaryFolderWithNioApi(java.io.File parentFolder) throws java.lang.ClassNotFoundException, java.lang.NoSuchMethodException, java.lang.reflect.InvocationTargetException, java.lang.IllegalAccessException- Throws:
java.lang.ClassNotFoundExceptionjava.lang.NoSuchMethodExceptionjava.lang.reflect.InvocationTargetExceptionjava.lang.IllegalAccessException
-
createTemporaryFolderWithFileApi
private static java.io.File createTemporaryFolderWithFileApi(java.io.File parentFolder) throws java.io.IOException- Throws:
java.io.IOException
-
getRoot
public java.io.File getRoot()
- Returns:
- the location of this temporary folder.
-
delete
public void delete()
Delete all files and folders under the temporary folder. Usually not called directly, since it is automatically applied by theRule.- Throws:
java.lang.AssertionError- if unable to clean up resources and deletion of resources is assured.
-
tryDelete
private boolean tryDelete()
Tries to delete all files and folders under the temporary folder and returns whether deletion was successful or not.- Returns:
trueif all resources are deleted successfully,falseotherwise.
-
recursiveDelete
private boolean recursiveDelete(java.io.File file)
-
-