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/27 03:16:37 UTC
svn commit: r1582152 -
/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/CustomConfigurationTest.java
Author: mattsicker
Date: Thu Mar 27 02:16:37 2014
New Revision: 1582152
URL: http://svn.apache.org/r1582152
Log:
Update test to use InitialLoggerContext rule.
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/CustomConfigurationTest.java
Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/CustomConfigurationTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/CustomConfigurationTest.java?rev=1582152&r1=1582151&r2=1582152&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/CustomConfigurationTest.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/config/CustomConfigurationTest.java Thu Mar 27 02:16:37 2014
@@ -18,7 +18,6 @@ package org.apache.logging.log4j.core.co
import java.io.File;
-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.Layout;
@@ -26,9 +25,9 @@ import org.apache.logging.log4j.core.Log
import org.apache.logging.log4j.core.appender.FileAppender;
import org.apache.logging.log4j.core.config.xml.XMLConfiguration;
import org.apache.logging.log4j.core.layout.PatternLayout;
-import org.apache.logging.log4j.status.StatusLogger;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
+import org.apache.logging.log4j.junit.InitialLoggerContext;
+import org.junit.Before;
+import org.junit.Rule;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
@@ -38,33 +37,26 @@ import static org.junit.Assert.assertTru
*/
public class CustomConfigurationTest {
- private static final String CONFIG = "log4j-props.xml";
+ public static final String LOG_FILE = "target/test.log";
- @BeforeClass
- public static void setupClass() {
- }
+ @Rule
+ public InitialLoggerContext init = new InitialLoggerContext("log4j-props.xml");
- @AfterClass
- public static void cleanupClass() {
- System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
- final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
- ctx.reconfigure();
- StatusLogger.getLogger().reset();
+ @Before
+ public void setUp() throws Exception {
+ new File(LOG_FILE).delete();
}
@Test
public void testConfig() {
- File file = new File("target/test.log");
- file.delete();
System.setProperty("log4j.level", "error");
- System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
- final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
+ final LoggerContext ctx = this.init.getContext();
ctx.reconfigure();
final Configuration config = ctx.getConfiguration();
assertTrue("Configuration is not an XMLConfiguration", config instanceof XMLConfiguration);
Layout layout = PatternLayout.createLayout(PatternLayout.SIMPLE_CONVERSION_PATTERN, config, null,
null,null, null, null, null);
- Appender appender = FileAppender.createAppender("target/test.log", "false", "false", "File", "true",
+ Appender appender = FileAppender.createAppender(LOG_FILE, "false", "false", "File", "true",
"false", "false", "4000", layout, null, "false", null, config);
appender.start();
config.addAppender(appender);
@@ -76,9 +68,9 @@ public class CustomConfigurationTest {
loggerConfig.addAppender(appender, null, null);
config.addLogger("org.apache.logging.log4j", loggerConfig);
ctx.updateLoggers();
- Logger logger = LogManager.getLogger("org.apache.logging.log4j.core.config.CustomConfigurationTest");
+ Logger logger = ctx.getLogger("org.apache.logging.log4j.core.config.CustomConfigurationTest");
logger.info("This is a test");
- file = new File("target/test.log");
+ final File file = new File(LOG_FILE);
assertTrue("log file not created", file.exists());
assertTrue("No data logged", file.length() > 0);