Assert the composite BooleanConsumer created by andThen

This commit is contained in:
Alex Herbert 2021-08-24 23:05:48 +01:00
parent dcc6568100
commit 3bd00a4247
1 changed files with 26 additions and 5 deletions

View File

@ -22,7 +22,8 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
/** /**
@ -50,12 +51,32 @@ public class BooleanConsumerTest {
nop.andThen(nop); nop.andThen(nop);
// Documented in Javadoc edge-case. // Documented in Javadoc edge-case.
assertThrows(NullPointerException.class, () -> nop.andThen(null)); assertThrows(NullPointerException.class, () -> nop.andThen(null));
//
final AtomicBoolean aBool1 = new AtomicBoolean(); final AtomicBoolean aBool1 = new AtomicBoolean();
final AtomicBoolean aBool2 = new AtomicBoolean(); final AtomicBoolean aBool2 = new AtomicBoolean();
accept(aBool1::lazySet, true).andThen(aBool2::lazySet);
accept(aBool1::lazySet, true); final BooleanConsumer bc = aBool1::lazySet;
accept(aBool2::lazySet, true); 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");
} }
} }