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.io.function;
19  
20  import static org.junit.jupiter.api.Assertions.assertEquals;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.io.IOException;
25  import java.util.concurrent.atomic.AtomicBoolean;
26  import java.util.concurrent.atomic.AtomicInteger;
27  
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Tests {@code Erase}.
32   */
33  final class EraseTest {
34  
35      private final AtomicInteger intRef = new AtomicInteger();
36      private final AtomicBoolean boolRef = new AtomicBoolean();
37  
38      @Test
39      void testAcceptIOBiConsumerOfTUTU() {
40          Erase.accept((e, f) -> boolRef.set(intRef.compareAndSet(0, e)), 1, true);
41          assertEquals(1, intRef.get());
42          assertTrue(boolRef.get());
43          assertThrows(IOException.class, () -> Erase.accept(TestUtils.throwingIOBiConsumer(), null, 1));
44      }
45  
46      @Test
47      void testAcceptIOConsumerOfTT() {
48          Erase.accept(e -> intRef.compareAndSet(0, e), 1);
49          assertEquals(1, intRef.get());
50          assertThrows(IOException.class, () -> Erase.accept(TestUtils.throwingIOConsumer(), 1));
51      }
52  
53      @Test
54      void testApplyIOBiFunctionOfQsuperTQsuperUQextendsRTU() {
55          assertTrue(Erase.<Integer, Boolean, Boolean>apply((i, b) -> boolRef.compareAndSet(false, intRef.compareAndSet(0, i.intValue())), 1, Boolean.TRUE));
56          assertThrows(IOException.class, () -> Erase.apply(TestUtils.throwingIOBiFunction(), 1, Boolean.TRUE));
57      }
58  
59      @Test
60      void testApplyIOFunctionOfQsuperTQextendsRT() {
61          assertTrue(Erase.<Integer, Boolean>apply(e -> intRef.compareAndSet(0, e), 1));
62          assertThrows(IOException.class, () -> Erase.apply(TestUtils.throwingIOFunction(), 1));
63      }
64  
65      @Test
66      void testCompare() {
67          assertEquals(0, Erase.compare(String::compareTo, "A", "A"));
68          assertEquals(-1, Erase.compare(String::compareTo, "A", "B"));
69          assertEquals(1, Erase.compare(String::compareTo, "B", "A"));
70          assertThrows(IOException.class, () -> Erase.compare(TestUtils.throwingIOComparator(), null, null));
71      }
72  
73      @Test
74      void testGet() {
75          assertEquals(0, Erase.get(() -> intRef.get()));
76          assertThrows(IOException.class, () -> Erase.get(TestUtils.throwingIOSupplier()));
77      }
78  
79      @Test
80      void testRethrow() {
81          assertThrows(IOException.class, () -> Erase.rethrow(new IOException()));
82      }
83  
84      @Test
85      void testRun() {
86          Erase.run(() -> intRef.set(1));
87          assertEquals(1, intRef.get());
88          assertThrows(IOException.class, () -> Erase.run(TestUtils.throwingIORunnable()));
89      }
90  
91      @Test
92      void testTest() {
93          assertTrue(Erase.test(e -> intRef.compareAndSet(0, e), 1));
94          assertThrows(IOException.class, () -> Erase.test(TestUtils.throwingIOPredicate(), 1));
95      }
96  
97  }