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