logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gary Gregory <garydgreg...@gmail.com>
Subject Fwd: logging-log4j2 git commit: Update tests to use LoggerContextRule.
Date Sat, 20 Feb 2016 04:27:01 GMT
Nice clean up Matt.

Gary
---------- Forwarded message ----------
From: <mattsicker@apache.org>
Date: Fri, Feb 19, 2016 at 8:14 PM
Subject: logging-log4j2 git commit: Update tests to use LoggerContextRule.
To: commits@logging.apache.org


Repository: logging-log4j2
Updated Branches:
  refs/heads/master 6e54d7160 -> a8c8fa727


Update tests to use 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/a8c8fa72
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/a8c8fa72
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/a8c8fa72

Branch: refs/heads/master
Commit: a8c8fa727000d3baa77c680a0ffa544b0d9084b6
Parents: 6e54d71
Author: Matt Sicker <boards@gmail.com>
Authored: Fri Feb 19 22:14:49 2016 -0600
Committer: Matt Sicker <boards@gmail.com>
Committed: Fri Feb 19 22:15:10 2016 -0600

----------------------------------------------------------------------
 .../logging/log4j/taglib/CatchingTagTest.java   | 43 +++++---------------
 .../logging/log4j/taglib/EntryTagTest.java      | 36 ++++------------
 .../logging/log4j/taglib/ExitTagTest.java       | 36 ++++------------
 .../logging/log4j/taglib/IfEnabledTagTest.java  | 26 +++---------
 .../taglib/LoggingMessageTagSupportTest.java    | 38 ++++-------------
 5 files changed, 39 insertions(+), 140 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/a8c8fa72/log4j-taglib/src/test/java/org/apache/logging/log4j/taglib/CatchingTagTest.java
----------------------------------------------------------------------
diff --git
a/log4j-taglib/src/test/java/org/apache/logging/log4j/taglib/CatchingTagTest.java
b/log4j-taglib/src/test/java/org/apache/logging/log4j/taglib/CatchingTagTest.java
index 006252d..a84bc80 100644
---
a/log4j-taglib/src/test/java/org/apache/logging/log4j/taglib/CatchingTagTest.java
+++
b/log4j-taglib/src/test/java/org/apache/logging/log4j/taglib/CatchingTagTest.java
@@ -16,50 +16,30 @@
  */
 package org.apache.logging.log4j.taglib;

-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
-
 import java.util.List;
-
 import javax.servlet.jsp.tagext.Tag;

 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.ConfigurationFactory;
-import org.apache.logging.log4j.status.StatusLogger;
+import org.apache.logging.log4j.junit.LoggerContextRule;
 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.springframework.mock.web.MockPageContext;

+import static org.junit.Assert.*;
+
 /**
  *
  */
 public class CatchingTagTest {
     private static final String CONFIG = "log4j-test1.xml";

-    @BeforeClass
-    public static void setUpClass() {
-
System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY,
CONFIG);
-        final LoggerContext context = LoggerContext.getContext(false);
-        context.getConfiguration();
-    }
-
-    @AfterClass
-    public static void cleanUpClass() {
-
System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
-        final LoggerContext context = LoggerContext.getContext(false);
-        context.reconfigure();
-        StatusLogger.getLogger().reset();
-    }
+    @ClassRule
+    public static LoggerContextRule context = new
LoggerContextRule(CONFIG);

-    private final Logger logger =
LogManager.getLogger("LoggingMessageTagSupportTestLogger");
+    private final Logger logger =
context.getLogger("LoggingMessageTagSupportTestLogger");
     private CatchingTag tag;

     @Before
@@ -96,11 +76,8 @@ public class CatchingTagTest {
     }

     private void verify(final String expected) {
-        final LoggerContext ctx = LoggerContext.getContext(false);
-        final Appender listApp =
ctx.getConfiguration().getAppender("List");
-        assertNotNull("Missing Appender", listApp);
-        assertTrue("Not a ListAppender", listApp instanceof ListAppender);
-        final List<String> events = ((ListAppender) listApp).getMessages();
+        final ListAppender listApp = context.getListAppender("List");
+        final List<String> events = listApp.getMessages();
         try
         {
             assertEquals("Incorrect number of messages.", 1,
events.size());
@@ -108,7 +85,7 @@ public class CatchingTagTest {
         }
         finally
         {
-            ((ListAppender) listApp).clear();
+            listApp.clear();
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/a8c8fa72/log4j-taglib/src/test/java/org/apache/logging/log4j/taglib/EntryTagTest.java
----------------------------------------------------------------------
diff --git
a/log4j-taglib/src/test/java/org/apache/logging/log4j/taglib/EntryTagTest.java
b/log4j-taglib/src/test/java/org/apache/logging/log4j/taglib/EntryTagTest.java
index 2bc34ce..26681e8 100644
---
a/log4j-taglib/src/test/java/org/apache/logging/log4j/taglib/EntryTagTest.java
+++
b/log4j-taglib/src/test/java/org/apache/logging/log4j/taglib/EntryTagTest.java
@@ -19,16 +19,11 @@ package org.apache.logging.log4j.taglib;
 import java.util.List;
 import javax.servlet.jsp.tagext.Tag;

-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.ConfigurationFactory;
-import org.apache.logging.log4j.status.StatusLogger;
+import org.apache.logging.log4j.junit.LoggerContextRule;
 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.springframework.mock.web.MockPageContext;

@@ -40,22 +35,10 @@ import static org.junit.Assert.*;
 public class EntryTagTest {
     private static final String CONFIG = "log4j-test1.xml";

-    @BeforeClass
-    public static void setUpClass() {
-
System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY,
CONFIG);
-        final LoggerContext context = LoggerContext.getContext(false);
-        context.getConfiguration();
-    }
-
-    @AfterClass
-    public static void cleanUpClass() {
-
System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
-        final LoggerContext context = LoggerContext.getContext(false);
-        context.reconfigure();
-        StatusLogger.getLogger().reset();
-    }
+    @ClassRule
+    public static LoggerContextRule context = new
LoggerContextRule(CONFIG);

-    private final Logger logger =
LogManager.getLogger("LoggingMessageTagSupportTestLogger");
+    private final Logger logger =
context.getLogger("LoggingMessageTagSupportTestLogger");
     private EntryTag tag;

     @Before
@@ -81,11 +64,8 @@ public class EntryTagTest {
     }

     private void verify(final String expected) {
-        final LoggerContext ctx = LoggerContext.getContext(false);
-        final Appender listApp =
ctx.getConfiguration().getAppender("List");
-        assertNotNull("Missing Appender", listApp);
-        assertTrue("Not a ListAppender", listApp instanceof ListAppender);
-        final List<String> events = ((ListAppender) listApp).getMessages();
+        final ListAppender listApp = context.getListAppender("List");
+        final List<String> events = listApp.getMessages();
         try
         {
             assertEquals("Incorrect number of messages.", 1,
events.size());
@@ -93,7 +73,7 @@ public class EntryTagTest {
         }
         finally
         {
-            ((ListAppender) listApp).clear();
+            listApp.clear();
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/a8c8fa72/log4j-taglib/src/test/java/org/apache/logging/log4j/taglib/ExitTagTest.java
----------------------------------------------------------------------
diff --git
a/log4j-taglib/src/test/java/org/apache/logging/log4j/taglib/ExitTagTest.java
b/log4j-taglib/src/test/java/org/apache/logging/log4j/taglib/ExitTagTest.java
index 015601b..3e5ab22 100644
---
a/log4j-taglib/src/test/java/org/apache/logging/log4j/taglib/ExitTagTest.java
+++
b/log4j-taglib/src/test/java/org/apache/logging/log4j/taglib/ExitTagTest.java
@@ -19,16 +19,11 @@ package org.apache.logging.log4j.taglib;
 import java.util.List;
 import javax.servlet.jsp.tagext.Tag;

-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.ConfigurationFactory;
-import org.apache.logging.log4j.status.StatusLogger;
+import org.apache.logging.log4j.junit.LoggerContextRule;
 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.springframework.mock.web.MockPageContext;

@@ -40,22 +35,10 @@ import static org.junit.Assert.*;
 public class ExitTagTest {
     private static final String CONFIG = "log4j-test1.xml";

-    @BeforeClass
-    public static void setUpClass() {
-
System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY,
CONFIG);
-        final LoggerContext context = LoggerContext.getContext(false);
-        context.getConfiguration();
-    }
-
-    @AfterClass
-    public static void cleanUpClass() {
-
System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
-        final LoggerContext context = LoggerContext.getContext(false);
-        context.reconfigure();
-        StatusLogger.getLogger().reset();
-    }
+    @ClassRule
+    public static LoggerContextRule context = new
LoggerContextRule(CONFIG);

-    private final Logger logger =
LogManager.getLogger("LoggingMessageTagSupportTestLogger");
+    private final Logger logger =
context.getLogger("LoggingMessageTagSupportTestLogger");
     private ExitTag tag;

     @Before
@@ -88,11 +71,8 @@ public class ExitTagTest {
     }

     private void verify(final String expected) {
-        final LoggerContext ctx = LoggerContext.getContext(false);
-        final Appender listApp =
ctx.getConfiguration().getAppender("List");
-        assertNotNull("Missing Appender", listApp);
-        assertTrue("Not a ListAppender", listApp instanceof ListAppender);
-        final List<String> events = ((ListAppender) listApp).getMessages();
+        final ListAppender listApp = context.getListAppender("List");
+        final List<String> events = listApp.getMessages();
         try
         {
             assertEquals("Incorrect number of messages.", 1,
events.size());
@@ -100,7 +80,7 @@ public class ExitTagTest {
         }
         finally
         {
-            ((ListAppender) listApp).clear();
+            listApp.clear();
         }
     }
 }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/a8c8fa72/log4j-taglib/src/test/java/org/apache/logging/log4j/taglib/IfEnabledTagTest.java
----------------------------------------------------------------------
diff --git
a/log4j-taglib/src/test/java/org/apache/logging/log4j/taglib/IfEnabledTagTest.java
b/log4j-taglib/src/test/java/org/apache/logging/log4j/taglib/IfEnabledTagTest.java
index 0d9c085..9596c5b 100644
---
a/log4j-taglib/src/test/java/org/apache/logging/log4j/taglib/IfEnabledTagTest.java
+++
b/log4j-taglib/src/test/java/org/apache/logging/log4j/taglib/IfEnabledTagTest.java
@@ -19,15 +19,11 @@ package org.apache.logging.log4j.taglib;
 import javax.servlet.jsp.tagext.Tag;

 import org.apache.logging.log4j.Level;
-import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.MarkerManager;
-import org.apache.logging.log4j.core.LoggerContext;
-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.LoggerContextRule;
 import org.junit.Before;
-import org.junit.BeforeClass;
+import org.junit.ClassRule;
 import org.junit.Test;
 import org.springframework.mock.web.MockPageContext;

@@ -39,22 +35,10 @@ import static org.junit.Assert.*;
 public class IfEnabledTagTest {
     private static final String CONFIG = "log4j-test1.xml";

-    @BeforeClass
-    public static void setUpClass() {
-
System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY,
CONFIG);
-        final LoggerContext context = LoggerContext.getContext(false);
-        context.getConfiguration();
-    }
-
-    @AfterClass
-    public static void cleanUpClass() {
-
System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
-        final LoggerContext context = LoggerContext.getContext(false);
-        context.reconfigure();
-        StatusLogger.getLogger().reset();
-    }
+    @ClassRule
+    public static LoggerContextRule context = new
LoggerContextRule(CONFIG);

-    private final Logger logger = LogManager.getLogger("IfEnabledTagTest");
+    private final Logger logger = context.getLogger("IfEnabledTagTest");
     private IfEnabledTag tag;

     @Before

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/a8c8fa72/log4j-taglib/src/test/java/org/apache/logging/log4j/taglib/LoggingMessageTagSupportTest.java
----------------------------------------------------------------------
diff --git
a/log4j-taglib/src/test/java/org/apache/logging/log4j/taglib/LoggingMessageTagSupportTest.java
b/log4j-taglib/src/test/java/org/apache/logging/log4j/taglib/LoggingMessageTagSupportTest.java
index 342b2da..26aa46c 100644
---
a/log4j-taglib/src/test/java/org/apache/logging/log4j/taglib/LoggingMessageTagSupportTest.java
+++
b/log4j-taglib/src/test/java/org/apache/logging/log4j/taglib/LoggingMessageTagSupportTest.java
@@ -19,21 +19,15 @@ package org.apache.logging.log4j.taglib;
 import java.io.Writer;
 import java.util.List;
 import java.util.concurrent.TimeUnit;
-
 import javax.servlet.jsp.tagext.BodyTag;
 import javax.servlet.jsp.tagext.Tag;

 import org.apache.logging.log4j.Level;
-import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import org.apache.logging.log4j.MarkerManager;
-import org.apache.logging.log4j.core.Appender;
-import org.apache.logging.log4j.core.LoggerContext;
-import org.apache.logging.log4j.core.config.ConfigurationFactory;
-import org.apache.logging.log4j.status.StatusLogger;
+import org.apache.logging.log4j.junit.LoggerContextRule;
 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.springframework.mock.web.MockBodyContent;
 import org.springframework.mock.web.MockPageContext;
@@ -46,22 +40,10 @@ import static org.junit.Assert.*;
 public class LoggingMessageTagSupportTest {
     private static final String CONFIG = "log4j-test1.xml";

-    @BeforeClass
-    public static void setUpClass() {
-
System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY,
CONFIG);
-        final LoggerContext context = LoggerContext.getContext(false);
-        context.getConfiguration();
-    }
-
-    @AfterClass
-    public static void cleanUpClass() {
-
System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
-        final LoggerContext context = LoggerContext.getContext(false);
-        context.reconfigure();
-        StatusLogger.getLogger().reset();
-    }
+    @ClassRule
+    public static LoggerContextRule context = new
LoggerContextRule(CONFIG);

-    private final Logger logger =
LogManager.getLogger("LoggingMessageTagSupportTestLogger");
+    private final Logger logger =
context.getLogger("LoggingMessageTagSupportTestLogger");
     private LoggingMessageTagSupport tag;

     private void setUp(final Level level) {
@@ -294,13 +276,9 @@ public class LoggingMessageTagSupportTest {
                 "This is another test");
     }

-    @SuppressWarnings("unchecked")
     private void verify(final String expected) {
-        final LoggerContext ctx = LoggerContext.getContext(false);
-        final Appender listApp =
ctx.getConfiguration().getAppender("List");
-        assertNotNull("Missing Appender", listApp);
-        assertTrue("Not a ListAppender", listApp instanceof ListAppender);
-        final List<String> events = ((ListAppender) listApp).getMessages();
+        final ListAppender listApp = context.getListAppender("List");
+        final List<String> events = listApp.getMessages();
         try
         {
             assertEquals("Incorrect number of messages.", 1,
events.size());
@@ -308,7 +286,7 @@ public class LoggingMessageTagSupportTest {
         }
         finally
         {
-            ((ListAppender) listApp).clear();
+            listApp.clear();
         }
     }





-- 
E-Mail: garydgregory@gmail.com | ggregory@apache.org
Java Persistence with Hibernate, Second Edition
<http://www.manning.com/bauer3/>
JUnit in Action, Second Edition <http://www.manning.com/tahchiev/>
Spring Batch in Action <http://www.manning.com/templier/>
Blog: http://garygregory.wordpress.com
Home: http://garygregory.com/
Tweet! http://twitter.com/GaryGregory

Mime
View raw message