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.assertThrows;
24  
25  import java.io.IOException;
26  
27  import org.apache.commons.lang3.AbstractLangTest;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Tests {@link FailableIntUnaryOperator}.
32   */
33  class FailableIntUnaryOperatorTest extends AbstractLangTest {
34  
35      @Test
36      void testAndThen_composesCorrectly() throws IOException {
37          final FailableIntUnaryOperator<IOException> times2 = v -> v * 2;
38          final FailableIntUnaryOperator<IOException> plus1 = v -> v + 1;
39          assertEquals(7, times2.andThen(plus1).applyAsInt(3));
40      }
41  
42      @Test
43      void testAndThen_nullAfter_throwsNullPointerException() throws Throwable {
44          final FailableIntUnaryOperator<IOException> op = v -> v;
45          assertNullPointerException(() -> op.andThen(null));
46      }
47  
48      @Test
49      void testApplyAsInt_returnsResult() throws IOException {
50          final FailableIntUnaryOperator<IOException> op = v -> v * 3;
51          assertEquals(15, op.applyAsInt(5));
52      }
53  
54      @Test
55      void testApplyAsInt_throwsException() {
56          final IOException expected = new IOException("fail");
57          final FailableIntUnaryOperator<IOException> op = v -> {
58              throw expected;
59          };
60          final IOException thrown = assertThrows(IOException.class, () -> op.applyAsInt(1));
61          assertEquals(expected, thrown);
62      }
63  
64      @Test
65      void testCompose_composesCorrectly() throws IOException {
66          final FailableIntUnaryOperator<IOException> times2 = v -> v * 2;
67          final FailableIntUnaryOperator<IOException> plus1 = v -> v + 1;
68          assertEquals(8, times2.compose(plus1).applyAsInt(3));
69      }
70  
71      @Test
72      void testCompose_nullBefore_throwsNullPointerException() throws Throwable {
73          final FailableIntUnaryOperator<IOException> op = v -> v;
74          assertNullPointerException(() -> op.compose(null));
75      }
76  
77      @Test
78      void testIdentity_returnsInput() throws IOException {
79          assertEquals(7, FailableIntUnaryOperator.<IOException>identity().applyAsInt(7));
80      }
81  
82      @Test
83      void testNop_applyAsIntReturnsZero() throws Throwable {
84          assertEquals(0, FailableIntUnaryOperator.nop().applyAsInt(5));
85      }
86  
87      @Test
88      void testNop_returnsNonNull() {
89          assertNotNull(FailableIntUnaryOperator.nop());
90      }
91  }