View Javadoc
1   package org.codehaus.plexus.util.cli;
2   
3   /*
4    * Copyright The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.util.Arrays;
20  import java.util.Locale;
21  import java.util.Properties;
22  
23  import org.codehaus.plexus.util.Os;
24  import org.junit.jupiter.api.Test;
25  
26  import static org.junit.jupiter.api.Assertions.assertEquals;
27  import static org.junit.jupiter.api.Assertions.assertNotNull;
28  import static org.junit.jupiter.api.Assertions.fail;
29  
30  /**
31   * <p>CommandLineUtilsTest class.</p>
32   *
33   * @author herve
34   * @version $Id: $Id
35   * @since 3.4.0
36   */
37  @SuppressWarnings({"JavaDoc", "deprecation"})
38  public class CommandLineUtilsTest {
39  
40      /**
41       * <p>testQuoteArguments.</p>
42       */
43      @Test
44      public void testQuoteArguments() {
45          try {
46              String result = CommandLineUtils.quote("Hello");
47              System.out.println(result);
48              assertEquals("Hello", result);
49              result = CommandLineUtils.quote("Hello World");
50              System.out.println(result);
51              assertEquals("\"Hello World\"", result);
52              result = CommandLineUtils.quote("\"Hello World\"");
53              System.out.println(result);
54              assertEquals("\'\"Hello World\"\'", result);
55          } catch (Exception e) {
56              fail(e.getMessage());
57          }
58          try {
59              CommandLineUtils.quote("\"Hello \'World\'\'");
60              fail();
61          } catch (Exception e) {
62          }
63      }
64  
65      /**
66       * Tests that case-insensitive environment variables are normalized to upper case.
67       *
68       * @throws java.lang.Exception if any.
69       */
70      @Test
71      public void testGetSystemEnvVarsCaseInsensitive() throws Exception {
72          Properties vars = CommandLineUtils.getSystemEnvVars(false);
73          for (Object o : vars.keySet()) {
74              String variable = (String) o;
75              assertEquals(variable.toUpperCase(Locale.ENGLISH), variable);
76          }
77      }
78  
79      /**
80       * Tests that environment variables on Windows are normalized to upper case. Does nothing on Unix platforms.
81       *
82       * @throws java.lang.Exception if any.
83       */
84      @Test
85      public void testGetSystemEnvVarsWindows() throws Exception {
86          if (!Os.isFamily(Os.FAMILY_WINDOWS)) {
87              return;
88          }
89          Properties vars = CommandLineUtils.getSystemEnvVars();
90          for (Object o : vars.keySet()) {
91              String variable = (String) o;
92              assertEquals(variable.toUpperCase(Locale.ENGLISH), variable);
93          }
94      }
95  
96      /**
97       * Tests the splitting of a command line into distinct arguments.
98       *
99       * @throws java.lang.Exception if any.
100      */
101     @org.junit.jupiter.api.Test
102     public void testTranslateCommandline() throws Exception {
103         assertCmdLineArgs(new String[] {}, null);
104         assertCmdLineArgs(new String[] {}, "");
105 
106         assertCmdLineArgs(new String[] {"foo", "bar"}, "foo bar");
107         assertCmdLineArgs(new String[] {"foo", "bar"}, "   foo   bar   ");
108 
109         assertCmdLineArgs(new String[] {"foo", " double quotes ", "bar"}, "foo \" double quotes \" bar");
110         assertCmdLineArgs(new String[] {"foo", " single quotes ", "bar"}, "foo ' single quotes ' bar");
111 
112         assertCmdLineArgs(new String[] {"foo", " \" ", "bar"}, "foo ' \" ' bar");
113         assertCmdLineArgs(new String[] {"foo", " ' ", "bar"}, "foo \" ' \" bar");
114     }
115 
116     private void assertCmdLineArgs(String[] expected, String cmdLine) throws Exception {
117         String[] actual = CommandLineUtils.translateCommandline(cmdLine);
118         assertNotNull(actual);
119         assertEquals(expected.length, actual.length);
120         assertEquals(Arrays.asList(expected), Arrays.asList(actual));
121     }
122 }