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