Simplify tests with LoggerContextRule.

Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit: http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/22a8b3d3
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/22a8b3d3
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/22a8b3d3

Branch: refs/heads/LOG4J2-1161
Commit: 22a8b3d35a021b7eb7148dce9e6d6ee4ff7e459a
Parents: 639bef4
Author: Matt Sicker <boa...@gmail.com>
Authored: Wed Oct 21 02:25:05 2015 -0500
Committer: Matt Sicker <boa...@gmail.com>
Committed: Wed Oct 21 02:31:51 2015 -0500

----------------------------------------------------------------------
 .../routing/JsonRoutingAppender2Test.java       | 40 ++++++--------------
 .../routing/JsonRoutingAppenderTest.java        | 40 ++++++--------------
 .../AsyncLoggersWithAsyncAppenderTest.java      | 37 ++++--------------
 .../AsyncLoggersWithAsyncLoggerConfigTest.java  | 37 ++++--------------
 4 files changed, 36 insertions(+), 118 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/22a8b3d3/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/routing/JsonRoutingAppender2Test.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/routing/JsonRoutingAppender2Test.java
 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/routing/JsonRoutingAppender2Test.java
index af7c078..21d37f5 100644
--- 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/routing/JsonRoutingAppender2Test.java
+++ 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/routing/JsonRoutingAppender2Test.java
@@ -18,17 +18,15 @@ package org.apache.logging.log4j.core.appender.routing;
 
 import java.io.File;
 import java.util.List;
+
 import org.apache.logging.log4j.EventLogger;
 import org.apache.logging.log4j.core.LogEvent;
-import org.apache.logging.log4j.core.LoggerContext;
-import org.apache.logging.log4j.core.config.Configuration;
-import org.apache.logging.log4j.core.config.ConfigurationFactory;
+import org.apache.logging.log4j.junit.CleanFiles;
+import org.apache.logging.log4j.junit.LoggerContextRule;
 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.ClassRule;
 import org.junit.Test;
+import org.junit.rules.RuleChain;
 
 import static org.junit.Assert.*;
 
@@ -37,39 +35,23 @@ import static org.junit.Assert.*;
  */
 public class JsonRoutingAppender2Test {
     private static final String CONFIG = "log4j-routing2.json";
-    private static Configuration config;
-    private static ListAppender listAppender;
-    private static LoggerContext ctx;
+    private static final String LOG_FILENAME = 
"target/rolling1/rollingtest-Unknown.log";
 
-    @BeforeClass
-    public static void setupClass() {
-        System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, 
CONFIG);
-        ctx = LoggerContext.getContext(false);
-        config = ctx.getConfiguration();
-        listAppender = config.getAppender("List");
-        final File file = new File("target/rolling1/rollingtest-Unknown.log");
-        file.delete();
-    }
+    private static LoggerContextRule context = new LoggerContextRule(CONFIG);
 
-    @AfterClass
-    public static void cleanupClass() {
-        System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
-        ctx.reconfigure();
-        StatusLogger.getLogger().reset();
-        final File file = new File("target/rolling1/rollingtest-Unknown.log");
-        file.delete();
-    }
+    @ClassRule
+    public static RuleChain rules = RuleChain.outerRule(new 
CleanFiles(LOG_FILENAME)).around(context);
 
     @Test
     public void routingTest() {
         StructuredDataMessage msg = new StructuredDataMessage("Test", "This is 
a test", "Service");
         EventLogger.logEvent(msg);
-        final List<LogEvent> list = listAppender.getEvents();
+        final List<LogEvent> list = 
context.getListAppender("List").getEvents();
         assertNotNull("No events generated", list);
         assertTrue("Incorrect number of events. Expected 1, got " + 
list.size(), list.size() == 1);
         msg = new StructuredDataMessage("Test", "This is a test", "Unknown");
         EventLogger.logEvent(msg);
-        final File file = new File("target/rolling1/rollingtest-Unknown.log");
+        final File file = new File(LOG_FILENAME);
         assertTrue("File was not created", file.exists());
     }
 }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/22a8b3d3/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/routing/JsonRoutingAppenderTest.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/routing/JsonRoutingAppenderTest.java
 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/routing/JsonRoutingAppenderTest.java
index 45e5412..dfd726c 100644
--- 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/routing/JsonRoutingAppenderTest.java
+++ 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/routing/JsonRoutingAppenderTest.java
@@ -18,17 +18,15 @@ package org.apache.logging.log4j.core.appender.routing;
 
 import java.io.File;
 import java.util.List;
+
 import org.apache.logging.log4j.EventLogger;
 import org.apache.logging.log4j.core.LogEvent;
-import org.apache.logging.log4j.core.LoggerContext;
-import org.apache.logging.log4j.core.config.Configuration;
-import org.apache.logging.log4j.core.config.ConfigurationFactory;
+import org.apache.logging.log4j.junit.CleanFiles;
+import org.apache.logging.log4j.junit.LoggerContextRule;
 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.ClassRule;
 import org.junit.Test;
+import org.junit.rules.RuleChain;
 
 import static org.junit.Assert.*;
 
@@ -37,39 +35,23 @@ import static org.junit.Assert.*;
  */
 public class JsonRoutingAppenderTest {
     private static final String CONFIG = "log4j-routing.json";
-    private static Configuration config;
-    private static ListAppender listAppender;
-    private static LoggerContext ctx;
+    private static final String LOG_FILENAME = 
"target/rolling1/rollingtest-Unknown.log";
 
-    @BeforeClass
-    public static void setupClass() {
-        System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, 
CONFIG);
-        ctx = LoggerContext.getContext(false);
-        config = ctx.getConfiguration();
-        listAppender = config.getAppender("List");
-        final File file = new File("target/rolling1/rollingtest-Unknown.log");
-        file.delete();
-    }
+    private static LoggerContextRule context = new LoggerContextRule(CONFIG);
 
-    @AfterClass
-    public static void cleanupClass() {
-        System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
-        ctx.reconfigure();
-        StatusLogger.getLogger().reset();
-        final File file = new File("target/rolling1/rollingtest-Unknown.log");
-        file.delete();
-    }
+    @ClassRule
+    public static RuleChain rules = RuleChain.outerRule(new 
CleanFiles(LOG_FILENAME)).around(context);
 
     @Test
     public void routingTest() {
         StructuredDataMessage msg = new StructuredDataMessage("Test", "This is 
a test", "Service");
         EventLogger.logEvent(msg);
-        final List<LogEvent> list = listAppender.getEvents();
+        final List<LogEvent> list = 
context.getListAppender("List").getEvents();
         assertNotNull("No events generated", list);
         assertTrue("Incorrect number of events. Expected 1, got " + 
list.size(), list.size() == 1);
         msg = new StructuredDataMessage("Test", "This is a test", "Unknown");
         EventLogger.logEvent(msg);
-        final File file = new File("target/rolling1/rollingtest-Unknown.log");
+        final File file = new File(LOG_FILENAME);
         assertTrue("File was not created", file.exists());
     }
 }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/22a8b3d3/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggersWithAsyncAppenderTest.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggersWithAsyncAppenderTest.java
 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggersWithAsyncAppenderTest.java
index ea52157..7613335 100644
--- 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggersWithAsyncAppenderTest.java
+++ 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggersWithAsyncAppenderTest.java
@@ -20,48 +20,25 @@ import java.util.List;
 
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.core.LoggerContext;
-import org.apache.logging.log4j.core.config.Configuration;
-import org.apache.logging.log4j.core.config.ConfigurationFactory;
-import org.apache.logging.log4j.core.util.Constants;
-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.apache.logging.log4j.junit.LoggerContextRule;
+import org.junit.ClassRule;
 import org.junit.Test;
 
 import static org.junit.Assert.*;
 
 public class AsyncLoggersWithAsyncAppenderTest {
-    private static Configuration config;
-    private static ListAppender listAppender;
-    private static LoggerContext ctx;
 
-    @BeforeClass
-    public static void beforeClass() {
-        System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY,
-                "AsyncLoggersWithAsyncAppenderTest.xml");
-        System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR, 
AsyncLoggerContextSelector.class.getName());
-        ctx = LoggerContext.getContext(false);
-        config = ctx.getConfiguration();
-        listAppender = config.getAppender("List");
-    }
-
-    @AfterClass
-    public static void cleanupClass() {
-        System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
-        System.clearProperty(Constants.LOG4J_CONTEXT_SELECTOR);
-        ctx.reconfigure();
-        StatusLogger.getLogger().reset();
-    }
+    @ClassRule
+    public static LoggerContextRule context = new 
LoggerContextRule("AsyncLoggersWithAsyncAppenderTest.xml",
+        AsyncLoggerContextSelector.class);
 
     @Test
-    public void testLoggingWorks() throws Exception {        
+    public void testLoggingWorks() throws Exception {
         final Logger logger = LogManager.getLogger();
         logger.error("This is a test");
         logger.warn("Hello world!");
         Thread.sleep(100);
-        final List<String> list = listAppender.getMessages();
+        final List<String> list = 
context.getListAppender("List").getMessages();
         assertNotNull("No events generated", list);
         assertTrue("Incorrect number of events. Expected 2, got " + 
list.size(), list.size() == 2);
         String msg = list.get(0);

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/22a8b3d3/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggersWithAsyncLoggerConfigTest.java
----------------------------------------------------------------------
diff --git 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggersWithAsyncLoggerConfigTest.java
 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggersWithAsyncLoggerConfigTest.java
index 5bd60d1..860ecd4 100644
--- 
a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggersWithAsyncLoggerConfigTest.java
+++ 
b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggersWithAsyncLoggerConfigTest.java
@@ -20,48 +20,25 @@ import java.util.List;
 
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
-import org.apache.logging.log4j.core.LoggerContext;
-import org.apache.logging.log4j.core.config.Configuration;
-import org.apache.logging.log4j.core.config.ConfigurationFactory;
-import org.apache.logging.log4j.core.util.Constants;
-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.apache.logging.log4j.junit.LoggerContextRule;
+import org.junit.ClassRule;
 import org.junit.Test;
 
 import static org.junit.Assert.*;
 
 public class AsyncLoggersWithAsyncLoggerConfigTest {
-    private static Configuration config;
-    private static ListAppender listAppender;
-    private static LoggerContext ctx;
 
-    @BeforeClass
-    public static void beforeClass() {
-        System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY,
-                "AsyncLoggersWithAsyncLoggerConfigTest.xml");
-        System.setProperty(Constants.LOG4J_CONTEXT_SELECTOR, 
AsyncLoggerContextSelector.class.getName());
-        ctx = LoggerContext.getContext(false);
-        config = ctx.getConfiguration();
-        listAppender = config.getAppender("List");
-    }
-
-    @AfterClass
-    public static void cleanupClass() {
-        System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
-        System.clearProperty(Constants.LOG4J_CONTEXT_SELECTOR);
-        ctx.reconfigure();
-        StatusLogger.getLogger().reset();
-    }
+    @ClassRule
+    public static LoggerContextRule context = new 
LoggerContextRule("AsyncLoggersWithAsyncLoggerConfigTest.xml",
+        AsyncLoggerContextSelector.class);
 
     @Test
-    public void testLoggingWorks() throws Exception {        
+    public void testLoggingWorks() throws Exception {
         final Logger logger = LogManager.getLogger();
         logger.error("This is a test");
         logger.warn("Hello world!");
         Thread.sleep(100);
-        final List<String> list = listAppender.getMessages();
+        final List<String> list = 
context.getListAppender("List").getMessages();
         assertNotNull("No events generated", list);
         assertTrue("Incorrect number of events. Expected 2, got " + 
list.size(), list.size() == 2);
         String msg = list.get(0);

Reply via email to