1/*2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8 *9 * https://www.apache.org/licenses/LICENSE-2.010 *11 * Unless required by applicable law or agreed to in writing, software12 * distributed under the License is distributed on an "AS IS" BASIS,13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.14 * See the License for the specific language governing permissions and15 * limitations under the License.16 */1718package org.apache.commons.lang3.function;
1920importstatic org.apache.commons.lang3.LangAssertions.assertNullPointerException;
21importstatic org.junit.jupiter.api.Assertions.assertEquals;
22importstatic org.junit.jupiter.api.Assertions.assertFalse;
23importstatic org.junit.jupiter.api.Assertions.assertThrows;
2425import java.lang.reflect.Method;
26import java.util.Arrays;
27import java.util.HashSet;
28import java.util.List;
29import java.util.Set;
30import java.util.stream.Collectors;
31import java.util.stream.Stream;
3233import org.apache.commons.lang3.exception.UncheckedException;
34import org.junit.jupiter.api.Test;
3536/**37 * Tests {@link MethodInvokers#asFailableFunction(Method)}.38 */39classMethodInvokersFailableFunctionTestextendsMethodFixtures {
4041 @Test
42void testApply0Arg() throws Throwable {
43 assertEquals(INSTANCE.getString(), MethodInvokers.asFailableFunction(getMethodForGetString()).apply(INSTANCE));
44 }
4546 @Test
47void testBuildVarArg() throws SecurityException, NoSuchMethodException {
48 MethodInvokers.asFailableFunction(getMethodForGetStringVarStringArgs());
49 }
5051 @Test
52void testConstructorForNull() throws SecurityException {
53 assertNullPointerException(() -> MethodInvokers.asFailableFunction((Method) null));
54 }
5556 @Test
57void testFindAndInvoke() throws SecurityException {
58// Finding59final 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// Invoking64final Set<Object> set = invokers.stream().map(i -> {
65try {
66return i.apply(MethodFixtures.INSTANCE);
67 } catch (final Throwable e) {
68thrownew UncheckedException(e);
69 }
70 }).collect(Collectors.toSet());
71 assertEquals(new HashSet<>(Arrays.asList(INSTANCE.getString(), INSTANCE.getString2())), set);
72 }
7374 @Test
75void testThrowsChecked() throws Exception {
76 assertThrows(Exception.class, () -> MethodInvokers.asFailableFunction(getMethodForGetStringThrowsChecked()).apply(INSTANCE));
77 }
7879 @Test
80void testToString() throws SecurityException, ReflectiveOperationException {
81// Should not blow up and must return _something_82 assertFalse(MethodInvokers.asFailableFunction(getMethodForGetString()).toString().isEmpty());
83 }
8485 }