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