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    *      http://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.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   * Tests {@link MethodInvokers#asFailableFunction(Method)}.
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          // Finding
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          // Invoking
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          // Should not blow up and must return _something_
81          assertFalse(MethodInvokers.asFailableFunction(getMethodForGetString()).toString().isEmpty());
82      }
83  
84  }