1 package org.codehaus.plexus.util.cli;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
32
33
34
35
36
37 @SuppressWarnings({"JavaDoc", "deprecation"})
38 public class CommandLineUtilsTest {
39
40
41
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
67
68
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
81
82
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
98
99
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 }