1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.text;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertNull;
21 import static org.junit.jupiter.api.Assertions.assertThrows;
22
23 import java.util.HashMap;
24 import java.util.Map;
25 import java.util.Properties;
26 import java.util.ResourceBundle;
27
28 import org.junit.jupiter.api.Test;
29
30
31
32
33
34
35 class StrLookupTest {
36
37 @Test
38 void testMapLookup() {
39 final Map<String, Object> map = new HashMap<>();
40 map.put("key", "value");
41 map.put("number", 2);
42 assertEquals("value", StrLookup.mapLookup(map).apply("key"));
43 assertEquals("2", StrLookup.mapLookup(map).apply("number"));
44 assertNull(StrLookup.mapLookup(map).apply(null));
45 assertNull(StrLookup.mapLookup(map).apply(""));
46 assertNull(StrLookup.mapLookup(map).apply("other"));
47 }
48
49 @Test
50 void testMapLookup_nullMap() {
51 final Map<String, ?> map = null;
52 assertNull(StrLookup.mapLookup(map).apply(null));
53 assertNull(StrLookup.mapLookup(map).apply(""));
54 assertNull(StrLookup.mapLookup(map).apply("any"));
55 }
56
57 @Test
58 void testNoneLookup() {
59 assertNull(StrLookup.noneLookup().apply(null));
60 assertNull(StrLookup.noneLookup().apply(""));
61 assertNull(StrLookup.noneLookup().apply("any"));
62 }
63
64 @Test
65 void testResourceBundleLookup() {
66 final ResourceBundle map = ResourceBundle.getBundle("org.apache.commons.text.example.testResourceBundleLookup");
67 assertEquals("value", StrLookup.resourceBundleLookup(map).apply("key"));
68 assertEquals("2", StrLookup.resourceBundleLookup(map).lookup("number"));
69 assertNull(StrLookup.resourceBundleLookup(map).apply(null));
70 assertNull(StrLookup.resourceBundleLookup(map).apply(""));
71 assertNull(StrLookup.resourceBundleLookup(map).apply("other"));
72 }
73
74 @Test
75 void testResourceBundleLookup_nullMap() {
76 final ResourceBundle resourceBundle = null;
77 assertNull(StrLookup.resourceBundleLookup(resourceBundle).apply(null));
78 assertNull(StrLookup.resourceBundleLookup(resourceBundle).apply(""));
79 assertNull(StrLookup.resourceBundleLookup(resourceBundle).apply("any"));
80 }
81
82 @Test
83 void testSystemPropertiesLookup() {
84 assertEquals(System.getProperty("os.name"), StrLookup.systemPropertiesLookup().apply("os.name"));
85 assertNull(StrLookup.systemPropertiesLookup().apply(""));
86 assertNull(StrLookup.systemPropertiesLookup().apply("other"));
87 assertThrows(NullPointerException.class, () -> StrLookup.systemPropertiesLookup().apply(null));
88 }
89
90
91
92
93
94
95 @Test
96 void testSystemPropertiesLookupReplacedProperties() {
97 final Properties oldProperties = System.getProperties();
98 final String osName = "os.name";
99 final String newOsName = oldProperties.getProperty(osName) + "_changed";
100
101 final StrLookup<String> sysLookup = StrLookup.systemPropertiesLookup();
102 final Properties newProps = new Properties();
103 newProps.setProperty(osName, newOsName);
104 System.setProperties(newProps);
105 try {
106 assertEquals(newOsName, sysLookup.apply(osName), "Changed properties not detected");
107 } finally {
108 System.setProperties(oldProperties);
109 }
110 }
111
112
113
114
115
116 @Test
117 void testSystemPropertiesLookupUpdatedProperty() {
118 final String osName = "os.name";
119 final String oldOs = System.getProperty(osName);
120 final String newOsName = oldOs + "_changed";
121
122 final StrLookup<String> sysLookup = StrLookup.systemPropertiesLookup();
123 System.setProperty(osName, newOsName);
124 try {
125 assertEquals(newOsName, sysLookup.apply(osName), "Changed properties not detected");
126 } finally {
127 System.setProperty(osName, oldOs);
128 }
129 }
130
131 }