View Javadoc
1   package org.codehaus.plexus.interpolation;
2   
3   /*
4    * Copyright 2007 The Codehaus Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.IOException;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import org.codehaus.plexus.interpolation.os.OperatingSystemUtils;
24  import org.junit.jupiter.api.BeforeEach;
25  import org.junit.jupiter.api.Test;
26  
27  import static org.junit.jupiter.api.Assertions.assertEquals;
28  import static org.junit.jupiter.api.Assertions.assertNotNull;
29  import static org.junit.jupiter.api.Assertions.assertNull;
30  
31  public class EnvarBasedValueSourceTest {
32  
33      @BeforeEach
34      public void setUp() {
35          EnvarBasedValueSource.resetStatics();
36      }
37  
38      @Test
39      void testNoArgConstructorIsCaseSensitive() throws IOException {
40          OperatingSystemUtils.setEnvVarSource(new OperatingSystemUtils.EnvVarSource() {
41              public Map<String, String> getEnvMap() {
42                  HashMap<String, String> map = new HashMap<String, String>();
43                  map.put("aVariable", "variable");
44                  return map;
45              }
46          });
47  
48          EnvarBasedValueSource source = new EnvarBasedValueSource();
49  
50          assertEquals("variable", source.getValue("aVariable"));
51          assertEquals("variable", source.getValue("env.aVariable"));
52          assertNull(source.getValue("AVARIABLE"));
53          assertNull(source.getValue("env.AVARIABLE"));
54      }
55  
56      @Test
57      void testCaseInsensitive() throws IOException {
58          OperatingSystemUtils.setEnvVarSource(new OperatingSystemUtils.EnvVarSource() {
59              public Map<String, String> getEnvMap() {
60                  HashMap<String, String> map = new HashMap<String, String>();
61                  map.put("aVariable", "variable");
62                  return map;
63              }
64          });
65  
66          EnvarBasedValueSource source = new EnvarBasedValueSource(false);
67  
68          assertEquals("variable", source.getValue("aVariable"));
69          assertEquals("variable", source.getValue("env.aVariable"));
70          assertEquals("variable", source.getValue("AVARIABLE"));
71          assertEquals("variable", source.getValue("env.AVARIABLE"));
72      }
73  
74      @Test
75      void testGetRealEnvironmentVariable() throws IOException {
76          OperatingSystemUtils.setEnvVarSource(new OperatingSystemUtils.DefaultEnvVarSource());
77  
78          EnvarBasedValueSource source = new EnvarBasedValueSource();
79  
80          String realEnvVar = "JAVA_HOME";
81  
82          String realValue = System.getenv().get(realEnvVar);
83          assertNotNull(realValue, "Can't run this test until " + realEnvVar + " env variable is set");
84  
85          assertEquals(realValue, source.getValue(realEnvVar));
86      }
87  }