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    *      http://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.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   * Tests {@link EnvironmentUtils}.
38   */
39  public class EnvironmentUtilsTest {
40  
41      /**
42       * Accessing environment variables is case-sensitive or not depending on the operating system but the values of the environment variable are always
43       * case-sensitive. So make sure that this assumption holds on all operating systems.
44       *
45       * @throws Exception the test failed
46       */
47      @Test
48      public void testCaseInsensitiveVariableLookup() throws Exception {
49          final Map<String, String> procEnvironment = EnvironmentUtils.getProcEnvironment();
50          // Check that case is preserved for values
51          EnvironmentUtils.addVariableToEnvironment(procEnvironment, "foo=bAr");
52          assertEquals("bAr", procEnvironment.get("foo"));
53      }
54  
55      /**
56       * Test to access the environment variables of the current process. Please note that this test does not run on java-gjc.
57       *
58       * @throws IOException the test failed
59       */
60      @Test
61      public void testGetProcEnvironment() throws IOException {
62          final Map<String, String> procEnvironment = EnvironmentUtils.getProcEnvironment();
63          // we assume that there is at least one environment variable
64          // for this process, i.e. $JAVA_HOME
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              // System.out.println(envArgs[i]);
71          }
72      }
73  
74      /**
75       * 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
76       * same environment variable.
77       *
78       * @throws IOException the test failed
79       */
80      @Test
81      public void testGetProcEnvironmentCaseInsensitiveLookup() throws IOException {
82          // run tests only on windows platforms
83          if (!OS.isFamilyWindows()) {
84              return;
85          }
86  
87          // ensure that we have the same value for upper and lowercase keys
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          // add an environment variable and check access
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      * Tests the behavior of the EnvironmentUtils.toStrings() when using a {@code null} environment.
105      */
106     @Test
107     public void testToStrings() {
108         // check for a non-existing environment when passing null
109         assertNull(EnvironmentUtils.toStrings(null));
110         // check for an environment when filling in two variables
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         // ensure the result does not depend on the hash ordering
118         Arrays.sort(expected);
119         Arrays.sort(envStrings);
120         assertArrayEquals(expected, envStrings);
121     }
122 
123     /**
124      * Tests the behavior of the EnvironmentUtils.toStrings() when using a {@code null} key given to the map.
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      * Tests the behavior of the EnvironmentUtils.toStrings() when using a {@code null} value given to the map.
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 }