This is an automated email from the ASF dual-hosted git repository. ggregory pushed a commit to branch release-2.x in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git
commit 4d97c9ef7393f37fe9b0bfe2862ae15ae1640c1a Author: Gary Gregory <[email protected]> AuthorDate: Wed Jan 19 07:47:22 2022 -0500 Sort methods. --- .../org/apache/logging/log4j/core/LoggerTest.java | 224 ++++++++++----------- 1 file changed, 112 insertions(+), 112 deletions(-) diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggerTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggerTest.java index 9ee22f1..1cf24fe 100644 --- a/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggerTest.java +++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggerTest.java @@ -56,11 +56,35 @@ import static org.junit.jupiter.api.Assertions.*; public class LoggerTest { static final String CONFIG = "log4j-test2.xml"; + private static void checkMessageFactory(final MessageFactory messageFactory1, final Logger testLogger1) { + if (messageFactory1 == null) { + assertEquals(AbstractLogger.DEFAULT_MESSAGE_FACTORY_CLASS, testLogger1.getMessageFactory().getClass()); + } else { + MessageFactory actual = testLogger1.getMessageFactory(); + if (actual instanceof MessageFactory2Adapter) { + actual = ((MessageFactory2Adapter) actual).getOriginal(); + } + assertEquals(messageFactory1, actual); + } + } + private static Logger testMessageFactoryMismatch(final String name, + final MessageFactory messageFactory1, + final MessageFactory messageFactory2) { + final Logger testLogger1 = (Logger) LogManager.getLogger(name, messageFactory1); + assertNotNull(testLogger1); + checkMessageFactory(messageFactory1, testLogger1); + final Logger testLogger2 = (Logger) LogManager.getLogger(name, messageFactory2); + assertNotNull(testLogger2); + checkMessageFactory(messageFactory2, testLogger2); + return testLogger1; + } org.apache.logging.log4j.Logger logger; org.apache.logging.log4j.Logger loggerChild; org.apache.logging.log4j.Logger loggerGrandchild; private final ListAppender app; + private final ListAppender host; + private final ListAppender noThrown; public LoggerTest(final LoggerContext context, @Named("List") final ListAppender app, @Named("HostTest") final ListAppender host, @Named("NoThrowable") final ListAppender noThrown) { @@ -77,6 +101,14 @@ public class LoggerTest { } @Test + public void basicFlow() { + logger.traceEntry(); + logger.traceExit(); + final List<LogEvent> events = app.getEvents(); + assertEventCount(events, 2); + } + + @Test public void builder() { logger.atDebug().withLocation().log("Hello"); Marker marker = MarkerManager.getMarker("test"); @@ -93,37 +125,6 @@ public class LoggerTest { } @Test - public void basicFlow() { - logger.traceEntry(); - logger.traceExit(); - final List<LogEvent> events = app.getEvents(); - assertEventCount(events, 2); - } - - @Test - public void simpleFlow() { - logger.entry(CONFIG); - logger.traceExit(0); - final List<LogEvent> events = app.getEvents(); - assertEventCount(events, 2); - } - - @Test - public void simpleFlowDepreacted() { - logger.entry(CONFIG); - logger.exit(0); - final List<LogEvent> events = app.getEvents(); - assertEventCount(events, 2); - } - - @Test - public void throwing() { - logger.throwing(new IllegalArgumentException("Test Exception")); - final List<LogEvent> events = app.getEvents(); - assertEventCount(events, 1); - } - - @Test public void catching() { try { throw new NullPointerException(); @@ -262,6 +263,20 @@ public class LoggerTest { } @Test + public void debugObject() { + logger.debug(new Date()); + final List<LogEvent> events = app.getEvents(); + assertEventCount(events, 1); + } + + @Test + public void debugWithParms() { + logger.debug("Hello, {}", "World"); + final List<LogEvent> events = app.getEvents(); + assertEventCount(events, 1); + } + + @Test public void getLogger_String_MessageFactoryMismatch(final TestInfo testInfo) { final Logger testLogger = testMessageFactoryMismatch(testInfo.getTestMethod().map(Method::getName).orElseThrow(AssertionError::new), StringFormatterMessageFactory.INSTANCE, ParameterizedMessageFactory.INSTANCE); @@ -280,76 +295,48 @@ public class LoggerTest { assertEquals(String.format("%,d", Integer.MAX_VALUE), events.get(0).getMessage().getFormattedMessage()); } - private static Logger testMessageFactoryMismatch(final String name, - final MessageFactory messageFactory1, - final MessageFactory messageFactory2) { - final Logger testLogger1 = (Logger) LogManager.getLogger(name, messageFactory1); - assertNotNull(testLogger1); - checkMessageFactory(messageFactory1, testLogger1); - final Logger testLogger2 = (Logger) LogManager.getLogger(name, messageFactory2); - assertNotNull(testLogger2); - checkMessageFactory(messageFactory2, testLogger2); - return testLogger1; - } - - private static void checkMessageFactory(final MessageFactory messageFactory1, final Logger testLogger1) { - if (messageFactory1 == null) { - assertEquals(AbstractLogger.DEFAULT_MESSAGE_FACTORY_CLASS, testLogger1.getMessageFactory().getClass()); - } else { - MessageFactory actual = testLogger1.getMessageFactory(); - if (actual instanceof MessageFactory2Adapter) { - actual = ((MessageFactory2Adapter) actual).getOriginal(); - } - assertEquals(messageFactory1, actual); - } - } - @Test - public void debugObject() { - logger.debug(new Date()); + public void mdc() { + ThreadContext.put("TestYear", "2010"); + logger.debug("Debug message"); + ThreadContext.clearMap(); + logger.debug("Debug message"); final List<LogEvent> events = app.getEvents(); - assertEventCount(events, 1); + assertEventCount(events, 2); } @Test - public void debugWithParms() { - logger.debug("Hello, {}", "World"); + public void paramWithExceptionTest() throws Exception { + logger.error("Throwing with parameters {}", "TestParam", new NullPointerException("Test Exception")); final List<LogEvent> events = app.getEvents(); - assertEventCount(events, 1); - } - - @Test - public void testImpliedThrowable(final LoggerContext context) { - final org.apache.logging.log4j.Logger testLogger = context.getLogger("org.apache.logging.log4j.hosttest"); - testLogger.debug("This is a test", new Throwable("Testing")); - final List<String> msgs = host.getMessages(); - assertEquals(1, msgs.size(), "Incorrect number of messages. Expected 1, actual " + msgs.size()); - final String expected = "java.lang.Throwable: Testing"; - assertTrue(msgs.get(0).contains(expected), "Incorrect message data"); + assertNotNull(events, "Log event list not returned"); + assertEquals(1, events.size(), "Incorrect number of log events"); + final LogEvent event = events.get(0); + final Throwable thrown = event.getThrown(); + assertNotNull(thrown, "No throwable present in log event"); + final Message msg = event.getMessage(); + assertEquals("Throwing with parameters {}", msg.getFormat()); + assertEquals("Throwing with parameters TestParam", msg.getFormattedMessage()); + assertArrayEquals(new Object[] { "TestParam", thrown }, msg.getParameters()); } - @Test - public void testSuppressedThrowable(final LoggerContext context) { - final org.apache.logging.log4j.Logger testLogger = context.getLogger("org.apache.logging.log4j.nothrown"); - testLogger.debug("This is a test", new Throwable("Testing")); - final List<String> msgs = noThrown.getMessages(); - assertEquals(1, msgs.size(), "Incorrect number of messages. Expected 1, actual " + msgs.size()); - final String suppressed = "java.lang.Throwable: Testing"; - assertFalse(msgs.get(0).contains(suppressed), "Incorrect message data"); + public void simpleFlow() { + logger.entry(CONFIG); + logger.traceExit(0); + final List<LogEvent> events = app.getEvents(); + assertEventCount(events, 2); } - @Test - public void mdc() { - ThreadContext.put("TestYear", "2010"); - logger.debug("Debug message"); - ThreadContext.clearMap(); - logger.debug("Debug message"); + public void simpleFlowDepreacted() { + logger.entry(CONFIG); + logger.exit(0); final List<LogEvent> events = app.getEvents(); assertEventCount(events, 2); } + @Test public void structuredData() { ThreadContext.put("loginId", "JohnDoe"); @@ -365,6 +352,36 @@ public class LoggerTest { assertEventCount(events, 1); } + + @Test + public void testAdditivity(final LoggerContext context) throws Exception { + final Logger localLogger = context.getLogger("org.apache.test"); + localLogger.error("Test parent additivity"); + final List<LogEvent> events = app.getEvents(); + assertEventCount(events, 1); + } + + @Test + public void testImpliedThrowable(final LoggerContext context) { + final org.apache.logging.log4j.Logger testLogger = context.getLogger("org.apache.logging.log4j.hosttest"); + testLogger.debug("This is a test", new Throwable("Testing")); + final List<String> msgs = host.getMessages(); + assertEquals(1, msgs.size(), "Incorrect number of messages. Expected 1, actual " + msgs.size()); + final String expected = "java.lang.Throwable: Testing"; + assertTrue(msgs.get(0).contains(expected), "Incorrect message data"); + } + + @Test + public void testLevelInheritance(final LoggerContext context) throws Exception { + final Configuration config = context.getConfiguration(); + final LoggerConfig loggerConfig = config.getLoggerConfig("org.apache.logging.log4j.core.LoggerTest"); + assertNotNull(loggerConfig); + assertEquals(loggerConfig.getName(), "org.apache.logging.log4j.core.LoggerTest"); + assertEquals(loggerConfig.getLevel(), Level.DEBUG); + final Logger localLogger = context.getLogger("org.apache.logging.log4j.core.LoggerTest"); + assertSame(localLogger.getLevel(), Level.DEBUG, "Incorrect level - expected DEBUG, actual " + localLogger.getLevel()); + } + @Test public void testReconfiguration(final LoggerContext context) throws Exception { final Configuration oldConfig = context.getConfiguration(); @@ -390,37 +407,20 @@ public class LoggerTest { } @Test - public void testAdditivity(final LoggerContext context) throws Exception { - final Logger localLogger = context.getLogger("org.apache.test"); - localLogger.error("Test parent additivity"); - final List<LogEvent> events = app.getEvents(); - assertEventCount(events, 1); - } - - @Test - public void testLevelInheritance(final LoggerContext context) throws Exception { - final Configuration config = context.getConfiguration(); - final LoggerConfig loggerConfig = config.getLoggerConfig("org.apache.logging.log4j.core.LoggerTest"); - assertNotNull(loggerConfig); - assertEquals(loggerConfig.getName(), "org.apache.logging.log4j.core.LoggerTest"); - assertEquals(loggerConfig.getLevel(), Level.DEBUG); - final Logger localLogger = context.getLogger("org.apache.logging.log4j.core.LoggerTest"); - assertSame(localLogger.getLevel(), Level.DEBUG, "Incorrect level - expected DEBUG, actual " + localLogger.getLevel()); + public void testSuppressedThrowable(final LoggerContext context) { + final org.apache.logging.log4j.Logger testLogger = context.getLogger("org.apache.logging.log4j.nothrown"); + testLogger.debug("This is a test", new Throwable("Testing")); + final List<String> msgs = noThrown.getMessages(); + assertEquals(1, msgs.size(), "Incorrect number of messages. Expected 1, actual " + msgs.size()); + final String suppressed = "java.lang.Throwable: Testing"; + assertFalse(msgs.get(0).contains(suppressed), "Incorrect message data"); } @Test - public void paramWithExceptionTest() throws Exception { - logger.error("Throwing with parameters {}", "TestParam", new NullPointerException("Test Exception")); + public void throwing() { + logger.throwing(new IllegalArgumentException("Test Exception")); final List<LogEvent> events = app.getEvents(); - assertNotNull(events, "Log event list not returned"); - assertEquals(1, events.size(), "Incorrect number of log events"); - final LogEvent event = events.get(0); - final Throwable thrown = event.getThrown(); - assertNotNull(thrown, "No throwable present in log event"); - final Message msg = event.getMessage(); - assertEquals("Throwing with parameters {}", msg.getFormat()); - assertEquals("Throwing with parameters TestParam", msg.getFormattedMessage()); - assertArrayEquals(new Object[] { "TestParam", thrown }, msg.getParameters()); + assertEventCount(events, 1); } }
