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