1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.juneau.commons.function;
18
19 import static org.junit.jupiter.api.Assertions.*;
20
21 import java.util.concurrent.atomic.AtomicInteger;
22
23 import org.apache.juneau.*;
24 import org.junit.jupiter.api.*;
25
26 class ThrowingConsumer4_Test extends TestBase {
27
28
29
30
31 @Test void a01_basic() {
32 var callCount = new AtomicInteger();
33 var receivedValues = new Object[4];
34
35 var consumer = (ThrowingConsumer4<String, Integer, Boolean, Double>)(a, b, c, d) -> {
36 callCount.incrementAndGet();
37 receivedValues[0] = a;
38 receivedValues[1] = b;
39 receivedValues[2] = c;
40 receivedValues[3] = d;
41 };
42
43 consumer.apply("test", 42, true, 3.14);
44 assertEquals(1, callCount.get());
45 assertEquals("test", receivedValues[0]);
46 assertEquals(42, receivedValues[1]);
47 assertEquals(true, receivedValues[2]);
48 assertEquals(3.14, receivedValues[3]);
49 }
50
51
52
53
54 @Test void b01_throwsCheckedException() {
55 var consumer = (ThrowingConsumer4<String, Integer, Boolean, Double>)(a, b, c, d) -> {
56 throw new Exception("Test exception");
57 };
58
59 var ex = assertThrows(RuntimeException.class, () -> {
60 consumer.apply("test", 42, true, 3.14);
61 });
62
63 assertTrue(ex.getCause() instanceof Exception);
64 assertEquals("Test exception", ex.getCause().getMessage());
65 }
66
67 @Test void b02_throwsRuntimeException() {
68 var consumer = (ThrowingConsumer4<String, Integer, Boolean, Double>)(a, b, c, d) -> {
69 throw new RuntimeException("Test runtime exception");
70 };
71
72 var ex = assertThrows(RuntimeException.class, () -> {
73 consumer.apply("test", 42, true, 3.14);
74 });
75
76 assertEquals("Test runtime exception", ex.getMessage());
77 assertNull(ex.getCause());
78 }
79
80
81
82
83 @Test void c01_usedAsConsumer4() {
84 var callCount = new AtomicInteger();
85
86 var consumer = (Consumer4<String, Integer, Boolean, Double>)(a, b, c, d) -> {
87 callCount.incrementAndGet();
88 };
89
90 consumer.apply("test", 42, true, 3.14);
91 assertEquals(1, callCount.get());
92 }
93
94 @Test void c02_lambdaExpression() {
95 var sum = new AtomicInteger(0);
96
97 ThrowingConsumer4<Integer, Integer, Integer, Integer> consumer = (a, b, c, d) -> {
98 sum.addAndGet(a + b + c + d);
99 };
100
101 consumer.apply(1, 2, 3, 4);
102 assertEquals(10, sum.get());
103 }
104
105 @Test void c03_andThen() {
106 var callCount1 = new AtomicInteger();
107 var callCount2 = new AtomicInteger();
108
109 ThrowingConsumer4<String, Integer, Boolean, Double> consumer1 = (a, b, c, d) -> callCount1.incrementAndGet();
110 ThrowingConsumer4<String, Integer, Boolean, Double> consumer2 = (a, b, c, d) -> callCount2.incrementAndGet();
111
112 var composed = consumer1.andThen(consumer2);
113 composed.apply("test", 42, true, 3.14);
114
115 assertEquals(1, callCount1.get());
116 assertEquals(1, callCount2.get());
117 }
118 }
119