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.assertFalse;
21  import static org.junit.jupiter.api.Assertions.assertThrows;
22  import static org.junit.jupiter.api.Assertions.assertTrue;
23  
24  import java.util.concurrent.atomic.AtomicBoolean;
25  
26  import org.apache.commons.lang3.AbstractLangTest;
27  import org.junit.jupiter.api.Assertions;
28  import org.junit.jupiter.api.Test;
29  
30  /**
31   * Tests {@link BooleanConsumer}.
32   */
33  public class BooleanConsumerTest extends AbstractLangTest {
34  
35      private BooleanConsumer accept(final BooleanConsumer consumer, final boolean expected) {
36          consumer.accept(expected);
37          return consumer;
38      }
39  
40      @Test
41      public void testAccept() {
42          final AtomicBoolean aBool = new AtomicBoolean();
43          accept(aBool::lazySet, true);
44          assertTrue(aBool.get());
45          accept(aBool::lazySet, false);
46          assertFalse(aBool.get());
47      }
48  
49      @Test
50      public void testAndThen() throws Throwable {
51          final BooleanConsumer nop = BooleanConsumer.nop();
52          nop.andThen(nop);
53          // Documented in Javadoc edge-case.
54          assertThrows(NullPointerException.class, () -> nop.andThen(null));
55  
56          final AtomicBoolean aBool1 = new AtomicBoolean();
57          final AtomicBoolean aBool2 = new AtomicBoolean();
58  
59          final BooleanConsumer bc = aBool1::lazySet;
60          final BooleanConsumer composite = bc.andThen(aBool2::lazySet);
61  
62          composite.accept(true);
63          assertTrue(aBool1.get());
64          assertTrue(aBool2.get());
65  
66          composite.accept(false);
67          assertFalse(aBool1.get());
68          assertFalse(aBool2.get());
69  
70          // Check order
71          final BooleanConsumer bad = value -> {
72              throw new IllegalStateException();
73          };
74          final BooleanConsumer badComposite = bad.andThen(aBool2::lazySet);
75  
76          Assertions.assertThrows(IllegalStateException.class, () -> badComposite.accept(true));
77          assertFalse(aBool2.get(), "Second consumer should not be invoked");
78      }
79  
80  }