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 static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertFalse;
24  
25  import java.io.ByteArrayOutputStream;
26  import java.io.File;
27  import java.util.HashMap;
28  import java.util.Map;
29  
30  import org.apache.commons.exec.CommandLine;
31  import org.apache.commons.exec.DefaultExecutor;
32  import org.apache.commons.exec.Executor;
33  import org.apache.commons.exec.OS;
34  import org.apache.commons.exec.PumpStreamHandler;
35  import org.apache.commons.exec.TestUtil;
36  import org.apache.commons.lang3.SystemProperties;
37  import org.junit.jupiter.api.AfterEach;
38  import org.junit.jupiter.api.BeforeEach;
39  import org.junit.jupiter.api.Disabled;
40  import org.junit.jupiter.api.Test;
41  import org.junit.jupiter.api.condition.DisabledOnOs;
42  
43  /**
44   * Test EXEC-36 see https://issues.apache.org/jira/browse/EXEC-36
45   */
46  class Exec36Test {
47  
48      private final Executor exec = DefaultExecutor.builder().get();
49      private final File testDir = new File("src/test/scripts");
50      private final File printArgsScript = TestUtil.resolveScriptFileForOS(testDir + "/printargs");
51  
52      private ByteArrayOutputStream baos;
53  
54      @BeforeEach
55      public void setUp() throws Exception {
56          // prepare a ready to Executor
57          this.baos = new ByteArrayOutputStream();
58          this.exec.setStreamHandler(new PumpStreamHandler(baos, baos));
59      }
60  
61      @AfterEach
62      public void tearDown() throws Exception {
63          this.baos.close();
64      }
65  
66      /**
67       *
68       * Original example from Kai Hu which only can be tested on Unix
69       *
70       * @throws Exception the test failed
71       */
72      @Test
73      @DisabledOnOs(org.junit.jupiter.api.condition.OS.WINDOWS)
74      void testExec36Part1() throws Exception {
75          CommandLine cmdl;
76  
77          /**
78           * ./script/jrake cruise:publish_installers INSTALLER_VERSION=unstable_2_1 \ INSTALLER_PATH="/var/lib/ cruise-agent/installers"
79           * INSTALLER_DOWNLOAD_SERVER='something' WITHOUT_HELP_DOC=true
80           */
81          // @formatter:off
82          final String expected =
83                  "./script/jrake\n"
84                  + "cruise:publish_installers\n"
85                  + "INSTALLER_VERSION=unstable_2_1\n"
86                  + "INSTALLER_PATH=\"/var/lib/ cruise-agent/installers\"\n"
87                  + "INSTALLER_DOWNLOAD_SERVER='something'\n"
88                  + "WITHOUT_HELP_DOC=true";
89          // @formatter:on
90          cmdl = new CommandLine(printArgsScript);
91          cmdl.addArgument("./script/jrake", false);
92          cmdl.addArgument("cruise:publish_installers", false);
93          cmdl.addArgument("INSTALLER_VERSION=unstable_2_1", false);
94          cmdl.addArgument("INSTALLER_PATH=\"/var/lib/ cruise-agent/installers\"", false);
95          cmdl.addArgument("INSTALLER_DOWNLOAD_SERVER='something'", false);
96          cmdl.addArgument("WITHOUT_HELP_DOC=true", false);
97  
98          final int exitValue = exec.execute(cmdl);
99          final String result = baos.toString().trim();
100         assertFalse(exec.isFailure(exitValue));
101         assertEquals(expected, result);
102     }
103 
104     /**
105      * Test a complex real example found at https://blogs.msdn.com/b/astebner/archive/2005/12/13/503471.aspx
106      *
107      * The command line is so weird that it even falls apart under Windows
108      *
109      * @throws Exception the test failed
110      */
111     @Test
112     void testExec36Part2() throws Exception {
113 
114         String expected;
115 
116         // the original command line
117         // dotnetfx.exe /q:a /c:"install.exe /l ""\Documents and Settings\myusername\Local Settings\Temp\netfx.log"" /q"
118 
119         if (OS.isFamilyWindows()) {
120             // @formatter:off
121             expected = "dotnetfx.exe\n"
122                     + "/q:a\n"
123                     + "/c:\"install.exe /l \"\"\\Documents and Settings\\myusername\\Local Settings\\Temp\\netfx.log\"\" /q\"";
124             // @formatter:on
125         } else if (OS.isFamilyUnix()) {
126             // @formatter:off
127             expected = "dotnetfx.exe\n"
128                     + "/q:a\n"
129                     + "/c:\"install.exe /l \"\"/Documents and Settings/myusername/Local Settings/Temp/netfx.log\"\" /q\"";
130             // @formatter:on
131         } else {
132             System.err.println("The test 'testExec36_3' does not support the following OS : " + SystemProperties.getOsName());
133             return;
134         }
135 
136         CommandLine cmdl;
137         final File file = new File("/Documents and Settings/myusername/Local Settings/Temp/netfx.log");
138         final Map<String, File> map = new HashMap<>();
139         map.put("FILE", file);
140 
141         cmdl = new CommandLine(printArgsScript);
142         cmdl.setSubstitutionMap(map);
143         cmdl.addArgument("dotnetfx.exe", false);
144         cmdl.addArgument("/q:a", false);
145         cmdl.addArgument("/c:\"install.exe /l \"\"${FILE}\"\" /q\"", false);
146 
147         final int exitValue = exec.execute(cmdl);
148         final String result = baos.toString().trim();
149         assertFalse(exec.isFailure(exitValue));
150 
151         if (OS.isFamilyUnix()) {
152             // the parameters fall literally apart under Windows - need to disable the check for Win32
153             assertEquals(expected, result);
154         }
155     }
156 
157     /**
158      * Some complex real-life command line from https://blogs.msdn.com/b/astebner/archive/2005/12/13/503471.aspx
159      */
160     @Test
161     @Disabled
162     void testExec36Part4() throws Exception {
163         CommandLine cmdl;
164         final String line = "./script/jrake cruise:publish_installers INSTALLER_VERSION=unstable_2_1 "
165                 + "INSTALLER_PATH=\"/var/lib/cruise-agent/installers\" INSTALLER_DOWNLOAD_SERVER='something'WITHOUT_HELP_DOC=true";
166         cmdl = CommandLine.parse(line);
167         final String[] args = cmdl.toStrings();
168         assertEquals("./script/jrake", args[0]);
169         assertEquals("cruise:publish_installers", args[1]);
170         assertEquals("INSTALLER_VERSION=unstable_2_1", args[2]);
171         assertEquals("INSTALLER_PATH=\"/var/lib/cruise-agent/installers\"", args[3]);
172         assertEquals("INSTALLER_DOWNLOAD_SERVER='something'", args[4]);
173         assertEquals("WITHOUT_HELP_DOC=true", args[5]);
174     }
175 
176     /**
177      * Some complex real-life command line from https://blogs.msdn.com/b/astebner/archive/2005/12/13/503471.aspx
178      */
179     @Test
180     @Disabled
181     void testExec36Part5() {
182 
183         CommandLine cmdl;
184 
185         final String line = "dotnetfx.exe /q:a "
186                 + "/c:\"install.exe /l \"\"c:\\Documents and Settings\\myusername\\Local Settings\\Temp\\netfx.log\"\" /q\"";
187 
188         cmdl = CommandLine.parse(line);
189         final String[] args = cmdl.toStrings();
190         assertEquals("dotnetfx.exe", args[0]);
191         assertEquals("/q:a", args[1]);
192         assertEquals("/c:\"install.exe /l \"\"c:\\Documents and Settings\\myusername\\Local Settings\\Temp\\netfx.log\"\" /q\"", args[2]);
193     }
194 
195     /**
196      * Test the following command line
197      *
198      * C:\CVS_DB\WeightsEngine /f WeightsEngine.mak CFG="WeightsEngine - Win32Release"
199      */
200     @Test
201     @Disabled
202     void testExec36Part6() {
203 
204         final String commandLine = "C:\\CVS_DB\\WeightsEngine /f WeightsEngine.mak CFG=\"WeightsEngine - Win32Release\"";
205 
206         final CommandLine cmdl = CommandLine.parse(commandLine);
207         final String[] args = cmdl.getArguments();
208         assertEquals("/f", args[0]);
209         assertEquals("WeightsEngine.mak", args[1]);
210         assertEquals("CFG=\"WeightsEngine - Win32Release\"", args[2]);
211     }
212 }