You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2020/08/23 18:24:23 UTC
[logging-log4j2] branch release-2.x updated: [LOG4J2-2653] Migrate
test to JUnit 5
This is an automated email from the ASF dual-hosted git repository.
mattsicker 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 6bc4750 [LOG4J2-2653] Migrate test to JUnit 5
6bc4750 is described below
commit 6bc47508e100c3365e3960e1f26bb1155aa68c90
Author: Matt Sicker <bo...@gmail.com>
AuthorDate: Sun Aug 23 13:23:38 2020 -0500
[LOG4J2-2653] Migrate test to JUnit 5
---
log4j-core/pom.xml | 4 +
.../log4j/core/appender/FileAppenderTest.java | 162 ++++++++++-----------
2 files changed, 78 insertions(+), 88 deletions(-)
diff --git a/log4j-core/pom.xml b/log4j-core/pom.xml
index 47f3ce5..b2931ba 100644
--- a/log4j-core/pom.xml
+++ b/log4j-core/pom.xml
@@ -200,6 +200,10 @@
<artifactId>junit-jupiter-params</artifactId>
</dependency>
<dependency>
+ <groupId>org.junit.jupiter</groupId>
+ <artifactId>junit-jupiter-params</artifactId>
+ </dependency>
+ <dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<scope>test</scope>
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/FileAppenderTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/FileAppenderTest.java
index 80ae3c9..b4bc578 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/FileAppenderTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/FileAppenderTest.java
@@ -16,12 +16,10 @@
*/
package org.apache.logging.log4j.core.appender;
-import static org.junit.Assert.assertFalse;
-import static org.junit.Assert.assertTrue;
-
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
+import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
@@ -38,58 +36,47 @@ import org.apache.logging.log4j.core.LogEvent;
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.core.layout.PatternLayout;
import org.apache.logging.log4j.core.util.Throwables;
-import org.apache.logging.log4j.junit.CleanFiles;
import org.apache.logging.log4j.message.SimpleMessage;
-import org.apache.logging.log4j.util.Strings;
-import org.junit.AfterClass;
-import org.junit.Assert;
-import org.junit.Ignore;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
-import org.junit.runners.Parameterized.Parameters;
+import org.junit.jupiter.api.AfterAll;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Disabled;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ValueSource;
+
+import static org.junit.jupiter.api.Assertions.*;
/**
* Tests {@link FileAppender}.
*/
-@RunWith(Parameterized.class)
public class FileAppenderTest {
- @Parameters(name = "createOnDemand = {0}")
- public static Boolean[] getParameters() {
- return new Boolean[] { false, true };
- }
-
private static final String FILE_NAME = "target/fileAppenderTest.log";
private static final Path PATH = Paths.get(FILE_NAME);
private static final int THREADS = 2;
- public FileAppenderTest(final boolean createOnDemand) {
- super();
- this.createOnDemand = createOnDemand;
+ @AfterAll
+ public static void cleanupClass() {
+ assertFalse(AbstractManager.hasManager(FILE_NAME), "Manager for " + FILE_NAME + " not removed");
}
- private final boolean createOnDemand;
- private final int threadCount = THREADS;
-
- @Rule
- public CleanFiles files = new CleanFiles(PATH);
-
- @AfterClass
- public static void cleanupClass() {
- assertTrue("Manager for " + FILE_NAME + " not removed", !AbstractManager.hasManager(FILE_NAME));
+ @BeforeEach
+ @AfterEach
+ void cleanFiles() throws IOException {
+ Files.deleteIfExists(PATH);
}
- @Test
- public void testAppender() throws Exception {
+ @ParameterizedTest
+ @ValueSource(booleans = { false, true })
+ public void testAppender(final boolean createOnDemand) throws Exception {
final int logEventCount = 1;
writer(false, logEventCount, "test", createOnDemand, false);
verifyFile(logEventCount);
}
- @Test
- public void testLazyCreate() throws Exception {
+ @ParameterizedTest
+ @ValueSource(booleans = { false, true })
+ public void testLazyCreate(final boolean createOnDemand) throws Exception {
final Layout<String> layout = createPatternLayout();
// @formatter:off
final FileAppender appender = FileAppender.newBuilder()
@@ -100,15 +87,15 @@ public class FileAppenderTest {
.withCreateOnDemand(createOnDemand)
.build();
// @formatter:on
- Assert.assertEquals(createOnDemand, appender.getManager().isCreateOnDemand());
+ assertEquals(createOnDemand, appender.getManager().isCreateOnDemand());
try {
- Assert.assertNotEquals(createOnDemand, Files.exists(PATH));
+ assertNotEquals(createOnDemand, Files.exists(PATH));
appender.start();
- Assert.assertNotEquals(createOnDemand, Files.exists(PATH));
+ assertNotEquals(createOnDemand, Files.exists(PATH));
} finally {
appender.stop();
}
- Assert.assertNotEquals(createOnDemand, Files.exists(PATH));
+ assertNotEquals(createOnDemand, Files.exists(PATH));
}
private static PatternLayout createPatternLayout() {
@@ -116,8 +103,9 @@ public class FileAppenderTest {
.build();
}
- @Test
- public void testSmallestBufferSize() throws Exception {
+ @ParameterizedTest
+ @ValueSource(booleans = { false, true })
+ public void testSmallestBufferSize(final boolean createOnDemand) throws Exception {
final Layout<String> layout = createPatternLayout();
// @formatter:off
final FileAppender appender = FileAppender.newBuilder()
@@ -131,79 +119,79 @@ public class FileAppenderTest {
try {
appender.start();
final File file = new File(FILE_NAME);
- assertTrue("Appender did not start", appender.isStarted());
- Assert.assertNotEquals(createOnDemand, Files.exists(PATH));
+ assertTrue(appender.isStarted(), "Appender did not start");
+ assertNotEquals(createOnDemand, Files.exists(PATH));
long curLen = file.length();
long prevLen = curLen;
- assertTrue("File length: " + curLen, curLen == 0);
+ assertEquals(0, curLen, "File length: " + curLen);
for (int i = 0; i < 100; ++i) {
final LogEvent event = Log4jLogEvent.newBuilder().setLoggerName("TestLogger") //
.setLoggerFqcn(FileAppenderTest.class.getName()).setLevel(Level.INFO) //
.setMessage(new SimpleMessage("Test")).setThreadName(this.getClass().getSimpleName()) //
.setTimeMillis(System.currentTimeMillis()).build();
- try {
- appender.append(event);
- curLen = file.length();
- assertTrue("File length: " + curLen, curLen > prevLen);
- // Give up control long enough for another thread/process to occasionally do something.
- Thread.sleep(25);
- } catch (final Exception ex) {
- throw ex;
- }
+ appender.append(event);
+ curLen = file.length();
+ assertTrue(curLen > prevLen, "File length: " + curLen);
+ // Give up control long enough for another thread/process to occasionally do something.
+ Thread.sleep(25);
prevLen = curLen;
}
} finally {
appender.stop();
}
- assertFalse("Appender did not stop", appender.isStarted());
+ assertFalse(appender.isStarted(), "Appender did not stop");
}
- @Test
- public void testLockingAppender() throws Exception {
+ @ParameterizedTest
+ @ValueSource(booleans = { false, true })
+ public void testLockingAppender(final boolean createOnDemand) throws Exception {
final int logEventCount = 1;
writer(true, logEventCount, "test", createOnDemand, false);
verifyFile(logEventCount);
}
- @Test
- public void testMultipleAppenderThreads() throws Exception {
- testMultipleLockingAppenderThreads(false, threadCount);
+ @ParameterizedTest
+ @ValueSource(booleans = { false, true })
+ public void testMultipleAppenderThreads(final boolean createOnDemand) throws Exception {
+ testMultipleLockingAppenderThreads(false, THREADS, createOnDemand);
}
- private void testMultipleLockingAppenderThreads(final boolean lock, final int threadCount)
+ private void testMultipleLockingAppenderThreads(final boolean lock, final int threadCount, boolean createOnDemand)
throws InterruptedException, Exception {
final ExecutorService threadPool = Executors.newFixedThreadPool(threadCount);
final Exception[] exceptionRef = new Exception[1];
final int logEventCount = 100;
- final Runnable runnable = new FileWriterRunnable(lock, logEventCount, exceptionRef);
+ final Runnable runnable = new FileWriterRunnable(createOnDemand, lock, logEventCount, exceptionRef);
for (int i = 0; i < threadCount; ++i) {
threadPool.execute(runnable);
}
threadPool.shutdown();
- Assert.assertTrue("The thread pool has not shutdown: " + threadPool,
- threadPool.awaitTermination(10, TimeUnit.SECONDS));
+ assertTrue(
+ threadPool.awaitTermination(10, TimeUnit.SECONDS), "The thread pool has not shutdown: " + threadPool);
if (exceptionRef[0] != null) {
throw exceptionRef[0];
}
verifyFile(threadCount * logEventCount);
}
- @Test
- public void testMultipleLockingAppenders() throws Exception {
- testMultipleLockingAppenderThreads(true, threadCount);
+ @ParameterizedTest
+ @ValueSource(booleans = { false, true })
+ public void testMultipleLockingAppenders(final boolean createOnDemand) throws Exception {
+ testMultipleLockingAppenderThreads(true, THREADS, createOnDemand);
}
- @Test
- @Ignore
- public void testMultipleVMs() throws Exception {
+ @ParameterizedTest
+ @ValueSource(booleans = { false, true })
+ @Disabled
+ public void testMultipleVMs(final boolean createOnDemand) throws Exception {
final String classPath = System.getProperty("java.class.path");
- final Integer logEventCount = 10;
+ final int logEventCount = 10;
final int processCount = 3;
final Process[] processes = new Process[processCount];
final ProcessBuilder[] builders = new ProcessBuilder[processCount];
for (int index = 0; index < processCount; ++index) {
builders[index] = new ProcessBuilder("java", "-cp", classPath, ProcessTest.class.getName(),
- "Process " + index, logEventCount.toString(), "true", Boolean.toString(createOnDemand));
+ "Process " + index, Integer.toString(logEventCount), "true", Boolean.toString(createOnDemand));
}
for (int index = 0; index < processCount; ++index) {
processes[index] = builders[index].start();
@@ -234,10 +222,10 @@ public class FileAppenderTest {
.withCreateOnDemand(createOnDemand)
.build();
// @formatter:on
- Assert.assertEquals(createOnDemand, appender.getManager().isCreateOnDemand());
+ assertEquals(createOnDemand, appender.getManager().isCreateOnDemand());
try {
appender.start();
- assertTrue("Appender did not start", appender.isStarted());
+ assertTrue(appender.isStarted(), "Appender did not start");
final boolean exists = Files.exists(PATH);
final String msg = String.format("concurrent = %s, createOnDemand = %s, file exists = %s", concurrent, createOnDemand,
exists);
@@ -245,27 +233,22 @@ public class FileAppenderTest {
// Can't really test createOnDemand && concurrent.
final boolean expectFileCreated = !createOnDemand;
if (concurrent && expectFileCreated) {
- Assert.assertTrue(msg, exists);
+ assertTrue(exists, msg);
} else if (expectFileCreated) {
- Assert.assertNotEquals(msg, createOnDemand, exists);
+ assertNotEquals(createOnDemand, exists, msg);
}
for (int i = 0; i < logEventCount; ++i) {
final LogEvent logEvent = Log4jLogEvent.newBuilder().setLoggerName("TestLogger")
.setLoggerFqcn(FileAppenderTest.class.getName()).setLevel(Level.INFO)
.setMessage(new SimpleMessage("Test")).setThreadName(name)
.setTimeMillis(System.currentTimeMillis()).build();
- try {
- appender.append(logEvent);
- Thread.sleep(25); // Give up control long enough for another thread/process to occasionally do
- // something.
- } catch (final Exception ex) {
- throw ex;
- }
+ appender.append(logEvent);
+ Thread.sleep(25); // Give up control long enough for another thread/process to occasionally do something.
}
} finally {
appender.stop();
}
- assertFalse("Appender did not stop", appender.isStarted());
+ assertFalse(appender.isStarted(), "Appender did not stop");
}
private void verifyFile(final int count) throws Exception {
@@ -274,24 +257,27 @@ public class FileAppenderTest {
final Pattern pattern = Pattern.compile(expected);
int lines = 0;
try (final BufferedReader is = new BufferedReader(new InputStreamReader(new FileInputStream(FILE_NAME)))) {
- String str = Strings.EMPTY;
+ String str;
while (is.ready()) {
str = is.readLine();
// System.out.println(str);
++lines;
final Matcher matcher = pattern.matcher(str);
- assertTrue("Unexpected data: " + str, matcher.matches());
+ assertTrue(matcher.matches(), "Unexpected data: " + str);
}
}
- Assert.assertEquals(count, lines);
+ assertEquals(count, lines);
}
- public class FileWriterRunnable implements Runnable {
+ public static class FileWriterRunnable implements Runnable {
+ private final boolean createOnDemand;
private final boolean lock;
private final int logEventCount;
private final Exception[] exceptionRef;
- public FileWriterRunnable(final boolean lock, final int logEventCount, final Exception[] exceptionRef) {
+ public FileWriterRunnable(
+ boolean createOnDemand, final boolean lock, final int logEventCount, final Exception[] exceptionRef) {
+ this.createOnDemand = createOnDemand;
this.lock = lock;
this.logEventCount = logEventCount;
this.exceptionRef = exceptionRef;