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.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertFalse;
22  import static org.junit.jupiter.api.Assertions.assertNull;
23  import static org.junit.jupiter.api.Assertions.assertThrows;
24  import static org.junit.jupiter.api.Assertions.assertTrue;
25  
26  import java.util.concurrent.atomic.AtomicBoolean;
27  import java.util.function.Function;
28  
29  import org.apache.commons.lang3.StringUtils;
30  import org.junit.jupiter.api.Test;
31  
32  /**
33   * Tests {@link Functions}.
34   */
35  class FunctionsTest {
36  
37      /**
38       * Tests {@link Functions#apply(Function, Object)}.
39       */
40      @Test
41      void testApply() {
42          final AtomicBoolean bool = new AtomicBoolean();
43          assertFalse(Functions.apply(bool::getAndSet, true));
44          assertTrue(bool.get());
45          assertNull(Functions.apply(null, "foo"));
46          assertNull(Functions.apply(null, null));
47      }
48  
49      @Test
50      void testApplyNonNull() {
51          assertEquals("A", Functions.applyNonNull("a", String::toUpperCase));
52          assertNull(Functions.applyNonNull((String) null, String::toUpperCase));
53          assertNull(Functions.applyNonNull("a", s -> null));
54          assertThrows(NullPointerException.class, () -> Functions.applyNonNull("a", null));
55      }
56  
57      @Test
58      void testApplyNonNull2() {
59          assertEquals("A", Functions.applyNonNull(" a ", String::toUpperCase, String::trim));
60          assertNull(Functions.applyNonNull((String) null, String::toUpperCase, String::trim));
61          assertNull(Functions.applyNonNull(" a ", s -> null, String::trim));
62          assertNull(Functions.applyNonNull(" a ", String::toUpperCase, s -> null));
63          assertThrows(NullPointerException.class, () -> Functions.applyNonNull(" a ", null, String::trim));
64          assertThrows(NullPointerException.class, () -> Functions.applyNonNull(" a ", String::toUpperCase, null));
65      }
66  
67      @Test
68      void testApplyNonNull3() {
69          assertEquals("CBA", Functions.applyNonNull(" abc ", String::toUpperCase, String::trim, StringUtils::reverse));
70          assertNull(Functions.applyNonNull((String) null, String::toUpperCase, String::trim, StringUtils::reverse));
71          assertNull(Functions.applyNonNull(" abc ", s -> null, String::trim, StringUtils::reverse));
72          assertNull(Functions.applyNonNull(" abc ", String::toUpperCase, s -> null, StringUtils::reverse));
73          assertNull(Functions.applyNonNull(" abc ", String::toUpperCase, String::trim, s -> null));
74          assertThrows(NullPointerException.class, () -> Functions.applyNonNull(" abc ", null, String::trim, StringUtils::reverse));
75          assertThrows(NullPointerException.class, () -> Functions.applyNonNull(" abc ", String::toUpperCase, null, StringUtils::reverse));
76          assertThrows(NullPointerException.class, () -> Functions.applyNonNull(" abc ", String::toUpperCase, String::trim, null));
77      }
78  
79      /**
80       * Tests {@link Functions#function(Function)}.
81       */
82      @Test
83      void testFunction() {
84          assertEquals("foo", Functions.function(String::valueOf).andThen(String::toString).apply("foo"));
85      }
86  }