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