You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by ma...@apache.org on 2014/03/26 05:48:34 UTC
svn commit: r1581693 - in /logging/log4j/log4j2/trunk/log4j-core/src/test:
java/org/apache/logging/log4j/core/config/MissingRootLoggerTest.java
resources/README.md resources/missingRootLogger.xml
Author: mattsicker
Date: Wed Mar 26 04:48:33 2014
New Revision: 1581693
URL: http://svn.apache.org/r1581693
Log:
Update MissingRootLoggerTest to use test rule.
- Added documentation about writing unit tests.
- Added documentation for the MissingRootLoggerTest.
- Updated said test with some better assert messages.
Added:
logging/log4j/log4j2/trunk/log4j-core/src/test/resources/README.md (with props)
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/MissingRootLoggerTest.java
logging/log4j/log4j2/trunk/log4j-core/src/test/resources/missingRootLogger.xml
Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/MissingRootLoggerTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/MissingRootLoggerTest.java?rev=1581693&r1=1581692&r2=1581693&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/MissingRootLoggerTest.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/MissingRootLoggerTest.java Wed Mar 26 04:48:33 2014
@@ -16,31 +16,32 @@
*/
package org.apache.logging.log4j.core.config;
-import java.util.Map;
-
import org.apache.logging.log4j.Level;
-import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.LoggerContext;
-import org.apache.logging.log4j.core.config.plugins.PluginManager;
+import org.apache.logging.log4j.junit.InitialLoggerContext;
+import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
+import java.util.Map;
+
+import static org.junit.Assert.*;
@RunWith(JUnit4.class)
public class MissingRootLoggerTest {
+ @Rule
+ public InitialLoggerContext context = new InitialLoggerContext("missingRootLogger.xml");
@Test
public void testMissingRootLogger() throws Exception {
- PluginManager.addPackage("org.apache.logging.log4j.test.appender");
- final LoggerContext ctx = Configurator.initialize("Test1", "missingRootLogger.xml");
- final Logger logger = LogManager.getLogger("sample.Logger1");
+ final LoggerContext ctx = context.getContext();
+ final Logger logger = ctx.getLogger("sample.Logger1");
+ assertTrue("Logger should have the INFO level enabled", logger.isInfoEnabled());
+ assertFalse("Logger should have the DEBUG level disabled", logger.isDebugEnabled());
final Configuration config = ctx.getConfiguration();
assertNotNull("Config not null", config);
// final String MISSINGROOT = "MissingRootTest";
@@ -48,32 +49,29 @@ public class MissingRootLoggerTest {
// MISSINGROOT.equals(config.getName()));
final Map<String, Appender> map = config.getAppenders();
assertNotNull("Appenders not null", map);
- assertEquals("Appenders Size", 2, map.size());
+ assertEquals("There should only be two appenders", 2, map.size());
assertTrue("Contains List", map.containsKey("List"));
assertTrue("Contains Console", map.containsKey("Console"));
final Map<String, LoggerConfig> loggerMap = config.getLoggers();
assertNotNull("loggerMap not null", loggerMap);
- assertEquals("loggerMap Size", 1, loggerMap.size());
+ assertEquals("There should only be one configured logger", 1, loggerMap.size());
// only the sample logger, no root logger in loggerMap!
assertTrue("contains key=sample", loggerMap.containsKey("sample"));
final LoggerConfig sample = loggerMap.get("sample");
final Map<String, Appender> sampleAppenders = sample.getAppenders();
- assertEquals("sampleAppenders Size", 1, sampleAppenders.size());
+ assertEquals("The sample logger should only have one appender", 1, sampleAppenders.size());
// sample only has List appender, not Console!
- assertTrue("sample has appender List", sampleAppenders.containsKey("List"));
+ assertTrue("The sample appender should be a ListAppender", sampleAppenders.containsKey("List"));
final AbstractConfiguration baseConfig = (AbstractConfiguration) config;
final LoggerConfig root = baseConfig.getRootLogger();
final Map<String, Appender> rootAppenders = root.getAppenders();
- assertEquals("rootAppenders Size", 1, rootAppenders.size());
+ assertEquals("The root logger should only have one appender", 1, rootAppenders.size());
// root only has Console appender!
- assertTrue("root has appender Console", rootAppenders.containsKey("Console"));
+ assertTrue("The root appender should be a ConsoleAppender", rootAppenders.containsKey("Console"));
assertEquals(Level.ERROR, root.getLevel());
-
- logger.isDebugEnabled();
- Configurator.shutdown(ctx);
}
}
Added: logging/log4j/log4j2/trunk/log4j-core/src/test/resources/README.md
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/resources/README.md?rev=1581693&view=auto
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/resources/README.md (added)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/resources/README.md Wed Mar 26 04:48:33 2014
@@ -0,0 +1,56 @@
+This directory is mainly for storing configuration files used in various unit tests. As such, there are a few things
+provided for your convenience in writing and maintaining unit tests.
+
+Test Plugins
+------------
+
+Included are various Log4j plugins that are only useful for writing tests. To enable these plugins for your test,
+make sure to include `org.apache.logging.log4j.test` in the root `packages` attribute of the config file. For example:
+
+ <Configuration packages="org.apache.logging.log4j.test">
+ <Appenders>
+ <List name="List"/>
+ </Appenders>
+ <Loggers>
+ <Logger name="org.apache.logging.log4j" level="debug">
+ <AppenderRef ref="List"/>
+ </Logger>
+ </Loggers>
+ </Configuration>
+
+Note that if you don't specify a layout for a ListAppender, your log messages will be stored in a list of LogEvents.
+If you use a SerializedLayout, your log messages will be stored in a list of byte arrays. If you specify any other
+type of layout, your log messages will be stored in a list of strings. For more details, check out the class
+`org.apache.logging.log4j.test.appender.ListAppender`.
+
+Specifying Configuration Files in JUnit Tests
+---------------------------------------------
+
+Added in JUnit 4.9, the concept of test fixtures (i.e., the `@Before`, `@After`, etc. methods) has been expanded into
+the concept of test rules. A test rule is a reusable test fixture such as the `TemporaryFolder` JUnit rule for creating
+temporary directories and files on a test-by-test (or suite) basis. To use a test rule, you need to use the
+`@Rule` or `@ClassRule` annotation to get a method-level or class-level test fixture respectively. For instance,
+suppose your test class uses the file named `MyTestConfig.xml` in this directory. Then you can use the following rule
+in your test class:
+
+ @Rule
+ public InitialLoggerContext context = new InitialLoggerContext("MyTestConfig.xml");
+
+ @Test
+ public void testSomeAwesomeFeature() {
+ final LoggerContext ctx = context.getContext();
+ final Logger logger = ctx.getLogger("org.apache.logging.log4j.my.awesome.test.logger");
+ final Configuration cfg = ctx.getConfiguration();
+ final ListAppender app = (ListAppender) cfg.getAppenders().get("List");
+ logger.warn("Test message");
+ final List<LogEvent> events = app.getEvents();
+ // etc.
+ }
+
+Using this rule will automatically create a new LoggerContext using the specified configuration file for you to
+retrieve via the `getContext()` method shown above. After the method finishes (or if you use `@ClassRule` and make
+the field `static`), the `LoggerContext` is automatically stopped. No longer do you need to set any system properties,
+reset the `StatusLogger` configuration, and all that other fun boilerplate code.
+
+If you have any questions about writing unit tests, feel free to send an email to the dev mailing list, or check out
+the JUnit documentation over at junit.org.
\ No newline at end of file
Propchange: logging/log4j/log4j2/trunk/log4j-core/src/test/resources/README.md
------------------------------------------------------------------------------
svn:eol-style = native
Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/resources/missingRootLogger.xml
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/resources/missingRootLogger.xml?rev=1581693&r1=1581692&r2=1581693&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/resources/missingRootLogger.xml (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/resources/missingRootLogger.xml Wed Mar 26 04:48:33 2014
@@ -1,5 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
-<Configuration name="MissingRootTest" status="DEBUG">
+<!--
+ 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="MissingRootTest" status="DEBUG" packages="org.apache.logging.log4j.test">
<Appenders>
<List name="List">
<PatternLayout>
@@ -8,12 +25,24 @@
</List>
</Appenders>
+<!--
+ This test intentionally excludes a root logger configuration. This is to demonstrate that by default, the root
+ logger is configured as a Logger at the ERROR level with only a ConsoleAppender. Thus, the following would be
+ implicit in a configuration that doesn't otherwise specify the root logger configuration:
+
+ <Appenders>
+ <Console name="Console">
+ <PatternLayout pattern="{the default pattern}"/>
+ </Console>
+ </Appenders>
+
+ <Loggers>
+ <Root level="error">
+ <AppenderRef ref="Console"/>
+ </Root>
+ </Loggers>
+-->
<Loggers>
- <!--
- <Root level="info" includeLocation="false">
- <AppenderRef ref="List"/>
- </Root>
- -->
<Logger name="sample" level="info" includeLocation="false">
<AppenderRef ref="List"/>
</Logger>