1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.commons.lang3.function;
19
20 import static org.apache.commons.lang3.LangAssertions.assertNullPointerException;
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22
23 import java.util.concurrent.atomic.AtomicInteger;
24
25 import org.apache.commons.lang3.AbstractLangTest;
26 import org.junit.jupiter.api.Assertions;
27 import org.junit.jupiter.api.Test;
28
29
30
31
32 class ByteConsumerTest extends AbstractLangTest {
33
34 private static final byte B0 = (byte) 0;
35 private static final byte B1 = (byte) 1;
36
37 private ByteConsumer accept(final ByteConsumer consumer, final byte expected) {
38 consumer.accept(expected);
39 return consumer;
40 }
41
42 @Test
43 void testAccept() {
44 final AtomicInteger ref = new AtomicInteger();
45 accept(v -> ref.lazySet(v), B1);
46 assertEquals(1, ref.get());
47 accept(v -> ref.lazySet(v), B0);
48 assertEquals(0, ref.get());
49 }
50
51 @Test
52 void testAndThen() throws Throwable {
53 final ByteConsumer nop = ByteConsumer.nop();
54 nop.andThen(nop);
55
56 assertNullPointerException(() -> nop.andThen(null));
57
58 final AtomicInteger ref1 = new AtomicInteger();
59 final AtomicInteger ref2 = new AtomicInteger();
60
61 final ByteConsumer bc = ref1::lazySet;
62 final ByteConsumer composite = bc.andThen(ref2::lazySet);
63
64 composite.accept(B1);
65 assertEquals(1, ref1.get());
66 assertEquals(1, ref2.get());
67
68 composite.accept(B0);
69 assertEquals(0, ref1.get());
70 assertEquals(0, ref2.get());
71
72
73 final ByteConsumer bad = value -> {
74 throw new IllegalStateException();
75 };
76 final ByteConsumer badComposite = bad.andThen(ref2::lazySet);
77
78 Assertions.assertThrows(IllegalStateException.class, () -> badComposite.accept(B1));
79 assertEquals(0, ref2.get(), "Second consumer should not be invoked");
80 }
81
82 }