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 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
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
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 }