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