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
The following commit(s) were added to refs/heads/master by this push:
new 63d5624 Port some test code from IO to NIO APIs
63d5624 is described below
commit 63d5624bb64166b4bb24936278131cc162f8523d
Author: Gary Gregory <[email protected]>
AuthorDate: Fri Jan 27 16:20:11 2023 -0500
Port some test code from IO to NIO APIs
---
.../apache/commons/exec/DefaultExecutorTest.java | 56 ++++++++++++++--------
.../org/apache/commons/exec/issues/Exec62Test.java | 38 ++++++++-------
2 files changed, 58 insertions(+), 36 deletions(-)
diff --git a/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java
b/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java
index 250b2c7..a6317a4 100644
--- a/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java
+++ b/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java
@@ -18,16 +18,33 @@
package org.apache.commons.exec;
-import org.apache.commons.exec.environment.EnvironmentUtils;
-import org.junit.*;
-import org.junit.jupiter.api.function.Executable;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+import static org.junit.jupiter.api.Assertions.assertThrows;
-import java.io.*;
+import java.io.BufferedReader;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileReader;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-import static org.junit.Assert.*;
+import org.apache.commons.exec.environment.EnvironmentUtils;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Ignore;
+import org.junit.Test;
/**
*/
@@ -610,19 +627,20 @@ public class DefaultExecutorTest {
* @throws Exception the test failed
*/
@Test
- public void testExecuteWithRedirectOutErr() throws Exception {
- final File outfile = File.createTempFile("EXEC", ".test");
- outfile.deleteOnExit();
- final CommandLine cl = new CommandLine(testScript);
- try (FileOutputStream outAndErr = new FileOutputStream(outfile)) {
- final PumpStreamHandler pumpStreamHandler = new
PumpStreamHandler(outAndErr);
- final DefaultExecutor executor = new DefaultExecutor();
- executor.setStreamHandler(pumpStreamHandler);
- final int exitValue = executor.execute(cl);
- assertFalse(exec.isFailure(exitValue));
- assertTrue(outfile.exists());
- }
- }
+ public void testExecuteWithRedirectOutErr() throws Exception {
+ final Path outFile = Files.createTempFile("EXEC", ".test");
+ final CommandLine cl = new CommandLine(testScript);
+ try (OutputStream outAndErr = Files.newOutputStream(outFile)) {
+ final PumpStreamHandler pumpStreamHandler = new
PumpStreamHandler(outAndErr);
+ final DefaultExecutor executor = new DefaultExecutor();
+ executor.setStreamHandler(pumpStreamHandler);
+ final int exitValue = executor.execute(cl);
+ assertFalse(exec.isFailure(exitValue));
+ assertTrue(Files.exists(outFile));
+ } finally {
+ Files.delete(outFile);
+ }
+ }
/**
* A generic test case to print the command line arguments to 'printargs'
script to solve
diff --git a/src/test/java/org/apache/commons/exec/issues/Exec62Test.java
b/src/test/java/org/apache/commons/exec/issues/Exec62Test.java
index daa4ae1..4fe0def 100644
--- a/src/test/java/org/apache/commons/exec/issues/Exec62Test.java
+++ b/src/test/java/org/apache/commons/exec/issues/Exec62Test.java
@@ -17,42 +17,48 @@
package org.apache.commons.exec.issues;
-import org.apache.commons.exec.*;
+import java.io.File;
+import java.io.OutputStream;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.concurrent.TimeoutException;
+
+import org.apache.commons.exec.CommandLine;
+import org.apache.commons.exec.DefaultExecutor;
+import org.apache.commons.exec.ExecuteWatchdog;
+import org.apache.commons.exec.OS;
+import org.apache.commons.exec.PumpStreamHandler;
+import org.apache.commons.exec.TestUtil;
import org.junit.After;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.util.concurrent.TimeoutException;
-
/**
* @see <a href="https://issues.apache.org/jira/browse/EXEC-62">EXEC-62</a>
*/
-public class Exec62Test
-{
- private File outputFile;
+public class Exec62Test {
+ private Path outputFile;
@Before
public void setUp() throws Exception {
- outputFile = File.createTempFile("foo", ".log");
+ outputFile = Files.createTempFile("foo", ".log");
}
@After
public void tearDown() throws Exception {
- outputFile.delete();
+ Files.delete(outputFile);
}
@Ignore("Test behaves differently between Mac OS X and Linux - don't know
why")
- @Test (expected = TimeoutException.class, timeout = 10000)
+ @Test(expected = TimeoutException.class, timeout = 10000)
public void testMe() throws Exception {
- if(OS.isFamilyUnix()) {
- execute ("exec-62");
+ if (OS.isFamilyUnix()) {
+ execute("exec-62");
}
}
- private void execute (final String scriptName) throws Exception {
+ private void execute(final String scriptName) throws Exception {
final ExecuteWatchdog watchdog = new ExecuteWatchdog(4000);
final CommandLine commandLine = new CommandLine("/bin/sh");
final File testScript =
TestUtil.resolveScriptForOS("./src/test/scripts/issues/" + scriptName);
@@ -63,7 +69,7 @@ public class Exec62Test
executor.setExitValues(null); // ignore exit values
executor.setWatchdog(watchdog);
- final FileOutputStream fos = new FileOutputStream(outputFile);
+ final OutputStream fos = Files.newOutputStream(outputFile);
final PumpStreamHandler streamHandler = new PumpStreamHandler(fos);
executor.setStreamHandler(streamHandler);
executor.execute(commandLine);
@@ -73,5 +79,3 @@ public class Exec62Test
}
}
}
-
-