1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.exec.issues;
19
20 import static org.junit.jupiter.api.Assertions.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertTrue;
22
23 import java.io.File;
24
25 import org.apache.commons.exec.CommandLine;
26 import org.apache.commons.exec.DefaultExecuteResultHandler;
27 import org.apache.commons.exec.DefaultExecutor;
28 import org.apache.commons.exec.ExecuteWatchdog;
29 import org.apache.commons.exec.Executor;
30 import org.apache.commons.exec.TestUtil;
31 import org.junit.jupiter.api.Test;
32
33
34
35
36 public class Exec44Test {
37
38 private final Executor exec = DefaultExecutor.builder().get();
39 private final File testDir = new File("src/test/scripts");
40 private final File foreverTestScript = TestUtil.resolveScriptForOS(testDir + "/forever");
41
42
43
44
45
46
47
48
49 @Test
50 public void testExec44() throws Exception {
51
52 final CommandLine cl = new CommandLine(foreverTestScript);
53 final DefaultExecuteResultHandler resultHandler = new DefaultExecuteResultHandler();
54 final ExecuteWatchdog watchdog = new ExecuteWatchdog(ExecuteWatchdog.INFINITE_TIMEOUT);
55
56 exec.setWatchdog(watchdog);
57 exec.execute(cl, resultHandler);
58
59
60 Thread.sleep(5000);
61 assertTrue(watchdog.isWatching(), "The watchdog is watching the process");
62
63
64 watchdog.destroyProcess();
65 assertTrue(watchdog.killedProcess(), "The watchdog has killed the process");
66 assertFalse(watchdog.isWatching(), "The watchdog is no longer watching any process");
67 }
68 }