commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pascalschumac...@apache.org
Subject commons-io git commit: TrailerTest: replace usages of IOUtils#closeQuietly with try-with-resources statements
Date Sun, 23 Apr 2017 15:12:32 GMT
Repository: commons-io
Updated Branches:
  refs/heads/master f5c46b9fc -> 722a5af4f


TrailerTest: replace usages of IOUtils#closeQuietly with try-with-resources statements

(side effects: close #7, close #21, close #20)


Project: http://git-wip-us.apache.org/repos/asf/commons-io/repo
Commit: http://git-wip-us.apache.org/repos/asf/commons-io/commit/722a5af4
Tree: http://git-wip-us.apache.org/repos/asf/commons-io/tree/722a5af4
Diff: http://git-wip-us.apache.org/repos/asf/commons-io/diff/722a5af4

Branch: refs/heads/master
Commit: 722a5af4f1786599f22e91a34af459883c7a6a8b
Parents: f5c46b9
Author: pascalschumacher <pascalschumacher@gmx.net>
Authored: Sun Apr 23 17:12:14 2017 +0200
Committer: pascalschumacher <pascalschumacher@gmx.net>
Committed: Sun Apr 23 17:12:14 2017 +0200

----------------------------------------------------------------------
 .../org/apache/commons/io/input/TailerTest.java | 32 +++++---------------
 1 file changed, 7 insertions(+), 25 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/commons-io/blob/722a5af4/src/test/java/org/apache/commons/io/input/TailerTest.java
----------------------------------------------------------------------
diff --git a/src/test/java/org/apache/commons/io/input/TailerTest.java b/src/test/java/org/apache/commons/io/input/TailerTest.java
index 5818f26..df16f45 100644
--- a/src/test/java/org/apache/commons/io/input/TailerTest.java
+++ b/src/test/java/org/apache/commons/io/input/TailerTest.java
@@ -125,11 +125,9 @@ public class TailerTest extends FileBasedTestCase {
         final Thread thread = new Thread(tailer);
         thread.start();
 
-        Writer out = new OutputStreamWriter(new FileOutputStream(file), charsetUTF8);
-        BufferedReader reader = null;
-        try{
+        try (Writer out = new OutputStreamWriter(new FileOutputStream(file), charsetUTF8);
+             BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(origin),
charsetUTF8))) {
             List<String> lines = new ArrayList<>();
-            reader = new BufferedReader(new InputStreamReader(new FileInputStream(origin),
charsetUTF8));
             String line;
             while((line = reader.readLine()) != null){
                 out.write(line);
@@ -151,10 +149,8 @@ public class TailerTest extends FileBasedTestCase {
                            + "\nAct: (" + actual.length() + ") "+ actual);
                }
            }
-        }finally{
+        } finally{
             tailer.stop();
-            IOUtils.closeQuietly(reader);
-            IOUtils.closeQuietly(out);
         }
     }
 
@@ -170,7 +166,6 @@ public class TailerTest extends FileBasedTestCase {
         thread.start();
 
         // Write some lines to the file
-        final FileWriter writer = null;
         try {
             writeString(file, "Line");
 
@@ -189,7 +184,6 @@ public class TailerTest extends FileBasedTestCase {
         } finally {
             tailer.stop();
             TestUtils.sleep(delay * 2);
-            IOUtils.closeQuietly(writer);
         }
     }
 
@@ -298,12 +292,9 @@ public class TailerTest extends FileBasedTestCase {
             throw new IOException("Cannot create file " + file
                     + " as the parent directory does not exist");
         }
-        final BufferedOutputStream output =
-                new BufferedOutputStream(new FileOutputStream(file));
-        try {
+        try (final BufferedOutputStream output =
+                new BufferedOutputStream(new FileOutputStream(file))) {
             TestUtils.generateTestData(output, size);
-        } finally {
-            IOUtils.closeQuietly(output);
         }
 
         // try to make sure file is found
@@ -328,31 +319,22 @@ public class TailerTest extends FileBasedTestCase {
 
     /** Append some lines to a file */
     private void write(final File file, final String... lines) throws Exception {
-        FileWriter writer = null;
-        try {
-            writer = new FileWriter(file, true);
+        try (FileWriter writer = new FileWriter(file, true)) {
             for (final String line : lines) {
                 writer.write(line + "\n");
             }
-        } finally {
-            IOUtils.closeQuietly(writer);
         }
     }
 
     /** Append a string to a file */
     private void writeString(final File file, final String ... strings) throws Exception
{
-        FileWriter writer = null;
-        try {
-            writer = new FileWriter(file, true);
+        try (FileWriter writer = new FileWriter(file, true)) {
             for (final String string : strings) {
                 writer.write(string);
             }
-        } finally {
-            IOUtils.closeQuietly(writer);
         }
     }
 
-
     @Test
     public void testStopWithNoFile() throws Exception {
         final File file = new File(getTestDirectory(),"nosuchfile");


Mime
View raw message