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    *      http://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.assertSame;
23  import static org.junit.jupiter.api.Assertions.assertTrue;
24  import static org.junit.jupiter.api.Assertions.fail;
25  
26  import java.util.function.Supplier;
27  
28  import org.apache.commons.lang3.AbstractLangTest;
29  import org.junit.jupiter.api.Test;
30  
31  public class ObjectsTest extends AbstractLangTest {
32  
33      public static class TestableFailableSupplier<O, E extends Exception> implements FailableSupplier<O, E> {
34          private final FailableSupplier<O, E> supplier;
35          private boolean invoked;
36  
37          TestableFailableSupplier(final FailableSupplier<O, E> pSupplier) {
38              this.supplier = pSupplier;
39          }
40  
41          @Override
42          public O get() throws E {
43              invoked = true;
44              return supplier.get();
45          }
46  
47          public boolean isInvoked() {
48              return invoked;
49          }
50      }
51  
52      public static class TestableSupplier<O> implements Supplier<O> {
53          private final Supplier<O> supplier;
54          private boolean invoked;
55  
56          TestableSupplier(final Supplier<O> pSupplier) {
57              this.supplier = pSupplier;
58          }
59  
60          @Override
61          public O get() {
62              invoked = true;
63              return supplier.get();
64          }
65  
66          public boolean isInvoked() {
67              return invoked;
68          }
69      }
70  
71      @Test
72      void testRequireNonNullObject() {
73          assertSame("foo", Objects.requireNonNull("foo"));
74          try {
75              Objects.requireNonNull(null);
76              fail("Expected Exception");
77          } catch (final NullPointerException e) {
78              assertEquals("The value must not be null.", e.getMessage());
79          }
80      }
81  
82      @Test
83      void testRequireNonNullObjectFailableSupplierString() {
84          final TestableFailableSupplier<String, ?> supplier = new TestableFailableSupplier<>(FailableSupplier.nul());
85          assertSame("foo", Objects.requireNonNull("foo", supplier));
86          assertFalse(supplier.isInvoked());
87          try {
88              Objects.requireNonNull(null, supplier);
89              fail("Expected Exception");
90          } catch (final NullPointerException e) {
91              assertEquals("The supplier must not return null.", e.getMessage());
92              assertTrue(supplier.isInvoked());
93          }
94          final TestableFailableSupplier<String, ?> supplier2 = new TestableFailableSupplier<>(FailableSupplier.nul());
95          try {
96              Objects.requireNonNull(null, supplier2);
97              fail("Expected Exception");
98          } catch (final NullPointerException e) {
99              assertEquals("The supplier must not return null.", e.getMessage());
100             assertTrue(supplier2.isInvoked());
101         }
102         final TestableFailableSupplier<String, ?> supplier3 = new TestableFailableSupplier<>(() -> "bar");
103         assertSame("bar", Objects.requireNonNull(null, supplier3));
104         final RuntimeException rte = new RuntimeException();
105         final TestableFailableSupplier<String, ?> supplier4 = new TestableFailableSupplier<>(() -> {
106             throw rte;
107         });
108         try {
109             Objects.requireNonNull(null, supplier4);
110             fail("Expected Exception");
111         } catch (final RuntimeException e) {
112             assertSame(rte, e);
113             assertTrue(supplier4.isInvoked());
114         }
115     }
116 
117     @Test
118     void testRequireNonNullObjectString() {
119         assertSame("foo", Objects.requireNonNull("foo", "bar"));
120         try {
121             Objects.requireNonNull(null, "bar");
122             fail("Expected Exception");
123         } catch (final NullPointerException e) {
124             assertEquals("bar", e.getMessage());
125         }
126     }
127 
128     @Test
129     void testRequireNonNullObjectSupplierString() {
130         final TestableSupplier<String> supplier = new TestableSupplier<>(() -> "bar");
131         assertSame("foo", Objects.requireNonNull("foo", supplier));
132         assertFalse(supplier.isInvoked());
133         try {
134             Objects.requireNonNull(null, supplier);
135             fail("Expected Exception");
136         } catch (final NullPointerException e) {
137             assertEquals("bar", e.getMessage());
138             assertTrue(supplier.isInvoked());
139         }
140     }
141 }