You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by gg...@apache.org on 2021/12/17 02:55:03 UTC

[logging-log4j2] branch release-2.x updated: Use NIO and refactor.

This is an automated email from the ASF dual-hosted git repository.

ggregory pushed a commit to branch release-2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/release-2.x by this push:
     new b6e53b8  Use NIO and refactor.
b6e53b8 is described below

commit b6e53b8941e0b9eb8386c8f700d03ee2f5a435ad
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Thu Dec 16 21:53:16 2021 -0500

    Use NIO and refactor.
---
 .../apache/log4j/config/XmlConfigurationTest.java  | 61 ++++++++++++----------
 1 file changed, 32 insertions(+), 29 deletions(-)

diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/config/XmlConfigurationTest.java b/log4j-1.2-api/src/test/java/org/apache/log4j/config/XmlConfigurationTest.java
index 4cfffb5..950c561 100644
--- a/log4j-1.2-api/src/test/java/org/apache/log4j/config/XmlConfigurationTest.java
+++ b/log4j-1.2-api/src/test/java/org/apache/log4j/config/XmlConfigurationTest.java
@@ -31,8 +31,11 @@ import org.apache.logging.log4j.spi.LoggerContextFactory;
 import org.junit.Test;
 
 import java.io.File;
-import java.io.FileInputStream;
+import java.io.IOException;
 import java.io.InputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
 import java.util.List;
 import java.util.Map;
 
@@ -44,29 +47,28 @@ import static org.junit.Assert.assertTrue;
  */
 public class XmlConfigurationTest {
 
-    @Test
-    public void testXML() throws Exception {
-        configure("target/test-classes/log4j1-file.xml");
-        Logger logger = LogManager.getLogger("test");
-        logger.debug("This is a test of the root logger");
-        File file = new File("target/temp.A1");
-        assertTrue("File A1 was not created", file.exists());
-        assertTrue("File A1 is empty", file.length() > 0);
-        file = new File("target/temp.A2");
-        assertTrue("File A2 was not created", file.exists());
-        assertTrue("File A2 is empty", file.length() > 0);
+    static LoggerContext configureXml(final String configLocation) throws IOException {
+        final Path path = Paths.get(configLocation);
+        final InputStream is = Files.newInputStream(path);
+        final ConfigurationSource source = new ConfigurationSource(is, path.toFile());
+        final LoggerContextFactory factory = org.apache.logging.log4j.LogManager.getFactory();
+        final LoggerContext context = (LoggerContext) org.apache.logging.log4j.LogManager.getContext(false);
+        final Configuration configuration = new XmlConfigurationFactory().getConfiguration(context, source);
+        assertNotNull("No configuration created", configuration);
+        Configurator.reconfigure(configuration);
+        return context;
     }
 
     @Test
     public void testListAppender() throws Exception {
-        LoggerContext loggerContext = configure("target/test-classes/log4j1-list.xml");
-        Logger logger = LogManager.getLogger("test");
+        final LoggerContext loggerContext = configureXml("target/test-classes/log4j1-list.xml");
+        final Logger logger = LogManager.getLogger("test");
         logger.debug("This is a test of the root logger");
-        Configuration configuration = loggerContext.getConfiguration();
-        Map<String, Appender> appenders = configuration.getAppenders();
+        final Configuration configuration = loggerContext.getConfiguration();
+        final Map<String, Appender> appenders = configuration.getAppenders();
         ListAppender eventAppender = null;
         ListAppender messageAppender = null;
-        for (Map.Entry<String, Appender> entry : appenders.entrySet()) {
+        for (final Map.Entry<String, Appender> entry : appenders.entrySet()) {
             if (entry.getKey().equals("list")) {
                 messageAppender = (ListAppender) ((AppenderAdapter.Adapter) entry.getValue()).getAppender();
             } else if (entry.getKey().equals("events")) {
@@ -75,22 +77,23 @@ public class XmlConfigurationTest {
         }
         assertNotNull("No Event Appender", eventAppender);
         assertNotNull("No Message Appender", messageAppender);
-        List<LoggingEvent> events = eventAppender.getEvents();
+        final List<LoggingEvent> events = eventAppender.getEvents();
         assertTrue("No events", events != null && events.size() > 0);
-        List<String> messages = messageAppender.getMessages();
+        final List<String> messages = messageAppender.getMessages();
         assertTrue("No messages", messages != null && messages.size() > 0);
     }
 
-    private LoggerContext configure(String configLocation) throws Exception {
-        File file = new File(configLocation);
-        InputStream is = new FileInputStream(file);
-        ConfigurationSource source = new ConfigurationSource(is, file);
-        LoggerContextFactory factory = org.apache.logging.log4j.LogManager.getFactory();
-        LoggerContext context = (LoggerContext) org.apache.logging.log4j.LogManager.getContext(false);
-        Configuration configuration = new XmlConfigurationFactory().getConfiguration(context, source);
-        assertNotNull("No configuration created", configuration);
-        Configurator.reconfigure(configuration);
-        return context;
+    @Test
+    public void testXML() throws Exception {
+        configureXml("target/test-classes/log4j1-file.xml");
+        final Logger logger = LogManager.getLogger("test");
+        logger.debug("This is a test of the root logger");
+        File file = new File("target/temp.A1");
+        assertTrue("File A1 was not created", file.exists());
+        assertTrue("File A1 is empty", file.length() > 0);
+        file = new File("target/temp.A2");
+        assertTrue("File A2 was not created", file.exists());
+        assertTrue("File A2 is empty", file.length() > 0);
     }
 
 }