View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   https://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.commons.exec.environment;
21  
22  import static org.junit.jupiter.api.Assertions.assertArrayEquals;
23  import static org.junit.jupiter.api.Assertions.assertEquals;
24  import static org.junit.jupiter.api.Assertions.assertFalse;
25  import static org.junit.jupiter.api.Assertions.assertNotNull;
26  import static org.junit.jupiter.api.Assertions.assertNull;
27  
28  import java.io.IOException;
29  import java.util.Arrays;
30  import java.util.HashMap;
31  import java.util.Map;
32  import java.util.Map.Entry;
33  
34  import org.apache.commons.exec.OS;
35  import org.apache.commons.lang3.StringUtils;
36  import org.junit.jupiter.api.Test;
37  
38  /**
39   * Tests {@link EnvironmentUtils}.
40   */
41  class EnvironmentUtilsTest {
42  
43      /**
44       * Accessing environment variables is case-sensitive or not depending on the operating system but the values of the environment variable are always
45       * case-sensitive. So make sure that this assumption holds on all operating systems.
46       *
47       * @throws Exception the test failed
48       */
49      @Test
50      void testCaseInsensitiveVariableLookup() throws Exception {
51          final Map<String, String> procEnvironment = EnvironmentUtils.getProcEnvironment();
52          // Check that case is preserved for values
53          EnvironmentUtils.addVariableToEnvironment(procEnvironment, "foo=bAr");
54          assertEquals("bAr", procEnvironment.get("foo"));
55      }
56  
57      /**
58       * Test to access the environment variables of the current process. Please note that this test does not run on java-gjc.
59       *
60       * @throws IOException the test failed
61       */
62      @Test
63      void testGetProcEnvironment() throws IOException {
64          final Map<String, String> procEnvironment = EnvironmentUtils.getProcEnvironment();
65          // we assume that there is at least one environment variable
66          // for this process, i.e. $JAVA_HOME
67          assertFalse(procEnvironment.isEmpty(), "Expecting non-zero environment size");
68          final String[] envArgs = EnvironmentUtils.toStrings(procEnvironment);
69          for (int i = 0; i < envArgs.length; i++) {
70              assertNotNull(envArgs[i], "Entry " + i + " should not be null");
71              assertFalse(envArgs[i].isEmpty(), "Entry " + i + " should not be empty");
72              // System.out.println(envArgs[i]);
73          }
74      }
75  
76      /**
77       * On Windows platforms test that accessing environment variables can be done in a case-insensitive way, e.g. "PATH", "Path" and "path" would reference the
78       * same environment variable.
79       *
80       * @throws IOException the test failed
81       */
82      @Test
83      void testGetProcEnvironmentCaseInsensitiveLookup() throws IOException {
84          // run tests only on windows platforms
85          if (!OS.isFamilyWindows()) {
86              return;
87          }
88  
89          // ensure that we have the same value for upper and lowercase keys
90          final Map<String, String> procEnvironment = EnvironmentUtils.getProcEnvironment();
91          for (final Entry<String, String> entry : procEnvironment.entrySet()) {
92              final String key = entry.getKey();
93              final String value = entry.getValue();
94              assertEquals(value, procEnvironment.get(StringUtils.toRootLowerCase(key)));
95              assertEquals(value, procEnvironment.get(StringUtils.toRootUpperCase(key)));
96          }
97  
98          // add an environment variable and check access
99          EnvironmentUtils.addVariableToEnvironment(procEnvironment, "foo=bar");
100         assertEquals("bar", procEnvironment.get("FOO"));
101         assertEquals("bar", procEnvironment.get("Foo"));
102         assertEquals("bar", procEnvironment.get("foo"));
103     }
104 
105     /**
106      * Tests the behavior of the EnvironmentUtils.toStrings() when using a {@code null} environment.
107      */
108     @Test
109     void testToStrings() {
110         // check for a non-existing environment when passing null
111         assertNull(EnvironmentUtils.toStrings(null));
112         // check for an environment when filling in two variables
113         final Map<String, String> env = new HashMap<>();
114         assertArrayEquals(new String[0], EnvironmentUtils.toStrings(env));
115         env.put("foo2", "bar2");
116         env.put("foo", "bar");
117         final String[] envStrings = EnvironmentUtils.toStrings(env);
118         final String[] expected = { "foo2=bar2", "foo=bar" };
119         // ensure the result does not depend on the hash ordering
120         Arrays.sort(expected);
121         Arrays.sort(envStrings);
122         assertArrayEquals(expected, envStrings);
123     }
124 
125     /**
126      * Tests the behavior of the EnvironmentUtils.toStrings() when using a {@code null} key given to the map.
127      */
128     @Test
129     void testToStringWithNullKey() {
130         final Map<String, String> env = new HashMap<>();
131         env.put(null, "TheNullKey");
132         final String[] strings = EnvironmentUtils.toStrings(env);
133         assertEquals(1, strings.length);
134         assertEquals("=TheNullKey", strings[0]);
135     }
136 
137     /**
138      * Tests the behavior of the EnvironmentUtils.toStrings() when using a {@code null} value given to the map.
139      */
140     @Test
141     void testToStringWithNullValue() {
142         final Map<String, String> env = new HashMap<>();
143         env.put("key", null);
144         final String[] strings = EnvironmentUtils.toStrings(env);
145         assertEquals(1, strings.length);
146         assertEquals("key=", strings[0]);
147     }
148 
149 }