Skip to content

Commit

Permalink
Merge pull request #3221 from strangelookingnerd/migrate_to_junit5
Browse files Browse the repository at this point in the history
Migrate tests to JUnit5
  • Loading branch information
jvz authored Dec 3, 2024
2 parents 64ba05a + c4e6431 commit 5df0a57
Show file tree
Hide file tree
Showing 640 changed files with 5,018 additions and 5,093 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,34 @@
/**
* Test {@link BasicConfigurator}.
*/
public class BasicConfiguratorTest {
class BasicConfiguratorTest {

@Test
public void testConfigure() {
void testConfigure() {
// TODO More...
BasicConfigurator.configure();
}

@Test
public void testResetConfiguration() {
void testResetConfiguration() {
// TODO More...
BasicConfigurator.resetConfiguration();
}

@Test
public void testConfigureAppender() {
void testConfigureAppender() {
BasicConfigurator.configure(null);
// TODO More...
}

@Test
public void testConfigureConsoleAppender() {
void testConfigureConsoleAppender() {
// TODO What to do? Map to Log4j 2 Appender deeper in the code?
BasicConfigurator.configure(new ConsoleAppender());
}

@Test
public void testConfigureNullAppender() {
void testConfigureNullAppender() {
// The NullAppender name is null and we do not want an NPE when the name is used as a key in a
// ConcurrentHashMap.
BasicConfigurator.configure(NullAppender.getNullAppender());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@

// config from log4j-core test-jar
@LoggerContextSource(value = "log4j2-calling-class.xml")
public class CallerInformationTest {
class CallerInformationTest {

@Test
public void testClassLogger(@Named("Class") final ListAppender app) {
void testClassLogger(@Named("Class") final ListAppender app) {
app.clear();
final Logger logger = Logger.getLogger("ClassLogger");
logger.info("Ignored message contents.");
Expand All @@ -44,7 +44,7 @@ public void testClassLogger(@Named("Class") final ListAppender app) {
}

@Test
public void testMethodLogger(@Named("Method") final ListAppender app) {
void testMethodLogger(@Named("Method") final ListAppender app) {
app.clear();
final Logger logger = Logger.getLogger("MethodLogger");
logger.info("More messages.");
Expand Down
75 changes: 39 additions & 36 deletions log4j-1.2-api/src/test/java/org/apache/log4j/CategoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertInstanceOf;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand Down Expand Up @@ -51,7 +52,7 @@
/**
* Tests of Category.
*/
public class CategoryTest {
class CategoryTest {

static ConfigurationFactory cf = new BasicConfigurationFactory();

Expand All @@ -61,26 +62,26 @@ public class CategoryTest {
private static final org.apache.log4j.ListAppender version1Appender = new org.apache.log4j.ListAppender();

@BeforeAll
public static void setupAll() {
static void setupAll() {
appender.start();
version1Appender.setName(VERSION1_APPENDER_NAME);
ConfigurationFactory.setConfigurationFactory(cf);
LoggerContext.getContext().reconfigure();
}

@AfterAll
public static void cleanupAll() {
static void cleanupAll() {
ConfigurationFactory.removeConfigurationFactory(cf);
appender.stop();
}

@BeforeEach
public void before() {
void before() {
appender.clear();
}

@Test
public void testExist() {
void testExist() {
assertNull(Category.exists("Does not exist for sure"));
}

Expand All @@ -89,7 +90,7 @@ public void testExist() {
*/
@Test
@SuppressWarnings("deprecation")
public void testForcedLog() {
void testForcedLog() {
final MockCategory category = new MockCategory("org.example.foo");
category.setAdditivity(false);
category.setHierarchy(LogManager.getHierarchy());
Expand All @@ -98,35 +99,35 @@ public void testForcedLog() {
category.info("Hello, World");
List<LogEvent> list = appender.getEvents();
int events = list.size();
assertEquals(events, 1, "Number of events");
assertEquals(1, events, "Number of events");
LogEvent event = list.get(0);
Message msg = event.getMessage();
assertNotNull(msg, "No message");
// LOG4J2-3080: use message type consistently
assertTrue(msg instanceof SimpleMessage, "Incorrect Message type");
assertInstanceOf(SimpleMessage.class, msg, "Incorrect Message type");
assertEquals("Hello, World", msg.getFormat());
appender.clear();
// Logging a String map
category.info(Collections.singletonMap("hello", "world"));
list = appender.getEvents();
events = list.size();
assertEquals(events, 1, "Number of events");
assertEquals(1, events, "Number of events");
event = list.get(0);
msg = event.getMessage();
assertNotNull(msg, "No message");
assertTrue(msg instanceof MapMessage, "Incorrect Message type");
assertInstanceOf(MapMessage.class, msg, "Incorrect Message type");
Object[] objects = msg.getParameters();
assertEquals("world", objects[0]);
appender.clear();
// Logging a generic map
category.info(Collections.singletonMap(1234L, "world"));
list = appender.getEvents();
events = list.size();
assertEquals(events, 1, "Number of events");
assertEquals(1, events, "Number of events");
event = list.get(0);
msg = event.getMessage();
assertNotNull(msg, "No message");
assertTrue(msg instanceof MapMessage, "Incorrect Message type");
assertInstanceOf(MapMessage.class, msg, "Incorrect Message type");
objects = msg.getParameters();
assertEquals("world", objects[0]);
appender.clear();
Expand All @@ -135,23 +136,23 @@ public void testForcedLog() {
category.info(obj);
list = appender.getEvents();
events = list.size();
assertEquals(events, 1, "Number of events");
assertEquals(1, events, "Number of events");
event = list.get(0);
msg = event.getMessage();
assertNotNull(msg, "No message");
assertTrue(msg instanceof ObjectMessage, "Incorrect Message type");
assertInstanceOf(ObjectMessage.class, msg, "Incorrect Message type");
objects = msg.getParameters();
assertEquals(obj, objects[0]);
appender.clear();

category.log(Priority.INFO, "Hello, World");
list = appender.getEvents();
events = list.size();
assertEquals(events, 1, "Number of events");
assertEquals(1, events, "Number of events");
event = list.get(0);
msg = event.getMessage();
assertNotNull(msg, "No message");
assertTrue(msg instanceof SimpleMessage, "Incorrect Message type");
assertInstanceOf(SimpleMessage.class, msg, "Incorrect Message type");
assertEquals("Hello, World", msg.getFormat());
appender.clear();
}
Expand All @@ -162,16 +163,16 @@ public void testForcedLog() {
* @throws Exception thrown if Category.getChainedPriority can not be found.
*/
@Test
public void testGetChainedPriorityReturnType() throws Exception {
void testGetChainedPriorityReturnType() throws Exception {
final Method method = Category.class.getMethod("getChainedPriority", (Class[]) null);
assertEquals(method.getReturnType(), Priority.class);
assertEquals(Priority.class, method.getReturnType());
}

/**
* Tests l7dlog(Priority, String, Throwable).
*/
@Test
public void testL7dlog() {
void testL7dlog() {
final Logger logger = Logger.getLogger("org.example.foo");
logger.setLevel(Level.ERROR);
final Priority debug = Level.DEBUG;
Expand All @@ -183,7 +184,7 @@ public void testL7dlog() {
* Tests l7dlog(Priority, String, Object[], Throwable).
*/
@Test
public void testL7dlog4Param() {
void testL7dlog4Param() {
final Logger logger = Logger.getLogger("org.example.foo");
logger.setLevel(Level.ERROR);
final Priority debug = Level.DEBUG;
Expand All @@ -195,7 +196,7 @@ public void testL7dlog4Param() {
* Test using a pre-existing Log4j 2 logger
*/
@Test
public void testExistingLog4j2Logger() {
void testExistingLog4j2Logger() {
// create the logger using LogManager
org.apache.logging.log4j.LogManager.getLogger("existingLogger");
// Logger will be the one created above
Expand All @@ -217,7 +218,7 @@ public void testExistingLog4j2Logger() {
*/
@Deprecated
@Test
public void testSetPriority() {
void testSetPriority() {
final Logger logger = Logger.getLogger("org.example.foo");
final Priority debug = Level.DEBUG;
logger.setPriority(debug);
Expand All @@ -230,12 +231,12 @@ public void testSetPriority() {
*/
@Deprecated
@Test
public void testSetPriorityNull() {
void testSetPriorityNull() {
Logger.getLogger("org.example.foo").setPriority(null);
}

@Test
public void testClassName() {
void testClassName() {
final Category category = Category.getInstance("TestCategory");
final Layout<String> layout =
PatternLayout.newBuilder().withPattern("%d %p %C{1.} [%t] %m%n").build();
Expand All @@ -245,7 +246,7 @@ public void testClassName() {
((org.apache.logging.log4j.core.Logger) category.getLogger()).addAppender(appender);
category.error("Test Message");
final List<String> msgs = appender.getMessages();
assertEquals(msgs.size(), 1, "Incorrect number of messages. Expected 1 got " + msgs.size());
assertEquals(1, msgs.size(), "Incorrect number of messages. Expected 1 got " + msgs.size());
final String msg = msgs.get(0);
appender.clear();
final String threadName = Thread.currentThread().getName();
Expand All @@ -256,14 +257,14 @@ public void testClassName() {
}

@Test
public void testStringLog() {
void testStringLog() {
final String payload = "payload";
testMessageImplementation(
payload, SimpleMessage.class, message -> assertEquals(message.getFormattedMessage(), payload));
payload, SimpleMessage.class, message -> assertEquals(payload, message.getFormattedMessage()));
}

@Test
public void testCharSequenceLog() {
void testCharSequenceLog() {
final CharSequence payload = new CharSequence() {

@Override
Expand Down Expand Up @@ -293,15 +294,15 @@ public String toString() {
}

@Test
public void testMapLog() {
void testMapLog() {
final String key = "key";
final Object value = 0xDEADBEEF;
final Map<String, Object> payload = Collections.singletonMap(key, value);
testMessageImplementation(payload, MapMessage.class, message -> assertEquals(message.getData(), payload));
}

@Test
public void testObjectLog() {
void testObjectLog() {
final Object payload = new Object();
testMessageImplementation(
payload, ObjectMessage.class, message -> assertEquals(message.getParameter(), payload));
Expand Down Expand Up @@ -335,7 +336,7 @@ private <M extends Message> void testMessageImplementation(
}

@Test
public void testAddAppender() {
void testAddAppender() {
try {
final Logger rootLogger = LogManager.getRootLogger();
int count = version1Appender.getEvents().size();
Expand Down Expand Up @@ -363,7 +364,7 @@ public void testAddAppender() {
}

@Test
public void testGetAppender() {
void testGetAppender() {
try {
final Logger rootLogger = LogManager.getRootLogger();
final org.apache.logging.log4j.core.Logger v2RootLogger =
Expand All @@ -372,15 +373,17 @@ public void testGetAppender() {
v2RootLogger.addAppender(appender);
final List<Appender> rootAppenders = Collections.list(rootLogger.getAllAppenders());
assertEquals(1, rootAppenders.size(), "only v1 appenders");
assertTrue(rootAppenders.get(0) instanceof org.apache.log4j.ListAppender, "appender is a v1 ListAppender");
assertInstanceOf(
org.apache.log4j.ListAppender.class, rootAppenders.get(0), "appender is a v1 ListAppender");
assertEquals(
VERSION1_APPENDER_NAME,
rootLogger.getAppender(VERSION1_APPENDER_NAME).getName(),
"explicitly named appender");
final Appender v2ListAppender = rootLogger.getAppender(VERSION2_APPENDER_NAME);
assertTrue(v2ListAppender instanceof AppenderWrapper, "explicitly named appender");
assertTrue(
((AppenderWrapper) v2ListAppender).getAppender() instanceof ListAppender,
assertInstanceOf(AppenderWrapper.class, v2ListAppender, "explicitly named appender");
assertInstanceOf(
ListAppender.class,
((AppenderWrapper) v2ListAppender).getAppender(),
"appender is a v2 ListAppender");

final Logger logger = LogManager.getLogger(CategoryTest.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,33 @@
*/
package org.apache.log4j;

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

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
* Used to test Log4j 1 support.
*/
public class ConsoleAppenderTest {
class ConsoleAppenderTest {

private ConsoleAppender consoleAppender;

@BeforeEach
public void beforeEach() {
void beforeEach() {
consoleAppender = new ConsoleAppender();
}

@Test
public void testFollow() {
void testFollow() {
// Only really care that it compiles, behavior is secondary at this level.
consoleAppender.setFollow(true);
assertTrue(consoleAppender.getFollow());
}

@Test
public void testTarget() {
void testTarget() {
// Only really care that it compiles, behavior is secondary at this level.
consoleAppender.setTarget(ConsoleAppender.SYSTEM_OUT);
assertEquals(ConsoleAppender.SYSTEM_OUT, consoleAppender.getTarget());
Expand Down
3 changes: 1 addition & 2 deletions log4j-1.2-api/src/test/java/org/apache/log4j/LayoutTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,8 @@ protected Layout createLayout() {
/**
* Tests format.
*
* @throws Exception derived tests, particular XMLLayoutTest, may throw exceptions.
*/
public void testFormat() throws Exception {
public void testFormat() {
final Logger logger = Logger.getLogger("org.apache.log4j.LayoutTest");
final LoggingEvent event =
new LoggingEvent("org.apache.log4j.Logger", logger, Level.INFO, "Hello, World", null);
Expand Down
Loading

0 comments on commit 5df0a57

Please sign in to comment.