Search in sources :

Example 1 with CleanupMode

use of org.junit.jupiter.api.io.CleanupMode in project junit5 by junit-team.

the class TempDirectory method injectFields.

private void injectFields(ExtensionContext context, Object testInstance, Class<?> testClass, Predicate<Field> predicate) {
    findAnnotatedFields(testClass, TempDir.class, predicate).forEach(field -> {
        assertSupportedType("field", field.getType());
        try {
            CleanupMode cleanupMode = determineCleanupModeForField(field);
            makeAccessible(field).set(testInstance, getPathOrFile(field, field.getType(), cleanupMode, context));
        } catch (Throwable t) {
            ExceptionUtils.throwAsUncheckedException(t);
        }
    });
}
Also used : TempDir(org.junit.jupiter.api.io.TempDir) CleanupMode(org.junit.jupiter.api.io.CleanupMode)

Example 2 with CleanupMode

use of org.junit.jupiter.api.io.CleanupMode in project junit5 by junit-team.

the class TempDirectory method getPathOrFile.

private Object getPathOrFile(AnnotatedElement sourceElement, Class<?> type, CleanupMode cleanupMode, ExtensionContext extensionContext) {
    Namespace namespace = // 
    getScope(extensionContext) == Scope.PER_DECLARATION ? // 
    NAMESPACE.append(sourceElement) : NAMESPACE;
    Path path = // 
    extensionContext.getStore(namespace).getOrComputeIfAbsent(KEY, __ -> createTempDir(cleanupMode, extensionContext), // 
    CloseablePath.class).get();
    return (type == Path.class) ? path : path.toFile();
}
Also used : Path(java.nio.file.Path) NoSuchFileException(java.nio.file.NoSuchFileException) ReflectionUtils.makeAccessible(org.junit.platform.commons.util.ReflectionUtils.makeAccessible) AnnotationUtils.findAnnotation(org.junit.platform.commons.util.AnnotationUtils.findAnnotation) CloseableResource(org.junit.jupiter.api.extension.ExtensionContext.Store.CloseableResource) ON_SUCCESS(org.junit.jupiter.api.io.CleanupMode.ON_SUCCESS) JupiterConfiguration(org.junit.jupiter.engine.config.JupiterConfiguration) ExtensionContext(org.junit.jupiter.api.extension.ExtensionContext) Constructor(java.lang.reflect.Constructor) LoggerFactory(org.junit.platform.commons.logging.LoggerFactory) NEVER(org.junit.jupiter.api.io.CleanupMode.NEVER) Parameter(java.lang.reflect.Parameter) Path(java.nio.file.Path) Namespace(org.junit.jupiter.api.extension.ExtensionContext.Namespace) SimpleFileVisitor(java.nio.file.SimpleFileVisitor) Files(java.nio.file.Files) Predicate(java.util.function.Predicate) Logger(org.junit.platform.commons.logging.Logger) ParameterResolutionException(org.junit.jupiter.api.extension.ParameterResolutionException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) IOException(java.io.IOException) AnnotationUtils.findAnnotatedFields(org.junit.platform.commons.util.AnnotationUtils.findAnnotatedFields) CleanupMode(org.junit.jupiter.api.io.CleanupMode) Field(java.lang.reflect.Field) BasicFileAttributes(java.nio.file.attribute.BasicFileAttributes) CONTINUE(java.nio.file.FileVisitResult.CONTINUE) ExtensionConfigurationException(org.junit.jupiter.api.extension.ExtensionConfigurationException) ReflectionUtils(org.junit.platform.commons.util.ReflectionUtils) Collectors.joining(java.util.stream.Collectors.joining) File(java.io.File) FileVisitResult(java.nio.file.FileVisitResult) ParameterContext(org.junit.jupiter.api.extension.ParameterContext) DEFAULT(org.junit.jupiter.api.io.CleanupMode.DEFAULT) EnumConfigurationParameterConverter(org.junit.jupiter.engine.config.EnumConfigurationParameterConverter) TreeMap(java.util.TreeMap) BeforeAllCallback(org.junit.jupiter.api.extension.BeforeAllCallback) TempDir(org.junit.jupiter.api.io.TempDir) ExceptionUtils(org.junit.platform.commons.util.ExceptionUtils) BeforeEachCallback(org.junit.jupiter.api.extension.BeforeEachCallback) TEMP_DIR_SCOPE_PROPERTY_NAME(org.junit.jupiter.engine.config.JupiterConfiguration.TEMP_DIR_SCOPE_PROPERTY_NAME) Collections(java.util.Collections) ParameterResolver(org.junit.jupiter.api.extension.ParameterResolver) SortedMap(java.util.SortedMap) AnnotatedElement(java.lang.reflect.AnnotatedElement) Namespace(org.junit.jupiter.api.extension.ExtensionContext.Namespace)

Example 3 with CleanupMode

use of org.junit.jupiter.api.io.CleanupMode in project junit5 by junit-team.

the class TempDirectory method resolveParameter.

/**
 * Resolve the current temporary directory for the {@link Parameter} in the
 * supplied {@link ParameterContext}.
 */
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) {
    Class<?> parameterType = parameterContext.getParameter().getType();
    assertSupportedType("parameter", parameterType);
    CleanupMode cleanupMode = determineCleanupModeForParameter(parameterContext);
    return getPathOrFile(parameterContext.getParameter(), parameterType, cleanupMode, extensionContext);
}
Also used : CleanupMode(org.junit.jupiter.api.io.CleanupMode)

Example 4 with CleanupMode

use of org.junit.jupiter.api.io.CleanupMode in project junit5 by junit-team.

the class DefaultJupiterConfigurationTests method getDefaultTempDirCleanupModeWithNoConfigParamSet.

@Test
void getDefaultTempDirCleanupModeWithNoConfigParamSet() {
    JupiterConfiguration configuration = new DefaultJupiterConfiguration(mock(ConfigurationParameters.class));
    CleanupMode cleanupMode = configuration.getDefaultTempDirCleanupMode();
    assertThat(cleanupMode).isEqualTo(ALWAYS);
}
Also used : ConfigurationParameters(org.junit.platform.engine.ConfigurationParameters) CleanupMode(org.junit.jupiter.api.io.CleanupMode) Test(org.junit.jupiter.api.Test)

Aggregations

CleanupMode (org.junit.jupiter.api.io.CleanupMode)4 TempDir (org.junit.jupiter.api.io.TempDir)2 File (java.io.File)1 IOException (java.io.IOException)1 AnnotatedElement (java.lang.reflect.AnnotatedElement)1 Constructor (java.lang.reflect.Constructor)1 Field (java.lang.reflect.Field)1 Parameter (java.lang.reflect.Parameter)1 DirectoryNotEmptyException (java.nio.file.DirectoryNotEmptyException)1 FileVisitResult (java.nio.file.FileVisitResult)1 CONTINUE (java.nio.file.FileVisitResult.CONTINUE)1 Files (java.nio.file.Files)1 NoSuchFileException (java.nio.file.NoSuchFileException)1 Path (java.nio.file.Path)1 SimpleFileVisitor (java.nio.file.SimpleFileVisitor)1 BasicFileAttributes (java.nio.file.attribute.BasicFileAttributes)1 Collections (java.util.Collections)1 SortedMap (java.util.SortedMap)1 TreeMap (java.util.TreeMap)1 Predicate (java.util.function.Predicate)1