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 import static org.junit.jupiter.api.Assertions.assertNotNull;
23 import static org.junit.jupiter.api.Assertions.assertThrows;
24
25 import java.io.IOException;
26
27 import org.apache.commons.lang3.AbstractLangTest;
28 import org.junit.jupiter.api.Test;
29
30
31
32
33 class FailableIntUnaryOperatorTest extends AbstractLangTest {
34
35 @Test
36 void testAndThen_composesCorrectly() throws IOException {
37 final FailableIntUnaryOperator<IOException> times2 = v -> v * 2;
38 final FailableIntUnaryOperator<IOException> plus1 = v -> v + 1;
39 assertEquals(7, times2.andThen(plus1).applyAsInt(3));
40 }
41
42 @Test
43 void testAndThen_nullAfter_throwsNullPointerException() throws Throwable {
44 final FailableIntUnaryOperator<IOException> op = v -> v;
45 assertNullPointerException(() -> op.andThen(null));
46 }
47
48 @Test
49 void testApplyAsInt_returnsResult() throws IOException {
50 final FailableIntUnaryOperator<IOException> op = v -> v * 3;
51 assertEquals(15, op.applyAsInt(5));
52 }
53
54 @Test
55 void testApplyAsInt_throwsException() {
56 final IOException expected = new IOException("fail");
57 final FailableIntUnaryOperator<IOException> op = v -> {
58 throw expected;
59 };
60 final IOException thrown = assertThrows(IOException.class, () -> op.applyAsInt(1));
61 assertEquals(expected, thrown);
62 }
63
64 @Test
65 void testCompose_composesCorrectly() throws IOException {
66 final FailableIntUnaryOperator<IOException> times2 = v -> v * 2;
67 final FailableIntUnaryOperator<IOException> plus1 = v -> v + 1;
68 assertEquals(8, times2.compose(plus1).applyAsInt(3));
69 }
70
71 @Test
72 void testCompose_nullBefore_throwsNullPointerException() throws Throwable {
73 final FailableIntUnaryOperator<IOException> op = v -> v;
74 assertNullPointerException(() -> op.compose(null));
75 }
76
77 @Test
78 void testIdentity_returnsInput() throws IOException {
79 assertEquals(7, FailableIntUnaryOperator.<IOException>identity().applyAsInt(7));
80 }
81
82 @Test
83 void testNop_applyAsIntReturnsZero() throws Throwable {
84 assertEquals(0, FailableIntUnaryOperator.nop().applyAsInt(5));
85 }
86
87 @Test
88 void testNop_returnsNonNull() {
89 assertNotNull(FailableIntUnaryOperator.nop());
90 }
91 }