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