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  import static org.junit.jupiter.api.Assertions.assertThrows;
24  
25  import java.util.HashMap;
26  import java.util.Map;
27  import java.util.concurrent.ConcurrentHashMap;
28  import java.util.function.Function;
29  import java.util.function.Supplier;
30  
31  import org.junit.jupiter.api.Test;
32  
33  /**
34   * Tests {@link FunctionStringLookup}.
35   */
36  class FunctionStringLookupTest {
37  
38      @Test
39      void testConcurrentHashMapNull() {
40          assertNull(FunctionStringLookup.on(new ConcurrentHashMap<>()).apply(null));
41      }
42  
43      @Test
44      void testHashMapNull() {
45          assertNull(FunctionStringLookup.on(new HashMap<>()).apply(null));
46      }
47  
48      @Test
49      void testNullFunction() {
50          assertNull(FunctionStringLookup.on((Function<String, Object>) null).apply(null));
51      }
52  
53      @Test
54      void testOne() {
55          final String key = "key";
56          final String value = "value";
57          final Map<String, String> map = new HashMap<>();
58          map.put(key, value);
59          assertEquals(value, FunctionStringLookup.on(map).apply(key));
60      }
61  
62      @Test
63      void testThrowsError() {
64          assertThrows(Error.class, () -> FunctionStringLookup.on(k -> throwError(Error::new)).apply("key"));
65      }
66  
67      @Test
68      void testThrowsIllegalStateException() {
69          assertThrows(IllegalStateException.class, () -> FunctionStringLookup.on(k -> throwRuntimeException(IllegalStateException::new)).apply("key"));
70      }
71  
72      @Test
73      void testThrowsNullPointerException() {
74          assertNull(FunctionStringLookup.on(k -> throwRuntimeException(NullPointerException::new)).apply("key"));
75      }
76  
77      @Test
78      void testThrowsRuntimeException() {
79          assertThrows(RuntimeException.class, () -> FunctionStringLookup.on(k -> throwRuntimeException(RuntimeException::new)).apply("key"));
80      }
81  
82      @Test
83      void testThrowsSecurityException() {
84          assertNull(FunctionStringLookup.on(k -> {
85              throw new SecurityException("test");
86          }).apply("key"));
87      }
88  
89      @Test
90      void testToString() {
91          // does not blow up and gives some kind of string.
92          assertFalse(FunctionStringLookup.on(new HashMap<>()).toString().isEmpty());
93      }
94  
95      <T extends Error> Object throwError(final Supplier<T> t) throws T {
96          throw t.get();
97      }
98  
99      <T extends RuntimeException> Object throwRuntimeException(final Supplier<T> t) throws T {
100         throw t.get();
101     }
102 }