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;
19  
20  import static org.junit.jupiter.api.Assertions.assertTrue;
21  
22  import java.io.File;
23  import java.util.concurrent.Executors;
24  
25  import org.junit.jupiter.api.Test;
26  import org.junitpioneer.jupiter.SetSystemProperty;
27  
28  /**
29   * Placeholder for mailing list question - provided a minimal test case to answer the question as sel-contained regression test.
30   */
31  @SetSystemProperty(key = "org.apache.commons.exec.lenient", value = "false")
32  @SetSystemProperty(key = "org.apache.commons.exec.debug", value = "true")
33  public class StandAloneTest {
34  
35      @Test
36      public void testDefaultExecutor() throws Exception {
37          if (OS.isFamilyUnix()) {
38              final File testScript = TestUtil.resolveScriptForOS("./src/test/scripts/standalone");
39              final Executor exec = new DefaultExecutor();
40              exec.setStreamHandler(new PumpStreamHandler());
41              final CommandLine cl = new CommandLine(testScript);
42              exec.execute(cl);
43              assertTrue(new File("./target/mybackup.gz").exists());
44          }
45      }
46  
47      @Test
48      public void testDefaultExecutorBuilder() throws Exception {
49          if (OS.isFamilyUnix()) {
50              final File testScript = TestUtil.resolveScriptForOS("./src/test/scripts/standalone");
51              // @formatter:off
52              final Executor exec = DefaultExecutor.builder()
53                      .setThreadFactory(Executors.defaultThreadFactory())
54                      .setExecuteStreamHandler(new PumpStreamHandler())
55                      .setWorkingDirectory(new File("."))
56                      .get();
57              // @formatter:on
58              exec.setStreamHandler(new PumpStreamHandler());
59              final CommandLine cl = new CommandLine(testScript);
60              exec.execute(cl);
61              assertTrue(new File("./target/mybackup.gz").exists());
62          }
63      }
64  
65      @Test
66      public void testDefaultExecutorDefaultBuilder() throws Exception {
67          if (OS.isFamilyUnix()) {
68              final File testScript = TestUtil.resolveScriptForOS("./src/test/scripts/standalone");
69              final Executor exec = DefaultExecutor.builder().get();
70              exec.setStreamHandler(new PumpStreamHandler());
71              final CommandLine cl = new CommandLine(testScript);
72              exec.execute(cl);
73              assertTrue(new File("./target/mybackup.gz").exists());
74          }
75      }
76  }