1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.text.lookup;
19
20 import static org.junit.jupiter.api.Assertions.assertEquals;
21 import static org.junit.jupiter.api.Assertions.assertFalse;
22 import static org.junit.jupiter.api.Assertions.assertNull;
23
24 import java.util.HashMap;
25 import java.util.Map;
26 import java.util.concurrent.ConcurrentHashMap;
27 import java.util.function.BiFunction;
28
29 import org.apache.commons.lang3.StringUtils;
30 import org.junit.jupiter.api.Test;
31
32
33
34
35 class BiFunctionStringLookupTest {
36
37 private static final String SEPARATOR = ".";
38
39 @SuppressWarnings("unchecked")
40 private final BiFunction<String, Map<String, Object>, Object> nestedMapBiFunction = (k, m) -> {
41 final String keyCandidate = StringUtils.substringBefore(k, SEPARATOR);
42 if (keyCandidate.isEmpty()) {
43 return m.get(k);
44 }
45 final Object value = m.get(keyCandidate);
46 if (value instanceof Map) {
47 return this.nestedMapBiFunction.apply(StringUtils.substringAfter(k, SEPARATOR),
48 (Map<String, Object>) value);
49 }
50 return value;
51 };
52
53 @Test
54 void testBiFunctionForNestedMap() {
55
56 final String subSubKey = "subsubkeyMap";
57 final String subSubValue = "subsubvalue";
58 final Map<String, String> subSubMap = new HashMap<>();
59 subSubMap.put(subSubKey, subSubValue);
60
61 final String subKey = "subkey";
62 final String subKeyMap = "subkeyMap";
63 final String subValue = "subvalue";
64 final Map<String, Object> rootSubMap = new HashMap<>();
65 rootSubMap.put(subKey, subValue);
66 rootSubMap.put(subKeyMap, subSubMap);
67
68 final String rootKey = "keyMap";
69 final String rootKey2 = "key2";
70 final String rootValue2 = "value2";
71 final Map<String, Object> rootMap = new HashMap<>();
72 rootMap.put(rootKey, rootSubMap);
73 rootMap.put(rootKey2, rootValue2);
74
75 final BiStringLookup<Map<String, Object>> stringLookup = StringLookupFactory.INSTANCE
76 .biFunctionStringLookup(nestedMapBiFunction);
77 assertEquals(rootValue2, stringLookup.lookup(rootKey2, rootMap));
78 assertEquals(subValue, stringLookup.lookup(rootKey + SEPARATOR + subKey, rootMap));
79 assertEquals(subSubValue,
80 stringLookup.lookup(rootKey + SEPARATOR + subKeyMap + SEPARATOR + subSubKey, rootMap));
81 }
82
83 @Test
84 void testConcurrentHashMapNull() {
85 assertNull(BiFunctionStringLookup.on(new ConcurrentHashMap<>()).apply(null));
86 }
87
88 @Test
89 void testHashMapNull() {
90 assertNull(BiFunctionStringLookup.on(new HashMap<>()).apply(null));
91 }
92
93 @Test
94 void testNullBiFunction() {
95 assertNull(BiFunctionStringLookup.on((BiFunction<String, Object, Object>) null).apply(null));
96 }
97
98 @Test
99 void testOne() {
100 final String key = "key";
101 final String value = "value";
102 final Map<String, String> map = new HashMap<>();
103 map.put(key, value);
104 assertEquals(value, FunctionStringLookup.on(map).apply(key));
105 }
106
107 @Test
108 void testToString() {
109
110 assertFalse(BiFunctionStringLookup.on(new HashMap<>()).toString().isEmpty());
111 }
112
113 }