Skip to content

Commit 37573b1

Browse files
refactor: Migrate asynclogger tests to junit5
Signed-off-by: Ninette Adhikari <[email protected]>
1 parent 1e3223c commit 37573b1

16 files changed

+155
-168
lines changed

log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerClassLoadDeadlockTest.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,30 +16,32 @@
1616
*/
1717
package org.apache.logging.log4j.core.async;
1818

19-
import static org.junit.Assert.assertNotNull;
19+
import static org.junit.jupiter.api.Assertions.assertNotNull;
2020

21+
import java.util.concurrent.TimeUnit;
2122
import org.apache.logging.log4j.core.config.ConfigurationFactory;
22-
import org.apache.logging.log4j.core.test.categories.AsyncLoggers;
23-
import org.junit.BeforeClass;
24-
import org.junit.Test;
25-
import org.junit.experimental.categories.Category;
23+
import org.junit.jupiter.api.BeforeAll;
24+
import org.junit.jupiter.api.Tag;
25+
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.Timeout;
2627

2728
/**
2829
* Test class loading deadlock condition from the LOG4J2-1457
2930
*/
30-
@Category(AsyncLoggers.class)
31+
@Tag("AsyncLoggers")
3132
public class AsyncLoggerClassLoadDeadlockTest {
3233

3334
static final int RING_BUFFER_SIZE = 128;
3435

35-
@BeforeClass
36+
@BeforeAll
3637
public static void beforeClass() {
3738
System.setProperty("Log4jContextSelector", "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");
3839
System.setProperty("AsyncLogger.RingBufferSize", String.valueOf(RING_BUFFER_SIZE));
3940
System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "AsyncLoggerConsoleTest.xml");
4041
}
4142

42-
@Test(timeout = 30000)
43+
@Test
44+
@Timeout(value = 30, unit = TimeUnit.SECONDS)
4345
public void testClassLoaderDeadlock() throws Exception {
4446
// touch the class so static init will be called
4547
final AsyncLoggerClassLoadDeadlock temp = new AsyncLoggerClassLoadDeadlock();

log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigAutoFlushTest.java

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
*/
1717
package org.apache.logging.log4j.core.async;
1818

19-
import static org.junit.Assert.assertNotNull;
20-
import static org.junit.Assert.assertTrue;
19+
import static org.junit.jupiter.api.Assertions.assertNotNull;
20+
import static org.junit.jupiter.api.Assertions.assertTrue;
2121

2222
import java.io.BufferedReader;
2323
import java.io.File;
@@ -26,23 +26,22 @@
2626
import org.apache.logging.log4j.Logger;
2727
import org.apache.logging.log4j.core.config.ConfigurationFactory;
2828
import org.apache.logging.log4j.core.test.CoreLoggerContexts;
29-
import org.apache.logging.log4j.core.test.categories.AsyncLoggers;
30-
import org.junit.BeforeClass;
31-
import org.junit.Test;
32-
import org.junit.experimental.categories.Category;
29+
import org.junit.jupiter.api.BeforeAll;
30+
import org.junit.jupiter.api.Tag;
31+
import org.junit.jupiter.api.Test;
3332

34-
@Category(AsyncLoggers.class)
33+
@Tag("AsyncLoggers")
3534
public class AsyncLoggerConfigAutoFlushTest {
3635

37-
@BeforeClass
36+
@BeforeAll
3837
public static void beforeClass() {
3938
System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "AsyncLoggerConfigAutoFlushTest.xml");
4039
}
4140

4241
@Test
4342
public void testFlushAtEndOfBatch() throws Exception {
4443
final File file = new File("target", "AsyncLoggerConfigAutoFlushTest.log");
45-
assertTrue("Deleted old file before test", !file.exists() || file.delete());
44+
assertTrue(!file.exists() || file.delete(), "Deleted old file before test");
4645

4746
final Logger log = LogManager.getLogger("com.foo.Bar");
4847
final String msg = "Message flushed with immediate flush=false";
@@ -53,6 +52,6 @@ public void testFlushAtEndOfBatch() throws Exception {
5352
reader.close();
5453
file.delete();
5554
assertNotNull("line1", line1);
56-
assertTrue("line1 correct", line1.contains(msg));
55+
assertTrue(line1.contains(msg), "line1 correct");
5756
}
5857
}

log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigErrorOnFormat.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@
1616
*/
1717
package org.apache.logging.log4j.core.async;
1818

19+
import static org.hamcrest.MatcherAssert.assertThat;
1920
import static org.hamcrest.Matchers.containsString;
20-
import static org.junit.Assert.assertNull;
21-
import static org.junit.Assert.assertThat;
22-
import static org.junit.Assert.assertTrue;
21+
import static org.junit.jupiter.api.Assertions.assertNull;
22+
import static org.junit.jupiter.api.Assertions.assertTrue;
2323

2424
import java.io.BufferedReader;
2525
import java.io.File;
@@ -29,26 +29,25 @@
2929
import org.apache.logging.log4j.core.config.ConfigurationFactory;
3030
import org.apache.logging.log4j.core.impl.DefaultLogEventFactory;
3131
import org.apache.logging.log4j.core.test.CoreLoggerContexts;
32-
import org.apache.logging.log4j.core.test.categories.AsyncLoggers;
3332
import org.apache.logging.log4j.message.AsynchronouslyFormattable;
3433
import org.apache.logging.log4j.message.Message;
35-
import org.junit.AfterClass;
36-
import org.junit.BeforeClass;
37-
import org.junit.Test;
38-
import org.junit.experimental.categories.Category;
34+
import org.junit.jupiter.api.AfterAll;
35+
import org.junit.jupiter.api.BeforeAll;
36+
import org.junit.jupiter.api.Tag;
37+
import org.junit.jupiter.api.Test;
3938

40-
@Category(AsyncLoggers.class)
39+
@Tag("AsyncLoggers")
4140
public class AsyncLoggerConfigErrorOnFormat {
4241

43-
@BeforeClass
42+
@BeforeAll
4443
public static void beforeClass() {
4544
System.setProperty("log4j2.enableThreadlocals", "true");
4645
System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "AsyncLoggerConfigErrorOnFormat.xml");
4746
// Log4jLogEvent.toString invokes message.toString
4847
System.setProperty("log4j2.logEventFactory", DefaultLogEventFactory.class.getName());
4948
}
5049

51-
@AfterClass
50+
@AfterAll
5251
public static void afterClass() {
5352
System.clearProperty("log4j2.enableThreadlocals");
5453
System.clearProperty("log4j2.logEventFactory");
@@ -57,7 +56,7 @@ public static void afterClass() {
5756
@Test
5857
public void testError() throws Exception {
5958
final File file = new File("target", "AsyncLoggerConfigErrorOnFormat.log");
60-
assertTrue("Deleted old file before test", !file.exists() || file.delete());
59+
assertTrue(!file.exists() || file.delete(), "Deleted old file before test");
6160

6261
final Logger log = LogManager.getLogger("com.foo.Bar");
6362
log.info(new ThrowsErrorOnFormatMessage());
@@ -71,7 +70,7 @@ public void testError() throws Exception {
7170
file.delete();
7271

7372
assertThat(line1, containsString("Second message"));
74-
assertNull("Expected only one line", line2);
73+
assertNull(line2, "Expected only one line");
7574
}
7675

7776
@AsynchronouslyFormattable // Shouldn't call getFormattedMessage early

log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigTest2.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
*/
1717
package org.apache.logging.log4j.core.async;
1818

19-
import static org.junit.Assert.assertNotNull;
20-
import static org.junit.Assert.assertTrue;
19+
import static org.junit.jupiter.api.Assertions.assertNotNull;
20+
import static org.junit.jupiter.api.Assertions.assertTrue;
2121

2222
import java.io.BufferedReader;
2323
import java.io.File;
@@ -27,18 +27,17 @@
2727
import org.apache.logging.log4j.core.LoggerContext;
2828
import org.apache.logging.log4j.core.config.ConfigurationFactory;
2929
import org.apache.logging.log4j.core.test.CoreLoggerContexts;
30-
import org.apache.logging.log4j.core.test.categories.AsyncLoggers;
31-
import org.junit.Test;
32-
import org.junit.experimental.categories.Category;
30+
import org.junit.jupiter.api.Tag;
31+
import org.junit.jupiter.api.Test;
3332

34-
@Category(AsyncLoggers.class)
33+
@Tag("AsyncLoggers")
3534
public class AsyncLoggerConfigTest2 {
3635

3736
@Test
3837
public void testConsecutiveReconfigure() throws Exception {
3938
System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "AsyncLoggerConfigTest2.xml");
4039
final File file = new File("target", "AsyncLoggerConfigTest2.log");
41-
assertTrue("Deleted old file before test", !file.exists() || file.delete());
40+
assertTrue(!file.exists() || file.delete(), "Deleted old file before test");
4241

4342
final Logger log = LogManager.getLogger("com.foo.Bar");
4443
final String msg = "Message before reconfig";
@@ -57,9 +56,9 @@ public void testConsecutiveReconfigure() throws Exception {
5756
final String line2 = reader.readLine();
5857
reader.close();
5958
file.delete();
60-
assertNotNull("line1", line1);
61-
assertNotNull("line2", line2);
62-
assertTrue("line1 " + line1, line1.contains(msg));
63-
assertTrue("line2 " + line2, line2.contains(msg2));
59+
assertNotNull(line1, "line1");
60+
assertNotNull(line2, "line2");
61+
assertTrue(line1.contains(msg), "line1 " + line1);
62+
assertTrue(line2.contains(msg2), "line2 " + line2);
6463
}
6564
}

log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerConfigWithAsyncEnabledTest.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@
1616
*/
1717
package org.apache.logging.log4j.core.async;
1818

19+
import static org.hamcrest.MatcherAssert.assertThat;
1920
import static org.hamcrest.Matchers.containsString;
20-
import static org.junit.Assert.assertThat;
21-
import static org.junit.Assert.assertTrue;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
2222

2323
import java.io.BufferedReader;
2424
import java.io.File;
@@ -27,24 +27,23 @@
2727
import org.apache.logging.log4j.Logger;
2828
import org.apache.logging.log4j.core.config.ConfigurationFactory;
2929
import org.apache.logging.log4j.core.test.CoreLoggerContexts;
30-
import org.apache.logging.log4j.core.test.categories.AsyncLoggers;
31-
import org.junit.AfterClass;
32-
import org.junit.BeforeClass;
33-
import org.junit.Test;
34-
import org.junit.experimental.categories.Category;
30+
import org.junit.jupiter.api.AfterAll;
31+
import org.junit.jupiter.api.BeforeAll;
32+
import org.junit.jupiter.api.Tag;
33+
import org.junit.jupiter.api.Test;
3534

36-
@Category(AsyncLoggers.class)
35+
@Tag("AsyncLoggers")
3736
public class AsyncLoggerConfigWithAsyncEnabledTest {
3837

39-
@BeforeClass
38+
@BeforeAll
4039
public static void beforeClass() {
4140
System.setProperty("log4j2.enableThreadlocals", "true");
4241
System.setProperty("log4j2.contextSelector", AsyncLoggerContextSelector.class.getCanonicalName());
4342
// Reuse the configuration from AsyncLoggerConfigTest4
4443
System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "AsyncLoggerConfigTest4.xml");
4544
}
4645

47-
@AfterClass
46+
@AfterAll
4847
public static void afterClass() {
4948
System.clearProperty("log4j2.enableThreadlocals");
5049
System.clearProperty("log4j2.contextSelector");
@@ -53,7 +52,7 @@ public static void afterClass() {
5352
@Test
5453
public void testParametersAreAvailableToLayout() throws Exception {
5554
final File file = new File("target", "AsyncLoggerConfigTest4.log");
56-
assertTrue("Deleted old file before test", !file.exists() || file.delete());
55+
assertTrue(!file.exists() || file.delete(), "Deleted old file before test");
5756

5857
final Logger log = LogManager.getLogger("com.foo.Bar");
5958
final String format = "Additive logging: {} for the price of {}!";

log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerContextSelectorInitialStateTest.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@
1616
*/
1717
package org.apache.logging.log4j.core.async;
1818

19-
import static org.junit.Assert.assertTrue;
19+
import static org.junit.jupiter.api.Assertions.assertTrue;
2020

21-
import org.apache.logging.log4j.core.test.categories.AsyncLoggers;
22-
import org.junit.Test;
23-
import org.junit.experimental.categories.Category;
21+
import org.junit.jupiter.api.Tag;
22+
import org.junit.jupiter.api.Test;
2423

25-
@Category(AsyncLoggers.class)
24+
@Tag("AsyncLoggers")
2625
public class AsyncLoggerContextSelectorInitialStateTest {
2726

2827
@Test

log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerContextSelectorTest.java

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,15 @@
1616
*/
1717
package org.apache.logging.log4j.core.async;
1818

19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertTrue;
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertTrue;
2121

2222
import java.util.List;
2323
import org.apache.logging.log4j.core.LoggerContext;
24-
import org.apache.logging.log4j.core.test.categories.AsyncLoggers;
25-
import org.junit.Test;
26-
import org.junit.experimental.categories.Category;
24+
import org.junit.jupiter.api.Tag;
25+
import org.junit.jupiter.api.Test;
2726

28-
@Category(AsyncLoggers.class)
27+
@Tag("AsyncLoggers")
2928
public class AsyncLoggerContextSelectorTest {
3029

3130
private static final String FQCN = AsyncLoggerContextSelectorTest.class.getName();

log4j-core-test/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerDefaultLocationTest.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,33 @@
1616
*/
1717
package org.apache.logging.log4j.core.async;
1818

19-
import static org.junit.Assert.assertEquals;
20-
import static org.junit.Assert.assertFalse;
21-
import static org.junit.Assert.assertNotNull;
22-
import static org.junit.Assert.assertNull;
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertFalse;
21+
import static org.junit.jupiter.api.Assertions.assertNotNull;
22+
import static org.junit.jupiter.api.Assertions.assertNull;
2323

2424
import org.apache.logging.log4j.LogManager;
2525
import org.apache.logging.log4j.Logger;
2626
import org.apache.logging.log4j.core.LogEvent;
2727
import org.apache.logging.log4j.core.LoggerContext;
2828
import org.apache.logging.log4j.core.config.ConfigurationFactory;
2929
import org.apache.logging.log4j.core.test.appender.ListAppender;
30-
import org.apache.logging.log4j.core.test.categories.AsyncLoggers;
3130
import org.apache.logging.log4j.core.util.Constants;
3231
import org.apache.logging.log4j.util.Strings;
33-
import org.junit.AfterClass;
34-
import org.junit.BeforeClass;
35-
import org.junit.Test;
36-
import org.junit.experimental.categories.Category;
32+
import org.junit.jupiter.api.AfterAll;
33+
import org.junit.jupiter.api.BeforeAll;
34+
import org.junit.jupiter.api.Tag;
35+
import org.junit.jupiter.api.Test;
3736

38-
@Category(AsyncLoggers.class)
37+
@Tag("AsyncLoggers")
3938
public class AsyncLoggerDefaultLocationTest {
4039

41-
@BeforeClass
40+
@BeforeAll
4241
public static void beforeClass() {
4342
System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "AsyncLoggerDefaultLocationTest.xml");
4443
}
4544

46-
@AfterClass
45+
@AfterAll
4746
public static void afterClass() {
4847
System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR, Strings.EMPTY);
4948
}
@@ -59,7 +58,7 @@ public void testAsyncLogWritesToLog() throws Exception {
5958
context.stop();
6059
assertEquals(1, app.getEvents().size());
6160
final LogEvent event = app.getEvents().get(0);
62-
assertFalse("includeLocation should be false", event.isIncludeLocation());
63-
assertNull("Location data should not be present", event.getSource());
61+
assertFalse(event.isIncludeLocation(), "includeLocation should be false");
62+
assertNull(event.getSource(), "Location data should not be present");
6463
}
6564
}

0 commit comments

Comments
 (0)