View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      https://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * 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 and
15   * limitations under the License.
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   * Tests {@link MethodInvokers#asFailableFunction(Method)}.
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          // Finding
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          // Invoking
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          // Should not blow up and must return _something_
82          assertFalse(MethodInvokers.asFailableFunction(getMethodForGetString()).toString().isEmpty());
83      }
84  
85  }