Repository: logging-log4j2 Updated Branches: refs/heads/release-2.x 7e7c6289f -> 4a12af136
These classes should not perform logging using Log4j to avoid accidentally loading or re-loading Log4j configurations. Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/4a12af13 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/4a12af13 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/4a12af13 Branch: refs/heads/release-2.x Commit: 4a12af13627b012a45d0303b0654bf08083e9867 Parents: 7e7c628 Author: Gary Gregory <[email protected]> Authored: Sun Jul 15 19:26:35 2018 -0600 Committer: Gary Gregory <[email protected]> Committed: Sun Jul 15 19:26:35 2018 -0600 ---------------------------------------------------------------------- .../junit/AbstractExternalFileCleaner.java | 280 ++++++++++--------- .../apache/logging/log4j/junit/CleanFiles.java | 44 +-- .../logging/log4j/junit/CleanFolders.java | 148 +++++----- 3 files changed, 248 insertions(+), 224 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4a12af13/log4j-core/src/test/java/org/apache/logging/log4j/junit/AbstractExternalFileCleaner.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/junit/AbstractExternalFileCleaner.java b/log4j-core/src/test/java/org/apache/logging/log4j/junit/AbstractExternalFileCleaner.java index b5f5476..24aef74 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/junit/AbstractExternalFileCleaner.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/junit/AbstractExternalFileCleaner.java @@ -18,6 +18,7 @@ package org.apache.logging.log4j.junit; import java.io.File; import java.io.IOException; +import java.io.PrintStream; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -27,141 +28,158 @@ import java.util.HashSet; import java.util.Map; import java.util.Set; -import org.apache.logging.log4j.Logger; -import org.apache.logging.log4j.Marker; -import org.apache.logging.log4j.MarkerManager; import org.junit.Assert; import org.junit.rules.ExternalResource; +/** + * This class should not perform logging using Log4j to avoid accidentally + * loading or re-loading Log4j configurations. + */ public abstract class AbstractExternalFileCleaner extends ExternalResource { - protected static final Marker CLEANER_MARKER = MarkerManager.getMarker("CLEANER"); - - private static final int SLEEP_RETRY_MILLIS = 200; - private final boolean cleanAfter; - private final boolean cleanBefore; - private final Set<Path> files; - private final int maxTries; - private final Logger logger; - - public AbstractExternalFileCleaner(final boolean before, final boolean after, final int maxTries, Logger logger, - final File... files) { - this.cleanBefore = before; - this.cleanAfter = after; - this.maxTries = maxTries; - this.files = new HashSet<>(files.length); - this.logger = logger; - for (final File file : files) { - this.files.add(file.toPath()); - } - } - - public AbstractExternalFileCleaner(final boolean before, final boolean after, final int maxTries, Logger logger, - final Path... files) { - this.cleanBefore = before; - this.cleanAfter = after; - this.maxTries = maxTries; - this.logger = logger; - this.files = new HashSet<>(Arrays.asList(files)); - } - - public AbstractExternalFileCleaner(final boolean before, final boolean after, final int maxTries, Logger logger, - final String... fileNames) { - this.cleanBefore = before; - this.cleanAfter = after; - this.maxTries = maxTries; - this.logger = logger; - this.files = new HashSet<>(fileNames.length); - for (final String fileName : fileNames) { - this.files.add(Paths.get(fileName)); - } - } - - @Override - protected void after() { - if (cleanAfter()) { - this.clean(); - } - } - - @Override - protected void before() { - if (cleanBefore()) { - this.clean(); - } - } - - protected void clean() { - final Map<Path, IOException> failures = new HashMap<>(); - // Clean and gather failures - for (final Path path : getPaths()) { - if (Files.exists(path)) { - for (int i = 0; i < getMaxTries(); i++) { - try { - if (clean(path, i)) { - if (failures.containsKey(path)) { - failures.remove(path); - } - break; - } - } catch (final IOException e) { - if (logger != null) { - logger.error("Caught exception cleaning {}", this, e); - } - // We will try again. - failures.put(path, e); - } - try { - Thread.sleep(SLEEP_RETRY_MILLIS); - } catch (final InterruptedException ignored) { - // ignore - } - } - } - } - // Fail on failures - if (failures.size() > 0) { - final StringBuilder sb = new StringBuilder(); - boolean first = true; - for (final Map.Entry<Path, IOException> failure : failures.entrySet()) { - failure.getValue().printStackTrace(); - if (!first) { - sb.append(", "); - } - sb.append(failure.getKey()).append(" failed with ").append(failure.getValue()); - first = false; - } - Assert.fail(sb.toString()); - } - - } - - protected abstract boolean clean(Path path, int tryIndex) throws IOException; - - public boolean cleanAfter() { - return cleanAfter; - } - - public boolean cleanBefore() { - return cleanBefore; - } - - public Logger getLogger() { - return logger; - } - - public int getMaxTries() { - return maxTries; - } - - public Set<Path> getPaths() { - return files; - } - - @Override - public String toString() { - return getClass().getSimpleName() + " [files=" + files + ", cleanAfter=" + cleanAfter + ", cleanBefore=" - + cleanBefore + "]"; - } + protected static final String CLEANER_MARKER = "CLEANER"; + + private static final int SLEEP_RETRY_MILLIS = 200; + private final boolean cleanAfter; + private final boolean cleanBefore; + private final Set<Path> files; + private final int maxTries; + private final PrintStream printStream; + + public AbstractExternalFileCleaner(final boolean before, final boolean after, final int maxTries, + final PrintStream logger, final File... files) { + this.cleanBefore = before; + this.cleanAfter = after; + this.maxTries = maxTries; + this.files = new HashSet<>(files.length); + this.printStream = logger; + for (final File file : files) { + this.files.add(file.toPath()); + } + } + + public AbstractExternalFileCleaner(final boolean before, final boolean after, final int maxTries, + final PrintStream logger, final Path... files) { + this.cleanBefore = before; + this.cleanAfter = after; + this.maxTries = maxTries; + this.printStream = logger; + this.files = new HashSet<>(Arrays.asList(files)); + } + + public AbstractExternalFileCleaner(final boolean before, final boolean after, final int maxTries, + final PrintStream logger, final String... fileNames) { + this.cleanBefore = before; + this.cleanAfter = after; + this.maxTries = maxTries; + this.printStream = logger; + this.files = new HashSet<>(fileNames.length); + for (final String fileName : fileNames) { + this.files.add(Paths.get(fileName)); + } + } + + @Override + protected void after() { + if (cleanAfter()) { + this.clean(); + } + } + + @Override + protected void before() { + if (cleanBefore()) { + this.clean(); + } + } + + protected void clean() { + final Map<Path, IOException> failures = new HashMap<>(); + // Clean and gather failures + for (final Path path : getPaths()) { + if (Files.exists(path)) { + for (int i = 0; i < getMaxTries(); i++) { + try { + if (clean(path, i)) { + if (failures.containsKey(path)) { + failures.remove(path); + } + break; + } + } catch (final IOException e) { + println(CLEANER_MARKER + ": Caught exception cleaning: " + this); + printStackTrace(e); + // We will try again. + failures.put(path, e); + } + try { + Thread.sleep(SLEEP_RETRY_MILLIS); + } catch (final InterruptedException ignored) { + // ignore + } + } + } + } + // Fail on failures + if (failures.size() > 0) { + final StringBuilder sb = new StringBuilder(); + boolean first = true; + for (final Map.Entry<Path, IOException> failure : failures.entrySet()) { + failure.getValue().printStackTrace(); + if (!first) { + sb.append(", "); + } + sb.append(failure.getKey()).append(" failed with ").append(failure.getValue()); + first = false; + } + Assert.fail(sb.toString()); + } + } + + protected abstract boolean clean(Path path, int tryIndex) throws IOException; + + public boolean cleanAfter() { + return cleanAfter; + } + + public boolean cleanBefore() { + return cleanBefore; + } + + public int getMaxTries() { + return maxTries; + } + + public Set<Path> getPaths() { + return files; + } + + public PrintStream getPrintStream() { + return printStream; + } + + protected void printf(final String format, final Object... args) { + if (printStream != null) { + printf(format, args); + } + } + + protected void println(final String msg) { + if (printStream != null) { + println(msg); + } + } + + protected void printStackTrace(final Throwable t) { + if (printStream != null) { + t.printStackTrace(printStream); + } + } + + @Override + public String toString() { + return getClass().getSimpleName() + " [files=" + files + ", cleanAfter=" + cleanAfter + ", cleanBefore=" + + cleanBefore + "]"; + } } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4a12af13/log4j-core/src/test/java/org/apache/logging/log4j/junit/CleanFiles.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/junit/CleanFiles.java b/log4j-core/src/test/java/org/apache/logging/log4j/junit/CleanFiles.java index de95bfb..65458ba 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/junit/CleanFiles.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/junit/CleanFiles.java @@ -31,34 +31,38 @@ import java.nio.file.Path; * @Rule * public CleanFiles files = new CleanFiles("path/to/file.txt"); * </pre> + * <p> + * This class should not perform logging using Log4j to avoid accidentally + * loading or re-loading Log4j configurations. + * </p> * */ public class CleanFiles extends AbstractExternalFileCleaner { - private static final int MAX_TRIES = 10; + private static final int MAX_TRIES = 10; - public CleanFiles(final boolean before, final boolean after, final int maxTries, final File... files) { - super(before, after, maxTries, null, files); - } + public CleanFiles(final boolean before, final boolean after, final int maxTries, final File... files) { + super(before, after, maxTries, null, files); + } - public CleanFiles(final boolean before, final boolean after, final int maxTries, final String... fileNames) { - super(before, after, maxTries, null, fileNames); - } + public CleanFiles(final boolean before, final boolean after, final int maxTries, final String... fileNames) { + super(before, after, maxTries, null, fileNames); + } - public CleanFiles(final File... files) { - super(true, true, MAX_TRIES, null, files); - } + public CleanFiles(final File... files) { + super(true, true, MAX_TRIES, null, files); + } - public CleanFiles(final Path... paths) { - super(true, true, MAX_TRIES, null, paths); - } + public CleanFiles(final Path... paths) { + super(true, true, MAX_TRIES, null, paths); + } - public CleanFiles(final String... fileNames) { - super(true, true, MAX_TRIES, null, fileNames); - } + public CleanFiles(final String... fileNames) { + super(true, true, MAX_TRIES, null, fileNames); + } - @Override - protected boolean clean(final Path path, final int tryIndex) throws IOException { - return Files.deleteIfExists(path); - } + @Override + protected boolean clean(final Path path, final int tryIndex) throws IOException { + return Files.deleteIfExists(path); + } } http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/4a12af13/log4j-core/src/test/java/org/apache/logging/log4j/junit/CleanFolders.java ---------------------------------------------------------------------- diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/junit/CleanFolders.java b/log4j-core/src/test/java/org/apache/logging/log4j/junit/CleanFolders.java index 6d16ae6..0048350 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/junit/CleanFolders.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/junit/CleanFolders.java @@ -18,87 +18,89 @@ package org.apache.logging.log4j.junit; import java.io.File; import java.io.IOException; +import java.io.PrintStream; import java.nio.file.FileVisitResult; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.SimpleFileVisitor; import java.nio.file.attribute.BasicFileAttributes; -import org.apache.logging.log4j.Logger; - /** - * A JUnit test rule to automatically delete folders recursively before (optional) and after (optional) a test is run. + * A JUnit test rule to automatically delete folders recursively before + * (optional) and after (optional) a test is run. + * <p> + * This class should not perform logging using Log4j to avoid accidentally + * loading or re-loading Log4j configurations. + * </p> */ public class CleanFolders extends AbstractExternalFileCleaner { - public static final class DeleteAllFileVisitor extends SimpleFileVisitor<Path> { - - private final Logger logger; - - public DeleteAllFileVisitor(final Logger logger) { - this.logger = logger; - } - - @Override - public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException { - if (logger != null) { - logger.debug(CLEANER_MARKER, "Deleting directory {}", dir); - } - final boolean deleted = Files.deleteIfExists(dir); - if (logger != null) { - logger.debug(CLEANER_MARKER, "Deleted directory {}: {}", dir, deleted); - } - return FileVisitResult.CONTINUE; - } - - @Override - public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException { - if (logger != null) { - logger.debug(CLEANER_MARKER, "Deleting file {} with {}", file, attrs); - } - final boolean deleted = Files.deleteIfExists(file); - if (logger != null) { - logger.debug(CLEANER_MARKER, "Deleted file {}: {}", file, deleted); - } - return FileVisitResult.CONTINUE; - } - } - - private static final int MAX_TRIES = 10; - - public CleanFolders(final boolean before, final boolean after, final int maxTries, final File... files) { - super(before, after, maxTries, null, files); - } - - public CleanFolders(final boolean before, final boolean after, final int maxTries, final String... fileNames) { - super(before, after, maxTries, null, fileNames); - } - - public CleanFolders(final File... folders) { - super(true, true, MAX_TRIES, null, folders); - } - - public CleanFolders(final Logger logger, final File... folders) { - super(true, true, MAX_TRIES, logger, folders); - } - - public CleanFolders(final Path... paths) { - super(true, true, MAX_TRIES, null, paths); - } - - public CleanFolders(final String... folderNames) { - super(true, true, MAX_TRIES, null, folderNames); - } - - private void cleanFolder(final Path folder, final int tryIndex) throws IOException { - if (Files.exists(folder) && Files.isDirectory(folder)) { - Files.walkFileTree(folder, new DeleteAllFileVisitor(getLogger())); - } - } - - @Override - protected boolean clean(final Path path, final int tryIndex) throws IOException { - cleanFolder(path, tryIndex); - return true; - } + public static final class DeleteAllFileVisitor extends SimpleFileVisitor<Path> { + + private final PrintStream printStream; + + public DeleteAllFileVisitor(final PrintStream logger) { + this.printStream = logger; + } + + @Override + public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException { + printf("%s Deleting directory %s", CLEANER_MARKER, dir); + final boolean deleted = Files.deleteIfExists(dir); + printf("%s Deleted directory %s: %s", CLEANER_MARKER, dir, deleted); + return FileVisitResult.CONTINUE; + } + + protected void printf(final String format, final Object... args) { + if (printStream != null) { + printf(format, args); + } + } + + @Override + public FileVisitResult visitFile(final Path file, final BasicFileAttributes attrs) throws IOException { + printf("%s Deleting file %s with %s", CLEANER_MARKER, file, attrs); + final boolean deleted = Files.deleteIfExists(file); + printf("%s Deleted file %s: %s", file, deleted); + return FileVisitResult.CONTINUE; + } + } + + private static final int MAX_TRIES = 10; + + public CleanFolders(final boolean before, final boolean after, final int maxTries, final File... files) { + super(before, after, maxTries, null, files); + } + + public CleanFolders(final boolean before, final boolean after, final int maxTries, final String... fileNames) { + super(before, after, maxTries, null, fileNames); + } + + public CleanFolders(final File... folders) { + super(true, true, MAX_TRIES, null, folders); + } + + public CleanFolders(final Path... paths) { + super(true, true, MAX_TRIES, null, paths); + } + + public CleanFolders(final PrintStream logger, final File... folders) { + super(true, true, MAX_TRIES, logger, folders); + } + + public CleanFolders(final String... folderNames) { + super(true, true, MAX_TRIES, null, folderNames); + } + + @Override + protected boolean clean(final Path path, final int tryIndex) throws IOException { + cleanFolder(path, tryIndex); + return true; + } + + private void cleanFolder(final Path folder, final int tryIndex) throws IOException { + if (Files.exists(folder) && Files.isDirectory(folder)) { + Files.walkFileTree(folder, new DeleteAllFileVisitor(getPrintStream())); + } + } }
