You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by vy...@apache.org on 2020/08/25 12:27:18 UTC

[logging-log4j2] branch release-2.x updated: Try improving RollingAppenderRestartTest to alleviate test report failures.

This is an automated email from the ASF dual-hosted git repository.

vy pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/release-2.x by this push:
     new 4867187  Try improving RollingAppenderRestartTest to alleviate test report failures.
4867187 is described below

commit 48671873a340e73a0c2e2920fd8d4b07494bebaa
Author: Volkan Yazıcı <vo...@gmail.com>
AuthorDate: Tue Aug 25 14:24:52 2020 +0200

    Try improving RollingAppenderRestartTest to alleviate test report failures.
---
 .../rolling/RollingAppenderRestartTest.java        | 48 +++++++++++++++-------
 1 file changed, 33 insertions(+), 15 deletions(-)

diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderRestartTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderRestartTest.java
index 80474b6..bf8b39b 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderRestartTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderRestartTest.java
@@ -16,21 +16,27 @@
  */
 package org.apache.logging.log4j.core.appender.rolling;
 
+import org.apache.commons.io.file.PathUtils;
 import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.junit.LoggerContextRule;
 import org.hamcrest.Matcher;
+import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.RuleChain;
 
 import java.io.File;
+import java.io.IOException;
 import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.nio.file.StandardOpenOption;
 import java.nio.file.attribute.BasicFileAttributeView;
 import java.nio.file.attribute.FileTime;
 import java.time.Instant;
 import java.time.temporal.ChronoUnit;
+import java.util.Arrays;
 
 import static org.apache.logging.log4j.hamcrest.Descriptors.that;
 import static org.apache.logging.log4j.hamcrest.FileMatchers.hasName;
@@ -38,37 +44,49 @@ import static org.hamcrest.Matchers.endsWith;
 import static org.hamcrest.Matchers.hasItemInArray;
 import static org.junit.Assert.assertTrue;
 
-/**
- *
- */
 public class RollingAppenderRestartTest {
 
     private static final String CONFIG = "log4j-rolling-restart.xml";
-    private static final String DIR = "target/rolling-restart";
 
-    private final LoggerContextRule loggerContextRule = LoggerContextRule.createShutdownTimeoutLoggerContextRule(CONFIG);
+    // Note that both paths are hardcoded in the configuration!
+    private static final Path DIR = Paths.get("target/rolling-restart");
+    private static final Path FILE = DIR.resolve("test.log");
+
+    private final LoggerContextRule loggerContextRule =
+            LoggerContextRule.createShutdownTimeoutLoggerContextRule(CONFIG);
 
     @Rule
-    public RuleChain chain = loggerContextRule.withCleanFoldersRule(false, true, 5, DIR);
+    public RuleChain chain =
+            loggerContextRule.withCleanFoldersRule(
+                    false, true, 5, DIR.toAbsolutePath().toString());
 
     @BeforeClass
     public static void setup() throws Exception {
-        File file = new File("target/rolling-restart/test.log");
-        Files.createDirectories(file.toPath().getParent());
-        Files.write(file.toPath(), "Hello, world".getBytes(), StandardOpenOption.CREATE);
+        tearDown();
+        Files.createDirectories(DIR);
+        Files.write(FILE, "Hello, world".getBytes(), StandardOpenOption.CREATE);
         FileTime newTime = FileTime.from(Instant.now().minus(2, ChronoUnit.DAYS));
-        Files.getFileAttributeView(file.toPath(), BasicFileAttributeView.class).setTimes(newTime, newTime, newTime);
+        Files
+                .getFileAttributeView(FILE, BasicFileAttributeView.class)
+                .setTimes(newTime, newTime, newTime);
+    }
+
+    @AfterClass
+    public static void tearDown() throws IOException {
+        if (DIR.toFile().exists()) {
+            PathUtils.deleteDirectory(DIR);
+        }
     }
 
     @Test
     public void testAppender() throws Exception {
         final Logger logger = loggerContextRule.getLogger();
         logger.info("This is test message number 1");
-
-        final File dir = new File(DIR);
-
         final Matcher<File[]> hasGzippedFile = hasItemInArray(that(hasName(that(endsWith(".gz")))));
-        final File[] files = dir.listFiles();
-        assertTrue("No gzipped files found", hasGzippedFile.matches(files));
+        final File[] files = DIR.toFile().listFiles();
+        assertTrue(
+                "was expecting files with '.gz' suffix, found: " + Arrays.toString(files),
+                hasGzippedFile.matches(files));
     }
+
 }