Skip to content

Commit

Permalink
Fix Windows test failures
Browse files Browse the repository at this point in the history
The `Cleanup*` JUnit 5 extensions from Log4j API 2.x don't work well
together with `Log4jExtension` and try to delete files too soon.

We replace them with `TempLoggingDir`.
  • Loading branch information
ppkarwasz committed Mar 27, 2024
1 parent c82b678 commit 937aaf0
Show file tree
Hide file tree
Showing 33 changed files with 317 additions and 466 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,31 @@
import static org.apache.logging.log4j.core.config.ConfigurationFactory.LOG4J1_CONFIGURATION_FILE_PROPERTY;
import static org.assertj.core.api.Assertions.assertThat;

import java.nio.file.Paths;
import java.nio.file.Path;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.test.junit.CleanUpDirectories;
import org.apache.logging.log4j.test.junit.TempLoggingDir;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.SetSystemProperty;
import org.junitpioneer.jupiter.RestoreSystemProperties;

/**
* Test configuration from Properties.
*/
public class PropertiesRollingWithPropertiesTest {
class PropertiesRollingWithPropertiesTest {

private static final String TEST_DIR = "target/PropertiesRollingWithPropertiesTest";
@TempLoggingDir
private static Path loggingPath;

@Test
@SetSystemProperty(key = "test.directory", value = TEST_DIR)
@SetSystemProperty(key = LOG4J1_CONFIGURATION_FILE_PROPERTY, value = "log4j1-rolling-properties.properties")
@CleanUpDirectories(TEST_DIR)
public void testProperties() throws Exception {
final Logger logger = LoggerContext.getContext(false).getLogger("test");
logger.debug("This is a test of the root logger");
assertThat(Paths.get(TEST_DIR, "somefile.log")).exists().isNotEmptyFile();
@RestoreSystemProperties
void testProperties() {
System.setProperty("test.directory", loggingPath.toString());
System.setProperty(LOG4J1_CONFIGURATION_FILE_PROPERTY, "log4j1-rolling-properties.properties");

try (final LoggerContext context = LoggerContext.getContext(false)) {
final Logger logger = context.getLogger("test");
logger.debug("This is a test of the root logger");
assertThat(loggingPath.resolve("somefile.log")).exists().isNotEmptyFile();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,31 @@
import static org.apache.logging.log4j.core.config.ConfigurationFactory.LOG4J1_CONFIGURATION_FILE_PROPERTY;
import static org.assertj.core.api.Assertions.assertThat;

import java.nio.file.Paths;
import java.nio.file.Path;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.test.junit.CleanUpDirectories;
import org.apache.logging.log4j.test.junit.TempLoggingDir;
import org.junit.jupiter.api.Test;
import org.junitpioneer.jupiter.SetSystemProperty;
import org.junitpioneer.jupiter.RestoreSystemProperties;

/**
* Test configuration from Properties.
* Test configuration from XML.
*/
public class XmlRollingWithPropertiesTest {
class XmlRollingWithPropertiesTest {

private static final String TEST_DIR = "target/XmlRollingWithPropertiesTest";
@TempLoggingDir
private static Path loggingPath;

@Test
@SetSystemProperty(key = "test.directory", value = TEST_DIR)
@SetSystemProperty(key = LOG4J1_CONFIGURATION_FILE_PROPERTY, value = "log4j1-rolling-properties.xml")
@CleanUpDirectories(TEST_DIR)
public void testProperties() {
// ${test.directory}/logs/etl.log
final Logger logger = LoggerContext.getContext(false).getLogger("test");
logger.debug("This is a test of the root logger");
assertThat(Paths.get(TEST_DIR, "logs/etl.log")).exists().isNotEmptyFile();
@RestoreSystemProperties
void testProperties() {
System.setProperty("test.directory", loggingPath.toString());
System.setProperty(LOG4J1_CONFIGURATION_FILE_PROPERTY, "log4j1-rolling-properties.xml");

try (final LoggerContext context = LoggerContext.getContext(false)) {
final Logger logger = context.getLogger("test");
logger.debug("This is a test of the root logger");
assertThat(loggingPath.resolve("logs/etl.log")).exists().isNotEmptyFile();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
# limitations under the License.
#

log4j.debug=true

# Properties for substitution
somelogfile=somefile.log
maxfilesize=256MB
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,19 @@
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.apache.logging.log4j.plugins.Named;
import org.apache.logging.log4j.test.junit.CleanUpDirectories;
import org.apache.logging.log4j.test.junit.TempLoggingDir;
import org.junit.jupiter.api.Test;

/**
*
*/
public class RollingAppenderCronEvery2DirectTest extends AbstractRollingListenerTest {
class RollingAppenderCronEvery2DirectTest extends AbstractRollingListenerTest {

private static final String CONFIG = "log4j-rolling-cron-every2-direct.xml";
private static final String DIR = "target/rolling-cron-every2Direct";
private final CountDownLatch rollover = new CountDownLatch(2);

@TempLoggingDir
private static Path loggingPath;

@Test
@CleanUpDirectories(DIR)
@LoggerContextSource(value = CONFIG, timeout = 10)
public void testAppender(final Logger logger, @Named("RollingFile") final RollingFileManager manager)
throws Exception {
@LoggerContextSource(timeout = 10)
void testAppender(final Logger logger, @Named("RollingFile") final RollingFileManager manager) throws Exception {
manager.addRolloverListener(this);
final long end = currentTimeMillis.get() + 5000;
final Random rand = new Random(end);
Expand All @@ -51,9 +47,7 @@ public void testAppender(final Logger logger, @Named("RollingFile") final Rollin
} while (currentTimeMillis.get() < end);

rollover.await();
final Path dir = Path.of(DIR);
assertThat(dir).isNotEmptyDirectory();
assertThat(dir).isDirectoryContaining("glob:**.gz");
assertThat(loggingPath).isNotEmptyDirectory().isDirectoryContaining("glob:**.gz");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,21 @@
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.apache.logging.log4j.plugins.Named;
import org.apache.logging.log4j.test.junit.CleanUpDirectories;
import org.apache.logging.log4j.test.junit.TempLoggingDir;
import org.junit.jupiter.api.Test;

/**
*
*/
public class RollingAppenderCronEvery2Test extends AbstractRollingListenerTest {
class RollingAppenderCronEvery2Test extends AbstractRollingListenerTest {

private static final String CONFIG = "log4j-rolling-cron-every2.xml";
private static final String DIR = "target/rolling-cron-every2";
private static final String FILE = "target/rolling-cron-every2/rollingtest.log";
private final CountDownLatch rollover = new CountDownLatch(3);

@TempLoggingDir
private static Path loggingPath;

@Test
@CleanUpDirectories(DIR)
@LoggerContextSource(value = CONFIG, timeout = 10)
public void testAppender(final Logger logger, @Named("RollingFile") final RollingFileManager manager)
throws Exception {
@LoggerContextSource(timeout = 10)
void testAppender(final Logger logger, @Named("RollingFile") final RollingFileManager manager) throws Exception {
manager.addRolloverListener(this);
assertThat(Path.of(FILE)).exists();
assertThat(loggingPath.resolve("rollingtest.log")).exists();
final long end = currentTimeMillis.get() + 5000;
final Random rand = new Random(end);
int count = 1;
Expand All @@ -53,11 +48,7 @@ public void testAppender(final Logger logger, @Named("RollingFile") final Rollin
} while (currentTimeMillis.get() < end);

rollover.await();

final Path dir = Path.of(DIR);
assertThat(dir).exists();
assertThat(dir).isNotEmptyDirectory();
assertThat(dir).isDirectoryContaining("glob:**.gz");
assertThat(loggingPath).isNotEmptyDirectory().isDirectoryContaining("glob:**.gz");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,44 +19,29 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.apache.logging.log4j.plugins.Named;
import org.apache.logging.log4j.status.StatusData;
import org.apache.logging.log4j.status.StatusListener;
import org.apache.logging.log4j.status.StatusLogger;
import org.apache.logging.log4j.test.junit.CleanUpDirectories;
import org.junit.jupiter.api.BeforeAll;
import org.apache.logging.log4j.test.junit.TempLoggingDir;
import org.junit.jupiter.api.Test;

/**
*
*/
public class RollingAppenderDirectWrite1906Test implements RolloverListener {

private static final String CONFIG = "log4j-rolling-direct-1906.xml";
class RollingAppenderDirectWrite1906Test implements RolloverListener {

private static final String DIR = "target/rolling-direct-1906";
private final CountDownLatch rollover = new CountDownLatch(2);

@BeforeAll
static void beforeAll() {
StatusLogger.getLogger().registerListener(new NoopStatusListener());
}
@TempLoggingDir
private static Path loggingPath;

@Test
@CleanUpDirectories(DIR)
@LoggerContextSource(value = CONFIG, timeout = 10)
public void testAppender(final LoggerContext context, @Named("RollingFile") final RollingFileManager manager)
@LoggerContextSource(timeout = 10)
void testAppender(final LoggerContext context, @Named("RollingFile") final RollingFileManager manager)
throws Exception {
manager.addRolloverListener(this);
final var logger = context.getLogger(getClass());
Expand All @@ -66,11 +51,10 @@ public void testAppender(final LoggerContext context, @Named("RollingFile") fina
Thread.sleep(50);
}
rollover.await();
final Path dir = Path.of(DIR);
assertThat(dir).isNotEmptyDirectory();
assertThat(dir).isDirectoryContaining("glob:**.log");
assertThat(loggingPath).isNotEmptyDirectory();
assertThat(loggingPath).isDirectoryContaining("glob:**.log");

try (final Stream<Path> files = Files.list(dir)) {
try (final Stream<Path> files = Files.list(loggingPath)) {
final AtomicInteger found = new AtomicInteger();
assertThat(files).allSatisfy(file -> {
final String expected = file.getFileName().toString();
Expand All @@ -96,17 +80,4 @@ public void rolloverTriggered(final String fileName) {}
public void rolloverComplete(final String fileName) {
rollover.countDown();
}

private static class NoopStatusListener implements StatusListener {
@Override
public void log(final StatusData data) {}

@Override
public Level getStatusLevel() {
return Level.TRACE;
}

@Override
public void close() throws IOException {}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,31 +18,31 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Stream;
import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.appender.RollingFileAppender;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.apache.logging.log4j.test.junit.CleanUpDirectories;
import org.apache.logging.log4j.test.junit.TempLoggingDir;
import org.junit.jupiter.api.Test;

/**
*
*/
public class RollingAppenderDirectWriteWithReconfigureTest extends AbstractRollingListenerTest {
class RollingAppenderDirectWriteWithReconfigureTest extends AbstractRollingListenerTest {

private static final String CONFIG = "log4j-rolling-direct-reconfigure.xml";

private static final String DIR = "target/rolling-direct-reconfigure";
private static final String CONFIG =
"org/apache/logging/log4j/core/appender/rolling/RollingAppenderDirectWriteWithReconfigureTest.xml";

private final CountDownLatch rollover = new CountDownLatch(1);

@TempLoggingDir
private static Path loggingPath;

@Test
@CleanUpDirectories(DIR)
@LoggerContextSource(value = CONFIG, timeout = 10)
public void testRollingFileAppenderWithReconfigure(final LoggerContext context) throws Exception {
@LoggerContextSource(timeout = 10)
void testRollingFileAppenderWithReconfigure(final LoggerContext context) throws Exception {
final var logger = context.getLogger(getClass());
logger.debug("Before reconfigure");

Expand All @@ -53,9 +53,10 @@ public void testRollingFileAppenderWithReconfigure(final LoggerContext context)
appender.getManager().addRolloverListener(this);
logger.debug("Force a rollover");
rollover.await();
final File dir = new File(DIR);
assertThat(dir).isNotEmptyDirectory();
assertThat(dir.listFiles()).hasSize(2);
assertThat(loggingPath).isNotEmptyDirectory();
try (final Stream<Path> files = Files.list(loggingPath)) {
assertThat(files).as("check log file count").hasSize(2);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,31 @@
*/
package org.apache.logging.log4j.core.appender.rolling;

import static org.apache.logging.log4j.core.test.hamcrest.Descriptors.that;
import static org.apache.logging.log4j.core.test.hamcrest.FileMatchers.hasName;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.endsWith;
import static org.hamcrest.Matchers.hasItemInArray;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.nio.file.Path;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.apache.logging.log4j.test.junit.CleanUpDirectories;
import org.junit.jupiter.api.Tag;
import org.apache.logging.log4j.test.junit.TempLoggingDir;
import org.junit.jupiter.api.Test;

/**
*
*/
@Tag("sleepy")
public class RollingAppenderTimeAndSizeDirectTest {

private static final String CONFIG = "log4j-rolling3-direct.xml";
class RollingAppenderTimeAndSizeDirectTest {

private static final String DIR = "target/rolling3Direct";
@TempLoggingDir
private static Path loggingPath;

@Test
@CleanUpDirectories(DIR)
@LoggerContextSource(value = CONFIG, timeout = 10)
public void testAppender(final Logger logger) throws Exception {
@LoggerContextSource(timeout = 10)
void testAppender(final Logger logger) throws Exception {
for (int i = 0; i < 100; ++i) {
logger.debug("This is test message number " + i);
Thread.sleep(10);
}
Thread.sleep(50);
final File dir = new File(DIR);
assertTrue(dir.exists() && dir.listFiles().length > 0, "Directory not created");
final File[] files = dir.listFiles();
assertNotNull(files);
assertThat(files, hasItemInArray(that(hasName(that(endsWith(".gz"))))));
assertThat(loggingPath)
.as("check logging directory")
.isNotEmptyDirectory()
.as("check contains compressed log file")
.isDirectoryContaining("glob:**.gz");
}
}
Loading

0 comments on commit 937aaf0

Please sign in to comment.