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