View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
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   * Tests {@link FunctionStringLookup}.
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          // Build map
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          // Use map
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         // does not blow up and gives some kind of string.
110         assertFalse(BiFunctionStringLookup.on(new HashMap<>()).toString().isEmpty());
111     }
112 
113 }