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 2022/01/11 19:21:19 UTC

[logging-log4j2] branch release-2.x updated: Use try-with-resources.

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 d499d2b  Use try-with-resources.
d499d2b is described below

commit d499d2b14243083c82ddb32eb4f96cf9ccc5fd7d
Author: Gary Gregory <ga...@gmail.com>
AuthorDate: Tue Jan 11 14:21:16 2022 -0500

    Use try-with-resources.
---
 .../org/apache/log4j/config/AsyncAppenderTest.java | 80 ++++++++--------------
 .../org/apache/log4j/config/TestConfigurator.java  | 32 ++++-----
 2 files changed, 44 insertions(+), 68 deletions(-)

diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/config/AsyncAppenderTest.java b/log4j-1.2-api/src/test/java/org/apache/log4j/config/AsyncAppenderTest.java
index 14dabc5..9badb7d 100644
--- a/log4j-1.2-api/src/test/java/org/apache/log4j/config/AsyncAppenderTest.java
+++ b/log4j-1.2-api/src/test/java/org/apache/log4j/config/AsyncAppenderTest.java
@@ -19,10 +19,6 @@ package org.apache.log4j.config;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
-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;
 
@@ -30,13 +26,9 @@ import org.apache.log4j.ListAppender;
 import org.apache.log4j.LogManager;
 import org.apache.log4j.Logger;
 import org.apache.log4j.bridge.AppenderAdapter;
-import org.apache.log4j.xml.XmlConfigurationFactory;
 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.ConfigurationSource;
-import org.apache.logging.log4j.core.config.Configurator;
-import org.apache.logging.log4j.spi.LoggerContextFactory;
 import org.junit.Test;
 
 /**
@@ -46,57 +38,41 @@ public class AsyncAppenderTest {
 
     @Test
     public void testAsyncXml() throws Exception {
-        final LoggerContext loggerContext = configure("target/test-classes/log4j1-async.xml");
-        final Logger logger = LogManager.getLogger("test");
-        logger.debug("This is a test of the root logger");
-        Thread.sleep(50);
-        final Configuration configuration = loggerContext.getConfiguration();
-        final Map<String, Appender> appenders = configuration.getAppenders();
-        ListAppender messageAppender = null;
-        for (final Map.Entry<String, Appender> entry : appenders.entrySet()) {
-            if (entry.getKey().equals("list")) {
-                messageAppender = (ListAppender) ((AppenderAdapter.Adapter) entry.getValue()).getAppender();
+        try (final LoggerContext loggerContext = TestConfigurator.configure("target/test-classes/log4j1-async.xml")) {
+            final Logger logger = LogManager.getLogger("test");
+            logger.debug("This is a test of the root logger");
+            Thread.sleep(50);
+            final Configuration configuration = loggerContext.getConfiguration();
+            final Map<String, Appender> appenders = configuration.getAppenders();
+            ListAppender messageAppender = null;
+            for (final Map.Entry<String, Appender> entry : appenders.entrySet()) {
+                if (entry.getKey().equals("list")) {
+                    messageAppender = (ListAppender) ((AppenderAdapter.Adapter) entry.getValue()).getAppender();
+                }
             }
+            assertNotNull("No Message Appender", messageAppender);
+            final List<String> messages = messageAppender.getMessages();
+            assertTrue("No messages", messages != null && messages.size() > 0);
         }
-        assertNotNull("No Message Appender", messageAppender);
-        final List<String> messages = messageAppender.getMessages();
-        assertTrue("No messages", messages != null && messages.size() > 0);
     }
 
     @Test
     public void testAsyncProperties() throws Exception {
-        final LoggerContext loggerContext = configure("target/test-classes/log4j1-async.properties");
-        final Logger logger = LogManager.getLogger("test");
-        logger.debug("This is a test of the root logger");
-        Thread.sleep(50);
-        final Configuration configuration = loggerContext.getConfiguration();
-        final Map<String, Appender> appenders = configuration.getAppenders();
-        ListAppender messageAppender = null;
-        for (final Map.Entry<String, Appender> entry : appenders.entrySet()) {
-            if (entry.getKey().equals("list")) {
-                messageAppender = (ListAppender) ((AppenderAdapter.Adapter) entry.getValue()).getAppender();
+        try (final LoggerContext loggerContext = TestConfigurator.configure("target/test-classes/log4j1-async.properties")) {
+            final Logger logger = LogManager.getLogger("test");
+            logger.debug("This is a test of the root logger");
+            Thread.sleep(50);
+            final Configuration configuration = loggerContext.getConfiguration();
+            final Map<String, Appender> appenders = configuration.getAppenders();
+            ListAppender messageAppender = null;
+            for (final Map.Entry<String, Appender> entry : appenders.entrySet()) {
+                if (entry.getKey().equals("list")) {
+                    messageAppender = (ListAppender) ((AppenderAdapter.Adapter) entry.getValue()).getAppender();
+                }
             }
+            assertNotNull("No Message Appender", messageAppender);
+            final List<String> messages = messageAppender.getMessages();
+            assertTrue("No messages", messages != null && messages.size() > 0);
         }
-        assertNotNull("No Message Appender", messageAppender);
-        final List<String> messages = messageAppender.getMessages();
-        assertTrue("No messages", messages != null && messages.size() > 0);
     }
-
-
-    private LoggerContext configure(final String configLocation) throws Exception {
-        final Path path = Paths.get(configLocation);
-        final InputStream is = Files.newInputStream(path);
-        final ConfigurationSource source = new ConfigurationSource(is, path.toFile());
-        final LoggerContext context = (LoggerContext) org.apache.logging.log4j.LogManager.getContext(false);
-        Configuration configuration;
-        if (configLocation.endsWith(".xml")) {
-            configuration = new XmlConfigurationFactory().getConfiguration(context, source);
-        } else {
-            configuration = new PropertiesConfigurationFactory().getConfiguration(context, source);
-        }
-        assertNotNull("No configuration created", configuration);
-        Configurator.reconfigure(configuration);
-        return context;
-    }
-
 }
diff --git a/log4j-1.2-api/src/test/java/org/apache/log4j/config/TestConfigurator.java b/log4j-1.2-api/src/test/java/org/apache/log4j/config/TestConfigurator.java
index 7cd39af..545f5e0 100644
--- a/log4j-1.2-api/src/test/java/org/apache/log4j/config/TestConfigurator.java
+++ b/log4j-1.2-api/src/test/java/org/apache/log4j/config/TestConfigurator.java
@@ -33,23 +33,23 @@ import org.apache.logging.log4j.core.config.Configurator;
 
 public class TestConfigurator {
 
-    @SuppressWarnings("resource")
-    static LoggerContext configure(String configLocation) throws IOException {
-        File file = new File(configLocation);
-        InputStream is = Files.newInputStream(Paths.get(configLocation));
-        ConfigurationSource source = new ConfigurationSource(is, file);
-        LoggerContext context = (LoggerContext) org.apache.logging.log4j.LogManager.getContext(false);
-        Configuration configuration = null;
-        if (configLocation.endsWith(PropertiesConfigurationFactory.FILE_EXTENSION)) {
-            configuration = new PropertiesConfigurationFactory().getConfiguration(context, source);
-        } else if (configLocation.endsWith(XmlConfigurationFactory.FILE_EXTENSION)) {
-            configuration = new XmlConfigurationFactory().getConfiguration(context, source);
-        } else {
-            fail("Test infra does not support " + configLocation);
+    static LoggerContext configure(final String configLocation) throws IOException {
+        final File file = new File(configLocation);
+        try (final InputStream inputStream = Files.newInputStream(Paths.get(configLocation))) {
+            final ConfigurationSource source = new ConfigurationSource(inputStream, file);
+            final LoggerContext context = (LoggerContext) org.apache.logging.log4j.LogManager.getContext(false);
+            Configuration configuration = null;
+            if (configLocation.endsWith(PropertiesConfigurationFactory.FILE_EXTENSION)) {
+                configuration = new PropertiesConfigurationFactory().getConfiguration(context, source);
+            } else if (configLocation.endsWith(XmlConfigurationFactory.FILE_EXTENSION)) {
+                configuration = new XmlConfigurationFactory().getConfiguration(context, source);
+            } else {
+                fail("Test infra does not support " + configLocation);
+            }
+            assertNotNull("No configuration created", configuration);
+            Configurator.reconfigure(configuration);
+            return context;
         }
-        assertNotNull("No configuration created", configuration);
-        Configurator.reconfigure(configuration);
-        return context;
     }
 
 }