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.HashMap;
28  import java.util.HashSet;
29  import java.util.List;
30  import java.util.Map;
31  import java.util.Set;
32  import java.util.function.Function;
33  import java.util.stream.Collectors;
34  import java.util.stream.Stream;
35  
36  import org.apache.commons.lang3.exception.CustomUncheckedException;
37  import org.junit.jupiter.api.Test;
38  
39  /**
40   * Tests {@link MethodInvokers#asFunction(Method)}.
41   */
42  class MethodInvokersFunctionTest extends MethodFixtures {
43  
44      @Test
45      void testApply0Arg() throws NoSuchMethodException, SecurityException {
46          final Function<MethodFixtures, String> func = MethodInvokers.asFunction(getMethodForGetString());
47          assertEquals(INSTANCE.getString(), func.apply(INSTANCE));
48      }
49  
50      @Test
51      void testApply0ArgThrowsUnchecked() throws NoSuchMethodException, SecurityException {
52          final Function<MethodFixtures, String> func = MethodInvokers.asFunction(getMethodForGetStringThrowsUnchecked());
53          assertThrows(CustomUncheckedException.class, () -> func.apply(INSTANCE));
54      }
55  
56      @Test
57      void testBuildVarArg() throws SecurityException, NoSuchMethodException {
58          MethodInvokers.asFunction(getMethodForGetStringVarStringArgs());
59      }
60  
61      @Test
62      void testConstructorForNull() throws SecurityException {
63          assertNullPointerException(() -> MethodInvokers.asFunction(null));
64      }
65  
66      @Test
67      void testFindAndInvoke() throws SecurityException {
68          // Finding
69          final List<Function<Object, Object>> invokers = Stream.of(MethodFixtures.class.getDeclaredMethods())
70              .filter(m -> m.isAnnotationPresent(AnnotationTestFixture.class)).map(MethodInvokers::asFunction).collect(Collectors.toList());
71          assertEquals(2, invokers.size());
72          // ...
73          // Invoking
74          final Set<Object> set1 = invokers.stream().map(i -> i.apply(MethodFixtures.INSTANCE)).collect(Collectors.toSet());
75          assertEquals(new HashSet<>(Arrays.asList(INSTANCE.getString(), INSTANCE.getString2())), set1);
76          final Set<Object> set2 = Stream.of(INSTANCE).map(invokers.get(0)).collect(Collectors.toSet());
77          final Set<Object> set3 = Stream.of(INSTANCE).map(invokers.get(1)).collect(Collectors.toSet());
78          set2.addAll(set3);
79          assertEquals(new HashSet<>(Arrays.asList(INSTANCE.getString(), INSTANCE.getString2())), set2);
80      }
81  
82      @Test
83      void testFullExample() throws SecurityException, ReflectiveOperationException {
84          final Method method = String.class.getMethod("length");
85          final Function<String, Integer> function = MethodInvokers.asFunction(method);
86          assertEquals(3, function.apply("ABC"));
87      }
88  
89      @Test
90      void testMapComputeIfAbsent() throws NoSuchMethodException, SecurityException {
91          final Map<MethodFixtures, String> map = new HashMap<>();
92          map.computeIfAbsent(INSTANCE, MethodInvokers.asFunction(getMethodForGetString()));
93          assertEquals(INSTANCE.getString(), map.get(INSTANCE));
94      }
95  
96      @Test
97      void testToString() throws SecurityException, ReflectiveOperationException {
98          // Should not blow up and must return _something_
99          assertFalse(MethodInvokers.asFunction(getMethodForGetString()).toString().isEmpty());
100     }
101 
102 }