1 package org.codehaus.plexus.interpolation.fixed;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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", null));
51 assertEquals("variable", source.getValue("env.aVariable", null));
52 assertNull(source.getValue("AVARIABLE", null));
53 assertNull(source.getValue("env.AVARIABLE", null));
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", null));
69 assertEquals("variable", source.getValue("env.aVariable", null));
70 assertEquals("variable", source.getValue("AVARIABLE", null));
71 assertEquals("variable", source.getValue("env.AVARIABLE", null));
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, null));
86 }
87 }