|
| 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 | + * https://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.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 | + * Tests {@link ByteConsumer}. |
| 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 | + // Documented in Javadoc edge-case. |
| 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 | + // Check order |
| 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 | +} |
0 commit comments