1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.exec.environment;
19
20 import static org.junit.jupiter.api.Assertions.assertArrayEquals;
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertFalse;
23 import static org.junit.jupiter.api.Assertions.assertNotNull;
24 import static org.junit.jupiter.api.Assertions.assertNull;
25
26 import java.io.IOException;
27 import java.util.Arrays;
28 import java.util.HashMap;
29 import java.util.Locale;
30 import java.util.Map;
31 import java.util.Map.Entry;
32
33 import org.apache.commons.exec.OS;
34 import org.junit.jupiter.api.Test;
35
36
37
38
39 public class EnvironmentUtilsTest {
40
41
42
43
44
45
46
47 @Test
48 public void testCaseInsensitiveVariableLookup() throws Exception {
49 final Map<String, String> procEnvironment = EnvironmentUtils.getProcEnvironment();
50
51 EnvironmentUtils.addVariableToEnvironment(procEnvironment, "foo=bAr");
52 assertEquals("bAr", procEnvironment.get("foo"));
53 }
54
55
56
57
58
59
60 @Test
61 public void testGetProcEnvironment() throws IOException {
62 final Map<String, String> procEnvironment = EnvironmentUtils.getProcEnvironment();
63
64
65 assertFalse(procEnvironment.isEmpty(), "Expecting non-zero environment size");
66 final String[] envArgs = EnvironmentUtils.toStrings(procEnvironment);
67 for (int i = 0; i < envArgs.length; i++) {
68 assertNotNull(envArgs[i], "Entry " + i + " should not be null");
69 assertFalse(envArgs[i].isEmpty(), "Entry " + i + " should not be empty");
70
71 }
72 }
73
74
75
76
77
78
79
80 @Test
81 public void testGetProcEnvironmentCaseInsensitiveLookup() throws IOException {
82
83 if (!OS.isFamilyWindows()) {
84 return;
85 }
86
87
88 final Map<String, String> procEnvironment = EnvironmentUtils.getProcEnvironment();
89 for (final Entry<String, String> entry : procEnvironment.entrySet()) {
90 final String key = entry.getKey();
91 final String value = entry.getValue();
92 assertEquals(value, procEnvironment.get(key.toLowerCase(Locale.ENGLISH)));
93 assertEquals(value, procEnvironment.get(key.toUpperCase(Locale.ENGLISH)));
94 }
95
96
97 EnvironmentUtils.addVariableToEnvironment(procEnvironment, "foo=bar");
98 assertEquals("bar", procEnvironment.get("FOO"));
99 assertEquals("bar", procEnvironment.get("Foo"));
100 assertEquals("bar", procEnvironment.get("foo"));
101 }
102
103
104
105
106 @Test
107 public void testToStrings() {
108
109 assertNull(EnvironmentUtils.toStrings(null));
110
111 final Map<String, String> env = new HashMap<>();
112 assertArrayEquals(new String[0], EnvironmentUtils.toStrings(env));
113 env.put("foo2", "bar2");
114 env.put("foo", "bar");
115 final String[] envStrings = EnvironmentUtils.toStrings(env);
116 final String[] expected = { "foo2=bar2", "foo=bar" };
117
118 Arrays.sort(expected);
119 Arrays.sort(envStrings);
120 assertArrayEquals(expected, envStrings);
121 }
122
123
124
125
126 @Test
127 public void testToStringWithNullKey() {
128 final Map<String, String> env = new HashMap<>();
129 env.put(null, "TheNullKey");
130 final String[] strings = EnvironmentUtils.toStrings(env);
131 assertEquals(1, strings.length);
132 assertEquals("=TheNullKey", strings[0]);
133 }
134
135
136
137
138 @Test
139 public void testToStringWithNullValue() {
140 final Map<String, String> env = new HashMap<>();
141 env.put("key", null);
142 final String[] strings = EnvironmentUtils.toStrings(env);
143 assertEquals(1, strings.length);
144 assertEquals("key=", strings[0]);
145 }
146
147 }