http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c02b66be/log4j-jul/src/test/java/org/apache/logging/log4j/jdk/CoreLoggerTest.java ---------------------------------------------------------------------- diff --git a/log4j-jul/src/test/java/org/apache/logging/log4j/jdk/CoreLoggerTest.java b/log4j-jul/src/test/java/org/apache/logging/log4j/jdk/CoreLoggerTest.java new file mode 100644 index 0000000..16d5831 --- /dev/null +++ b/log4j-jul/src/test/java/org/apache/logging/log4j/jdk/CoreLoggerTest.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 CoreLoggerTest { + + 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(ApiLogger.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/c02b66be/log4j-jul/src/test/java/org/apache/logging/log4j/jdk/CustomJdkLevel.java ---------------------------------------------------------------------- diff --git a/log4j-jul/src/test/java/org/apache/logging/log4j/jdk/CustomJdkLevel.java b/log4j-jul/src/test/java/org/apache/logging/log4j/jdk/CustomJdkLevel.java new file mode 100644 index 0000000..d125d0e --- /dev/null +++ b/log4j-jul/src/test/java/org/apache/logging/log4j/jdk/CustomJdkLevel.java @@ -0,0 +1,40 @@ +/* + * 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. + */ +package org.apache.logging.log4j.jdk; + +import java.util.logging.Level; + +/** + * Custom JUL Level for unit tests. + */ +public class CustomJdkLevel extends Level { + + private static final long serialVersionUID = 4681718777617726164L; + + protected CustomJdkLevel(final String name, final int value) { + super(name, value); + } + + // inside CONFIG range; should map to INFO + public static final Level TEST = new CustomJdkLevel("TEST", 600); + + // just 1 below Level.SEVERE; should map to ERROR + public static final Level DEFCON_2 = new CustomJdkLevel("DEFCON_2", 999); + + // above Level.SEVERE; should map to FATAL + public static final Level DEFCON_1 = new CustomJdkLevel("DEFCON_1", 10000); +} http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c02b66be/log4j-jul/src/test/java/org/apache/logging/log4j/jdk/LevelsTest.java ---------------------------------------------------------------------- diff --git a/log4j-jul/src/test/java/org/apache/logging/log4j/jdk/LevelsTest.java b/log4j-jul/src/test/java/org/apache/logging/log4j/jdk/LevelsTest.java new file mode 100644 index 0000000..b4a6056 --- /dev/null +++ b/log4j-jul/src/test/java/org/apache/logging/log4j/jdk/LevelsTest.java @@ -0,0 +1,49 @@ +package org.apache.logging.log4j.jdk; + +import java.util.Arrays; +import java.util.Collection; + +import org.apache.logging.log4j.Level; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.Parameterized; + +import static org.junit.Assert.*; + +@RunWith(Parameterized.class) +public class LevelsTest { + + private final java.util.logging.Level level; + private final Level expectedLevel; + + public LevelsTest(final java.util.logging.Level level, final Level expectedLevel) { + this.level = level; + this.expectedLevel = expectedLevel; + } + + @Parameterized.Parameters + public static Collection<Object[]> data() { + return Arrays.asList( + new Object[][]{ + {CustomJdkLevel.TEST, Level.INFO}, + {CustomJdkLevel.DEFCON_2, Level.ERROR}, + {CustomJdkLevel.DEFCON_1, Level.FATAL}, + {java.util.logging.Level.OFF, Level.OFF}, + {java.util.logging.Level.ALL, Level.ALL}, + {java.util.logging.Level.SEVERE, Level.ERROR}, + {java.util.logging.Level.WARNING, Level.WARN}, + {java.util.logging.Level.INFO, Level.INFO}, + {java.util.logging.Level.CONFIG, Level.INFO}, + {java.util.logging.Level.FINE, Level.DEBUG}, + {java.util.logging.Level.FINER, Level.DEBUG}, + {java.util.logging.Level.FINEST, Level.TRACE} + } + ); + } + + @Test + public void testToLevel() throws Exception { + final Level actualLevel = Levels.toLevel(level); + assertEquals(expectedLevel, actualLevel); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/c02b66be/log4j-jul/src/test/resources/log4j2-test.xml ---------------------------------------------------------------------- diff --git a/log4j-jul/src/test/resources/log4j2-test.xml b/log4j-jul/src/test/resources/log4j2-test.xml new file mode 100644 index 0000000..af6a050 --- /dev/null +++ b/log4j-jul/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>