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