1/*2 * Licensed to the Apache Software Foundation (ASF) under one or more3 * contributor license agreements. See the NOTICE file distributed with4 * this work for additional information regarding copyright ownership.5 * The ASF licenses this file to You under the Apache License, Version 2.06 * (the "License"); you may not use this file except in compliance with7 * the License. You may obtain a copy of the License at8 *9 * https://www.apache.org/licenses/LICENSE-2.010 *11 * Unless required by applicable law or agreed to in writing, software12 * 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 and15 * limitations under the License.16 */1718package org.apache.commons.lang3.function;
1920importstatic org.apache.commons.lang3.LangAssertions.assertNullPointerException;
21importstatic org.junit.jupiter.api.Assertions.assertEquals;
22importstatic org.junit.jupiter.api.Assertions.assertNotNull;
23importstatic org.junit.jupiter.api.Assertions.assertNull;
24importstatic org.junit.jupiter.api.Assertions.assertThrows;
2526import java.io.IOException;
2728import org.apache.commons.lang3.AbstractLangTest;
29import org.junit.jupiter.api.Test;
3031/**32 * Tests {@link FailableBiFunction}.33 */34classFailableBiFunctionTestextendsAbstractLangTest {
3536 @Test
37void testAndThen_composesCorrectly() throws IOException {
38final FailableBiFunction<Integer, Integer, Integer, IOException> sum = (a, b) -> a + b;
39final FailableFunction<Integer, String, IOException> toStr = i -> "result:" + i;
40 assertEquals("result:3", sum.andThen(toStr).apply(1, 2));
41 }
4243 @Test
44void testAndThen_firstThrows_secondNotInvoked() {
45final IOException expected = new IOException("first");
46final FailableBiFunction<String, String, String, IOException> f = (a, b) -> {
47throw expected;
48 };
49final FailableFunction<String, String, IOException> after = s -> s + "!";
50final IOException thrown = assertThrows(IOException.class, () -> f.andThen(after).apply("a", "b"));
51 assertEquals(expected, thrown);
52 }
5354 @Test
55void testAndThen_nullAfter_throwsNullPointerException() throws Throwable {
56final FailableBiFunction<String, String, String, IOException> f = (a, b) -> a + b;
57 assertNullPointerException(() -> f.andThen(null));
58 }
5960 @Test
61void testApply_returnsResult() throws IOException {
62final FailableBiFunction<String, String, String, IOException> f = (a, b) -> a + b;
63 assertEquals("ab", f.apply("a", "b"));
64 }
6566 @Test
67void testApply_throwsException() {
68final IOException expected = new IOException("fail");
69final FailableBiFunction<String, String, String, IOException> f = (a, b) -> {
70throw expected;
71 };
72final IOException thrown = assertThrows(IOException.class, () -> f.apply("a", "b"));
73 assertEquals(expected, thrown);
74 }
7576 @Test
77void testNop_applyReturnsNull() throws Throwable {
78 assertNull(FailableBiFunction.nop().apply("a", "b"));
79 }
8081 @Test
82void testNop_returnsNonNull() {
83 assertNotNull(FailableBiFunction.nop());
84 }
85 }