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