1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4;
18
19 import static org.junit.jupiter.api.Assertions.assertFalse;
20 import static org.junit.jupiter.api.Assertions.assertNotNull;
21 import static org.junit.jupiter.api.Assertions.assertSame;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24
25 import java.io.IOException;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.Collection;
29 import java.util.Collections;
30 import java.util.HashMap;
31 import java.util.List;
32 import java.util.Map;
33
34 import org.apache.commons.collections4.functors.AbstractPredicateTest;
35 import org.apache.commons.collections4.functors.AllPredicate;
36 import org.apache.commons.collections4.functors.EqualPredicate;
37 import org.apache.commons.collections4.functors.ExceptionPredicate;
38 import org.apache.commons.collections4.functors.FalsePredicate;
39 import org.apache.commons.collections4.functors.NotNullPredicate;
40 import org.apache.commons.collections4.functors.NullPredicate;
41 import org.apache.commons.collections4.functors.TruePredicate;
42 import org.junit.jupiter.api.Test;
43
44
45
46
47 @SuppressWarnings("boxing")
48 public class PredicateUtilsTest extends AbstractPredicateTest {
49
50 @Override
51 protected Predicate<?> generatePredicate() {
52 return TruePredicate.truePredicate();
53 }
54
55 @Test
56 @SuppressWarnings("unchecked")
57 public void testAllPredicate() {
58 assertPredicateTrue(AllPredicate.allPredicate(), null);
59 assertTrue(AllPredicate.allPredicate(TruePredicate.truePredicate(), TruePredicate.truePredicate(), TruePredicate.truePredicate()).test(null));
60 assertFalse(AllPredicate.allPredicate(TruePredicate.truePredicate(), FalsePredicate.falsePredicate(), TruePredicate.truePredicate()).test(null));
61 assertFalse(AllPredicate.allPredicate(FalsePredicate.falsePredicate(), FalsePredicate.falsePredicate(), TruePredicate.truePredicate()).test(null));
62 assertFalse(AllPredicate.allPredicate(FalsePredicate.falsePredicate(), FalsePredicate.falsePredicate(), FalsePredicate.falsePredicate()).evaluate(null));
63 final Collection<Predicate<Object>> coll = new ArrayList<>();
64 coll.add(TruePredicate.truePredicate());
65 coll.add(TruePredicate.truePredicate());
66 coll.add(TruePredicate.truePredicate());
67 assertTrue(AllPredicate.allPredicate(coll).evaluate(null));
68 coll.clear();
69 coll.add(TruePredicate.truePredicate());
70 coll.add(FalsePredicate.falsePredicate());
71 coll.add(TruePredicate.truePredicate());
72 assertFalse(AllPredicate.allPredicate(coll).evaluate(null));
73 coll.clear();
74 coll.add(FalsePredicate.falsePredicate());
75 coll.add(FalsePredicate.falsePredicate());
76 coll.add(TruePredicate.truePredicate());
77 assertFalse(AllPredicate.allPredicate(coll).evaluate(null));
78 coll.clear();
79 coll.add(FalsePredicate.falsePredicate());
80 coll.add(FalsePredicate.falsePredicate());
81 coll.add(FalsePredicate.falsePredicate());
82 assertFalse(AllPredicate.allPredicate(coll).evaluate(null));
83 coll.clear();
84 coll.add(FalsePredicate.falsePredicate());
85 assertPredicateFalse(AllPredicate.allPredicate(coll), null);
86 coll.clear();
87 coll.add(TruePredicate.truePredicate());
88 assertPredicateTrue(AllPredicate.allPredicate(coll), null);
89 coll.clear();
90 assertPredicateTrue(AllPredicate.allPredicate(coll), null);
91 }
92
93 @Test
94 public void testAllPredicateEx1() {
95 assertThrows(NullPointerException.class, () -> AllPredicate.allPredicate((Predicate<Object>[]) null));
96 }
97
98 @Test
99 @SuppressWarnings("unchecked")
100 public void testAllPredicateEx2() {
101 assertThrows(NullPointerException.class, () -> AllPredicate.<Object>allPredicate(new Predicate[] { null }));
102 }
103
104 @Test
105 public void testAllPredicateEx3() {
106 assertThrows(NullPointerException.class, () -> AllPredicate.allPredicate(null, null));
107 }
108
109 @Test
110 public void testAllPredicateEx4() {
111 assertThrows(NullPointerException.class, () -> AllPredicate.allPredicate((Collection<Predicate<Object>>) null));
112 }
113
114 @Test
115 public void testAllPredicateEx5() {
116 AllPredicate.allPredicate(Collections.emptyList());
117 }
118
119 @Test
120 public void testAllPredicateEx6() {
121 final Collection<Predicate<Object>> coll = new ArrayList<>();
122 coll.add(null);
123 coll.add(null);
124 assertThrows(NullPointerException.class, () -> AllPredicate.allPredicate(coll));
125 }
126
127 @Test
128 public void testAndPredicate() {
129 assertTrue(PredicateUtils.andPredicate(TruePredicate.truePredicate(), TruePredicate.truePredicate()).evaluate(null));
130 assertFalse(PredicateUtils.andPredicate(TruePredicate.truePredicate(), FalsePredicate.falsePredicate()).evaluate(null));
131 assertFalse(PredicateUtils.andPredicate(FalsePredicate.falsePredicate(), TruePredicate.truePredicate()).evaluate(null));
132 assertFalse(PredicateUtils.andPredicate(FalsePredicate.falsePredicate(), FalsePredicate.falsePredicate()).evaluate(null));
133 }
134
135 @Test
136 public void testAndPredicateEx() {
137 assertThrows(NullPointerException.class, () -> PredicateUtils.andPredicate(null, null));
138 }
139
140 @Test
141 @SuppressWarnings("unchecked")
142 public void testAnyPredicate() {
143 assertPredicateFalse(PredicateUtils.anyPredicate(), null);
144
145 assertTrue(PredicateUtils.anyPredicate(TruePredicate.truePredicate(), TruePredicate.truePredicate(), TruePredicate.truePredicate()).evaluate(null));
146 assertTrue(PredicateUtils.anyPredicate(TruePredicate.truePredicate(), FalsePredicate.falsePredicate(), TruePredicate.truePredicate()).evaluate(null));
147 assertTrue(PredicateUtils.anyPredicate(FalsePredicate.falsePredicate(), FalsePredicate.falsePredicate(), TruePredicate.truePredicate()).evaluate(null));
148 assertFalse(PredicateUtils.anyPredicate(FalsePredicate.falsePredicate(), FalsePredicate.falsePredicate(), FalsePredicate.falsePredicate()).evaluate(null));
149 final Collection<Predicate<Object>> coll = new ArrayList<>();
150 coll.add(TruePredicate.truePredicate());
151 coll.add(TruePredicate.truePredicate());
152 coll.add(TruePredicate.truePredicate());
153 assertTrue(PredicateUtils.anyPredicate(coll).evaluate(null));
154 coll.clear();
155 coll.add(TruePredicate.truePredicate());
156 coll.add(FalsePredicate.falsePredicate());
157 coll.add(TruePredicate.truePredicate());
158 assertTrue(PredicateUtils.anyPredicate(coll).evaluate(null));
159 coll.clear();
160 coll.add(FalsePredicate.falsePredicate());
161 coll.add(FalsePredicate.falsePredicate());
162 coll.add(TruePredicate.truePredicate());
163 assertTrue(PredicateUtils.anyPredicate(coll).evaluate(null));
164 coll.clear();
165 coll.add(FalsePredicate.falsePredicate());
166 coll.add(FalsePredicate.falsePredicate());
167 coll.add(FalsePredicate.falsePredicate());
168 assertFalse(PredicateUtils.anyPredicate(coll).evaluate(null));
169 coll.clear();
170 coll.add(FalsePredicate.falsePredicate());
171 assertPredicateFalse(PredicateUtils.anyPredicate(coll), null);
172 coll.clear();
173 coll.add(TruePredicate.truePredicate());
174 assertPredicateTrue(PredicateUtils.anyPredicate(coll), null);
175 coll.clear();
176 assertPredicateFalse(PredicateUtils.anyPredicate(coll), null);
177 }
178
179 @Test
180 public void testAnyPredicateEx1() {
181 assertThrows(NullPointerException.class, () -> PredicateUtils.anyPredicate((Predicate<Object>[]) null));
182 }
183
184 @Test
185 @SuppressWarnings("unchecked")
186 public void testAnyPredicateEx2() {
187 assertThrows(NullPointerException.class, () -> PredicateUtils.anyPredicate(new Predicate[] {null}));
188 }
189
190 @Test
191 public void testAnyPredicateEx3() {
192 assertThrows(NullPointerException.class, () -> PredicateUtils.anyPredicate(null, null));
193 }
194
195 @Test
196 public void testAnyPredicateEx4() {
197 assertThrows(NullPointerException.class, () -> PredicateUtils.anyPredicate((Collection<Predicate<Object>>) null));
198 }
199
200 @Test
201 public void testAnyPredicateEx5() {
202 PredicateUtils.anyPredicate(Collections.emptyList());
203 }
204
205 @Test
206 public void testAnyPredicateEx6() {
207 final Collection<Predicate<Object>> coll = new ArrayList<>();
208 coll.add(null);
209 coll.add(null);
210 assertThrows(NullPointerException.class, () -> PredicateUtils.anyPredicate(coll));
211 }
212
213 @Test
214 public void testAsPredicateTransformer() {
215 assertFalse(PredicateUtils.asPredicate(TransformerUtils.nopTransformer()).evaluate(false));
216 assertTrue(PredicateUtils.asPredicate(TransformerUtils.nopTransformer()).evaluate(true));
217 }
218
219 @Test
220 public void testAsPredicateTransformerEx1() {
221 assertThrows(NullPointerException.class, () -> PredicateUtils.asPredicate(null));
222 }
223
224 @Test
225 public void testAsPredicateTransformerEx2() {
226 assertThrows(FunctorException.class, () -> PredicateUtils.asPredicate(TransformerUtils.nopTransformer()).evaluate(null));
227 }
228
229 @Test
230 public void testEitherPredicate() {
231 assertFalse(PredicateUtils.eitherPredicate(TruePredicate.truePredicate(), TruePredicate.truePredicate()).evaluate(null));
232 assertTrue(PredicateUtils.eitherPredicate(TruePredicate.truePredicate(), FalsePredicate.falsePredicate()).evaluate(null));
233 assertTrue(PredicateUtils.eitherPredicate(FalsePredicate.falsePredicate(), TruePredicate.truePredicate()).evaluate(null));
234 assertFalse(PredicateUtils.eitherPredicate(FalsePredicate.falsePredicate(), FalsePredicate.falsePredicate()).evaluate(null));
235 }
236
237 @Test
238 public void testEitherPredicateEx() {
239 assertThrows(NullPointerException.class, () -> PredicateUtils.eitherPredicate(null, null));
240 }
241
242 @Test
243 public void testExceptionPredicate() {
244 assertNotNull(PredicateUtils.exceptionPredicate());
245 assertSame(PredicateUtils.exceptionPredicate(), PredicateUtils.exceptionPredicate());
246
247 assertThrows(FunctorException.class, () -> PredicateUtils.exceptionPredicate().evaluate(null));
248
249 assertThrows(FunctorException.class, () -> PredicateUtils.exceptionPredicate().evaluate(cString));
250 }
251
252 @Test
253 public void testFalsePredicate() {
254 assertNotNull(FalsePredicate.falsePredicate());
255 assertSame(FalsePredicate.falsePredicate(), FalsePredicate.falsePredicate());
256 assertFalse(FalsePredicate.falsePredicate().evaluate(null));
257 assertFalse(FalsePredicate.falsePredicate().evaluate(cObject));
258 assertFalse(FalsePredicate.falsePredicate().evaluate(cString));
259 assertFalse(FalsePredicate.falsePredicate().evaluate(cInteger));
260 }
261
262 @Test
263 public void testIdentityPredicate() {
264 assertSame(NullPredicate.nullPredicate(), PredicateUtils.identityPredicate(null));
265 assertNotNull(PredicateUtils.identityPredicate(6));
266 assertFalse(PredicateUtils.identityPredicate(6).evaluate(null));
267 assertFalse(PredicateUtils.<Object>identityPredicate(6).evaluate(cObject));
268 assertFalse(PredicateUtils.<Object>identityPredicate(6).evaluate(cString));
269 assertTrue(PredicateUtils.identityPredicate(6).evaluate(cInteger));
270 assertTrue(PredicateUtils.identityPredicate(cInteger).evaluate(cInteger));
271 }
272
273 @Test
274 public void testInstanceOfPredicate() {
275 assertNotNull(PredicateUtils.instanceofPredicate(String.class));
276 assertFalse(PredicateUtils.instanceofPredicate(String.class).evaluate(null));
277 assertFalse(PredicateUtils.instanceofPredicate(String.class).evaluate(cObject));
278 assertTrue(PredicateUtils.instanceofPredicate(String.class).evaluate(cString));
279 assertFalse(PredicateUtils.instanceofPredicate(String.class).evaluate(cInteger));
280 }
281
282 @Test
283 public void testInvokerPredicate() {
284 final List<Object> list = new ArrayList<>();
285 assertTrue(PredicateUtils.invokerPredicate("isEmpty").evaluate(list));
286 list.add(new Object());
287 assertFalse(PredicateUtils.invokerPredicate("isEmpty").evaluate(list));
288 }
289
290 @Test
291 public void testInvokerPredicate2() {
292 final List<String> list = new ArrayList<>();
293 assertFalse(PredicateUtils.invokerPredicate(
294 "contains", new Class[]{Object.class}, new Object[]{cString}).evaluate(list));
295 list.add(cString);
296 assertTrue(PredicateUtils.invokerPredicate(
297 "contains", new Class[]{Object.class}, new Object[]{cString}).evaluate(list));
298 }
299
300 @Test
301 public void testInvokerPredicate2Ex1() {
302 assertThrows(NullPointerException.class, () -> PredicateUtils.invokerPredicate(null, null, null));
303 }
304
305 @Test
306 public void testInvokerPredicate2Ex2() {
307 assertThrows(FunctorException.class, () -> PredicateUtils.
308 invokerPredicate("contains", new Class[] {Object.class}, new Object[] {cString}).evaluate(null));
309 }
310
311 @Test
312 public void testInvokerPredicate2Ex3() {
313 assertThrows(FunctorException.class, () -> PredicateUtils.invokerPredicate(
314 "noSuchMethod", new Class[] {Object.class}, new Object[] {cString}).evaluate(new Object()));
315 }
316
317 @Test
318 public void testInvokerPredicateEx1() {
319 assertThrows(NullPointerException.class, () -> PredicateUtils.invokerPredicate(null));
320 }
321
322 @Test
323 public void testInvokerPredicateEx2() {
324 assertThrows(FunctorException.class, () -> PredicateUtils.invokerPredicate("isEmpty").evaluate(null));
325 }
326
327 @Test
328 public void testInvokerPredicateEx3() {
329 assertThrows(FunctorException.class, () -> PredicateUtils.invokerPredicate("noSuchMethod").evaluate(new Object()));
330 }
331
332 @Test
333 public void testIsNotNullPredicate() {
334 assertNotNull(PredicateUtils.notNullPredicate());
335 assertSame(PredicateUtils.notNullPredicate(), PredicateUtils.notNullPredicate());
336 assertFalse(PredicateUtils.notNullPredicate().evaluate(null));
337 assertTrue(PredicateUtils.notNullPredicate().evaluate(cObject));
338 assertTrue(PredicateUtils.notNullPredicate().evaluate(cString));
339 assertTrue(PredicateUtils.notNullPredicate().evaluate(cInteger));
340 }
341
342 @Test
343 public void testNeitherPredicate() {
344 assertFalse(PredicateUtils.neitherPredicate(TruePredicate.truePredicate(), TruePredicate.truePredicate()).evaluate(null));
345 assertFalse(PredicateUtils.neitherPredicate(TruePredicate.truePredicate(), FalsePredicate.falsePredicate()).evaluate(null));
346 assertFalse(PredicateUtils.neitherPredicate(FalsePredicate.falsePredicate(), TruePredicate.truePredicate()).evaluate(null));
347 assertTrue(PredicateUtils.neitherPredicate(FalsePredicate.falsePredicate(), FalsePredicate.falsePredicate()).evaluate(null));
348 }
349
350 @Test
351 public void testNeitherPredicateEx() {
352 assertThrows(NullPointerException.class, () -> PredicateUtils.neitherPredicate(null, null));
353 }
354
355 @Test
356 @SuppressWarnings("unchecked")
357 public void testNonePredicate() {
358 assertPredicateTrue(PredicateUtils.nonePredicate(), null);
359 assertFalse(PredicateUtils.nonePredicate(TruePredicate.truePredicate(), TruePredicate.truePredicate(), TruePredicate.truePredicate()).evaluate(null));
360 assertFalse(PredicateUtils.nonePredicate(TruePredicate.truePredicate(), FalsePredicate.falsePredicate(), TruePredicate.truePredicate()).evaluate(null));
361 assertFalse(PredicateUtils.nonePredicate(FalsePredicate.falsePredicate(), FalsePredicate.falsePredicate(), TruePredicate.truePredicate()).evaluate(null));
362 assertTrue(PredicateUtils.nonePredicate(FalsePredicate.falsePredicate(), FalsePredicate.falsePredicate(), FalsePredicate.falsePredicate()).evaluate(null));
363 final Collection<Predicate<Object>> coll = new ArrayList<>();
364 coll.add(TruePredicate.truePredicate());
365 coll.add(TruePredicate.truePredicate());
366 coll.add(TruePredicate.truePredicate());
367 assertFalse(PredicateUtils.nonePredicate(coll).evaluate(null));
368 coll.clear();
369 coll.add(TruePredicate.truePredicate());
370 coll.add(FalsePredicate.falsePredicate());
371 coll.add(TruePredicate.truePredicate());
372 assertFalse(PredicateUtils.nonePredicate(coll).evaluate(null));
373 coll.clear();
374 coll.add(FalsePredicate.falsePredicate());
375 coll.add(FalsePredicate.falsePredicate());
376 coll.add(TruePredicate.truePredicate());
377 assertFalse(PredicateUtils.nonePredicate(coll).evaluate(null));
378 coll.clear();
379 coll.add(FalsePredicate.falsePredicate());
380 coll.add(FalsePredicate.falsePredicate());
381 coll.add(FalsePredicate.falsePredicate());
382 assertTrue(PredicateUtils.nonePredicate(coll).evaluate(null));
383 coll.clear();
384 coll.add(FalsePredicate.falsePredicate());
385 assertPredicateTrue(PredicateUtils.nonePredicate(coll), null);
386 coll.clear();
387 coll.add(TruePredicate.truePredicate());
388 assertPredicateFalse(PredicateUtils.nonePredicate(coll), null);
389 coll.clear();
390 assertPredicateTrue(PredicateUtils.nonePredicate(coll), null);
391 }
392
393 @Test
394 public void testNonePredicateEx1() {
395 assertThrows(NullPointerException.class, () -> PredicateUtils.nonePredicate((Predicate<Object>[]) null));
396 }
397
398 @Test
399 @SuppressWarnings("unchecked")
400 public void testNonePredicateEx2() {
401 assertThrows(NullPointerException.class, () -> PredicateUtils.nonePredicate(new Predicate[] {null}));
402 }
403
404 @Test
405 @SuppressWarnings("unchecked")
406 public void testNonePredicateEx3() {
407 assertThrows(NullPointerException.class, () -> PredicateUtils.nonePredicate(null, null));
408 }
409
410 @Test
411 public void testNonePredicateEx4() {
412 assertThrows(NullPointerException.class, () -> PredicateUtils.nonePredicate((Collection<Predicate<Object>>) null));
413 }
414
415 @Test
416 public void testNonePredicateEx5() {
417 PredicateUtils.nonePredicate(Collections.emptyList());
418 }
419
420 @Test
421 public void testNonePredicateEx6() {
422 final Collection<Predicate<Object>> coll = new ArrayList<>();
423 coll.add(null);
424 coll.add(null);
425 assertThrows(NullPointerException.class, () -> PredicateUtils.nonePredicate(coll));
426 }
427
428 @Test
429 public void testNotPredicate() {
430 assertNotNull(PredicateUtils.notPredicate(TruePredicate.truePredicate()));
431 assertFalse(PredicateUtils.notPredicate(TruePredicate.truePredicate()).evaluate(null));
432 assertFalse(PredicateUtils.notPredicate(TruePredicate.truePredicate()).evaluate(cObject));
433 assertFalse(PredicateUtils.notPredicate(TruePredicate.truePredicate()).evaluate(cString));
434 assertFalse(PredicateUtils.notPredicate(TruePredicate.truePredicate()).evaluate(cInteger));
435 }
436
437 @Test
438 public void testNotPredicateEx() {
439 assertThrows(NullPointerException.class, () -> PredicateUtils.notPredicate(null));
440 }
441
442 @Test
443 public void testNullIsExceptionPredicate() {
444 assertTrue(PredicateUtils.nullIsExceptionPredicate(TruePredicate.truePredicate()).evaluate(new Object()));
445 assertThrows(FunctorException.class, () -> PredicateUtils.nullIsExceptionPredicate(TruePredicate.truePredicate()).evaluate(null));
446 }
447
448 @Test
449 public void testNullIsExceptionPredicateEx1() {
450 assertThrows(NullPointerException.class, () -> PredicateUtils.nullIsExceptionPredicate(null));
451 }
452
453 @Test
454 public void testNullIsFalsePredicate() {
455 assertFalse(PredicateUtils.nullIsFalsePredicate(TruePredicate.truePredicate()).evaluate(null));
456 assertTrue(PredicateUtils.nullIsFalsePredicate(TruePredicate.truePredicate()).evaluate(new Object()));
457 assertFalse(PredicateUtils.nullIsFalsePredicate(FalsePredicate.falsePredicate()).evaluate(new Object()));
458 }
459
460 @Test
461 public void testNullIsFalsePredicateEx1() {
462 assertThrows(NullPointerException.class, () -> PredicateUtils.nullIsFalsePredicate(null));
463 }
464
465 @Test
466 public void testNullIsTruePredicate() {
467 assertTrue(PredicateUtils.nullIsTruePredicate(TruePredicate.truePredicate()).evaluate(null));
468 assertTrue(PredicateUtils.nullIsTruePredicate(TruePredicate.truePredicate()).evaluate(new Object()));
469 assertFalse(PredicateUtils.nullIsTruePredicate(FalsePredicate.falsePredicate()).evaluate(new Object()));
470 }
471
472 @Test
473 public void testNullIsTruePredicateEx1() {
474 assertThrows(NullPointerException.class, () -> PredicateUtils.nullIsTruePredicate(null));
475 }
476
477 @Test
478 @SuppressWarnings("unchecked")
479 public void testOnePredicate() {
480 assertPredicateFalse(PredicateUtils.onePredicate((Predicate<Object>[]) new Predicate[] {}), null);
481 assertFalse(PredicateUtils.onePredicate(TruePredicate.truePredicate(), TruePredicate.truePredicate(), TruePredicate.truePredicate()).evaluate(null));
482 assertFalse(PredicateUtils.onePredicate(TruePredicate.truePredicate(), FalsePredicate.falsePredicate(), TruePredicate.truePredicate()).evaluate(null));
483 assertTrue(PredicateUtils.onePredicate(TruePredicate.truePredicate(), FalsePredicate.falsePredicate(), FalsePredicate.falsePredicate()).evaluate(null));
484 assertTrue(PredicateUtils.onePredicate(FalsePredicate.falsePredicate(), TruePredicate.truePredicate(), FalsePredicate.falsePredicate()).evaluate(null));
485 assertTrue(PredicateUtils.onePredicate(FalsePredicate.falsePredicate(), FalsePredicate.falsePredicate(), TruePredicate.truePredicate()).evaluate(null));
486 assertFalse(PredicateUtils.onePredicate(FalsePredicate.falsePredicate(), FalsePredicate.falsePredicate(), FalsePredicate.falsePredicate()).evaluate(null));
487 final Collection<Predicate<Object>> coll = new ArrayList<>();
488 coll.add(TruePredicate.truePredicate());
489 coll.add(TruePredicate.truePredicate());
490 coll.add(TruePredicate.truePredicate());
491 assertFalse(PredicateUtils.onePredicate(coll).evaluate(null));
492 coll.clear();
493 coll.add(TruePredicate.truePredicate());
494 coll.add(FalsePredicate.falsePredicate());
495 coll.add(TruePredicate.truePredicate());
496 assertFalse(PredicateUtils.onePredicate(coll).evaluate(null));
497 coll.clear();
498 coll.add(FalsePredicate.falsePredicate());
499 coll.add(FalsePredicate.falsePredicate());
500 coll.add(TruePredicate.truePredicate());
501 assertTrue(PredicateUtils.onePredicate(coll).evaluate(null));
502 coll.clear();
503 coll.add(FalsePredicate.falsePredicate());
504 coll.add(FalsePredicate.falsePredicate());
505 coll.add(FalsePredicate.falsePredicate());
506 assertFalse(PredicateUtils.onePredicate(coll).evaluate(null));
507 coll.clear();
508 coll.add(FalsePredicate.falsePredicate());
509 assertPredicateFalse(PredicateUtils.onePredicate(coll), null);
510 coll.clear();
511 coll.add(TruePredicate.truePredicate());
512 assertPredicateTrue(PredicateUtils.onePredicate(coll), null);
513 coll.clear();
514 assertPredicateFalse(PredicateUtils.onePredicate(coll), null);
515 }
516
517 @Test
518 public void testOnePredicateEx1() {
519 assertThrows(NullPointerException.class, () -> PredicateUtils.onePredicate((Predicate<Object>[]) null));
520 }
521
522 @Test
523 @SuppressWarnings("unchecked")
524 public void testOnePredicateEx2() {
525 assertThrows(NullPointerException.class, () -> PredicateUtils.onePredicate(new Predicate[] {null}));
526 }
527
528 @Test
529 public void testOnePredicateEx3() {
530 assertThrows(NullPointerException.class, () -> PredicateUtils.onePredicate(null, null));
531 }
532
533 @Test
534 public void testOnePredicateEx4() {
535 assertThrows(NullPointerException.class, () -> PredicateUtils.onePredicate((Collection<Predicate<Object>>) null));
536 }
537
538 @Test
539 @SuppressWarnings("unchecked")
540 public void testOnePredicateEx5() {
541 PredicateUtils.onePredicate(Collections.EMPTY_LIST);
542 }
543
544 @Test
545 public void testOnePredicateEx6() {
546 assertThrows(NullPointerException.class, () -> PredicateUtils.onePredicate(Arrays.asList(null, null)));
547 }
548
549 @Test
550 public void testOrPredicate() {
551 assertTrue(PredicateUtils.orPredicate(TruePredicate.truePredicate(), TruePredicate.truePredicate()).evaluate(null));
552 assertTrue(PredicateUtils.orPredicate(TruePredicate.truePredicate(), FalsePredicate.falsePredicate()).evaluate(null));
553 assertTrue(PredicateUtils.orPredicate(FalsePredicate.falsePredicate(), TruePredicate.truePredicate()).evaluate(null));
554 assertFalse(PredicateUtils.orPredicate(FalsePredicate.falsePredicate(), FalsePredicate.falsePredicate()).evaluate(null));
555 }
556
557 @Test
558 public void testOrPredicateEx() {
559 assertThrows(NullPointerException.class, () -> PredicateUtils.orPredicate(null, null));
560 }
561
562
563
564
565
566 @Test
567 public void testSingletonPatternInSerialization() throws ClassNotFoundException, IOException {
568 final Object[] singletons = {
569 ExceptionPredicate.INSTANCE,
570 FalsePredicate.INSTANCE,
571 NotNullPredicate.INSTANCE,
572 NullPredicate.INSTANCE,
573 TruePredicate.INSTANCE
574 };
575
576 for (final Object original : singletons) {
577 TestUtils.assertSameAfterSerialization(
578 "Singleton pattern broken for " + original.getClass(),
579 original
580 );
581 }
582 }
583
584 @Test
585 public void testTransformedPredicate() {
586 assertTrue(PredicateUtils.transformedPredicate(
587 TransformerUtils.nopTransformer(),
588 TruePredicate.truePredicate()).evaluate(new Object()));
589
590 final Map<Object, Object> map = new HashMap<>();
591 map.put(Boolean.TRUE, "Hello");
592 final Transformer<Object, Object> t = TransformerUtils.mapTransformer(map);
593 final Predicate<Object> p = EqualPredicate.<Object>equalPredicate("Hello");
594 assertFalse(PredicateUtils.transformedPredicate(t, p).evaluate(null));
595 assertTrue(PredicateUtils.transformedPredicate(t, p).evaluate(Boolean.TRUE));
596
597 assertThrows(NullPointerException.class, () -> PredicateUtils.transformedPredicate(null, null));
598 }
599
600 @Test
601 public void testTruePredicate() {
602 assertNotNull(TruePredicate.truePredicate());
603 assertSame(TruePredicate.truePredicate(), TruePredicate.truePredicate());
604 assertTrue(TruePredicate.truePredicate().evaluate(null));
605 assertTrue(TruePredicate.truePredicate().evaluate(cObject));
606 assertTrue(TruePredicate.truePredicate().evaluate(cString));
607 assertTrue(TruePredicate.truePredicate().evaluate(cInteger));
608 }
609
610 @Test
611 public void testUniquePredicate() {
612 final Predicate<Object> p = PredicateUtils.uniquePredicate();
613 assertTrue(p.evaluate(new Object()));
614 assertTrue(p.evaluate(new Object()));
615 assertTrue(p.evaluate(new Object()));
616 assertTrue(p.evaluate(cString));
617 assertFalse(p.evaluate(cString));
618 assertFalse(p.evaluate(cString));
619 }
620
621 }