This is an automated email from the ASF dual-hosted git repository.
pkarwasz pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
The following commit(s) were added to refs/heads/main by this push:
new d591d96e35 Modernize `RollingAppenderDirectCronTest`
d591d96e35 is described below
commit d591d96e35b1f034d74396663cee76d6501aa8fb
Author: Piotr P. Karwasz <[email protected]>
AuthorDate: Thu Jul 4 09:54:14 2024 +0200
Modernize `RollingAppenderDirectCronTest`
---
.../rolling/RollingAppenderDirectCronTest.java | 81 +++++++++-------------
.../rolling/RollingAppenderDirectCronTest.xml | 38 ++++++++++
2 files changed, 72 insertions(+), 47 deletions(-)
diff --git
a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDirectCronTest.java
b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDirectCronTest.java
index 429b2c3bdc..e897ccfc1e 100644
---
a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDirectCronTest.java
+++
b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/appender/rolling/RollingAppenderDirectCronTest.java
@@ -16,62 +16,57 @@
*/
package org.apache.logging.log4j.core.appender.rolling;
-import static org.junit.Assert.assertTrue;
-import static org.junit.Assert.fail;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.awaitility.Awaitility.waitAtMost;
+import static org.junit.jupiter.api.Assertions.fail;
-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.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
+import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.logging.log4j.Logger;
+import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.RollingFileAppender;
-import org.apache.logging.log4j.core.test.junit.LoggerContextRule;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.RuleChain;
+import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
+import org.apache.logging.log4j.core.test.junit.Named;
+import org.apache.logging.log4j.test.junit.TempLoggingDir;
+import org.apache.logging.log4j.test.junit.UsingStatusListener;
+import org.junit.jupiter.api.Test;
-/**
- *
- */
-public class RollingAppenderDirectCronTest {
-
- private static final String CONFIG = "log4j-rolling-direct-cron.xml";
- private static final String DIR = "target/rolling-direct-cron";
-
- private final LoggerContextRule loggerContextRule =
- LoggerContextRule.createShutdownTimeoutLoggerContextRule(CONFIG);
+@UsingStatusListener
+class RollingAppenderDirectCronTest {
- @Rule
- public RuleChain chain = loggerContextRule.withCleanFoldersRule(DIR);
+ private static final Pattern FILE_PATTERN =
+
Pattern.compile("test-(\\d{4}-\\d{2}-\\d{2}T\\d{2}-\\d{2}-\\d{2})\\.log");
- private final Pattern filePattern =
Pattern.compile(".*(\\d\\d-\\d\\d-\\d\\d-\\d\\d-\\d\\d-\\d\\d).*$");
+ @TempLoggingDir
+ private static Path loggingPath;
@Test
- public void testAppender() throws Exception {
- // TODO Is there a better way to test than putting the thread to sleep
all over the place?
- final RollingFileAppender app =
loggerContextRule.getAppender("RollingFile");
- final Logger logger = loggerContextRule.getLogger();
- logger.debug("This is test message number 1");
+
@LoggerContextSource("classpath:appender/rolling/RollingAppenderDirectCronTest.xml")
+ void testAppender(final LoggerContext ctx, @Named("RollingFile") final
RollingFileAppender app) throws Exception {
+ final Logger logger =
ctx.getLogger(RollingAppenderDirectCronTest.class);
+ int msgNumber = 1;
+ logger.debug("This is test message number {}.", msgNumber++);
+ assertThat(loggingPath).isNotEmptyDirectory();
final RolloverDelay delay = new RolloverDelay(app.getManager());
delay.waitForRollover();
- final File dir = new File(DIR);
- final File[] files = dir.listFiles();
- assertTrue("Directory not created", dir.exists() && files != null &&
files.length > 0);
- delay.reset(3);
+ delay.reset(3);
final int MAX_TRIES = 30;
for (int i = 0; i < MAX_TRIES; ++i) {
- logger.debug("Adding new event {}", i);
- Thread.sleep(100);
+ logger.debug("This is test message number {}.", msgNumber++);
+ Thread.sleep(110);
}
delay.waitForRollover();
}
- private class RolloverDelay implements RolloverListener {
+ private static class RolloverDelay implements RolloverListener {
private volatile CountDownLatch latch;
public RolloverDelay(final RollingFileManager manager) {
@@ -80,13 +75,7 @@ public class RollingAppenderDirectCronTest {
}
public void waitForRollover() {
- try {
- if (!latch.await(3, TimeUnit.SECONDS)) {
- fail("failed to rollover");
- }
- } catch (InterruptedException ex) {
- fail("failed to rollover");
- }
+ waitAtMost(3, TimeUnit.SECONDS).until(() -> latch.getCount() == 0);
}
public void reset(final int count) {
@@ -98,16 +87,14 @@ public class RollingAppenderDirectCronTest {
@Override
public void rolloverComplete(final String fileName) {
- final java.util.regex.Matcher matcher =
filePattern.matcher(fileName);
- assertTrue("Invalid file name: " + fileName, matcher.matches());
- final Path path = new File(fileName).toPath();
+ final Path path = Paths.get(fileName);
+ final Matcher matcher =
FILE_PATTERN.matcher(path.getFileName().toString());
+ assertThat(matcher).as("Rolled file").matches();
try {
final List<String> lines = Files.readAllLines(path);
- assertTrue("Not enough lines in " + fileName + ":" +
lines.size(), lines.size() > 0);
- assertTrue(
- "log and file times don't match. file: " +
matcher.group(1) + ", log: " + lines.get(0),
- lines.get(0).startsWith(matcher.group(1)));
- } catch (IOException ex) {
+ assertThat(lines).isNotEmpty();
+ assertThat(lines.get(0)).startsWith(matcher.group(1));
+ } catch (final IOException ex) {
fail("Unable to read file " + fileName + ": " +
ex.getMessage());
}
latch.countDown();
diff --git
a/log4j-core-test/src/test/resources/appender/rolling/RollingAppenderDirectCronTest.xml
b/log4j-core-test/src/test/resources/appender/rolling/RollingAppenderDirectCronTest.xml
new file mode 100644
index 0000000000..5d340145fb
--- /dev/null
+++
b/log4j-core-test/src/test/resources/appender/rolling/RollingAppenderDirectCronTest.xml
@@ -0,0 +1,38 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+ ~ Licensed to the Apache Software Foundation (ASF) under one or more
+ ~ contributor license agreements. See the NOTICE file distributed with
+ ~ this work for additional information regarding copyright ownership.
+ ~ The ASF licenses this file to you under the Apache License, Version 2.0
+ ~ (the "License"); you may not use this file except in compliance with
+ ~ the License. You may obtain a copy of the License at
+ ~
+ ~ http://www.apache.org/licenses/LICENSE-2.0
+ ~
+ ~ Unless required by applicable law or agreed to in writing, software
+ ~ distributed under the License is distributed on an "AS IS" BASIS,
+ ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ ~ See the License for the specific language governing permissions and
+ ~ limitations under the License.
+ -->
+<Configuration status="off" name="XMLConfigTest">
+ <Properties>
+ <Property name="logDir">${test:logging.path}</Property>
+ </Properties>
+ <Appenders>
+ <RollingFile name="RollingFile"
filePattern="${logDir}/%d{MM-dd-yy-HH-mm-ss}/processor.log">
+ <PatternLayout>
+ <Pattern>%d} %p %C{1.} [%t] %m%n</Pattern>
+ </PatternLayout>
+ <TimeBasedTriggeringPolicy />
+ <DirectWriteRolloverStrategy/>
+ </RollingFile>
+ </Appenders>
+
+ <Loggers>
+ <Root level="info">
+ <AppenderRef ref="RollingFile"/>
+ </Root>
+ </Loggers>
+
+</Configuration>