From 3bd00a424767c851fab1131a41253bac65e53201 Mon Sep 17 00:00:00 2001 From: Alex Herbert Date: Tue, 24 Aug 2021 23:05:48 +0100 Subject: [PATCH] Assert the composite BooleanConsumer created by andThen --- .../lang3/function/BooleanConsumerTest.java | 31 ++++++++++++++++--- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/test/java/org/apache/commons/lang3/function/BooleanConsumerTest.java b/src/test/java/org/apache/commons/lang3/function/BooleanConsumerTest.java index e86b7e0da..2f7a90663 100644 --- a/src/test/java/org/apache/commons/lang3/function/BooleanConsumerTest.java +++ b/src/test/java/org/apache/commons/lang3/function/BooleanConsumerTest.java @@ -22,7 +22,8 @@ import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import java.util.concurrent.atomic.AtomicBoolean; - +import java.util.concurrent.atomic.AtomicInteger; +import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; /** @@ -50,12 +51,32 @@ public class BooleanConsumerTest { nop.andThen(nop); // Documented in Javadoc edge-case. assertThrows(NullPointerException.class, () -> nop.andThen(null)); - // + final AtomicBoolean aBool1 = new AtomicBoolean(); final AtomicBoolean aBool2 = new AtomicBoolean(); - accept(aBool1::lazySet, true).andThen(aBool2::lazySet); - accept(aBool1::lazySet, true); - accept(aBool2::lazySet, true); + + final BooleanConsumer bc = aBool1::lazySet; + final BooleanConsumer composite = bc.andThen(aBool2::lazySet); + + composite.accept(true); + assertTrue(aBool1.get()); + assertTrue(aBool2.get()); + + composite.accept(false); + assertFalse(aBool1.get()); + assertFalse(aBool2.get()); + + // Check order + final BooleanConsumer bad = new BooleanConsumer() { + @Override + public void accept(boolean value) { + throw new IllegalStateException(); + } + }; + final BooleanConsumer badComposite = bad.andThen(aBool2::lazySet); + + Assertions.assertThrows(IllegalStateException.class, () -> badComposite.accept(true)); + assertFalse(aBool2.get(), "Second consumer should not be invoked"); } }