1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.juneau.commons.utils;
18
19 import static org.apache.juneau.commons.utils.PredicateUtils.*;
20 import static org.junit.jupiter.api.Assertions.*;
21
22 import java.io.ByteArrayOutputStream;
23 import java.io.PrintStream;
24 import java.util.*;
25 import java.util.function.*;
26
27 import org.junit.jupiter.api.*;
28
29 class PredicateUtils_Test {
30
31
32
33
34 @Test
35 void a00_constructor() {
36
37
38
39 }
40
41
42
43
44 @Test
45 void a001_consumeIf() {
46 List<String> consumed = new ArrayList<>();
47 Consumer<String> consumer = consumed::add;
48
49
50 consumeIf(null, consumer, "test");
51 assertEquals(1, consumed.size());
52 assertEquals("test", consumed.get(0));
53
54
55 consumed.clear();
56 Predicate<String> matches = s -> s.equals("match");
57 consumeIf(matches, consumer, "match");
58 assertEquals(1, consumed.size());
59 assertEquals("match", consumed.get(0));
60
61
62 consumed.clear();
63 Predicate<String> noMatch = s -> s.equals("match");
64 consumeIf(noMatch, consumer, "nomatch");
65 assertTrue(consumed.isEmpty());
66
67
68 List<Integer> intConsumed = new ArrayList<>();
69 Consumer<Integer> intConsumer = intConsumed::add;
70 Predicate<Integer> even = i -> i % 2 == 0;
71 consumeIf(even, intConsumer, 2);
72 assertEquals(1, intConsumed.size());
73 assertEquals(2, intConsumed.get(0));
74
75 consumeIf(even, intConsumer, 3);
76 assertEquals(1, intConsumed.size());
77 }
78
79
80
81
82 @Test
83 void a002_peek() {
84
85 PrintStream originalErr = System.err;
86 ByteArrayOutputStream errCapture = new ByteArrayOutputStream();
87 System.setErr(new PrintStream(errCapture));
88
89 try {
90
91 Function<String, String> peekFunc = peek();
92 String result = peekFunc.apply("test value");
93
94
95 assertEquals("test value", result);
96
97
98 String output = errCapture.toString();
99 assertTrue(output.contains("test value"), "Output should contain 'test value', but was: " + output);
100
101
102 errCapture.reset();
103 Function<Object, Object> peekFunc2 = peek();
104 Object result2 = peekFunc2.apply(null);
105 assertNull(result2);
106 String output2 = errCapture.toString();
107 assertTrue(output2.contains("null"), "Output should contain 'null', but was: " + output2);
108
109
110 errCapture.reset();
111 Function<Integer, Integer> peekInt = peek();
112 Integer result3 = peekInt.apply(123);
113 assertEquals(123, result3);
114 String output3 = errCapture.toString();
115 assertTrue(output3.contains("123"), "Output should contain '123', but was: " + output3);
116 } finally {
117 System.setErr(originalErr);
118 }
119 }
120
121
122
123
124 @Test
125 void a003_peek_withMessage() {
126
127 PrintStream originalErr = System.err;
128 ByteArrayOutputStream errCapture = new ByteArrayOutputStream();
129 System.setErr(new PrintStream(errCapture));
130
131 try {
132
133 Function<String, String> peekFunc = peek("Processing: {0}", s -> s.toUpperCase());
134 String result = peekFunc.apply("test");
135
136
137 assertEquals("test", result);
138
139
140 String output = errCapture.toString();
141 assertTrue(output.contains("Processing: TEST"), "Output should contain 'Processing: TEST', but was: " + output);
142
143
144 errCapture.reset();
145 Function<Integer, Integer> peekInt = peek("Value: {0}", i -> i * 2);
146 Integer result2 = peekInt.apply(5);
147 assertEquals(5, result2);
148 String output2 = errCapture.toString();
149 assertTrue(output2.contains("Value: 10"), "Output should contain 'Value: 10', but was: " + output2);
150
151
152 errCapture.reset();
153 Function<String, String> peekNull = peek("Null value: {0}", s -> s == null ? "null" : s);
154 String result3 = peekNull.apply(null);
155 assertNull(result3);
156 String output3 = errCapture.toString();
157 assertTrue(output3.contains("Null value: null"), "Output should contain 'Null value: null', but was: " + output3);
158
159
160 errCapture.reset();
161 class Person {
162 String name;
163 Person(String name) { this.name = name; }
164 }
165 Function<Person, Person> peekPerson = peek("Person: {0}", p -> p.name);
166 Person person = new Person("John");
167 Person result4 = peekPerson.apply(person);
168 assertSame(person, result4);
169 String output4 = errCapture.toString();
170 assertTrue(output4.contains("Person: John"), "Output should contain 'Person: John', but was: " + output4);
171 } finally {
172 System.setErr(originalErr);
173 }
174 }
175
176
177
178
179 @Test
180 void a004_test() {
181
182 assertTrue(test(null, "any value"));
183 assertTrue(test(null, null));
184 assertTrue(test(null, 123));
185
186
187 Predicate<String> matches = s -> s.equals("match");
188 assertTrue(test(matches, "match"));
189
190
191 assertFalse(test(matches, "nomatch"));
192
193
194 Predicate<Integer> even = i -> i % 2 == 0;
195 assertTrue(test(even, 2));
196 assertTrue(test(even, 4));
197 assertFalse(test(even, 3));
198 assertFalse(test(even, 5));
199
200
201 Predicate<String> notNull = s -> s != null;
202 assertTrue(test(notNull, "test"));
203 assertFalse(test(notNull, null));
204
205
206 Predicate<Object> alwaysTrue = o -> true;
207 assertTrue(test(alwaysTrue, "anything"));
208 assertTrue(test(alwaysTrue, null));
209 assertTrue(test(alwaysTrue, 123));
210
211
212 Predicate<Object> alwaysFalse = o -> false;
213 assertFalse(test(alwaysFalse, "anything"));
214 assertFalse(test(alwaysFalse, null));
215 assertFalse(test(alwaysFalse, 123));
216 }
217 }