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.assertNotNull;
23  import static org.junit.jupiter.api.Assertions.assertNull;
24  import static org.junit.jupiter.api.Assertions.assertSame;
25  import static org.junit.jupiter.api.Assertions.assertThrows;
26  
27  import java.io.IOException;
28  
29  import org.apache.commons.lang3.AbstractLangTest;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Tests {@link FailableFunction}.
34   */
35  class FailableFunctionTest extends AbstractLangTest {
36  
37      @Test
38      void testAndThen_composesCorrectly() throws IOException {
39          final FailableFunction<String, Integer, IOException> length = String::length;
40          final FailableFunction<Integer, String, IOException> toStr = i -> "len=" + i;
41          assertEquals("len=5", length.andThen(toStr).apply("hello"));
42      }
43  
44      @Test
45      void testAndThen_nullAfter_throwsNullPointerException() throws Throwable {
46          final FailableFunction<String, String, IOException> f = s -> s;
47          assertNullPointerException(() -> f.andThen(null));
48      }
49  
50      @Test
51      void testApply_returnsResult() throws IOException {
52          final FailableFunction<String, Integer, IOException> f = String::length;
53          assertEquals(5, f.apply("hello"));
54      }
55  
56      @Test
57      void testApply_throwsException() {
58          final IOException expected = new IOException("fail");
59          final FailableFunction<String, String, IOException> f = s -> {
60              throw expected;
61          };
62          final IOException thrown = assertThrows(IOException.class, () -> f.apply("x"));
63          assertEquals(expected, thrown);
64      }
65  
66      @Test
67      void testCompose_composesCorrectly() throws IOException {
68          final FailableFunction<String, String, IOException> upper = String::toUpperCase;
69          final FailableFunction<String, String, IOException> trim = String::trim;
70          assertEquals("HELLO", upper.compose(trim).apply("  hello  "));
71      }
72  
73      @Test
74      void testCompose_nullBefore_throwsNullPointerException() throws Throwable {
75          final FailableFunction<String, String, IOException> f = s -> s;
76          assertNullPointerException(() -> f.compose(null));
77      }
78  
79      @Test
80      void testFunction_returnsArgument() throws IOException {
81          final FailableFunction<String, String, IOException> f = String::toUpperCase;
82          final FailableFunction<String, String, IOException> wrapped = FailableFunction.function(f);
83          assertEquals("HELLO", wrapped.apply("hello"));
84      }
85  
86      @Test
87      void testIdentity_returnsInput() throws IOException {
88          final String input = "hello";
89          assertSame(input, FailableFunction.<String, IOException>identity().apply(input));
90      }
91  
92      @Test
93      void testNop_applyReturnsNull() throws Throwable {
94          assertNull(FailableFunction.nop().apply("x"));
95      }
96  
97      @Test
98      void testNop_returnsNonNull() {
99          assertNotNull(FailableFunction.nop());
100     }
101 }