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