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