Class TempDirectory
- 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
TempDirectoryis 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
TempDirfor 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 private static classTempDirectory.CloseablePathprivate static classTempDirectory.UndeletableFileException
-
Field Summary
Fields Modifier and Type Field Description private static java.lang.StringKEYprivate static ExtensionContext.NamespaceNAMESPACEprivate static java.lang.StringTEMP_DIR_PREFIX
-
Constructor Summary
Constructors Constructor Description TempDirectory()
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private voidassertSupportedType(java.lang.String target, java.lang.Class<?> type)private voidassertValidFieldCandidate(java.lang.reflect.Field field)voidbeforeAll(ExtensionContext context)Perform field injection for non-private,staticfields (i.e., class fields) of typePathorFilethat are annotated with@TempDir.voidbeforeEach(ExtensionContext context)Perform field injection for non-private, non-static fields (i.e., instance fields) of typePathorFilethat are annotated with@TempDir.private static TempDirectory.CloseablePathcreateTempDir()private java.lang.ObjectgetPathOrFile(java.lang.Class<?> type, ExtensionContext extensionContext)private voidinjectFields(ExtensionContext context, java.lang.Object testInstance, java.lang.Class<?> testClass, java.util.function.Predicate<java.lang.reflect.Field> predicate)private voidinjectInstanceFields(ExtensionContext context, java.lang.Object instance)private voidinjectStaticFields(ExtensionContext context, java.lang.Class<?> testClass)java.lang.ObjectresolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)Resolve the current temporary directory for theParameterin the suppliedParameterContext.booleansupportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
-
-
-
Field Detail
-
NAMESPACE
private static final ExtensionContext.Namespace NAMESPACE
-
KEY
private static final java.lang.String KEY
- See Also:
- Constant Field Values
-
TEMP_DIR_PREFIX
private static final java.lang.String TEMP_DIR_PREFIX
- See Also:
- Constant Field Values
-
-
Method Detail
-
beforeAll
public void beforeAll(ExtensionContext context)
Perform field injection for non-private,staticfields (i.e., class fields) of typePathorFilethat are annotated with@TempDir.- Specified by:
beforeAllin 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 typePathorFilethat are annotated with@TempDir.- Specified by:
beforeEachin interfaceBeforeEachCallback- Parameters:
context- the current extension context; nevernull
-
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)
-
assertValidFieldCandidate
private void assertValidFieldCandidate(java.lang.reflect.Field field)
-
supportsParameter
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
- Specified by:
supportsParameterin interfaceParameterResolver- Parameters:
parameterContext- the context for the parameter for which an argument should be resolved; nevernullextensionContext- the extension context for theExecutableabout to be invoked; nevernull- Returns:
trueif 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 theParameterin the suppliedParameterContext.- Specified by:
resolveParameterin interfaceParameterResolver- Parameters:
parameterContext- the context for the parameter for which an argument should be resolved; nevernullextensionContext- the extension context for theExecutableabout to be invoked; nevernull- Returns:
- the resolved argument for the parameter; may only be
nullif the parameter type is not a primitive - See Also:
ParameterResolver.supportsParameter(org.junit.jupiter.api.extension.ParameterContext, org.junit.jupiter.api.extension.ExtensionContext),ParameterContext
-
assertSupportedType
private void assertSupportedType(java.lang.String target, java.lang.Class<?> type)
-
getPathOrFile
private java.lang.Object getPathOrFile(java.lang.Class<?> type, ExtensionContext extensionContext)
-
createTempDir
private static TempDirectory.CloseablePath createTempDir()
-
-