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.assertThrows;
21 import static org.junit.jupiter.api.Assertions.assertTrue;
22 import static org.junit.jupiter.api.Assumptions.assumeFalse;
23 import static org.junit.jupiter.api.Assumptions.assumeTrue;
24
25 import java.io.File;
26 import java.util.concurrent.TimeUnit;
27
28 import org.apache.commons.exec.AbstractExecTest;
29 import org.apache.commons.exec.CommandLine;
30 import org.apache.commons.exec.DefaultExecutor;
31 import org.apache.commons.exec.ExecuteException;
32 import org.apache.commons.exec.ExecuteWatchdog;
33 import org.apache.commons.exec.PumpStreamHandler;
34 import org.junit.jupiter.api.Test;
35 import org.junit.jupiter.api.Timeout;
36 import org.junit.jupiter.api.condition.DisabledOnOs;
37 import org.junit.jupiter.api.condition.EnabledOnOs;
38
39
40
41
42
43
44 public class Exec65Test extends AbstractExecTest {
45
46
47
48
49
50
51
52
53 @Test
54
55 @EnabledOnOs(value = org.junit.jupiter.api.condition.OS.MAC)
56 @Timeout(value = TEST_TIMEOUT, unit = TimeUnit.MILLISECONDS)
57 public void testExec65WithSleepUsingShellScript() throws Exception {
58 final DefaultExecutor executor = DefaultExecutor.builder().get();
59 executor.setStreamHandler(new PumpStreamHandler(System.out, System.err));
60 executor.setWatchdog(new ExecuteWatchdog(WATCHDOG_TIMEOUT));
61 final CommandLine command = new CommandLine(resolveTestScript("sleep"));
62
63 assertThrows(ExecuteException.class, () -> executor.execute(command));
64 }
65
66
67
68
69 @Test
70 @Timeout(value = TEST_TIMEOUT, unit = TimeUnit.MILLISECONDS)
71 public void testExec65WithSleepUsingShellScriptAndJDKOnly() throws Exception {
72
73 final Process process = Runtime.getRuntime().exec(resolveTestScript("sleep").getAbsolutePath());
74 Thread.sleep(WATCHDOG_TIMEOUT);
75
76 process.destroy();
77
78 process.waitFor();
79
80 assertTrue(process.exitValue() != 0);
81 }
82
83
84
85
86
87 @Test
88 @DisabledOnOs(org.junit.jupiter.api.condition.OS.WINDOWS)
89 @Timeout(value = TEST_TIMEOUT, unit = TimeUnit.MILLISECONDS)
90 public void testExec65WithSudoUsingShellScript() throws Exception {
91 assumeFalse(new File(".").getAbsolutePath().contains("travis"),
92 "Test is skipped on travis, because we have to be a sudoer to make the other tests pass.");
93
94 assumeTrue(System.getenv("GITHUB_WORKFLOW") == null);
95
96 final DefaultExecutor executor = DefaultExecutor.builder().get();
97 executor.setStreamHandler(new PumpStreamHandler(System.out, System.err, System.in));
98 executor.setWatchdog(new ExecuteWatchdog(WATCHDOG_TIMEOUT));
99 final CommandLine command = new CommandLine(resolveTestScript("issues", "exec-65"));
100
101 assertThrows(ExecuteException.class, () -> executor.execute(command));
102 }
103
104 @Test
105 @DisabledOnOs(org.junit.jupiter.api.condition.OS.WINDOWS)
106 @Timeout(value = TEST_TIMEOUT, unit = TimeUnit.MILLISECONDS)
107 public void testExec65WitSleepUsingSleepCommandDirectly() throws Exception {
108 final ExecuteWatchdog watchdog = new ExecuteWatchdog(WATCHDOG_TIMEOUT);
109 final DefaultExecutor executor = DefaultExecutor.builder().get();
110 final CommandLine command = new CommandLine("sleep");
111 command.addArgument("60");
112 executor.setStreamHandler(new PumpStreamHandler(System.out, System.err));
113 executor.setWatchdog(watchdog);
114
115 assertThrows(ExecuteException.class, () -> executor.execute(command));
116 }
117 }