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.assertNotNull;
22 import static org.junit.jupiter.api.Assertions.assertTrue;
23
24 import java.io.File;
25
26 import org.apache.commons.exec.CommandLine;
27 import org.apache.commons.exec.DefaultExecuteResultHandler;
28 import org.apache.commons.exec.DefaultExecutor;
29 import org.apache.commons.exec.ExecuteWatchdog;
30 import org.apache.commons.exec.Executor;
31 import org.apache.commons.exec.TestUtil;
32 import org.junit.jupiter.api.Test;
33
34
35
36
37 public class Exec34Test {
38
39 private final Executor exec = DefaultExecutor.builder().get();
40 private final File testDir = new File("src/test/scripts");
41 private final File pingScript = TestUtil.resolveScriptForOS(testDir + "/ping");
42
43
44
45
46
47
48
49
50 @Test
51 public void testExec34_1() throws Exception {
52
53 final CommandLine cmdLine = new CommandLine(pingScript);
54 cmdLine.addArgument("10");
55
56 final ExecuteWatchdog watchdog = new ExecuteWatchdog(Integer.MAX_VALUE);
57 final DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();
58 exec.setWatchdog(watchdog);
59 exec.execute(cmdLine, handler);
60 assertTrue(watchdog.isWatching());
61 watchdog.destroyProcess();
62 assertTrue(watchdog.killedProcess(), "Watchdog should have killed the process");
63 assertFalse(watchdog.isWatching(), "Watchdog is no longer watching the process");
64 }
65
66
67
68
69
70
71 @Test
72 public void testExec34_2() throws Exception {
73
74 final CommandLine cmdLine = new CommandLine(pingScript);
75 cmdLine.addArgument("10");
76
77 final ExecuteWatchdog watchdog = new ExecuteWatchdog(5000);
78 final DefaultExecuteResultHandler handler = new DefaultExecuteResultHandler();
79 exec.setWatchdog(watchdog);
80 exec.execute(cmdLine, handler);
81 handler.waitFor();
82 assertTrue(handler.hasResult(), "Process has exited");
83 assertNotNull(handler.getException(), "Process was aborted");
84 assertTrue(watchdog.killedProcess(), "Watchdog should have killed the process");
85 assertFalse(watchdog.isWatching(), "Watchdog is no longer watching the process");
86 }
87 }