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/05/21 06:58:02 UTC
svn commit: r1596458 -
/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/
Author: mattsicker
Date: Wed May 21 04:58:01 2014
New Revision: 1596458
URL: http://svn.apache.org/r1596458
Log:
Refactor tests to use InitialLoggerContext and other assertion clean-ups.
Modified:
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/LogEventFactoryTest.java
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggerDateTest.java
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggerTest.java
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggerUpdateTest.java
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/ShutdownDisabledTest.java
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/StrictXMLConfigTest.java
logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/XMLEvents.java
Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/LogEventFactoryTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/LogEventFactoryTest.java?rev=1596458&r1=1596457&r2=1596458&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/LogEventFactoryTest.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/LogEventFactoryTest.java Wed May 21 04:58:01 2014
@@ -17,24 +17,23 @@
package org.apache.logging.log4j.core;
import java.util.List;
-import java.util.Map;
import org.apache.logging.log4j.Level;
-import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Marker;
-import org.apache.logging.log4j.core.config.Configuration;
-import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.config.Property;
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.core.impl.LogEventFactory;
import org.apache.logging.log4j.core.util.Constants;
+import org.apache.logging.log4j.junit.InitialLoggerContext;
import org.apache.logging.log4j.message.Message;
-import org.apache.logging.log4j.status.StatusLogger;
import org.apache.logging.log4j.test.appender.ListAppender;
-import org.junit.AfterClass;
import org.junit.Before;
-import org.junit.BeforeClass;
+import org.junit.ClassRule;
import org.junit.Test;
+import org.junit.rules.RuleChain;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
import static org.junit.Assert.*;
@@ -44,48 +43,43 @@ import static org.junit.Assert.*;
public class LogEventFactoryTest {
private static final String CONFIG = "log4j2-config.xml";
- private static Configuration config;
- private static ListAppender app;
- private static LoggerContext ctx;
-
- @BeforeClass
- public static void setupClass() {
- System.setProperty(Constants.LOG4J_LOG_EVENT_FACTORY, TestLogEventFactory.class.getName());
- System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
- ctx = (LoggerContext) LogManager.getContext(false);
- config = ctx.getConfiguration();
- for (final Map.Entry<String, Appender> entry : config.getAppenders().entrySet()) {
- if (entry.getKey().equals("List")) {
- app = (ListAppender) entry.getValue();
- break;
- }
- }
- if (app == null) {
- fail("No List Appender could be found");
- }
- }
+ private static final InitialLoggerContext context = new InitialLoggerContext(CONFIG);
- @AfterClass
- public static void cleanupClass() {
- System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
- ctx.reconfigure();
- StatusLogger.getLogger().reset();
- }
+ private ListAppender app;
+
+ // this would look so cool using lambdas
+ @ClassRule
+ public static RuleChain chain = RuleChain.outerRule(new TestRule() {
+ @Override
+ public Statement apply(final Statement base, final Description description) {
+ return new Statement() {
+ @Override
+ public void evaluate() throws Throwable {
+ System.setProperty(Constants.LOG4J_LOG_EVENT_FACTORY, TestLogEventFactory.class.getName());
+ try {
+ base.evaluate();
+ } finally {
+ System.clearProperty(Constants.LOG4J_LOG_EVENT_FACTORY);
+ }
+ }
+ };
+ }
+ }).around(context);
@Before
public void before() {
- app.clear();
+ app = context.getListAppender("List").clear();
}
@Test
public void testEvent() {
- final org.apache.logging.log4j.Logger logger = LogManager.getLogger("org.apache.test.LogEventFactory");
+ final org.apache.logging.log4j.Logger logger = context.getLogger("org.apache.test.LogEventFactory");
logger.error("error message");
final List<LogEvent> events = app.getEvents();
assertNotNull("No events", events);
- assertTrue("Incorrect number of events. Expected 1, actual " + events.size(), events.size() == 1);
+ assertEquals("Incorrect number of events. Expected 1, actual " + events.size(), 1, events.size());
final LogEvent event = events.get(0);
- assertTrue("Test LogEventFactory wasn't used", event.getLoggerName().equals("Test"));
+ assertEquals("Test LogEventFactory wasn't used", "Test", event.getLoggerName());
}
public static class TestLogEventFactory implements LogEventFactory {
Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggerDateTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggerDateTest.java?rev=1596458&r1=1596457&r2=1596458&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggerDateTest.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggerDateTest.java Wed May 21 04:58:01 2014
@@ -17,16 +17,11 @@
package org.apache.logging.log4j.core;
import java.util.Calendar;
-import java.util.Map;
-import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.appender.FileAppender;
-import org.apache.logging.log4j.core.config.Configuration;
-import org.apache.logging.log4j.core.config.ConfigurationFactory;
-import org.apache.logging.log4j.status.StatusLogger;
-import org.junit.AfterClass;
+import org.apache.logging.log4j.junit.InitialLoggerContext;
import org.junit.Before;
-import org.junit.BeforeClass;
+import org.junit.ClassRule;
import org.junit.Test;
import static org.junit.Assert.*;
@@ -37,32 +32,14 @@ import static org.junit.Assert.*;
public class LoggerDateTest {
private static final String CONFIG = "log4j-date.xml";
- private static Configuration config;
- private static FileAppender fileApp;
- private static LoggerContext ctx;
-
- @BeforeClass
- public static void setupClass() {
- System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
- ctx = (LoggerContext) LogManager.getContext(false);
- }
+ private FileAppender fileApp;
- @AfterClass
- public static void cleanupClass() {
- System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
- ctx.reconfigure();
- StatusLogger.getLogger().reset();
- }
+ @ClassRule
+ public static InitialLoggerContext context = new InitialLoggerContext(CONFIG);
@Before
public void before() {
- config = ctx.getConfiguration();
- for (final Map.Entry<String, Appender> entry : config.getAppenders().entrySet()) {
- if (entry.getKey().equals("File")) {
- fileApp = (FileAppender) entry.getValue();
- }
- }
- assertNotNull("No FileAppender", fileApp);
+ fileApp = (FileAppender) context.getRequiredAppender("File");
}
Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggerTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggerTest.java?rev=1596458&r1=1596457&r2=1596458&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggerTest.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggerTest.java Wed May 21 04:58:01 2014
@@ -20,22 +20,19 @@ import java.io.File;
import java.util.Date;
import java.util.List;
import java.util.Locale;
-import java.util.Map;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.MarkerManager;
import org.apache.logging.log4j.ThreadContext;
import org.apache.logging.log4j.core.config.Configuration;
-import org.apache.logging.log4j.core.config.ConfigurationFactory;
+import org.apache.logging.log4j.junit.InitialLoggerContext;
import org.apache.logging.log4j.message.MessageFactory;
import org.apache.logging.log4j.message.ParameterizedMessageFactory;
import org.apache.logging.log4j.message.StringFormatterMessageFactory;
import org.apache.logging.log4j.message.StructuredDataMessage;
-import org.apache.logging.log4j.status.StatusLogger;
import org.apache.logging.log4j.test.appender.ListAppender;
-import org.junit.AfterClass;
import org.junit.Before;
-import org.junit.BeforeClass;
+import org.junit.ClassRule;
import org.junit.Test;
import static org.junit.Assert.*;
@@ -46,53 +43,29 @@ import static org.junit.Assert.*;
public class LoggerTest {
private static final String CONFIG = "log4j-test2.xml";
- private static Configuration config;
- private static ListAppender app;
- private static ListAppender host;
- private static ListAppender noThrown;
- private static LoggerContext ctx;
-
- @BeforeClass
- public static void setupClass() {
- System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
- ctx = (LoggerContext) LogManager.getContext(false);
- }
-
- @AfterClass
- public static void cleanupClass() {
- System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
- ctx.reconfigure();
- StatusLogger.getLogger().reset();
- }
+ private ListAppender app;
+ private ListAppender host;
+ private ListAppender noThrown;
+
+ @ClassRule
+ public static InitialLoggerContext context = new InitialLoggerContext(CONFIG);
@Before
public void before() {
- config = ctx.getConfiguration();
- for (final Map.Entry<String, Appender> entry : config.getAppenders().entrySet()) {
- if (entry.getKey().equals("List")) {
- app = (ListAppender) entry.getValue();
- } else if (entry.getKey().equals("HostTest")) {
- host = (ListAppender) entry.getValue();
- } else if (entry.getKey().equals("NoThrowable")) {
- noThrown = (ListAppender) entry.getValue();
- }
- }
- assertNotNull("No Appender", app);
- assertNotNull("No Host Appender", host);
- app.clear();
- host.clear();
+ app = context.getListAppender("List").clear();
+ host = context.getListAppender("HostTest").clear();
+ noThrown = context.getListAppender("NoThrowable").clear();
}
- org.apache.logging.log4j.Logger logger = LogManager.getLogger("LoggerTest");
+ org.apache.logging.log4j.Logger logger = context.getLogger("LoggerTest");
@Test
public void basicFlow() {
logger.entry();
logger.exit();
final List<LogEvent> events = app.getEvents();
- assertTrue("Incorrect number of events. Expected 2, actual " + events.size(), events.size() == 2);
- app.clear();
+ assertEquals("Incorrect number of events. Expected 2, actual " + events.size(), 2, events.size());
}
@Test
@@ -100,16 +73,14 @@ public class LoggerTest {
logger.entry(CONFIG);
logger.exit(0);
final List<LogEvent> events = app.getEvents();
- assertTrue("Incorrect number of events. Expected 2, actual " + events.size(), events.size() == 2);
- app.clear();
+ assertEquals("Incorrect number of events. Expected 2, actual " + events.size(), 2, events.size());
}
@Test
public void throwing() {
logger.throwing(new IllegalArgumentException("Test Exception"));
final List<LogEvent> events = app.getEvents();
- assertTrue("Incorrect number of events. Expected 1, actual " + events.size(), events.size() == 1);
- app.clear();
+ assertEquals("Incorrect number of events. Expected 1, actual " + events.size(), 1, events.size());
}
@Test
@@ -120,16 +91,14 @@ public class LoggerTest {
logger.catching(e);
}
final List<LogEvent> events = app.getEvents();
- assertTrue("Incorrect number of events. Expected 1, actual " + events.size(), events.size() == 1);
- app.clear();
+ assertEquals("Incorrect number of events. Expected 1, actual " + events.size(), 1, events.size());
}
@Test
public void debug() {
logger.debug("Debug message");
final List<LogEvent> events = app.getEvents();
- assertTrue("Incorrect number of events. Expected 1, actual " + events.size(), events.size() == 1);
- app.clear();
+ assertEquals("Incorrect number of events. Expected 1, actual " + events.size(), 1, events.size());
}
@Test
@@ -138,7 +107,7 @@ public class LoggerTest {
StringFormatterMessageFactory.INSTANCE, ParameterizedMessageFactory.INSTANCE);
testLogger.debug("%,d", Integer.MAX_VALUE);
final List<LogEvent> events = app.getEvents();
- assertTrue("Incorrect number of events. Expected 1, actual " + events.size(), events.size() == 1);
+ assertEquals("Incorrect number of events. Expected 1, actual " + events.size(), 1, events.size());
assertEquals(String.format("%,d", Integer.MAX_VALUE), events.get(0).getMessage().getFormattedMessage());
}
@@ -147,11 +116,13 @@ public class LoggerTest {
final Logger testLogger = testMessageFactoryMismatch("getLogger_String_MessageFactoryMismatchNull", StringFormatterMessageFactory.INSTANCE, null);
testLogger.debug("%,d", Integer.MAX_VALUE);
final List<LogEvent> events = app.getEvents();
- assertTrue("Incorrect number of events. Expected 1, actual " + events.size(), events.size() == 1);
+ assertEquals("Incorrect number of events. Expected 1, actual " + events.size(), 1, events.size());
assertEquals(String.format("%,d", Integer.MAX_VALUE), events.get(0).getMessage().getFormattedMessage());
}
- private Logger testMessageFactoryMismatch(final String name, final MessageFactory messageFactory1, final MessageFactory messageFactory2) {
+ private static Logger testMessageFactoryMismatch(final String name,
+ final MessageFactory messageFactory1,
+ final MessageFactory messageFactory2) {
final Logger testLogger = (Logger) LogManager.getLogger(name, messageFactory1);
assertNotNull(testLogger);
assertEquals(messageFactory1, testLogger.getMessageFactory());
@@ -164,24 +135,22 @@ public class LoggerTest {
public void debugObject() {
logger.debug(new Date());
final List<LogEvent> events = app.getEvents();
- assertTrue("Incorrect number of events. Expected 1, actual " + events.size(), events.size() == 1);
- app.clear();
+ assertEquals("Incorrect number of events. Expected 1, actual " + events.size(), 1, events.size());
}
@Test
public void debugWithParms() {
logger.debug("Hello, {}", "World");
final List<LogEvent> events = app.getEvents();
- assertTrue("Incorrect number of events. Expected 1, actual " + events.size(), events.size() == 1);
- app.clear();
+ assertEquals("Incorrect number of events. Expected 1, actual " + events.size(), 1, events.size());
}
@Test
public void testImpliedThrowable() {
- final org.apache.logging.log4j.Logger testLogger = LogManager.getLogger("org.apache.logging.log4j.hosttest");
+ 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();
- assertTrue("Incorrect number of messages. Expected 1, actual " + msgs.size(), msgs.size() == 1);
+ assertEquals("Incorrect number of messages. Expected 1, actual " + msgs.size(), 1, msgs.size());
final String expected = "java.lang.Throwable: Testing";
assertTrue("Incorrect message data", msgs.get(0).contains(expected));
}
@@ -189,10 +158,10 @@ public class LoggerTest {
@Test
public void testSuppressedThrowable() {
- final org.apache.logging.log4j.Logger testLogger = LogManager.getLogger("org.apache.logging.log4j.nothrown");
+ 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();
- assertTrue("Incorrect number of messages. Expected 1, actual " + msgs.size(), msgs.size() == 1);
+ assertEquals("Incorrect number of messages. Expected 1, actual " + msgs.size(), 1, msgs.size());
final String suppressed = "java.lang.Throwable: Testing";
assertTrue("Incorrect message data", !msgs.get(0).contains(suppressed));
}
@@ -205,8 +174,7 @@ public class LoggerTest {
ThreadContext.clearMap();
logger.debug("Debug message");
final List<LogEvent> events = app.getEvents();
- assertTrue("Incorrect number of events. Expected 2, actual " + events.size(), events.size() == 2);
- app.clear();
+ assertEquals("Incorrect number of events. Expected 2, actual " + events.size(), 2, events.size());
}
@Test
@@ -221,12 +189,12 @@ public class LoggerTest {
logger.info(MarkerManager.getMarker("EVENT"), msg);
ThreadContext.clearMap();
final List<LogEvent> events = app.getEvents();
- assertTrue("Incorrect number of events. Expected 1, actual " + events.size(), events.size() == 1);
- app.clear();
+ assertEquals("Incorrect number of events. Expected 1, actual " + events.size(), 1, events.size());
}
@Test
public void testReconfiguration() throws Exception {
+ final Configuration oldConfig = context.getConfiguration();
final int MONITOR_INTERVAL_SECONDS = 1;
final File file = new File("target/test-classes/" + CONFIG);
final long orig = file.lastModified();
@@ -237,17 +205,17 @@ public class LoggerTest {
logger.debug("Reconfigure");
}
Thread.sleep(100);
- final Configuration cfg = ctx.getConfiguration();
- assertNotNull("No configuration", cfg);
- assertTrue("Reconfiguration failed", cfg != config);
+ final Configuration newConfig = context.getConfiguration();
+ assertNotNull("No configuration", newConfig);
+ assertNotSame("Reconfiguration failed", newConfig, oldConfig);
}
@Test
public void testAdditivity() throws Exception {
- final Logger localLogger = (Logger) LogManager.getLogger("org.apache.test");
+ final Logger localLogger = context.getLogger("org.apache.test");
localLogger.error("Test parent additivity");
final List<LogEvent> events = app.getEvents();
- assertTrue("Incorrect number of events. Expected 1, actual " + events.size(), events.size() == 1);
+ assertEquals("Incorrect number of events. Expected 1, actual " + events.size(), 1, events.size());
}
}
Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggerUpdateTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggerUpdateTest.java?rev=1596458&r1=1596457&r2=1596458&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggerUpdateTest.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/LoggerUpdateTest.java Wed May 21 04:58:01 2014
@@ -17,18 +17,14 @@
package org.apache.logging.log4j.core;
import java.util.List;
-import java.util.Map;
-import org.apache.logging.log4j.Level;
-import org.apache.logging.log4j.LogManager;
+import org.apache.logging.log4j.*;
import org.apache.logging.log4j.core.config.Configuration;
-import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.core.config.LoggerConfig;
-import org.apache.logging.log4j.status.StatusLogger;
+import org.apache.logging.log4j.junit.InitialLoggerContext;
import org.apache.logging.log4j.test.appender.ListAppender;
-import org.junit.AfterClass;
import org.junit.Before;
-import org.junit.BeforeClass;
+import org.junit.ClassRule;
import org.junit.Test;
import static org.junit.Assert.*;
@@ -39,51 +35,22 @@ import static org.junit.Assert.*;
public class LoggerUpdateTest {
private static final String CONFIG = "log4j-test2.xml";
- private static Configuration config;
- private static ListAppender app;
- private static ListAppender host;
- private static ListAppender noThrown;
- private static LoggerContext ctx;
-
- @BeforeClass
- public static void setupClass() {
- System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
- ctx = (LoggerContext) LogManager.getContext(false);
- }
+ private ListAppender app;
- @AfterClass
- public static void cleanupClass() {
- System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
- ctx.reconfigure();
- StatusLogger.getLogger().reset();
- }
+ @ClassRule
+ public static InitialLoggerContext context = new InitialLoggerContext(CONFIG);
@Before
public void before() {
- config = ctx.getConfiguration();
- for (final Map.Entry<String, Appender> entry : config.getAppenders().entrySet()) {
- if (entry.getKey().equals("List")) {
- app = (ListAppender) entry.getValue();
- } else if (entry.getKey().equals("HostTest")) {
- host = (ListAppender) entry.getValue();
- } else if (entry.getKey().equals("NoThrowable")) {
- noThrown = (ListAppender) entry.getValue();
- }
- }
- assertNotNull("No Appender", app);
- assertNotNull("No Host Appender", host);
- app.clear();
- host.clear();
+ app = context.getListAppender("List").clear();
}
-
- org.apache.logging.log4j.Logger logger = LogManager.getLogger("com.apache.test");
-
@Test
public void resetLevel() {
+ final org.apache.logging.log4j.Logger logger = context.getLogger("com.apache.test");
logger.entry();
List<LogEvent> events = app.getEvents();
- assertTrue("Incorrect number of events. Expected 1, actual " + events.size(), events.size() == 1);
+ assertEquals("Incorrect number of events. Expected 1, actual " + events.size(), 1, events.size());
app.clear();
final LoggerContext ctx = (LoggerContext) LogManager.getContext(false);
final Configuration config = ctx.getConfiguration();
@@ -95,8 +62,7 @@ public class LoggerUpdateTest {
ctx.updateLoggers(); // This causes all Loggers to refetch information from their LoggerConfig.
logger.entry();
events = app.getEvents();
- assertTrue("Incorrect number of events. Expected 0, actual " + events.size(), events.size() == 0);
- app.clear();
+ assertEquals("Incorrect number of events. Expected 0, actual " + events.size(), 0, events.size());
}
}
Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/ShutdownDisabledTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/ShutdownDisabledTest.java?rev=1596458&r1=1596457&r2=1596458&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/ShutdownDisabledTest.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/ShutdownDisabledTest.java Wed May 21 04:58:01 2014
@@ -16,14 +16,9 @@
*/
package org.apache.logging.log4j.core;
-import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.config.Configuration;
-import org.apache.logging.log4j.core.config.ConfigurationFactory;
-import org.apache.logging.log4j.status.StatusLogger;
-import org.apache.logging.log4j.test.appender.ListAppender;
-import org.junit.AfterClass;
-import org.junit.Before;
-import org.junit.BeforeClass;
+import org.apache.logging.log4j.junit.InitialLoggerContext;
+import org.junit.ClassRule;
import org.junit.Test;
import static org.junit.Assert.*;
@@ -34,30 +29,13 @@ import static org.junit.Assert.*;
public class ShutdownDisabledTest {
private static final String CONFIG = "log4j-test3.xml";
- private static Configuration config;
- private static ListAppender app;
- private static LoggerContext ctx;
-
- @BeforeClass
- public static void setupClass() {
- System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
- ctx = (LoggerContext) LogManager.getContext(false);
- }
-
- @AfterClass
- public static void cleanupClass() {
- System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
- ctx.reconfigure();
- StatusLogger.getLogger().reset();
- }
- @Before
- public void before() {
- config = ctx.getConfiguration();
- }
+ @ClassRule
+ public static InitialLoggerContext context = new InitialLoggerContext(CONFIG);
@Test
public void testShutdownFlag() {
+ final Configuration config = context.getConfiguration();
assertNotNull("No configuration", config);
assertFalse("Shutdown hook is enabled", config.isShutdownHookEnabled());
}
Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/StrictXMLConfigTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/StrictXMLConfigTest.java?rev=1596458&r1=1596457&r2=1596458&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/StrictXMLConfigTest.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/StrictXMLConfigTest.java Wed May 21 04:58:01 2014
@@ -19,18 +19,14 @@ package org.apache.logging.log4j.core;
import java.util.Date;
import java.util.List;
import java.util.Locale;
-import java.util.Map;
-import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.MarkerManager;
import org.apache.logging.log4j.ThreadContext;
-import org.apache.logging.log4j.core.config.Configuration;
-import org.apache.logging.log4j.core.config.ConfigurationFactory;
+import org.apache.logging.log4j.junit.InitialLoggerContext;
import org.apache.logging.log4j.message.StructuredDataMessage;
-import org.apache.logging.log4j.status.StatusLogger;
import org.apache.logging.log4j.test.appender.ListAppender;
-import org.junit.AfterClass;
-import org.junit.BeforeClass;
+import org.junit.Before;
+import org.junit.ClassRule;
import org.junit.Test;
import static org.junit.Assert.*;
@@ -41,40 +37,24 @@ import static org.junit.Assert.*;
public class StrictXMLConfigTest {
private static final String CONFIG = "log4j-strict1.xml";
- private static Configuration config;
- private static ListAppender app;
- private static LoggerContext ctx;
-
- @BeforeClass
- public static void setupClass() {
- System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
- ctx = (LoggerContext) LogManager.getContext(false);
- config = ctx.getConfiguration();
- for (final Map.Entry<String, Appender> entry : config.getAppenders().entrySet()) {
- if (entry.getKey().equals("List")) {
- app = (ListAppender) entry.getValue();
- assertNotNull(app);
- break;
- }
- }
- }
+ private ListAppender app;
+
+ @ClassRule
+ public static InitialLoggerContext context = new InitialLoggerContext(CONFIG);
- @AfterClass
- public static void cleanupClass() {
- System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
- ctx.reconfigure();
- StatusLogger.getLogger().reset();
+ @Before
+ public void setUp() throws Exception {
+ app = context.getListAppender("List").clear();
}
- org.apache.logging.log4j.Logger logger = LogManager.getLogger("LoggerTest");
+ org.apache.logging.log4j.Logger logger = context.getLogger("LoggerTest");
@Test
public void basicFlow() {
logger.entry();
logger.exit();
final List<LogEvent> events = app.getEvents();
- assertTrue("Incorrect number of events. Expected 2, actual " + events.size(), events.size() == 2);
- app.clear();
+ assertEquals("Incorrect number of events. Expected 2, actual " + events.size(), 2, events.size());
}
@Test
@@ -82,16 +62,14 @@ public class StrictXMLConfigTest {
logger.entry(CONFIG);
logger.exit(0);
final List<LogEvent> events = app.getEvents();
- assertTrue("Incorrect number of events. Expected 2, actual " + events.size(), events.size() == 2);
- app.clear();
+ assertEquals("Incorrect number of events. Expected 2, actual " + events.size(), 2, events.size());
}
@Test
public void throwing() {
logger.throwing(new IllegalArgumentException("Test Exception"));
final List<LogEvent> events = app.getEvents();
- assertTrue("Incorrect number of events. Expected 1, actual " + events.size(), events.size() == 1);
- app.clear();
+ assertEquals("Incorrect number of events. Expected 1, actual " + events.size(), 1, events.size());
}
@Test
@@ -102,32 +80,28 @@ public class StrictXMLConfigTest {
logger.catching(e);
}
final List<LogEvent> events = app.getEvents();
- assertTrue("Incorrect number of events. Expected 1, actual " + events.size(), events.size() == 1);
- app.clear();
+ assertEquals("Incorrect number of events. Expected 1, actual " + events.size(), 1, events.size());
}
@Test
public void debug() {
logger.debug("Debug message");
final List<LogEvent> events = app.getEvents();
- assertTrue("Incorrect number of events. Expected 1, actual " + events.size(), events.size() == 1);
- app.clear();
+ assertEquals("Incorrect number of events. Expected 1, actual " + events.size(), 1, events.size());
}
@Test
public void debugObject() {
logger.debug(new Date());
final List<LogEvent> events = app.getEvents();
- assertTrue("Incorrect number of events. Expected 1, actual " + events.size(), events.size() == 1);
- app.clear();
+ assertEquals("Incorrect number of events. Expected 1, actual " + events.size(), 1, events.size());
}
@Test
public void debugWithParms() {
logger.debug("Hello, {}", "World");
final List<LogEvent> events = app.getEvents();
- assertTrue("Incorrect number of events. Expected 1, actual " + events.size(), events.size() == 1);
- app.clear();
+ assertEquals("Incorrect number of events. Expected 1, actual " + events.size(), 1, events.size());
}
@Test
@@ -138,8 +112,7 @@ public class StrictXMLConfigTest {
ThreadContext.clearMap();
logger.debug("Debug message");
final List<LogEvent> events = app.getEvents();
- assertTrue("Incorrect number of events. Expected 2, actual " + events.size(), events.size() == 2);
- app.clear();
+ assertEquals("Incorrect number of events. Expected 2, actual " + events.size(), 2, events.size());
}
@Test
@@ -154,8 +127,7 @@ public class StrictXMLConfigTest {
logger.info(MarkerManager.getMarker("EVENT"), msg);
ThreadContext.clearMap();
final List<LogEvent> events = app.getEvents();
- assertTrue("Incorrect number of events. Expected 1, actual " + events.size(), events.size() == 1);
- app.clear();
+ assertEquals("Incorrect number of events. Expected 1, actual " + events.size(), 1, events.size());
}
}
Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/XMLEvents.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/XMLEvents.java?rev=1596458&r1=1596457&r2=1596458&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/XMLEvents.java (original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/XMLEvents.java Wed May 21 04:58:01 2014
@@ -20,9 +20,9 @@ import java.util.Locale;
import org.apache.logging.log4j.EventLogger;
import org.apache.logging.log4j.ThreadContext;
-import org.apache.logging.log4j.core.config.ConfigurationFactory;
+import org.apache.logging.log4j.junit.InitialLoggerContext;
import org.apache.logging.log4j.message.StructuredDataMessage;
-import org.junit.BeforeClass;
+import org.junit.ClassRule;
import org.junit.Test;
/**
@@ -32,10 +32,8 @@ public class XMLEvents {
private static final String CONFIG = "xml-events.xml";
- @BeforeClass
- public static void setupClass() {
- System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, CONFIG);
- }
+ @ClassRule
+ public static InitialLoggerContext context = new InitialLoggerContext(CONFIG);
@Test
public void testEvents() {
@@ -50,11 +48,11 @@ public class XMLEvents {
msg.setCompletionStatus("Transfer Complete");
EventLogger.logEvent(msg);
ThreadContext.clearMap();
-
+ // TODO: do something with the results
}
- private class TransferMessage extends StructuredDataMessage {
+ private static class TransferMessage extends StructuredDataMessage {
/**
* Generated serial version ID.