logging-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mattsic...@apache.org
Subject svn commit: r1582174 - /logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/routing/RoutingAppenderWithJndiTest.java
Date Thu, 27 Mar 2014 03:46:52 GMT
Author: mattsicker
Date: Thu Mar 27 03:46:52 2014
New Revision: 1582174

URL: http://svn.apache.org/r1582174
Log:
Update to use InitialLoggerContext rule.

Modified:
    logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/routing/RoutingAppenderWithJndiTest.java

Modified: logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/routing/RoutingAppenderWithJndiTest.java
URL: http://svn.apache.org/viewvc/logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/routing/RoutingAppenderWithJndiTest.java?rev=1582174&r1=1582173&r2=1582174&view=diff
==============================================================================
--- logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/routing/RoutingAppenderWithJndiTest.java
(original)
+++ logging/log4j/log4j2/trunk/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/routing/RoutingAppenderWithJndiTest.java
Thu Mar 27 03:46:52 2014
@@ -20,26 +20,16 @@ import static org.junit.Assert.assertNot
 import static org.junit.Assert.assertTrue;
 
 import java.io.File;
-import java.util.Map;
 
 import javax.naming.Context;
 import javax.naming.InitialContext;
 import javax.naming.NamingException;
 
 import org.apache.logging.log4j.EventLogger;
-import org.apache.logging.log4j.LogManager;
-import org.apache.logging.log4j.core.Appender;
-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.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.After;
-import org.junit.AfterClass;
-import org.junit.Before;
-import org.junit.BeforeClass;
-import org.junit.Test;
+import org.junit.*;
 import org.mockejb.jndi.MockContextFactory;
 
 /**
@@ -47,44 +37,27 @@ import org.mockejb.jndi.MockContextFacto
  */
 public class RoutingAppenderWithJndiTest {
 
-    private static final String CONFIG = "log4j-routing-by-jndi.xml";
-    private static Configuration config;
-    private static ListAppender listAppender1;
-    private static ListAppender listAppender2;
-    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("List1")) {
-                listAppender1 = (ListAppender) entry.getValue();
-            }
-            if (entry.getKey().equals("List2")) {
-                listAppender2 = (ListAppender) entry.getValue();
-            }
-        }
-    }
+    public static final String JNDI_CONTEXT_NAME = "java:comp/env/logging/context-name";
+    private ListAppender listAppender1;
+    private ListAppender listAppender2;
+
+    @Rule
+    public InitialLoggerContext init = new InitialLoggerContext("log4j-routing-by-jndi.xml");
 
     @Before
     public void before() throws NamingException {
         MockContextFactory.setAsInitial();
+        listAppender1 = (ListAppender) this.init.getAppender("List1");
+        listAppender2 = (ListAppender) this.init.getAppender("List2");
     }
 
     @After
     public void after() {
+        listAppender1.clear();
+        listAppender2.clear();
         MockContextFactory.revertSetAsInitial();
     }
 
-    @AfterClass
-    public static void cleanupClass() {
-        System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
-        ctx.reconfigure();
-        StatusLogger.getLogger().reset();
-    }
-
     @Test
     public void routingTest() throws NamingException {
         // default route when there's no jndi resource
@@ -95,7 +68,7 @@ public class RoutingAppenderWithJndiTest
 
         // now set jndi resource to Application1
         Context context = new InitialContext();
-        context.bind("java:comp/env/logging/context-name", "Application1");
+        context.bind(JNDI_CONTEXT_NAME, "Application1");
 
         msg = new StructuredDataMessage("Test", "This is a message from Application1", "Context");
         EventLogger.logEvent(msg);
@@ -103,7 +76,7 @@ public class RoutingAppenderWithJndiTest
         assertTrue("Incorrect number of events. Expected 1, got " + listAppender1.getEvents().size(),
listAppender1.getEvents().size() == 1);
 
         // now set jndi resource to Application2
-        context.rebind("java:comp/env/logging/context-name", "Application2");
+        context.rebind(JNDI_CONTEXT_NAME, "Application2");
 
         msg = new StructuredDataMessage("Test", "This is a message from Application2", "Context");
         EventLogger.logEvent(msg);



Mime
View raw message