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 e4c8c6028d26bc0f0582a9a1409550b8cb816d31 Author: Gary Gregory <[email protected]> AuthorDate: Tue Nov 25 07:19:28 2025 -0500 Sort members --- .../org/apache/commons/exec/TimeoutObserver.java | 14 +-- .../apache/commons/exec/DefaultExecutorTest.java | 72 ++++++------- .../org/apache/commons/exec/issues/Exec36Test.java | 112 ++++++++++----------- 3 files changed, 99 insertions(+), 99 deletions(-) diff --git a/src/main/java/org/apache/commons/exec/TimeoutObserver.java b/src/main/java/org/apache/commons/exec/TimeoutObserver.java index 4bca85ed..06634a77 100644 --- a/src/main/java/org/apache/commons/exec/TimeoutObserver.java +++ b/src/main/java/org/apache/commons/exec/TimeoutObserver.java @@ -28,13 +28,6 @@ import java.util.function.Consumer; */ public interface TimeoutObserver extends Consumer<Watchdog> { - /** - * Called when the watchdog times out. - * - * @param w the watchdog that timed out. - */ - void timeoutOccured(Watchdog w); - /** * {@inheritDoc} * @@ -44,4 +37,11 @@ public interface TimeoutObserver extends Consumer<Watchdog> { default void accept(final Watchdog w) { timeoutOccured(w); } + + /** + * Called when the watchdog times out. + * + * @param w the watchdog that timed out. + */ + void timeoutOccured(Watchdog w); } diff --git a/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java b/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java index 1f6626b3..cff7086f 100644 --- a/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java +++ b/src/test/java/org/apache/commons/exec/DefaultExecutorTest.java @@ -92,42 +92,6 @@ class DefaultExecutorTest { private final Path environmentSript = TestUtil.resolveScriptPathForOS(testDir + "/environment"); // private final File wrapperScript = TestUtil.resolveScriptForOS(testDir + "/wrapper"); - /** - * Start any processes in a loop to make sure that we do not leave any handles/resources open. - * - * @throws Exception the test failed - */ - @Test - @Disabled - void testExecuteStability() throws Exception { - - // make a plain-vanilla test - for (int i = 0; i < 100; i++) { - final Map<String, String> env = new HashMap<>(); - env.put("TEST_ENV_VAR", Integer.toString(i)); - final CommandLine cl = new CommandLine(testScript); - final int exitValue = exec.execute(cl, env); - assertFalse(exec.isFailure(exitValue)); - assertEquals("FOO." + i + ".", baos.toString().trim()); - baos.reset(); - } - - // now be nasty and use the watchdog to kill out sub-processes - for (int i = 0; i < 100; i++) { - final Map<String, String> env = new HashMap<>(); - env.put("TEST_ENV_VAR", Integer.toString(i)); - final DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); - final CommandLine cl = new CommandLine(foreverTestScript); - final ExecuteWatchdog watchdog = new ExecuteWatchdog(500); - exec.setWatchdog(watchdog); - exec.execute(cl, env, resultHandler); - resultHandler.waitFor(WAITFOR_TIMEOUT); - assertTrue(resultHandler.hasResult(), "ResultHandler received a result"); - assertNotNull(resultHandler.getException()); - baos.reset(); - } - } - private int getOccurrences(final String data, final char c) { int result = 0; @@ -419,6 +383,42 @@ class DefaultExecutorTest { assertThrows(IOException.class, () -> executor.execute(cl)); } + /** + * Start any processes in a loop to make sure that we do not leave any handles/resources open. + * + * @throws Exception the test failed + */ + @Test + @Disabled + void testExecuteStability() throws Exception { + + // make a plain-vanilla test + for (int i = 0; i < 100; i++) { + final Map<String, String> env = new HashMap<>(); + env.put("TEST_ENV_VAR", Integer.toString(i)); + final CommandLine cl = new CommandLine(testScript); + final int exitValue = exec.execute(cl, env); + assertFalse(exec.isFailure(exitValue)); + assertEquals("FOO." + i + ".", baos.toString().trim()); + baos.reset(); + } + + // now be nasty and use the watchdog to kill out sub-processes + for (int i = 0; i < 100; i++) { + final Map<String, String> env = new HashMap<>(); + env.put("TEST_ENV_VAR", Integer.toString(i)); + final DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler(); + final CommandLine cl = new CommandLine(foreverTestScript); + final ExecuteWatchdog watchdog = new ExecuteWatchdog(500); + exec.setWatchdog(watchdog); + exec.execute(cl, env, resultHandler); + resultHandler.waitFor(WAITFOR_TIMEOUT); + assertTrue(resultHandler.hasResult(), "ResultHandler received a result"); + assertNotNull(resultHandler.getException()); + baos.reset(); + } + } + /** * Start a script looping forever (asynchronously) and check if the ExecuteWatchdog kicks in killing the runaway process. To make killing a process more * testable the "forever" scripts write each second a '.' into "./target/forever.txt" (a marker file). After a test run we should have a few dots in there. diff --git a/src/test/java/org/apache/commons/exec/issues/Exec36Test.java b/src/test/java/org/apache/commons/exec/issues/Exec36Test.java index bfaeb2dc..a06e4f9c 100644 --- a/src/test/java/org/apache/commons/exec/issues/Exec36Test.java +++ b/src/test/java/org/apache/commons/exec/issues/Exec36Test.java @@ -51,62 +51,6 @@ class Exec36Test { private ByteArrayOutputStream baos; - /** - * Some complex real-life command line from https://blogs.msdn.com/b/astebner/archive/2005/12/13/503471.aspx - */ - @Test - @Disabled - void testExec36Part4() throws Exception { - CommandLine cmdl; - final String line = "./script/jrake cruise:publish_installers INSTALLER_VERSION=unstable_2_1 " - + "INSTALLER_PATH=\"/var/lib/cruise-agent/installers\" INSTALLER_DOWNLOAD_SERVER='something'WITHOUT_HELP_DOC=true"; - cmdl = CommandLine.parse(line); - final String[] args = cmdl.toStrings(); - assertEquals("./script/jrake", args[0]); - assertEquals("cruise:publish_installers", args[1]); - assertEquals("INSTALLER_VERSION=unstable_2_1", args[2]); - assertEquals("INSTALLER_PATH=\"/var/lib/cruise-agent/installers\"", args[3]); - assertEquals("INSTALLER_DOWNLOAD_SERVER='something'", args[4]); - assertEquals("WITHOUT_HELP_DOC=true", args[5]); - } - - /** - * Some complex real-life command line from https://blogs.msdn.com/b/astebner/archive/2005/12/13/503471.aspx - */ - @Test - @Disabled - void testExec36Part5() { - - CommandLine cmdl; - - final String line = "dotnetfx.exe /q:a " - + "/c:\"install.exe /l \"\"c:\\Documents and Settings\\myusername\\Local Settings\\Temp\\netfx.log\"\" /q\""; - - cmdl = CommandLine.parse(line); - final String[] args = cmdl.toStrings(); - assertEquals("dotnetfx.exe", args[0]); - assertEquals("/q:a", args[1]); - assertEquals("/c:\"install.exe /l \"\"c:\\Documents and Settings\\myusername\\Local Settings\\Temp\\netfx.log\"\" /q\"", args[2]); - } - - /** - * Test the following command line - * - * C:\CVS_DB\WeightsEngine /f WeightsEngine.mak CFG="WeightsEngine - Win32Release" - */ - @Test - @Disabled - void testExec36Part6() { - - final String commandLine = "C:\\CVS_DB\\WeightsEngine /f WeightsEngine.mak CFG=\"WeightsEngine - Win32Release\""; - - final CommandLine cmdl = CommandLine.parse(commandLine); - final String[] args = cmdl.getArguments(); - assertEquals("/f", args[0]); - assertEquals("WeightsEngine.mak", args[1]); - assertEquals("CFG=\"WeightsEngine - Win32Release\"", args[2]); - } - @BeforeEach public void setUp() throws Exception { // prepare a ready to Executor @@ -209,4 +153,60 @@ class Exec36Test { assertEquals(expected, result); } } + + /** + * Some complex real-life command line from https://blogs.msdn.com/b/astebner/archive/2005/12/13/503471.aspx + */ + @Test + @Disabled + void testExec36Part4() throws Exception { + CommandLine cmdl; + final String line = "./script/jrake cruise:publish_installers INSTALLER_VERSION=unstable_2_1 " + + "INSTALLER_PATH=\"/var/lib/cruise-agent/installers\" INSTALLER_DOWNLOAD_SERVER='something'WITHOUT_HELP_DOC=true"; + cmdl = CommandLine.parse(line); + final String[] args = cmdl.toStrings(); + assertEquals("./script/jrake", args[0]); + assertEquals("cruise:publish_installers", args[1]); + assertEquals("INSTALLER_VERSION=unstable_2_1", args[2]); + assertEquals("INSTALLER_PATH=\"/var/lib/cruise-agent/installers\"", args[3]); + assertEquals("INSTALLER_DOWNLOAD_SERVER='something'", args[4]); + assertEquals("WITHOUT_HELP_DOC=true", args[5]); + } + + /** + * Some complex real-life command line from https://blogs.msdn.com/b/astebner/archive/2005/12/13/503471.aspx + */ + @Test + @Disabled + void testExec36Part5() { + + CommandLine cmdl; + + final String line = "dotnetfx.exe /q:a " + + "/c:\"install.exe /l \"\"c:\\Documents and Settings\\myusername\\Local Settings\\Temp\\netfx.log\"\" /q\""; + + cmdl = CommandLine.parse(line); + final String[] args = cmdl.toStrings(); + assertEquals("dotnetfx.exe", args[0]); + assertEquals("/q:a", args[1]); + assertEquals("/c:\"install.exe /l \"\"c:\\Documents and Settings\\myusername\\Local Settings\\Temp\\netfx.log\"\" /q\"", args[2]); + } + + /** + * Test the following command line + * + * C:\CVS_DB\WeightsEngine /f WeightsEngine.mak CFG="WeightsEngine - Win32Release" + */ + @Test + @Disabled + void testExec36Part6() { + + final String commandLine = "C:\\CVS_DB\\WeightsEngine /f WeightsEngine.mak CFG=\"WeightsEngine - Win32Release\""; + + final CommandLine cmdl = CommandLine.parse(commandLine); + final String[] args = cmdl.getArguments(); + assertEquals("/f", args[0]); + assertEquals("WeightsEngine.mak", args[1]); + assertEquals("CFG=\"WeightsEngine - Win32Release\"", args[2]); + } }
