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.util;
21  
22  import static org.junit.jupiter.api.Assertions.assertEquals;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  
25  import java.util.HashMap;
26  import java.util.Map;
27  
28  import org.apache.commons.exec.environment.EnvironmentUtils;
29  import org.junit.jupiter.api.Test;
30  
31  /**
32   */
33  class MapUtilTest {
34  
35      /**
36       * Test copying of map
37       */
38      @Test
39      void testCopyMap() throws Exception {
40          final Map<String, String> procEnvironment = new HashMap<>();
41          procEnvironment.put("JAVA_HOME", "/usr/opt/java");
42          final Map<String, String> result = MapUtils.copy(procEnvironment);
43          assertEquals(1, result.size());
44          assertEquals(1, procEnvironment.size());
45          assertEquals("/usr/opt/java", result.get("JAVA_HOME"));
46          result.remove("JAVA_HOME");
47          assertTrue(result.isEmpty());
48          assertEquals(1, procEnvironment.size());
49      }
50  
51      /**
52       * Test merging of maps
53       */
54      @Test
55      void testMergeMap() throws Exception {
56          final Map<String, String> procEnvironment = EnvironmentUtils.getProcEnvironment();
57          final Map<String, String> applicationEnvironment = new HashMap<>();
58          applicationEnvironment.put("appMainClass", "foo.bar.Main");
59          final Map<String, String> result = MapUtils.merge(procEnvironment, applicationEnvironment);
60          assertEquals(procEnvironment.size() + applicationEnvironment.size(), result.size());
61          assertEquals("foo.bar.Main", result.get("appMainClass"));
62      }
63  
64      /**
65       * Test prefixing of map
66       */
67      @Test
68      void testPrefixMap() throws Exception {
69          final Map<String, String> procEnvironment = new HashMap<>();
70          procEnvironment.put("JAVA_HOME", "/usr/opt/java");
71          final Map<String, String> result = MapUtils.prefix(procEnvironment, "env");
72          assertEquals(procEnvironment.size(), result.size());
73          assertEquals("/usr/opt/java", result.get("env.JAVA_HOME"));
74      }
75  }