Add unit test for JUL adaptor.
Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/18015e93 Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/18015e93 Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/18015e93 Branch: refs/heads/LOG4J2-608 Commit: 18015e93490de6102430904b2d5274052685996f Parents: fd65d6b Author: Matt Sicker <[email protected]> Authored: Mon Sep 1 20:13:53 2014 -0500 Committer: Matt Sicker <[email protected]> Committed: Mon Sep 1 20:13:53 2014 -0500 ---------------------------------------------------------------------- .../apache/logging/log4j/jdk/LoggerTest.java | 101 +++++++++++++++++++ log4j-jdk/src/test/resources/log4j2-test.xml | 39 +++++++ 2 files changed, 140 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/18015e93/log4j-jdk/src/test/java/org/apache/logging/log4j/jdk/LoggerTest.java ---------------------------------------------------------------------- diff --git a/log4j-jdk/src/test/java/org/apache/logging/log4j/jdk/LoggerTest.java b/log4j-jdk/src/test/java/org/apache/logging/log4j/jdk/LoggerTest.java new file mode 100644 index 0000000..007e3a6 --- /dev/null +++ b/log4j-jdk/src/test/java/org/apache/logging/log4j/jdk/LoggerTest.java @@ -0,0 +1,101 @@ +package org.apache.logging.log4j.jdk; + +import java.util.List; +import java.util.logging.Logger; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.core.LogEvent; +import org.apache.logging.log4j.core.impl.Log4jLogEvent; +import org.apache.logging.log4j.test.appender.ListAppender; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.instanceOf; +import static org.junit.Assert.*; + +public class LoggerTest { + + public static final String LOGGER_NAME = "Test"; + private Logger logger; + private ListAppender eventAppender; + private ListAppender stringAppender; + + @BeforeClass + public static void setUpClass() { + System.setProperty("java.util.logging.manager", LogManager.class.getName()); + } + + @Before + public void setUp() throws Exception { + logger = Logger.getLogger(LOGGER_NAME); + assertThat(logger.getLevel(), equalTo(java.util.logging.Level.FINE)); + eventAppender = ListAppender.getListAppender("TestAppender"); + stringAppender = ListAppender.getListAppender("StringAppender"); + } + + @After + public void tearDown() throws Exception { + eventAppender.clear(); + } + + @Test + public void testLog() throws Exception { + logger.info("Informative message here."); + final List<LogEvent> events = eventAppender.getEvents(); + assertThat(events, hasSize(1)); + final LogEvent event = events.get(0); + assertThat(event, instanceOf(Log4jLogEvent.class)); + assertEquals(Level.INFO, event.getLevel()); + assertEquals(LOGGER_NAME, event.getLoggerName()); + assertEquals("Informative message here.", event.getMessage().getFormattedMessage()); + assertEquals(Logger.class.getName(), event.getLoggerFqcn()); + } + + @Test + public void testLogWithCallingClass() throws Exception { + final Logger log = Logger.getLogger("Test.CallerClass"); + log.config("Calling from LoggerTest"); + final List<String> messages = stringAppender.getMessages(); + assertThat(messages, hasSize(1)); + final String message = messages.get(0); + assertEquals(getClass().getName(), message); + } + + @Test + public void testLogUsingCustomLevel() throws Exception { + logger.log(CustomJdkLevel.TEST, "Test level"); + final List<LogEvent> events = eventAppender.getEvents(); + assertThat(events, hasSize(1)); + final LogEvent event = events.get(0); + assertThat(event.getLevel(), equalTo(Level.INFO)); + final String levelName = event.getContextMap().get(org.apache.logging.log4j.jdk.Logger.LEVEL); + assertThat(levelName, equalTo(CustomJdkLevel.TEST.getName())); + } + + @Test + public void testSetLevel() throws Exception { + logger.setLevel(java.util.logging.Level.SEVERE); + assertThat(logger.getLevel(), equalTo(java.util.logging.Level.SEVERE)); + } + + @Test + public void testIsLoggable() throws Exception { + assertThat(logger.isLoggable(java.util.logging.Level.SEVERE), equalTo(true)); + assertThat(logger.isLoggable(CustomJdkLevel.DEFCON_1), equalTo(true)); + } + + @Test + public void testGetName() throws Exception { + assertThat(logger.getName(), equalTo(LOGGER_NAME)); + } + + @Test + public void testGlobalLoggerName() throws Exception { + final Logger root = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); + assertThat(root.getName(), equalTo(Logger.GLOBAL_LOGGER_NAME)); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/18015e93/log4j-jdk/src/test/resources/log4j2-test.xml ---------------------------------------------------------------------- diff --git a/log4j-jdk/src/test/resources/log4j2-test.xml b/log4j-jdk/src/test/resources/log4j2-test.xml new file mode 100644 index 0000000..af6a050 --- /dev/null +++ b/log4j-jdk/src/test/resources/log4j2-test.xml @@ -0,0 +1,39 @@ +<!-- + ~ Licensed to the Apache Software Foundation (ASF) under one or more + ~ contributor license agreements. See the NOTICE file distributed with + ~ this work for additional information regarding copyright ownership. + ~ The ASF licenses this file to You under the Apache License, Version 2.0 + ~ (the "License"); you may not use this file except in compliance with + ~ the License. You may obtain a copy of the License at + ~ + ~ http://www.apache.org/licenses/LICENSE-2.0 + ~ + ~ Unless required by applicable law or agreed to in writing, software + ~ distributed under the License is distributed on an "AS IS" BASIS, + ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + ~ See the License for the specific language governing permissions and + ~ limitations under the License. + --> + +<Configuration name="LoggerTest" status="DEBUG"> + <Appenders> + <List name="TestAppender"/> + <List name="StringAppender"> + <PatternLayout pattern="%class"/> + </List> + <Console name="Console" target="SYSTEM_ERR"> + <BasicLayout/> + </Console> + </Appenders> + <Loggers> + <Logger name="Test" level="DEBUG" additivity="false"> + <AppenderRef ref="TestAppender"/> + </Logger> + <Logger name="Test.CallerClass" level="DEBUG" additivity="false"> + <AppenderRef ref="StringAppender"/> + </Logger> + <Root level="ERROR"> + <AppenderRef ref="Console"/> + </Root> + </Loggers> +</Configuration>
