1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.lang3.function;
19
20 import static org.apache.commons.lang3.LangAssertions.assertNullPointerException;
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertFalse;
23 import static org.junit.jupiter.api.Assertions.assertThrows;
24
25 import java.lang.reflect.Method;
26 import java.util.Arrays;
27 import java.util.HashMap;
28 import java.util.HashSet;
29 import java.util.List;
30 import java.util.Map;
31 import java.util.Set;
32 import java.util.function.Function;
33 import java.util.stream.Collectors;
34 import java.util.stream.Stream;
35
36 import org.apache.commons.lang3.exception.CustomUncheckedException;
37 import org.junit.jupiter.api.Test;
38
39
40
41
42 class MethodInvokersFunctionTest extends MethodFixtures {
43
44 @Test
45 void testApply0Arg() throws NoSuchMethodException, SecurityException {
46 final Function<MethodFixtures, String> func = MethodInvokers.asFunction(getMethodForGetString());
47 assertEquals(INSTANCE.getString(), func.apply(INSTANCE));
48 }
49
50 @Test
51 void testApply0ArgThrowsUnchecked() throws NoSuchMethodException, SecurityException {
52 final Function<MethodFixtures, String> func = MethodInvokers.asFunction(getMethodForGetStringThrowsUnchecked());
53 assertThrows(CustomUncheckedException.class, () -> func.apply(INSTANCE));
54 }
55
56 @Test
57 void testBuildVarArg() throws SecurityException, NoSuchMethodException {
58 MethodInvokers.asFunction(getMethodForGetStringVarStringArgs());
59 }
60
61 @Test
62 void testConstructorForNull() throws SecurityException {
63 assertNullPointerException(() -> MethodInvokers.asFunction(null));
64 }
65
66 @Test
67 void testFindAndInvoke() throws SecurityException {
68
69 final List<Function<Object, Object>> invokers = Stream.of(MethodFixtures.class.getDeclaredMethods())
70 .filter(m -> m.isAnnotationPresent(AnnotationTestFixture.class)).map(MethodInvokers::asFunction).collect(Collectors.toList());
71 assertEquals(2, invokers.size());
72
73
74 final Set<Object> set1 = invokers.stream().map(i -> i.apply(MethodFixtures.INSTANCE)).collect(Collectors.toSet());
75 assertEquals(new HashSet<>(Arrays.asList(INSTANCE.getString(), INSTANCE.getString2())), set1);
76 final Set<Object> set2 = Stream.of(INSTANCE).map(invokers.get(0)).collect(Collectors.toSet());
77 final Set<Object> set3 = Stream.of(INSTANCE).map(invokers.get(1)).collect(Collectors.toSet());
78 set2.addAll(set3);
79 assertEquals(new HashSet<>(Arrays.asList(INSTANCE.getString(), INSTANCE.getString2())), set2);
80 }
81
82 @Test
83 void testFullExample() throws SecurityException, ReflectiveOperationException {
84 final Method method = String.class.getMethod("length");
85 final Function<String, Integer> function = MethodInvokers.asFunction(method);
86 assertEquals(3, function.apply("ABC"));
87 }
88
89 @Test
90 void testMapComputeIfAbsent() throws NoSuchMethodException, SecurityException {
91 final Map<MethodFixtures, String> map = new HashMap<>();
92 map.computeIfAbsent(INSTANCE, MethodInvokers.asFunction(getMethodForGetString()));
93 assertEquals(INSTANCE.getString(), map.get(INSTANCE));
94 }
95
96 @Test
97 void testToString() throws SecurityException, ReflectiveOperationException {
98
99 assertFalse(MethodInvokers.asFunction(getMethodForGetString()).toString().isEmpty());
100 }
101
102 }