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    *      https://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;
19  
20  import static org.junit.jupiter.api.Assertions.assertTrue;
21  
22  import java.io.File;
23  import java.nio.file.Path;
24  import java.nio.file.Paths;
25  import java.util.concurrent.Executors;
26  
27  import org.junit.jupiter.api.Test;
28  import org.junit.jupiter.api.condition.DisabledOnOs;
29  import org.junitpioneer.jupiter.SetSystemProperty;
30  
31  /**
32   * Placeholder for mailing list question - provided a minimal test case to answer the question as sel-contained regression test.
33   */
34  @SetSystemProperty(key = "org.apache.commons.exec.lenient", value = "false")
35  @SetSystemProperty(key = "org.apache.commons.exec.debug", value = "true")
36  public class StandAloneTest {
37  
38      @Test
39      @DisabledOnOs(org.junit.jupiter.api.condition.OS.WINDOWS)
40      public void testDefaultExecutorBuilderFromFile() throws Exception {
41          final Path testScript = TestUtil.resolveScriptPathForOS("./src/test/scripts/standalone");
42          // @formatter:off
43          final Executor exec = DefaultExecutor.builder()
44                  .setThreadFactory(Executors.defaultThreadFactory())
45                  .setExecuteStreamHandler(new PumpStreamHandler())
46                  .setWorkingDirectory(new File("."))
47                  .get();
48          // @formatter:on
49          exec.setStreamHandler(new PumpStreamHandler());
50          final CommandLine cl = new CommandLine(testScript);
51          exec.execute(cl);
52          assertTrue(new File("./target/mybackup.gz").exists());
53      }
54  
55      @Test
56      @DisabledOnOs(org.junit.jupiter.api.condition.OS.WINDOWS)
57      public void testDefaultExecutorBuilderFromPath() throws Exception {
58          final Path testScript = TestUtil.resolveScriptPathForOS("./src/test/scripts/standalone");
59          // @formatter:off
60          final Executor exec = DefaultExecutor.builder()
61                  .setThreadFactory(Executors.defaultThreadFactory())
62                  .setExecuteStreamHandler(new PumpStreamHandler())
63                  .setWorkingDirectory(Paths.get("."))
64                  .get();
65          // @formatter:on
66          exec.setStreamHandler(new PumpStreamHandler());
67          final CommandLine cl = new CommandLine(testScript);
68          exec.execute(cl);
69          assertTrue(new File("./target/mybackup.gz").exists());
70      }
71  
72      @Test
73      @DisabledOnOs(org.junit.jupiter.api.condition.OS.WINDOWS)
74      public void testDefaultExecutorDefaultBuilder() throws Exception {
75          final Path testScript = TestUtil.resolveScriptPathForOS("./src/test/scripts/standalone");
76          final Executor exec = DefaultExecutor.builder().get();
77          exec.setStreamHandler(new PumpStreamHandler());
78          final CommandLine cl = new CommandLine(testScript);
79          exec.execute(cl);
80          assertTrue(new File("./target/mybackup.gz").exists());
81      }
82  
83      @Test
84      @DisabledOnOs(org.junit.jupiter.api.condition.OS.WINDOWS)
85      public void testDefaultExecutorFromFile() throws Exception {
86          final Path testScript = TestUtil.resolveScriptPathForOS("./src/test/scripts/standalone");
87          final Executor exec = new DefaultExecutor();
88          exec.setStreamHandler(new PumpStreamHandler());
89          final CommandLine cl = new CommandLine(testScript);
90          exec.execute(cl);
91          assertTrue(new File("./target/mybackup.gz").exists());
92      }
93  
94      @Test
95      @DisabledOnOs(org.junit.jupiter.api.condition.OS.WINDOWS)
96      public void testDefaultExecutorFromPath() throws Exception {
97          final Path testScript = TestUtil.resolveScriptPathForOS("./src/test/scripts/standalone");
98          final Executor exec = new DefaultExecutor();
99          exec.setStreamHandler(new PumpStreamHandler());
100         final CommandLine cl = new CommandLine(testScript);
101         exec.execute(cl);
102         assertTrue(new File("./target/mybackup.gz").exists());
103     }
104 }