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 @Deprecated
36 class StrLookupTest {
37
38 @Test
39 void testMapLookup() {
40 final Map<String, Object> map = new HashMap<>();
41 map.put("key", "value");
42 map.put("number", 2);
43 assertEquals("value", StrLookup.mapLookup(map).apply("key"));
44 assertEquals("2", StrLookup.mapLookup(map).apply("number"));
45 assertNull(StrLookup.mapLookup(map).apply(null));
46 assertNull(StrLookup.mapLookup(map).apply(""));
47 assertNull(StrLookup.mapLookup(map).apply("other"));
48 }
49
50 @Test
51 void testMapLookup_nullMap() {
52 final Map<String, ?> map = null;
53 assertNull(StrLookup.mapLookup(map).apply(null));
54 assertNull(StrLookup.mapLookup(map).apply(""));
55 assertNull(StrLookup.mapLookup(map).apply("any"));
56 }
57
58 @Test
59 void testNoneLookup() {
60 assertNull(StrLookup.noneLookup().apply(null));
61 assertNull(StrLookup.noneLookup().apply(""));
62 assertNull(StrLookup.noneLookup().apply("any"));
63 }
64
65 @Test
66 void testResourceBundleLookup() {
67 final ResourceBundle map = ResourceBundle.getBundle("org.apache.commons.text.example.testResourceBundleLookup");
68 assertEquals("value", StrLookup.resourceBundleLookup(map).apply("key"));
69 assertEquals("2", StrLookup.resourceBundleLookup(map).lookup("number"));
70 assertNull(StrLookup.resourceBundleLookup(map).apply(null));
71 assertNull(StrLookup.resourceBundleLookup(map).apply(""));
72 assertNull(StrLookup.resourceBundleLookup(map).apply("other"));
73 }
74
75 @Test
76 void testResourceBundleLookup_nullMap() {
77 final ResourceBundle resourceBundle = null;
78 assertNull(StrLookup.resourceBundleLookup(resourceBundle).apply(null));
79 assertNull(StrLookup.resourceBundleLookup(resourceBundle).apply(""));
80 assertNull(StrLookup.resourceBundleLookup(resourceBundle).apply("any"));
81 }
82
83 @Test
84 void testSystemPropertiesLookup() {
85 assertEquals(System.getProperty("os.name"), StrLookup.systemPropertiesLookup().apply("os.name"));
86 assertNull(StrLookup.systemPropertiesLookup().apply(""));
87 assertNull(StrLookup.systemPropertiesLookup().apply("other"));
88 assertThrows(NullPointerException.class, () -> StrLookup.systemPropertiesLookup().apply(null));
89 }
90
91
92
93
94
95
96 @Test
97 void testSystemPropertiesLookupReplacedProperties() {
98 final Properties oldProperties = System.getProperties();
99 final String osName = "os.name";
100 final String newOsName = oldProperties.getProperty(osName) + "_changed";
101
102 final StrLookup<String> sysLookup = StrLookup.systemPropertiesLookup();
103 final Properties newProps = new Properties();
104 newProps.setProperty(osName, newOsName);
105 System.setProperties(newProps);
106 try {
107 assertEquals(newOsName, sysLookup.apply(osName), "Changed properties not detected");
108 } finally {
109 System.setProperties(oldProperties);
110 }
111 }
112
113
114
115
116
117 @Test
118 void testSystemPropertiesLookupUpdatedProperty() {
119 final String osName = "os.name";
120 final String oldOs = System.getProperty(osName);
121 final String newOsName = oldOs + "_changed";
122
123 final StrLookup<String> sysLookup = StrLookup.systemPropertiesLookup();
124 System.setProperty(osName, newOsName);
125 try {
126 assertEquals(newOsName, sysLookup.apply(osName), "Changed properties not detected");
127 } finally {
128 System.setProperty(osName, oldOs);
129 }
130 }
131
132 }