- java.lang.Object
-
- org.junit.jupiter.engine.extension.TempDirectory
-
- All Implemented Interfaces:
BeforeAllCallback
,BeforeEachCallback
,Extension
,ParameterResolver
class TempDirectory extends java.lang.Object implements BeforeAllCallback, BeforeEachCallback, ParameterResolver
TempDirectory
is a JUnit Jupiter extension that creates and cleans up temporary directories if field in a test class or a parameter in a lifecycle method or test method is annotated with@TempDir
.Consult the Javadoc for
TempDir
for details on the contract.- Since:
- 5.4
- See Also:
TempDir
,Files.createTempDirectory(java.nio.file.Path, java.lang.String, java.nio.file.attribute.FileAttribute<?>...)
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description (package private) static class
TempDirectory.CloseablePath
private static class
TempDirectory.FieldContext
(package private) static interface
TempDirectory.FileOperations
(package private) static class
TempDirectory.Scope
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.String
CHILD_FAILED
private JupiterConfiguration
configuration
private static java.lang.String
FAILURE_TRACKER
(package private) static java.lang.String
FILE_OPERATIONS_KEY
private static java.lang.String
KEY
(package private) static ExtensionContext.Namespace
NAMESPACE
-
Constructor Summary
Constructors Constructor Description TempDirectory(JupiterConfiguration configuration)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private void
assertNonFinalField(java.lang.reflect.Field field)
private void
assertSupportedType(java.lang.String target, java.lang.Class<?> type)
void
beforeAll(ExtensionContext context)
Perform field injection for non-private,static
fields (i.e., class fields) of typePath
orFile
that are annotated with@TempDir
.void
beforeEach(ExtensionContext context)
Perform field injection for non-private, non-static fields (i.e., instance fields) of typePath
orFile
that are annotated with@TempDir
.(package private) static TempDirectory.CloseablePath
createTempDir(TempDirFactory factory, CleanupMode cleanupMode, AnnotatedElementContext elementContext, ExtensionContext extensionContext)
private CleanupMode
determineCleanupMode(TempDir tempDir)
private CleanupMode
determineCleanupModeForField(java.lang.reflect.Field field)
private CleanupMode
determineCleanupModeForParameter(ParameterContext parameterContext)
private TempDirFactory
determineTempDirFactory(TempDir tempDir, TempDirectory.Scope scope)
private TempDirFactory
determineTempDirFactoryForField(java.lang.reflect.Field field, TempDirectory.Scope scope)
private TempDirFactory
determineTempDirFactoryForParameter(ParameterContext parameterContext, TempDirectory.Scope scope)
private java.lang.Object
getPathOrFile(AnnotatedElementContext elementContext, java.lang.Class<?> type, TempDirFactory factory, CleanupMode cleanupMode, TempDirectory.Scope scope, ExtensionContext extensionContext)
private TempDirectory.Scope
getScope(ExtensionContext context)
private void
injectFields(ExtensionContext context, java.lang.Object testInstance, java.lang.Class<?> testClass, java.util.function.Predicate<java.lang.reflect.Field> predicate)
private void
injectInstanceFields(ExtensionContext context, java.lang.Object instance)
private void
injectStaticFields(ExtensionContext context, java.lang.Class<?> testClass)
private static void
installFailureTracker(ExtensionContext context)
java.lang.Object
resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
Resolve the current temporary directory for theParameter
in the suppliedParameterContext
.private static boolean
selfOrChildFailed(ExtensionContext context)
boolean
supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
-
-
-
Field Detail
-
NAMESPACE
static final ExtensionContext.Namespace NAMESPACE
-
KEY
private static final java.lang.String KEY
- See Also:
- Constant Field Values
-
FAILURE_TRACKER
private static final java.lang.String FAILURE_TRACKER
- See Also:
- Constant Field Values
-
CHILD_FAILED
private static final java.lang.String CHILD_FAILED
- See Also:
- Constant Field Values
-
FILE_OPERATIONS_KEY
static final java.lang.String FILE_OPERATIONS_KEY
- See Also:
- Constant Field Values
-
configuration
private final JupiterConfiguration configuration
-
-
Constructor Detail
-
TempDirectory
public TempDirectory(JupiterConfiguration configuration)
-
-
Method Detail
-
beforeAll
public void beforeAll(ExtensionContext context)
Perform field injection for non-private,static
fields (i.e., class fields) of typePath
orFile
that are annotated with@TempDir
.- Specified by:
beforeAll
in interfaceBeforeAllCallback
- Parameters:
context
- the current extension context; nevernull
-
beforeEach
public void beforeEach(ExtensionContext context)
Perform field injection for non-private, non-static fields (i.e., instance fields) of typePath
orFile
that are annotated with@TempDir
.- Specified by:
beforeEach
in interfaceBeforeEachCallback
- Parameters:
context
- the current extension context; nevernull
-
installFailureTracker
private static void installFailureTracker(ExtensionContext context)
-
injectStaticFields
private void injectStaticFields(ExtensionContext context, java.lang.Class<?> testClass)
-
injectInstanceFields
private void injectInstanceFields(ExtensionContext context, java.lang.Object instance)
-
injectFields
private void injectFields(ExtensionContext context, java.lang.Object testInstance, java.lang.Class<?> testClass, java.util.function.Predicate<java.lang.reflect.Field> predicate)
-
supportsParameter
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
- Specified by:
supportsParameter
in interfaceParameterResolver
- Parameters:
parameterContext
- the context for the parameter for which an argument should be resolved; nevernull
extensionContext
- the extension context for theExecutable
about to be invoked; nevernull
- Returns:
true
if this resolver can resolve an argument for the parameter- See Also:
ParameterResolver.resolveParameter(org.junit.jupiter.api.extension.ParameterContext, org.junit.jupiter.api.extension.ExtensionContext)
,ParameterContext
-
resolveParameter
public java.lang.Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
Resolve the current temporary directory for theParameter
in the suppliedParameterContext
.- Specified by:
resolveParameter
in interfaceParameterResolver
- Parameters:
parameterContext
- the context for the parameter for which an argument should be resolved; nevernull
extensionContext
- the extension context for theExecutable
about to be invoked; nevernull
- Returns:
- the resolved argument for the parameter; may only be
null
if the parameter type is not a primitive - See Also:
ParameterResolver.supportsParameter(org.junit.jupiter.api.extension.ParameterContext, org.junit.jupiter.api.extension.ExtensionContext)
,ParameterContext
-
determineCleanupModeForField
private CleanupMode determineCleanupModeForField(java.lang.reflect.Field field)
-
determineCleanupModeForParameter
private CleanupMode determineCleanupModeForParameter(ParameterContext parameterContext)
-
determineCleanupMode
private CleanupMode determineCleanupMode(TempDir tempDir)
-
getScope
private TempDirectory.Scope getScope(ExtensionContext context)
-
determineTempDirFactoryForField
private TempDirFactory determineTempDirFactoryForField(java.lang.reflect.Field field, TempDirectory.Scope scope)
-
determineTempDirFactoryForParameter
private TempDirFactory determineTempDirFactoryForParameter(ParameterContext parameterContext, TempDirectory.Scope scope)
-
determineTempDirFactory
private TempDirFactory determineTempDirFactory(TempDir tempDir, TempDirectory.Scope scope)
-
assertNonFinalField
private void assertNonFinalField(java.lang.reflect.Field field)
-
assertSupportedType
private void assertSupportedType(java.lang.String target, java.lang.Class<?> type)
-
getPathOrFile
private java.lang.Object getPathOrFile(AnnotatedElementContext elementContext, java.lang.Class<?> type, TempDirFactory factory, CleanupMode cleanupMode, TempDirectory.Scope scope, ExtensionContext extensionContext)
-
createTempDir
static TempDirectory.CloseablePath createTempDir(TempDirFactory factory, CleanupMode cleanupMode, AnnotatedElementContext elementContext, ExtensionContext extensionContext)
-
selfOrChildFailed
private static boolean selfOrChildFailed(ExtensionContext context)
-
-