You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@logging.apache.org by vy...@apache.org on 2021/06/24 15:28:33 UTC

[logging-log4j2] branch master updated: Replace try finally statement with try with resources statement (#510)

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

vy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git


The following commit(s) were added to refs/heads/master by this push:
     new 5ba64e8  Replace try finally statement with try with resources statement (#510)
5ba64e8 is described below

commit 5ba64e8e9ee2920861aedffc449bfd51b6eb92e7
Author: Arturo Bernal <ar...@gmail.com>
AuthorDate: Thu Jun 24 17:28:24 2021 +0200

    Replace try finally statement with try with resources statement (#510)
---
 .../apache/logging/log4j/core/EventParameterMemoryLeakTest.java    | 7 ++-----
 .../log4j/core/ReusableParameterizedMessageMemoryLeakTest.java     | 7 ++-----
 .../log4j/core/async/AsyncAppenderConfigTest_LOG4J2_2032.java      | 4 +---
 .../log4j/core/async/AsyncLoggerTestArgumentFreedOnError.java      | 7 ++-----
 .../apache/logging/log4j/core/async/QueueFullAsyncLoggerTest3.java | 7 ++-----
 .../apache/logging/log4j/core/config/NestedLoggerConfigTest.java   | 5 +----
 .../log4j/core/impl/NestedLoggingFromThrowableMessageTest.java     | 5 +----
 7 files changed, 11 insertions(+), 31 deletions(-)

diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/EventParameterMemoryLeakTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/EventParameterMemoryLeakTest.java
index 944e987..b7a28bc 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/EventParameterMemoryLeakTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/EventParameterMemoryLeakTest.java
@@ -73,12 +73,9 @@ public class EventParameterMemoryLeakTest {
         assertThat(line3, containsString("paramValue"));
         assertThat(line4, containsString("paramValue"));
         assertNull(line5, "Expected only three lines");
-        GarbageCollectionHelper gcHelper = new GarbageCollectionHelper();
-        gcHelper.run();
-        try {
+        try (GarbageCollectionHelper gcHelper = new GarbageCollectionHelper()) {
+            gcHelper.run();
             assertTrue(latch.await(30, TimeUnit.SECONDS), "Parameter should have been garbage collected");
-        } finally {
-            gcHelper.close();
         }
     }
 
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/ReusableParameterizedMessageMemoryLeakTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/ReusableParameterizedMessageMemoryLeakTest.java
index b162d27..93081ee 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/ReusableParameterizedMessageMemoryLeakTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/ReusableParameterizedMessageMemoryLeakTest.java
@@ -34,12 +34,9 @@ public class ReusableParameterizedMessageMemoryLeakTest {
                 "foo {}", new ParameterObject("paramValue", latch));
         // Large enough for the parameters, but smaller than the default reusable array size.
         message.swapParameters(new Object[5]);
-        GarbageCollectionHelper gcHelper = new GarbageCollectionHelper();
-        gcHelper.run();
-        try {
+        try (GarbageCollectionHelper gcHelper = new GarbageCollectionHelper()) {
+            gcHelper.run();
             assertTrue(latch.await(30, TimeUnit.SECONDS), "Parameter should have been garbage collected");
-        } finally {
-            gcHelper.close();
         }
     }
 
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncAppenderConfigTest_LOG4J2_2032.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncAppenderConfigTest_LOG4J2_2032.java
index f1e7d64..f0e1685 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncAppenderConfigTest_LOG4J2_2032.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncAppenderConfigTest_LOG4J2_2032.java
@@ -50,13 +50,11 @@ public class AsyncAppenderConfigTest_LOG4J2_2032 {
         log.info("Text containing curly braces: {}", "Curly{}");
         CoreLoggerContexts.stopLoggerContext(file); // stop async thread
 
-        final BufferedReader reader = new BufferedReader(new FileReader(file));
-        try {
+        try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
             final String line1 = reader.readLine();
             System.out.println(line1);
             assertTrue("line1 correct", line1.contains(" Text containing curly braces: Curly{} "));
         } finally {
-            reader.close();
             file.delete();
         }
     }
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTestArgumentFreedOnError.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTestArgumentFreedOnError.java
index 3d767fd..6c1487a 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTestArgumentFreedOnError.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/AsyncLoggerTestArgumentFreedOnError.java
@@ -57,13 +57,10 @@ public class AsyncLoggerTestArgumentFreedOnError {
         final AsyncLogger log = (AsyncLogger) LogManager.getLogger("com.foo.Bar");
         CountDownLatch garbageCollectionLatch = new CountDownLatch(1);
         log.fatal(new ThrowingMessage(garbageCollectionLatch));
-        GarbageCollectionHelper gcHelper = new GarbageCollectionHelper();
-        gcHelper.run();
-        try {
+        try (GarbageCollectionHelper gcHelper = new GarbageCollectionHelper()) {
+            gcHelper.run();
             assertTrue("Parameter should have been garbage collected",
                     garbageCollectionLatch.await(30, TimeUnit.SECONDS));
-        } finally {
-            gcHelper.close();
         }
     }
 
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerTest3.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerTest3.java
index 198cb6b..5813bec 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerTest3.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/QueueFullAsyncLoggerTest3.java
@@ -85,12 +85,9 @@ public class QueueFullAsyncLoggerTest3 extends QueueFullAbstractTest {
         }
         blockingAppender.countDownLatch.countDown();
 
-        final GarbageCollectionHelper gcHelper = new GarbageCollectionHelper();
-        gcHelper.run();
-        try {
+        try (GarbageCollectionHelper gcHelper = new GarbageCollectionHelper()) {
+            gcHelper.run();
             assertTrue("Parameter should have been garbage collected", garbageCollectionLatch.await(30, TimeUnit.SECONDS));
-        } finally {
-            gcHelper.close();
         }
     }
 
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/NestedLoggerConfigTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/NestedLoggerConfigTest.java
index fb14bdf..edec79c 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/NestedLoggerConfigTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/NestedLoggerConfigTest.java
@@ -68,14 +68,11 @@ public class NestedLoggerConfigTest {
     }
 
     private Configuration loadConfiguration(String resourcePath) throws IOException {
-        InputStream in = getClass().getClassLoader().getResourceAsStream(resourcePath);
-        try {
+        try (InputStream in = getClass().getClassLoader().getResourceAsStream(resourcePath)) {
             Configuration configuration = new XmlConfiguration(new LoggerContext("test"), new ConfigurationSource(in));
             configuration.initialize();
             configuration.start();
             return configuration;
-        } finally {
-            in.close();
         }
     }
 }
diff --git a/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/NestedLoggingFromThrowableMessageTest.java b/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/NestedLoggingFromThrowableMessageTest.java
index 5f58af7..dd4a5c5 100644
--- a/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/NestedLoggingFromThrowableMessageTest.java
+++ b/log4j-core/src/test/java/org/apache/logging/log4j/core/impl/NestedLoggingFromThrowableMessageTest.java
@@ -87,14 +87,11 @@ public class NestedLoggingFromThrowableMessageTest {
 
     private static Set<String> readUniqueLines(File input) throws IOException {
         Set<String> lines = new HashSet<>();
-        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(input)));
-        try {
+        try (BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(input)))) {
             String line;
             while ((line = reader.readLine()) != null) {
                 assertTrue("Read duplicate line: " + line, lines.add(line));
             }
-        } finally {
-            reader.close();
         }
         return lines;
     }