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.HashSet;
28 import java.util.List;
29 import java.util.Set;
30 import java.util.stream.Collectors;
31 import java.util.stream.Stream;
32
33 import org.apache.commons.lang3.exception.UncheckedException;
34 import org.junit.jupiter.api.Test;
35
36
37
38
39 class MethodInvokersFailableFunctionTest extends MethodFixtures {
40
41 @Test
42 void testApply0Arg() throws Throwable {
43 assertEquals(INSTANCE.getString(), MethodInvokers.asFailableFunction(getMethodForGetString()).apply(INSTANCE));
44 }
45
46 @Test
47 void testBuildVarArg() throws SecurityException, NoSuchMethodException {
48 MethodInvokers.asFailableFunction(getMethodForGetStringVarStringArgs());
49 }
50
51 @Test
52 void testConstructorForNull() throws SecurityException {
53 assertNullPointerException(() -> MethodInvokers.asFailableFunction((Method) null));
54 }
55
56 @Test
57 void testFindAndInvoke() throws SecurityException {
58
59 final List<FailableFunction<Object, Object, Throwable>> invokers = Stream.of(MethodFixtures.class.getDeclaredMethods())
60 .filter(m -> m.isAnnotationPresent(AnnotationTestFixture.class)).map(MethodInvokers::asFailableFunction).collect(Collectors.toList());
61 assertEquals(2, invokers.size());
62
63
64 final Set<Object> set = invokers.stream().map(i -> {
65 try {
66 return i.apply(MethodFixtures.INSTANCE);
67 } catch (final Throwable e) {
68 throw new UncheckedException(e);
69 }
70 }).collect(Collectors.toSet());
71 assertEquals(new HashSet<>(Arrays.asList(INSTANCE.getString(), INSTANCE.getString2())), set);
72 }
73
74 @Test
75 void testThrowsChecked() throws Exception {
76 assertThrows(Exception.class, () -> MethodInvokers.asFailableFunction(getMethodForGetStringThrowsChecked()).apply(INSTANCE));
77 }
78
79 @Test
80 void testToString() throws SecurityException, ReflectiveOperationException {
81
82 assertFalse(MethodInvokers.asFailableFunction(getMethodForGetString()).toString().isEmpty());
83 }
84
85 }