Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor parts of log4j-core-test to migrate to JUnit5 #3061

Draft
wants to merge 4 commits into
base: 2.x
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@
*/
package org.apache.logging.log4j;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.fasterxml.jackson.databind.ObjectWriter;
import java.io.IOException;
import org.apache.logging.log4j.MarkerManager.Log4jMarker;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* Tests {@link MarkerMixIn}.
Expand All @@ -36,7 +39,7 @@ public abstract class MarkerMixInTest {
private ObjectReader reader;
private ObjectWriter writer;

@Before
@BeforeEach
public void setUp() {
final ObjectMapper log4jObjectMapper = newObjectMapper();
writer = log4jObjectMapper.writer();
Expand All @@ -50,9 +53,9 @@ public void setUp() {
public void testNameOnly() throws IOException {
final Marker expected = MarkerManager.getMarker("A");
final String str = writeValueAsString(expected);
Assert.assertFalse(str.contains("parents"));
assertFalse(str.contains("parents"));
final Marker actual = reader.readValue(str);
Assert.assertEquals(expected, actual);
assertEquals(expected, actual);
}

@Test
Expand All @@ -61,9 +64,9 @@ public void testOneParent() throws IOException {
final Marker parent = MarkerManager.getMarker("PARENT_MARKER");
expected.addParents(parent);
final String str = writeValueAsString(expected);
Assert.assertTrue(str.contains("PARENT_MARKER"));
assertTrue(str.contains("PARENT_MARKER"));
final Marker actual = reader.readValue(str);
Assert.assertEquals(expected, actual);
assertEquals(expected, actual);
}

/**
Expand All @@ -85,9 +88,9 @@ public void testTwoParents() throws IOException {
expected.addParents(parent1);
expected.addParents(parent2);
final String str = writeValueAsString(expected);
Assert.assertTrue(str.contains("PARENT_MARKER1"));
Assert.assertTrue(str.contains("PARENT_MARKER2"));
assertTrue(str.contains("PARENT_MARKER1"));
assertTrue(str.contains("PARENT_MARKER2"));
final Marker actual = reader.readValue(str);
Assert.assertEquals(expected, actual);
assertEquals(expected, actual);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package org.apache.logging.log4j.core;

import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -50,7 +50,9 @@ public void run() {

private void trigger() {
Holder.LOGGER.info("Attempt to trigger");
assertTrue("Logger is of type " + Holder.LOGGER.getClass().getName(), Holder.LOGGER instanceof TestLogger);
assertTrue(
Holder.LOGGER instanceof TestLogger,
"Logger is of type " + Holder.LOGGER.getClass().getName());
if (((TestLogger) Holder.LOGGER).getEntries().isEmpty()) {
System.out.println("Logger contains no messages");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
*/
package org.apache.logging.log4j.core.appender;

import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.BufferedReader;
import java.io.File;
Expand All @@ -26,18 +26,17 @@
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.test.CoreLoggerContexts;
import org.apache.logging.log4j.core.test.categories.Layouts;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

/**
* Tests a "compact" XML file, no extra spaces or end of lines.
*/
@Category(Layouts.Xml.class)
@Tag("Layouts.Xml")
public class XmlCompactFileAppenderTest {

@BeforeClass
@BeforeAll
public static void beforeClass() {
System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "XmlCompactFileAppenderTest.xml");
}
Expand All @@ -57,21 +56,21 @@ public void testFlushAtEndOfBatch() throws Exception {
} finally {
file.delete();
}
assertNotNull("line1", line1);
assertNotNull(line1, "line1");
final String msg1 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
assertTrue("line1 incorrect: [" + line1 + "], does not contain: [" + msg1 + ']', line1.contains(msg1));
assertTrue(line1.contains(msg1), "line1 incorrect: [" + line1 + "], does not contain: [" + msg1 + ']');

final String msg2 = "<Events xmlns=\"http://logging.apache.org/log4j/2.0/events\">";
assertTrue("line1 incorrect: [" + line1 + "], does not contain: [" + msg2 + ']', line1.contains(msg2));
assertTrue(line1.contains(msg2), "line1 incorrect: [" + line1 + "], does not contain: [" + msg2 + ']');

final String msg3 = "<Event ";
assertTrue("line1 incorrect: [" + line1 + "], does not contain: [" + msg3 + ']', line1.contains(msg3));
assertTrue(line1.contains(msg3), "line1 incorrect: [" + line1 + "], does not contain: [" + msg3 + ']');

final String msg4 = logMsg;
assertTrue("line1 incorrect: [" + line1 + "], does not contain: [" + msg4 + ']', line1.contains(msg4));
assertTrue(line1.contains(msg4), "line1 incorrect: [" + line1 + "], does not contain: [" + msg4 + ']');

final String location = "testFlushAtEndOfBatch";
assertTrue("no location", !line1.contains(location));
assertTrue(!line1.contains(location), "no location");

assertTrue(line1.indexOf('\r') == -1);
assertTrue(line1.indexOf('\n') == -1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package org.apache.logging.log4j.core.appender;

import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.File;
import java.nio.charset.Charset;
Expand All @@ -26,18 +26,17 @@
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.test.CoreLoggerContexts;
import org.apache.logging.log4j.core.test.categories.Layouts;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

/**
* Tests a "complete" XML file a.k.a. a well-formed XML file.
*/
@Category(Layouts.Xml.class)
@Tag("Layouts.Xml")
public class XmlFileAppenderTest {

@BeforeClass
@BeforeAll
public static void beforeClass() {
System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, "XmlFileAppenderTest.xml");
}
Expand Down Expand Up @@ -65,11 +64,11 @@ public void testFlushAtEndOfBatch() throws Exception {

for (int i = 0; i < expect.length; i++) {
assertTrue(
"Expected line " + i + " to contain " + expect[i] + " but got: " + lines.get(i),
lines.get(i).contains(expect[i]));
lines.get(i).contains(expect[i]),
"Expected line " + i + " to contain " + expect[i] + " but got: " + lines.get(i));
}

final String location = "testFlushAtEndOfBatch";
assertTrue("no location", !lines.get(0).contains(location));
assertTrue(!lines.get(0).contains(location), "no location");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import java.sql.SQLException;
import org.apache.logging.log4j.core.config.Property;
import org.apache.logging.log4j.core.test.appender.db.jdbc.JdbcH2TestHelper;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class DriverManagerH2ConnectionSourceTest extends AbstractH2Test {

Expand All @@ -40,7 +40,7 @@ public void testH2Properties() throws SQLException {
.build();
// @formatter:on
try (final Connection conn = source.getConnection()) {
Assert.assertFalse(conn.isClosed());
Assertions.assertFalse(conn.isClosed());
}
}

Expand All @@ -54,7 +54,7 @@ public void testH2UserAndPassword() throws SQLException {
.build();
// @formatter:on
try (final Connection conn = source.getConnection()) {
Assert.assertFalse(conn.isClosed());
Assertions.assertFalse(conn.isClosed());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
import static org.junit.Assert.assertEquals;

import org.apache.logging.log4j.core.LoggerContext;
import org.apache.logging.log4j.core.test.categories.Appenders;
import org.apache.logging.log4j.core.test.junit.LoggerContextSource;
import org.junit.experimental.categories.Category;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.api.Test;

/**
Expand All @@ -30,7 +29,7 @@
*
* @see <a href="https://issues.apache.org/jira/browse/LOG4J2-2916">LOG4J2-2916</a>
*/
@Category(Appenders.Kafka.class)
@Tag("Appenders.Kafka")
@LoggerContextSource("KafkaManagerProducerThreadLeakTest.xml")
class KafkaManagerProducerThreadLeakTest {

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

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.jupiter.MockitoExtension;

@RunWith(MockitoJUnitRunner.class)
@ExtendWith(MockitoExtension.class)
public class NoSqlAppenderTest {

@Mock
Expand All @@ -35,18 +35,18 @@ public class NoSqlAppenderTest {
public void testNoProvider() {
final NoSqlAppender appender = NoSqlAppender.createAppender("myName01", null, null, null, null);

assertNull("The appender should be null.", appender);
assertNull(appender, "The appender should be null.");
}

@Test
public void testProvider() {
final NoSqlAppender appender = NoSqlAppender.createAppender("myName01", null, null, null, provider);

assertNotNull("The appender should not be null.", appender);
assertNotNull(appender, "The appender should not be null.");
assertEquals(
"The toString value is not correct.",
"myName01{ manager=noSqlManager{ description=myName01, bufferSize=0, provider=" + provider + " } }",
appender.toString());
appender.toString(),
"The toString value is not correct.");

appender.stop();
}
Expand All @@ -55,12 +55,12 @@ public void testProvider() {
public void testProviderBuffer() {
final NoSqlAppender appender = NoSqlAppender.createAppender("anotherName02", null, null, "25", provider);

assertNotNull("The appender should not be null.", appender);
assertNotNull(appender, "The appender should not be null.");
assertEquals(
"The toString value is not correct.",
"anotherName02{ manager=noSqlManager{ description=anotherName02, bufferSize=25, provider=" + provider
+ " } }",
appender.toString());
appender.toString(),
"The toString value is not correct.");

appender.stop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package org.apache.logging.log4j.core.appender.rolling;

import static org.junit.Assert.assertTrue;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assumptions.assumeTrue;

import java.nio.file.DirectoryStream;
Expand Down Expand Up @@ -81,13 +81,14 @@ public void performTest(final LoggerContext loggerContext) throws Exception {
if (path.toFile().getName().startsWith(ROLLED)) {
rolled = true;
final List<String> lines = Files.readAllLines(path);
assertTrue("No messages in " + path.toFile().getName(), lines.size() > 0);
assertTrue(
"Missing message for " + path.toFile().getName(),
lines.get(0).startsWith(PREFIX + "1"));
lines.size() > 0, "No messages in " + path.toFile().getName());
assertTrue(
lines.get(0).startsWith(PREFIX + "1"),
"Missing message for " + path.toFile().getName());
}
}
}
assertTrue("File did not roll", rolled);
assertTrue(rolled, "File did not roll");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
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.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class RollingFileAppenderAccessTest {

Expand All @@ -49,8 +49,8 @@ public void testAccessManagerWithBuilder() throws IOException {
final RollingFileManager manager = appender.getManager();
// Since the RolloverStrategy and TriggeringPolicy are immutable, we could also use generics to type their
// access.
Assert.assertNotNull(manager.getRolloverStrategy());
Assert.assertNotNull(manager.getTriggeringPolicy());
Assertions.assertNotNull(manager.getRolloverStrategy());
Assertions.assertNotNull(manager.getTriggeringPolicy());
}
}

Expand Down Expand Up @@ -84,8 +84,8 @@ public void testAccessManagerWithStrings() throws IOException {
final RollingFileManager manager = appender.getManager();
// Since the RolloverStrategy and TriggeringPolicy are immutable, we could also use generics to type their
// access.
Assert.assertNotNull(manager.getRolloverStrategy());
Assert.assertNotNull(manager.getTriggeringPolicy());
Assertions.assertNotNull(manager.getRolloverStrategy());
Assertions.assertNotNull(manager.getTriggeringPolicy());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
package org.apache.logging.log4j.core.appender.rolling;

import org.apache.logging.log4j.core.appender.RollingFileAppender;
import org.junit.Assert;
import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class RollingFileAppenderBuilderTest {

Expand All @@ -27,6 +27,6 @@ public class RollingFileAppenderBuilderTest {
*/
@Test
public void testDefaultImmediateFlush() {
Assert.assertTrue(RollingFileAppender.newBuilder().isImmediateFlush());
Assertions.assertTrue(RollingFileAppender.newBuilder().isImmediateFlush());
}
}
Loading