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

ggregory pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/commons-exec.git

commit 359074f6d0916959761bf40b8f55872be0e7b275
Author: Gary Gregory <[email protected]>
AuthorDate: Sun Dec 31 15:47:15 2023 -0500

    Use tryt-with-resources
---
 .../apache/commons/exec/DefaultExecutorTest.java   | 27 +++++++++++-----------
 .../org/apache/commons/exec/issues/Exec49Test.java | 26 +++++----------------
 2 files changed, 19 insertions(+), 34 deletions(-)

diff --git a/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java 
b/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java
index 53b0b48b..3b5a83b8 100644
--- a/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java
+++ b/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java
@@ -137,15 +137,13 @@ public class DefaultExecutorTest {
     }
 
     private String readFile(final File file) throws Exception {
-
         String text;
         final StringBuilder contents = new StringBuilder();
-        final BufferedReader reader = new BufferedReader(new FileReader(file));
-
-        while ((text = reader.readLine()) != null) {
-            contents.append(text).append(System.lineSeparator());
+        try (BufferedReader reader = new BufferedReader(new FileReader(file))) 
{
+            while ((text = reader.readLine()) != null) {
+                contents.append(text).append(System.lineSeparator());
+            }
         }
-        reader.close();
         return contents.toString();
     }
 
@@ -642,14 +640,15 @@ public class DefaultExecutorTest {
     @Test
     public void testExecuteWithRedirectedStreams() throws Exception {
         if (OS.isFamilyUnix()) {
-            final FileInputStream fis = new FileInputStream("./NOTICE.txt");
-            final CommandLine cl = new CommandLine(redirectScript);
-            final PumpStreamHandler pumpStreamHandler = new 
PumpStreamHandler(baos, baos, fis);
-            final DefaultExecutor executor = DefaultExecutor.builder().get();
-            executor.setWorkingDirectory(new File("."));
-            executor.setStreamHandler(pumpStreamHandler);
-            final int exitValue = executor.execute(cl);
-            fis.close();
+            final int exitValue;
+            try (FileInputStream fis = new FileInputStream("./NOTICE.txt")) {
+                final CommandLine cl = new CommandLine(redirectScript);
+                final PumpStreamHandler pumpStreamHandler = new 
PumpStreamHandler(baos, baos, fis);
+                final DefaultExecutor executor = 
DefaultExecutor.builder().get();
+                executor.setWorkingDirectory(new File("."));
+                executor.setStreamHandler(pumpStreamHandler);
+                exitValue = executor.execute(cl);
+            }
             final String result = baos.toString().trim();
             assertTrue(result.indexOf("Finished reading from stdin") > 0, 
result);
             assertFalse(exec.isFailure(exitValue), () -> "exitValue=" + 
exitValue);
diff --git a/src/test/java/org/apache/commons/exec/issues/Exec49Test.java 
b/src/test/java/org/apache/commons/exec/issues/Exec49Test.java
index aa26e85a..725cb04e 100644
--- a/src/test/java/org/apache/commons/exec/issues/Exec49Test.java
+++ b/src/test/java/org/apache/commons/exec/issues/Exec49Test.java
@@ -46,31 +46,24 @@ public class Exec49Test {
      */
     @Test
     public void testExec49_1() throws Exception {
-
         if (OS.isFamilyUnix()) {
-
             final CommandLine cl = CommandLine.parse("/bin/ls");
             cl.addArgument("/opt");
-
             // redirect stdout/stderr to pipedOutputStream
             final PipedOutputStream pipedOutputStream = new 
PipedOutputStream();
             final PumpStreamHandler psh = new 
PumpStreamHandler(pipedOutputStream);
             exec.setStreamHandler(psh);
-
             // start an asynchronous process to enable the main thread
             System.out.println("Preparing to execute process - commandLine=" + 
cl.toString());
             final DefaultExecuteResultHandler handler = new 
DefaultExecuteResultHandler();
             exec.execute(cl, handler);
             System.out.println("Process spun off successfully - process=" + 
cl.getExecutable());
-
-            int x;
-            final PipedInputStream pis = new 
PipedInputStream(pipedOutputStream);
-            while ((x = pis.read()) >= 0) {
+            try (PipedInputStream pis = new 
PipedInputStream(pipedOutputStream)) {
+                while (pis.read() >= 0) {
 //                 System.out.println("pis.available() " + pis.available());
 //                 System.out.println("x " + x);
+                }
             }
-            pis.close();
-
             handler.waitFor(WAIT);
             handler.getExitValue(); // will fail if process has not finished
         }
@@ -84,31 +77,24 @@ public class Exec49Test {
      */
     @Test
     public void testExec49_2() throws Exception {
-
         if (OS.isFamilyUnix()) {
-
             final CommandLine cl = CommandLine.parse("/bin/ls");
             cl.addArgument("/opt");
-
             // redirect only stdout to pipedOutputStream
             final PipedOutputStream pipedOutputStream = new 
PipedOutputStream();
             final PumpStreamHandler psh = new 
PumpStreamHandler(pipedOutputStream, new ByteArrayOutputStream());
             exec.setStreamHandler(psh);
-
             // start an asynchronous process to enable the main thread
             System.out.println("Preparing to execute process - commandLine=" + 
cl.toString());
             final DefaultExecuteResultHandler handler = new 
DefaultExecuteResultHandler();
             exec.execute(cl, handler);
             System.out.println("Process spun off successfully - process=" + 
cl.getExecutable());
-
-            int x;
-            final PipedInputStream pis = new 
PipedInputStream(pipedOutputStream);
-            while ((x = pis.read()) >= 0) {
+            try (PipedInputStream pis = new 
PipedInputStream(pipedOutputStream)) {
+                while (pis.read() >= 0) {
 //                 System.out.println("pis.available() " + pis.available());
 //                 System.out.println("x " + x);
+                }
             }
-            pis.close();
-
             handler.waitFor(WAIT);
             handler.getExitValue(); // will fail if process has not finished
         }

Reply via email to