View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.commons.exec.issues;
21  
22  import java.io.File;
23  import java.io.OutputStream;
24  import java.nio.file.Files;
25  import java.nio.file.Path;
26  import java.util.concurrent.TimeUnit;
27  import java.util.concurrent.TimeoutException;
28  
29  import org.apache.commons.exec.CommandLine;
30  import org.apache.commons.exec.DefaultExecutor;
31  import org.apache.commons.exec.ExecuteWatchdog;
32  import org.apache.commons.exec.PumpStreamHandler;
33  import org.apache.commons.exec.TestUtil;
34  import org.junit.jupiter.api.AfterEach;
35  import org.junit.jupiter.api.BeforeEach;
36  import org.junit.jupiter.api.Disabled;
37  import org.junit.jupiter.api.Test;
38  import org.junit.jupiter.api.Timeout;
39  import org.junit.jupiter.api.condition.DisabledOnOs;
40  
41  /**
42   * @see <a href="https://issues.apache.org/jira/browse/EXEC-62">EXEC-62</a>
43   */
44  class Exec62Test {
45      private Path outputFile;
46  
47      private void execute(final String scriptName) throws Exception {
48          final ExecuteWatchdog watchdog = new ExecuteWatchdog(4000);
49          final CommandLine commandLine = new CommandLine("/bin/sh");
50          final File testScript = TestUtil.resolveScriptFileForOS("./src/test/scripts/issues/" + scriptName);
51  
52          commandLine.addArgument(testScript.getAbsolutePath());
53  
54          final DefaultExecutor executor = DefaultExecutor.builder().get();
55          executor.setExitValues(null); // ignore exit values
56          executor.setWatchdog(watchdog);
57  
58          try (OutputStream fos = Files.newOutputStream(outputFile)) {
59              final PumpStreamHandler streamHandler = new PumpStreamHandler(fos);
60              executor.setStreamHandler(streamHandler);
61              executor.execute(commandLine);
62  
63              if (watchdog.killedProcess()) {
64                  throw new TimeoutException(String.format("Transcode process was killed on timeout %1$s ms, command line %2$s", 4000, commandLine.toString()));
65              }
66          }
67      }
68  
69      @BeforeEach
70      public void setUp() throws Exception {
71          outputFile = Files.createTempFile("foo", ".log");
72      }
73  
74      @AfterEach
75      public void tearDown() throws Exception {
76          Files.delete(outputFile);
77      }
78  
79      @Disabled("Test behaves differently between macOS X and Linux - don't know why")
80      @Test
81      @DisabledOnOs(org.junit.jupiter.api.condition.OS.WINDOWS)
82      @Timeout(value = 10, unit = TimeUnit.SECONDS)
83      void testMe() throws Exception {
84          execute("exec-62");
85      }
86  }