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: [2/2] logging-log4j2 git commit: Use try-with-resources
Date Wed, 21 Oct 2015 07:04:39 GMT
Nice clean ups Matt.

Gary
---------- Forwarded message ----------
From: <mattsicker@apache.org>
Date: Tue, Oct 20, 2015 at 11:25 PM
Subject: [2/2] logging-log4j2 git commit: Use try-with-resources
To: commits@logging.apache.org


Use try-with-resources


Project: http://git-wip-us.apache.org/repos/asf/logging-log4j2/repo
Commit:
http://git-wip-us.apache.org/repos/asf/logging-log4j2/commit/aad67342
Tree: http://git-wip-us.apache.org/repos/asf/logging-log4j2/tree/aad67342
Diff: http://git-wip-us.apache.org/repos/asf/logging-log4j2/diff/aad67342

Branch: refs/heads/master
Commit: aad6734263272df070846170e75720664dab99b0
Parents: b0df3f4
Author: Matt Sicker <boards@gmail.com>
Authored: Wed Oct 21 01:26:08 2015 -0500
Committer: Matt Sicker <boards@gmail.com>
Committed: Wed Oct 21 01:26:08 2015 -0500

----------------------------------------------------------------------
 .../log4j/core/appender/JsonCompleteFileAppenderTest.java |  5 +----
 .../appender/MemoryMappedFileAppenderLocationTest.java    |  5 +----
 .../core/appender/MemoryMappedFileAppenderRemapTest.java  |  5 +----
 .../core/appender/MemoryMappedFileAppenderSimpleTest.java |  5 +----
 .../core/appender/RandomAccessFileAppenderTests.java      |  5 +----
 .../log4j/core/appender/XmlCompactFileAppenderTest.java   |  4 +---
 .../log4j/core/appender/XmlCompleteFileAppenderTest.java  |  4 +---
 .../logging/log4j/core/appender/XmlFileAppenderTest.java  |  4 +---
 .../core/appender/XmlRandomAccessFileAppenderTest.java    |  4 +---
 .../core/appender/db/jdbc/AbstractJdbcAppenderTest.java   |  5 +----
 .../logging/log4j/core/async/perftest/PerfTest.java       |  5 +----
 .../logging/log4j/core/config/ConfigurationTest.java      |  5 +----
 .../logging/log4j/core/net/ssl/SslConfigurationTest.java  | 10 ++--------
 .../java/org/apache/logging/log4j/core/util/Compare.java  |  9 ++-------
 .../logging/log4j/perf/jmh/JdbcAppenderBenchmark.java     |  5 +----
 15 files changed, 17 insertions(+), 63 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/aad67342/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/JsonCompleteFileAppenderTest.java
----------------------------------------------------------------------
diff --git
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/JsonCompleteFileAppenderTest.java
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/JsonCompleteFileAppenderTest.java
index 5d9b6a1..6634fde 100644
---
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/JsonCompleteFileAppenderTest.java
+++
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/JsonCompleteFileAppenderTest.java
@@ -55,16 +55,13 @@ public class JsonCompleteFileAppenderTest {
         String line4;
         String line5;
         String line6;
-        final BufferedReader reader = new BufferedReader(new
FileReader(this.logFile));
-        try {
+        try (final BufferedReader reader = new BufferedReader(new
FileReader(this.logFile))) {
             line1 = reader.readLine();
             line2 = reader.readLine();
             line3 = reader.readLine();
             line4 = reader.readLine();
             line5 = reader.readLine();
             line6 = reader.readLine();
-        } finally {
-            reader.close();
         }
         assertNotNull("line1", line1);
         final String msg1 = "[";

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/aad67342/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderLocationTest.java
----------------------------------------------------------------------
diff --git
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderLocationTest.java
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderLocationTest.java
index cad76bc..54c2cee 100644
---
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderLocationTest.java
+++
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderLocationTest.java
@@ -75,13 +75,10 @@ public class MemoryMappedFileAppenderLocationTest {
         assertEquals("Shrunk to actual used size", 474 + 2 * LINESEP,
f.length());

         String line1, line2, line3;
-        final BufferedReader reader = new BufferedReader(new
FileReader(LOGFILE));
-        try {
+        try (final BufferedReader reader = new BufferedReader(new
FileReader(LOGFILE))) {
             line1 = reader.readLine();
             line2 = reader.readLine();
             line3 = reader.readLine();
-        } finally {
-            reader.close();
         }
         assertNotNull(line1);
         assertThat(line1, containsString("Test log1"));

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/aad67342/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderRemapTest.java
----------------------------------------------------------------------
diff --git
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderRemapTest.java
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderRemapTest.java
index 3257fc6..87e4d35 100644
---
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderRemapTest.java
+++
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderRemapTest.java
@@ -75,14 +75,11 @@ public class MemoryMappedFileAppenderRemapTest {
         assertEquals("Shrunk to actual used size", 658 + 3 * LINESEP,
f.length());

         String line1, line2, line3, line4;
-        final BufferedReader reader = new BufferedReader(new
FileReader(LOGFILE));
-        try {
+        try (final BufferedReader reader = new BufferedReader(new
FileReader(LOGFILE))) {
             line1 = reader.readLine();
             line2 = reader.readLine();
             line3 = reader.readLine();
             line4 = reader.readLine();
-        } finally {
-            reader.close();
         }
         assertNotNull(line1);
         assertThat(line1, containsString("Test log1"));

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/aad67342/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderSimpleTest.java
----------------------------------------------------------------------
diff --git
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderSimpleTest.java
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderSimpleTest.java
index b444203..ccdd9d2 100644
---
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderSimpleTest.java
+++
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/MemoryMappedFileAppenderSimpleTest.java
@@ -70,13 +70,10 @@ public class MemoryMappedFileAppenderSimpleTest {
         assertEquals("Shrunk to actual used size", 186 + 2 * LINESEP,
f.length());

         String line1, line2, line3;
-        final BufferedReader reader = new BufferedReader(new
FileReader(LOGFILE));
-        try {
+        try (final BufferedReader reader = new BufferedReader(new
FileReader(LOGFILE))) {
             line1 = reader.readLine();
             line2 = reader.readLine();
             line3 = reader.readLine();
-        } finally {
-            reader.close();
         }
         assertNotNull(line1);
         assertThat(line1, containsString("Test log1"));

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/aad67342/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/RandomAccessFileAppenderTests.java
----------------------------------------------------------------------
diff --git
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/RandomAccessFileAppenderTests.java
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/RandomAccessFileAppenderTests.java
index 8068d59..c25f666 100644
---
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/RandomAccessFileAppenderTests.java
+++
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/RandomAccessFileAppenderTests.java
@@ -79,11 +79,8 @@ public class RandomAccessFileAppenderTests {
         this.init.getContext().stop(); // stop async thread

         String line;
-        final BufferedReader reader = new BufferedReader(new
FileReader(this.logFile));
-        try {
+        try (final BufferedReader reader = new BufferedReader(new
FileReader(this.logFile))) {
             line = reader.readLine();
-        } finally {
-            reader.close();
         }
         assertNotNull(line);
         assertThat(line, containsString(message));

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/aad67342/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/XmlCompactFileAppenderTest.java
----------------------------------------------------------------------
diff --git
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/XmlCompactFileAppenderTest.java
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/XmlCompactFileAppenderTest.java
index 221c942..f1248b4 100644
---
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/XmlCompactFileAppenderTest.java
+++
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/XmlCompactFileAppenderTest.java
@@ -49,12 +49,10 @@ public class XmlCompactFileAppenderTest {
         log.info(logMsg);
         CoreLoggerContexts.stopLoggerContext(false, file); // stop async
thread

-        final BufferedReader reader = new BufferedReader(new
FileReader(file));
         String line1;
-        try {
+        try (final BufferedReader reader = new BufferedReader(new
FileReader(file))) {
             line1 = reader.readLine();
         } finally {
-            reader.close();
             file.delete();
         }
         assertNotNull("line1", line1);

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/aad67342/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/XmlCompleteFileAppenderTest.java
----------------------------------------------------------------------
diff --git
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/XmlCompleteFileAppenderTest.java
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/XmlCompleteFileAppenderTest.java
index 7378d79..344b221 100644
---
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/XmlCompleteFileAppenderTest.java
+++
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/XmlCompleteFileAppenderTest.java
@@ -50,18 +50,16 @@ public class XmlCompleteFileAppenderTest {
         log.info(logMsg);
         CoreLoggerContexts.stopLoggerContext(false, file); // stop async
thread

-        final BufferedReader reader = new BufferedReader(new
FileReader(file));
         String line1;
         String line2;
         String line3;
         String line4;
-        try {
+        try (final BufferedReader reader = new BufferedReader(new
FileReader(file))) {
             line1 = reader.readLine();
             line2 = reader.readLine();
             line3 = reader.readLine();
             line4 = reader.readLine();
         } finally {
-            reader.close();
             file.delete();
         }
         assertNotNull("line1", line1);

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/aad67342/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/XmlFileAppenderTest.java
----------------------------------------------------------------------
diff --git
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/XmlFileAppenderTest.java
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/XmlFileAppenderTest.java
index ebb1cfe..b509b30 100644
---
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/XmlFileAppenderTest.java
+++
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/XmlFileAppenderTest.java
@@ -50,16 +50,14 @@ public class XmlFileAppenderTest {
         log.info(logMsg);
         CoreLoggerContexts.stopLoggerContext(false, file); // stop async
thread

-        final BufferedReader reader = new BufferedReader(new
FileReader(file));
         String line1;
         String line2;
         String line3;
-        try {
+        try (final BufferedReader reader = new BufferedReader(new
FileReader(file))) {
             line1 = reader.readLine();
             line2 = reader.readLine();
             line3 = reader.readLine();
         } finally {
-            reader.close();
             file.delete();
         }
         assertNotNull("line1", line1);

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/aad67342/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/XmlRandomAccessFileAppenderTest.java
----------------------------------------------------------------------
diff --git
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/XmlRandomAccessFileAppenderTest.java
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/XmlRandomAccessFileAppenderTest.java
index 9397ddd..7d1c2c5 100644
---
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/XmlRandomAccessFileAppenderTest.java
+++
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/XmlRandomAccessFileAppenderTest.java
@@ -52,18 +52,16 @@ public class XmlRandomAccessFileAppenderTest {
         log.info(logMsg);
         CoreLoggerContexts.stopLoggerContext(false, file); // stop async
thread

-        final BufferedReader reader = new BufferedReader(new
FileReader(file));
         String line1;
         String line2;
         String line3;
         String line4;
-        try {
+        try (final BufferedReader reader = new BufferedReader(new
FileReader(file))) {
             line1 = reader.readLine();
             line2 = reader.readLine();
             line3 = reader.readLine();
             line4 = reader.readLine();
         } finally {
-            reader.close();
             file.delete();
         }
         assertNotNull("line1", line1);

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/aad67342/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/jdbc/AbstractJdbcAppenderTest.java
----------------------------------------------------------------------
diff --git
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/jdbc/AbstractJdbcAppenderTest.java
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/jdbc/AbstractJdbcAppenderTest.java
index b66fba8..2d5f6ce 100644
---
a/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/jdbc/AbstractJdbcAppenderTest.java
+++
b/log4j-core/src/test/java/org/apache/logging/log4j/core/appender/db/jdbc/AbstractJdbcAppenderTest.java
@@ -58,11 +58,8 @@ public abstract class AbstractJdbcAppenderTest {

     protected void setUp(final String tableName, final String
configFileName) throws SQLException {
         this.connection = this.newConnection();
-        final Statement statement = this.connection.createStatement();
-        try {
+        try (final Statement statement =
this.connection.createStatement()) {

 statement.executeUpdate(this.toCreateTableSqlString(tableName));
-        } finally {
-            statement.close();
         }

 System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY,
                 "org/apache/logging/log4j/core/appender/db/jdbc/" +
configFileName);

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/aad67342/log4j-core/src/test/java/org/apache/logging/log4j/core/async/perftest/PerfTest.java
----------------------------------------------------------------------
diff --git
a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/perftest/PerfTest.java
b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/perftest/PerfTest.java
index 463c020..4501758 100644
---
a/log4j-core/src/test/java/org/apache/logging/log4j/core/async/perftest/PerfTest.java
+++
b/log4j-core/src/test/java/org/apache/logging/log4j/core/async/perftest/PerfTest.java
@@ -162,12 +162,9 @@ public class PerfTest {
         println(result);

         if (file != null) {
-            final FileWriter writer = new FileWriter(file, true);
-            try {
+            try (final FileWriter writer = new FileWriter(file, true)) {
                 writer.write(result);
                 writer.write(System.getProperty("line.separator"));
-            } finally {
-                writer.close();
             }
         }
     }

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/aad67342/log4j-core/src/test/java/org/apache/logging/log4j/core/config/ConfigurationTest.java
----------------------------------------------------------------------
diff --git
a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/ConfigurationTest.java
b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/ConfigurationTest.java
index 45f5c55..02bc7ac 100644
---
a/log4j-core/src/test/java/org/apache/logging/log4j/core/config/ConfigurationTest.java
+++
b/log4j-core/src/test/java/org/apache/logging/log4j/core/config/ConfigurationTest.java
@@ -137,14 +137,11 @@ public class ConfigurationTest {
         logger.debug("This is test message number {}", random);
         int count = 0;
         String line = Strings.EMPTY;
-        final BufferedReader in = new BufferedReader(new
FileReader(this.logFileName));
-        try {
+        try (final BufferedReader in = new BufferedReader(new
FileReader(this.logFileName))) {
             while (in.ready()) {
                 ++count;
                 line = in.readLine();
             }
-        } finally {
-            in.close();
         }
         assertThat(count, is(equalTo(1)));
         assertThat(line, endsWith(Long.toString(random)));

http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/aad67342/log4j-core/src/test/java/org/apache/logging/log4j/core/net/ssl/SslConfigurationTest.java
----------------------------------------------------------------------
diff --git
a/log4j-core/src/test/java/org/apache/logging/log4j/core/net/ssl/SslConfigurationTest.java
b/log4j-core/src/test/java/org/apache/logging/log4j/core/net/ssl/SslConfigurationTest.java
index 4ed1b2c..5283535 100644
---
a/log4j-core/src/test/java/org/apache/logging/log4j/core/net/ssl/SslConfigurationTest.java
+++
b/log4j-core/src/test/java/org/apache/logging/log4j/core/net/ssl/SslConfigurationTest.java
@@ -50,19 +50,13 @@ public class SslConfigurationTest {
         final TrustStoreConfiguration tsc = new
TrustStoreConfiguration(TestConstants.TRUSTSTORE_FILE, null, null, null);
         final SslConfiguration sc =
SslConfiguration.createSSLConfiguration(null, null, tsc);
         final SSLSocketFactory factory = sc.getSslSocketFactory();
-        final SSLSocket clientSocket = (SSLSocket)
factory.createSocket(TLS_TEST_HOST, TLS_TEST_PORT);
-        try {
-            final OutputStream os = clientSocket.getOutputStream();
-            try {
+        try (final SSLSocket clientSocket = (SSLSocket)
factory.createSocket(TLS_TEST_HOST, TLS_TEST_PORT)) {
+            try (final OutputStream os = clientSocket.getOutputStream()) {
                 os.write("GET config/login_verify2?".getBytes());
                 Assert.fail("Expected IOException");
             } catch (final IOException e) {
                 // Expected, do nothing.
-            } finally {
-                os.close();
             }
-        } finally {
-            clientSocket.close();
         }
     }


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/aad67342/log4j-core/src/test/java/org/apache/logging/log4j/core/util/Compare.java
----------------------------------------------------------------------
diff --git
a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/Compare.java
b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/Compare.java
index 49ea30a..c9fefa4 100644
---
a/log4j-core/src/test/java/org/apache/logging/log4j/core/util/Compare.java
+++
b/log4j-core/src/test/java/org/apache/logging/log4j/core/util/Compare.java
@@ -57,14 +57,9 @@ public class Compare {
                                   final String file1,
                                   final String file2)
         throws IOException {
-        final BufferedReader in1 = new BufferedReader(new
FileReader(file1));
-        final BufferedReader in2 = new BufferedReader(new
InputStreamReader(
-            open(testClass, file2)));
-        try {
+        try (final BufferedReader in1 = new BufferedReader(new
FileReader(file1));
+             final BufferedReader in2 = new BufferedReader(new
InputStreamReader(open(testClass, file2)))) {
             return compare(testClass, file1, file2, in1, in2);
-        } finally {
-            in1.close();
-            in2.close();
         }
     }


http://git-wip-us.apache.org/repos/asf/logging-log4j2/blob/aad67342/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/JdbcAppenderBenchmark.java
----------------------------------------------------------------------
diff --git
a/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/JdbcAppenderBenchmark.java
b/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/JdbcAppenderBenchmark.java
index 148ad2d..4f475b1 100644
---
a/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/JdbcAppenderBenchmark.java
+++
b/log4j-perf/src/main/java/org/apache/logging/log4j/perf/jmh/JdbcAppenderBenchmark.java
@@ -166,11 +166,8 @@ public class JdbcAppenderBenchmark {
     }

     private void createTable(final Connection connection, final String
createSQL) throws SQLException {
-        final Statement statement = connection.createStatement();
-        try {
+        try (final Statement statement = connection.createStatement()) {
             statement.executeUpdate(createSQL);
-        } finally {
-            statement.close();
         }
     }





-- 
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