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 FailableDoubleUnaryOperator}.
32   */
33  class FailableDoubleUnaryOperatorTest extends AbstractLangTest {
34  
35      @Test
36      void testAndThen_composesCorrectly() throws IOException {
37          final FailableDoubleUnaryOperator<IOException> times2 = v -> v * 2;
38          final FailableDoubleUnaryOperator<IOException> plus1 = v -> v + 1;
39          assertEquals(7.0, times2.andThen(plus1).applyAsDouble(3.0));
40      }
41  
42      @Test
43      void testAndThen_nullAfter_throwsNullPointerException() throws Throwable {
44          final FailableDoubleUnaryOperator<IOException> op = v -> v;
45          assertNullPointerException(() -> op.andThen(null));
46      }
47  
48      @Test
49      void testApplyAsDouble_returnsResult() throws IOException {
50          final FailableDoubleUnaryOperator<IOException> op = v -> v * 2;
51          assertEquals(6.0, op.applyAsDouble(3.0));
52      }
53  
54      @Test
55      void testApplyAsDouble_throwsException() {
56          final IOException expected = new IOException("fail");
57          final FailableDoubleUnaryOperator<IOException> op = v -> {
58              throw expected;
59          };
60          final IOException thrown = assertThrows(IOException.class, () -> op.applyAsDouble(1.0));
61          assertEquals(expected, thrown);
62      }
63  
64      @Test
65      void testCompose_composesCorrectly() throws IOException {
66          final FailableDoubleUnaryOperator<IOException> times2 = v -> v * 2;
67          final FailableDoubleUnaryOperator<IOException> plus1 = v -> v + 1;
68          assertEquals(8.0, times2.compose(plus1).applyAsDouble(3.0));
69      }
70  
71      @Test
72      void testCompose_nullBefore_throwsNullPointerException() throws Throwable {
73          final FailableDoubleUnaryOperator<IOException> op = v -> v;
74          assertNullPointerException(() -> op.compose(null));
75      }
76  
77      @Test
78      void testIdentity_returnsInput() throws IOException {
79          assertEquals(3.14, FailableDoubleUnaryOperator.<IOException>identity().applyAsDouble(3.14));
80      }
81  
82      @Test
83      void testNop_applyAsDoubleReturnsZero() throws Throwable {
84          assertEquals(0d, FailableDoubleUnaryOperator.nop().applyAsDouble(5.0));
85      }
86  
87      @Test
88      void testNop_returnsNonNull() {
89          assertNotNull(FailableDoubleUnaryOperator.nop());
90      }
91  }