This is an automated email from the ASF dual-hosted git repository. mattsicker pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit ced914618fc06cfdf0a9373c451dd486618c7be1 Author: Matt Sicker <mattsic...@apache.org> AuthorDate: Sat Jun 18 17:44:15 2022 -0500 Revert "Use commons-io for file/directory cleaner JUnit extensions" This reverts commit dbbb1cd6fcb0643607a2e21bef2f42d8091751cb. --- log4j-api-test/pom.xml | 4 -- .../log4j/test/junit/AbstractFileCleaner.java | 23 ++++----- .../log4j/test/junit/CleanUpDirectories.java | 8 ++-- .../logging/log4j/test/junit/CleanUpFiles.java | 8 ++-- .../logging/log4j/test/junit/DirectoryCleaner.java | 56 +++++++++++++++++----- .../logging/log4j/test/junit/FileCleaner.java | 35 +++++++++----- pom.xml | 1 + 7 files changed, 88 insertions(+), 47 deletions(-) diff --git a/log4j-api-test/pom.xml b/log4j-api-test/pom.xml index 67b2ea43b9..724f5b4499 100644 --- a/log4j-api-test/pom.xml +++ b/log4j-api-test/pom.xml @@ -83,10 +83,6 @@ <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> </dependency> - <dependency> - <groupId>commons-io</groupId> - <artifactId>commons-io</artifactId> - </dependency> <!-- Required for JSON support --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> diff --git a/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/AbstractFileCleaner.java b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/AbstractFileCleaner.java index 7a3b2e0405..351361d0b9 100644 --- a/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/AbstractFileCleaner.java +++ b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/AbstractFileCleaner.java @@ -17,7 +17,6 @@ package org.apache.logging.log4j.test.junit; -import org.apache.commons.io.file.PathUtils; import org.junit.jupiter.api.extension.BeforeEachCallback; import org.junit.jupiter.api.extension.ExtensionContext; @@ -26,8 +25,8 @@ import java.io.InterruptedIOException; import java.nio.file.Files; import java.nio.file.Path; import java.util.Collection; -import java.util.LinkedHashMap; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.LockSupport; import java.util.stream.Collectors; @@ -36,8 +35,10 @@ import static org.junit.jupiter.api.Assertions.fail; abstract class AbstractFileCleaner implements BeforeEachCallback { - public static final String MAX_TRIES_PROPERTY = "log4j2.junit.fileCleanerMaxTries"; - public static final String SLEEP_PERIOD_MILLIS_PROPERTY = "log4j2.junit.fileCleanerSleepPeriodMillis"; + private static final int MAX_TRIES = Integer.getInteger("log4j2.junit.fileCleanerMaxTries", 10); + + private static final int SLEEP_PERIOD_MILLIS = Integer.getInteger("log4j2.junit.fileCleanerSleepPeriodMillis", 200); + private static final int SLEEP_BASE_PERIOD_MILLIS = SLEEP_PERIOD_MILLIS / MAX_TRIES; @Override public void beforeEach(final ExtensionContext context) throws Exception { @@ -49,16 +50,15 @@ abstract class AbstractFileCleaner implements BeforeEachCallback { if (paths.isEmpty()) { return; } - final Map<Path, IOException> failures = new LinkedHashMap<>(); - final int maxTries = context.getConfigurationParameter(MAX_TRIES_PROPERTY, Integer::valueOf).orElse(10); - final int sleepPeriodMillis = context.getConfigurationParameter(SLEEP_PERIOD_MILLIS_PROPERTY, Integer::valueOf).orElse(200); - final int sleepBasePeriodMillis = sleepPeriodMillis / maxTries; + final Map<Path, IOException> failures = new ConcurrentHashMap<>(); for (final Path path : paths) { if (Files.exists(path)) { - for (int i = 0, sleepMillis = sleepBasePeriodMillis; i < maxTries; i++, sleepMillis <<= 1) { + for (int i = 0, sleepMillis = SLEEP_BASE_PERIOD_MILLIS; i < MAX_TRIES; i++, sleepMillis <<= 1) { try { - PathUtils.delete(path); - failures.remove(path); + if (delete(path)) { + failures.remove(path); + break; + } } catch (final IOException e) { failures.put(path, e); } @@ -81,4 +81,5 @@ abstract class AbstractFileCleaner implements BeforeEachCallback { abstract Collection<Path> getPathsForTest(final ExtensionContext context); + abstract boolean delete(final Path path) throws IOException; } diff --git a/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/CleanUpDirectories.java b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/CleanUpDirectories.java index 20b58fb708..bdc185e133 100644 --- a/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/CleanUpDirectories.java +++ b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/CleanUpDirectories.java @@ -17,8 +17,6 @@ package org.apache.logging.log4j.test.junit; -import org.junit.jupiter.api.extension.ExtendWith; - import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; @@ -26,11 +24,13 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import org.junit.jupiter.api.extension.ExtendWith; + /** * JUnit extension to automatically clean up a list of directories and their contents before and after test execution. * This will automatically retry deletion up to 10 times per file while pausing for 200ms each time. - * These can be overridden with system properties {@value AbstractFileCleaner#MAX_TRIES_PROPERTY} and - * {@value AbstractFileCleaner#SLEEP_PERIOD_MILLIS_PROPERTY}. + * These can be overridden with system properties {@code log4j2.junit.fileCleanerMaxTries} and + * {@code log4j2.junit.fileCleanerSleepPeriodMillis}. * * @see DirectoryCleaner * @see CleanUpFiles diff --git a/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/CleanUpFiles.java b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/CleanUpFiles.java index 55245841fc..1ad6bcaf27 100644 --- a/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/CleanUpFiles.java +++ b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/CleanUpFiles.java @@ -17,8 +17,6 @@ package org.apache.logging.log4j.test.junit; -import org.junit.jupiter.api.extension.ExtendWith; - import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Inherited; @@ -26,11 +24,13 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; +import org.junit.jupiter.api.extension.ExtendWith; + /** * JUnit extension to automatically clean up a list of files before and after test execution. * This will automatically retry deletion up to 10 times per file while pausing for 200ms each time. - * These can be overridden with system properties {@value AbstractFileCleaner#MAX_TRIES_PROPERTY} and - * {@value AbstractFileCleaner#SLEEP_PERIOD_MILLIS_PROPERTY}. + * These can be overridden with system properties {@code log4j2.junit.fileCleanerMaxTries} and + * {@code log4j2.junit.fileCleanerSleepPeriodMillis}. * * @see FileCleaner * @see CleanUpDirectories diff --git a/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/DirectoryCleaner.java b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/DirectoryCleaner.java index e7c8496b41..a0f8e02dea 100644 --- a/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/DirectoryCleaner.java +++ b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/DirectoryCleaner.java @@ -17,24 +17,54 @@ package org.apache.logging.log4j.test.junit; -import org.junit.jupiter.api.extension.ExtensionContext; -import org.junit.platform.commons.support.AnnotationSupport; - +import java.io.IOException; +import java.nio.file.FileVisitResult; +import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; +import java.nio.file.SimpleFileVisitor; +import java.nio.file.attribute.BasicFileAttributes; import java.util.Collection; -import java.util.stream.Collectors; -import java.util.stream.Stream; +import java.util.HashSet; + +import org.junit.jupiter.api.extension.ExtensionContext; class DirectoryCleaner extends AbstractFileCleaner { @Override Collection<Path> getPathsForTest(final ExtensionContext context) { - final Stream<CleanUpDirectories> testClassAnnotation = - AnnotationSupport.findAnnotation(context.getTestClass(), CleanUpDirectories.class).stream(); - final Stream<CleanUpDirectories> testMethodAnnotation = - AnnotationSupport.findAnnotation(context.getTestMethod(), CleanUpDirectories.class).stream(); - return Stream.concat(testClassAnnotation, testMethodAnnotation) - .flatMap(annotation -> Stream.of(annotation.value())) - .map(Path::of) - .collect(Collectors.toSet()); + final Collection<Path> paths = new HashSet<>(); + final CleanUpDirectories testClassAnnotation = context.getRequiredTestClass().getAnnotation(CleanUpDirectories.class); + if (testClassAnnotation != null) { + for (final String path : testClassAnnotation.value()) { + paths.add(Paths.get(path)); + } + } + final CleanUpDirectories testMethodAnnotation = context.getRequiredTestMethod().getAnnotation(CleanUpDirectories.class); + if (testMethodAnnotation != null) { + for (final String path : testMethodAnnotation.value()) { + paths.add(Paths.get(path)); + } + } + return paths; + } + + @Override + boolean delete(final Path path) throws IOException { + if (Files.exists(path) && Files.isDirectory(path)) { + Files.walkFileTree(path, new SimpleFileVisitor<Path>() { + @Override + public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException { + Files.deleteIfExists(file); + return FileVisitResult.CONTINUE; + } + + @Override + public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException { + Files.deleteIfExists(dir); + return FileVisitResult.CONTINUE; + } + }); + } + return true; } } diff --git a/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/FileCleaner.java b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/FileCleaner.java index 70bb190809..3ca16ac717 100644 --- a/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/FileCleaner.java +++ b/log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/FileCleaner.java @@ -17,23 +17,36 @@ package org.apache.logging.log4j.test.junit; -import org.junit.jupiter.api.extension.ExtensionContext; -import org.junit.platform.commons.support.AnnotationSupport; - +import java.io.IOException; +import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.Paths; import java.util.Collection; -import java.util.stream.Collectors; -import java.util.stream.Stream; +import java.util.HashSet; + +import org.junit.jupiter.api.extension.ExtensionContext; class FileCleaner extends AbstractFileCleaner { @Override Collection<Path> getPathsForTest(final ExtensionContext context) { - final Stream<CleanUpFiles> testClassAnnotation = AnnotationSupport.findAnnotation(context.getTestClass(), CleanUpFiles.class).stream(); - final Stream<CleanUpFiles> testMethodAnnotation = AnnotationSupport.findAnnotation(context.getTestMethod(), CleanUpFiles.class).stream(); - return Stream.concat(testClassAnnotation, testMethodAnnotation) - .flatMap(annotation -> Stream.of(annotation.value())) - .map(Path::of) - .collect(Collectors.toSet()); + final Collection<Path> paths = new HashSet<>(); + final CleanUpFiles testClassAnnotation = context.getRequiredTestClass().getAnnotation(CleanUpFiles.class); + if (testClassAnnotation != null) { + for (final String path : testClassAnnotation.value()) { + paths.add(Paths.get(path)); + } + } + final CleanUpFiles testMethodAnnotation = context.getRequiredTestMethod().getAnnotation(CleanUpFiles.class); + if (testMethodAnnotation != null) { + for (final String path : testMethodAnnotation.value()) { + paths.add(Paths.get(path)); + } + } + return paths; } + @Override + boolean delete(final Path path) throws IOException { + return Files.deleteIfExists(path); + } } diff --git a/pom.xml b/pom.xml index deb06c82ff..3d61913ebe 100644 --- a/pom.xml +++ b/pom.xml @@ -858,6 +858,7 @@ <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.11.0</version> + <scope>test</scope> </dependency> <!-- Used for testing JsonTemplateLayout --> <dependency>