Skip to content

Commit 41e6282

Browse files
authored
Use a random ${test:logging.path} (#2954)
The `@TempLoggingDir` JUnit 5 extensions create a totally deterministic directory to hold the log files for tests. Since the directory is deleted only if all the tests succeed, this causes a problem with test reruns.
1 parent e1715dc commit 41e6282

File tree

2 files changed

+18
-10
lines changed

2 files changed

+18
-10
lines changed

log4j-api-test/src/main/java/org/apache/logging/log4j/test/junit/TempLoggingDirectory.java

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
import org.junit.jupiter.api.extension.ParameterResolutionException;
4040
import org.junit.jupiter.api.extension.ParameterResolver;
4141
import org.junit.jupiter.api.io.CleanupMode;
42+
import org.junit.platform.commons.PreconditionViolationException;
4243
import org.junit.platform.commons.support.AnnotationSupport;
4344
import org.junit.platform.commons.support.ModifierSupport;
4445

@@ -51,8 +52,8 @@ public void beforeAll(ExtensionContext context) throws Exception {
5152
Path loggingPath = null;
5253
for (final Field field : fields) {
5354
if (loggingPath != null) {
54-
StatusLogger.getLogger()
55-
.warn("Multiple static fields with @TempLoggingDir annotation are not supported.");
55+
throw new PreconditionViolationException(
56+
"Multiple static fields with @TempLoggingDir annotation are not supported.");
5657
} else {
5758
final CleanupMode cleanup = determineCleanupMode(field);
5859
loggingPath = createLoggingPath(context, cleanup).getPath();
@@ -79,8 +80,8 @@ public void beforeEach(ExtensionContext context) throws Exception {
7980
final Object instance = context.getRequiredTestInstance();
8081
for (final Field field : fields) {
8182
if (loggingPath != null) {
82-
StatusLogger.getLogger()
83-
.warn("Multiple instance fields with @TempLoggingDir annotation are not supported.");
83+
throw new PreconditionViolationException(
84+
"Multiple instance fields with @TempLoggingDir annotation are not supported.");
8485
} else {
8586
final CleanupMode cleanup = determineCleanupMode(field);
8687
loggingPath = createLoggingPath(context, cleanup).getPath();
@@ -102,8 +103,11 @@ public boolean supportsParameter(ParameterContext parameterContext, ExtensionCon
102103
@Override
103104
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext)
104105
throws ParameterResolutionException {
105-
final TempLoggingDir annotation =
106-
parameterContext.findAnnotation(TempLoggingDir.class).get();
106+
final TempLoggingDir annotation = parameterContext
107+
.findAnnotation(TempLoggingDir.class)
108+
.orElseThrow(() -> new PreconditionViolationException(String.format(
109+
"Missing `%s` annotation on parameter `%s`",
110+
TempLoggingDir.class.getSimpleName(), parameterContext)));
107111
// Get or create a temporary directory
108112
PathHolder holder = ExtensionContextAnchor.getAttribute(PathHolder.class, PathHolder.class, extensionContext);
109113
if (holder == null || !extensionContext.equals(holder.getMainContext())) {
@@ -142,12 +146,16 @@ private Path determinePerClassPath(ExtensionContext context) {
142146
final Path basePath = (baseDir != null ? Paths.get(baseDir, "target") : Paths.get(".")).resolve("logs");
143147
final Class<?> clazz = context.getRequiredTestClass();
144148
final Package pkg = clazz.getPackage();
145-
final String dir =
146-
pkg.getName().replaceAll("[.$]", File.separatorChar == '\\' ? "\\\\" : File.separator);
149+
final String dir = pkg.getName()
150+
.replaceAll("org\\.apache\\.(logging\\.)?log4j\\.", "")
151+
.replaceAll("[.$]", File.separatorChar == '\\' ? "\\\\" : File.separator);
147152
// Create a temporary directory that uses the simple class name as prefix
148153
Path packagePath = basePath.resolve(dir);
149154
Files.createDirectories(packagePath);
150-
return Files.createTempDirectory(packagePath, clazz.getSimpleName());
155+
// Use a UNIX timestamp to (roughly) sort directories by execution time.
156+
return Files.createTempDirectory(
157+
packagePath,
158+
String.format("%s_%08x_", clazz.getSimpleName(), System.currentTimeMillis() / 1000));
151159
} catch (final IOException e) {
152160
throw new ExtensionContextException("Failed to create temporary directory.", e);
153161
}

log4j-api-test/src/test/java/org/apache/logging/log4j/test/junit/TempLoggingDirectoryTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
@UsingTestProperties
2828
public class TempLoggingDirectoryTest {
2929

30-
private static final Pattern PER_CLASS_PATH = Pattern.compile("TempLoggingDirectoryTest\\d+");
30+
private static final Pattern PER_CLASS_PATH = Pattern.compile("TempLoggingDirectoryTest_[0-9a-f]{8,}_\\d+");
3131
private static final Path PER_TEST_PATH = Paths.get("testInjectedFields");
3232

3333
@TempLoggingDir

0 commit comments

Comments
 (0)