1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.base;
18
19 import static com.google.common.base.CharMatcher.WHITESPACE;
20 import static com.google.common.collect.Lists.newArrayList;
21
22 import com.google.common.annotations.GwtCompatible;
23 import com.google.common.annotations.GwtIncompatible;
24 import com.google.common.collect.ImmutableSet;
25 import com.google.common.testing.ClassSanityTester;
26 import com.google.common.testing.EqualsTester;
27 import com.google.common.testing.NullPointerTester;
28 import com.google.common.testing.SerializableTester;
29
30 import junit.framework.AssertionFailedError;
31 import junit.framework.TestCase;
32
33 import java.io.Serializable;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.Collection;
37 import java.util.Collections;
38 import java.util.Iterator;
39 import java.util.List;
40 import java.util.regex.Pattern;
41
42
43
44
45
46
47 @GwtCompatible(emulated = true)
48 public class PredicatesTest extends TestCase {
49 private static final Predicate<Integer> TRUE = Predicates.alwaysTrue();
50 private static final Predicate<Integer> FALSE = Predicates.alwaysFalse();
51 private static final Predicate<Integer> NEVER_REACHED =
52 new Predicate<Integer>() {
53 @Override
54 public boolean apply(Integer i) {
55 throw new AssertionFailedError(
56 "This predicate should never have been evaluated");
57 }
58 };
59
60
61 static class IsOdd implements Predicate<Integer>, Serializable {
62 private static final long serialVersionUID = 0x150ddL;
63 @Override
64 public boolean apply(Integer i) {
65 return (i.intValue() & 1) == 1;
66 }
67 @Override public int hashCode() {
68 return 0x150dd;
69 }
70 @Override public boolean equals(Object obj) {
71 return obj instanceof IsOdd;
72 }
73 @Override public String toString() {
74 return "IsOdd";
75 }
76 }
77
78
79
80
81
82
83
84 private static IsOdd isOdd() {
85 return new IsOdd();
86 }
87
88
89
90
91
92 public void testAlwaysTrue_apply() {
93 assertEvalsToTrue(Predicates.alwaysTrue());
94 }
95
96 public void testAlwaysTrue_equality() throws Exception {
97 new EqualsTester()
98 .addEqualityGroup(TRUE, Predicates.alwaysTrue())
99 .addEqualityGroup(isOdd())
100 .addEqualityGroup(Predicates.alwaysFalse())
101 .testEquals();
102 }
103
104 @GwtIncompatible("SerializableTester")
105 public void testAlwaysTrue_serialization() {
106 checkSerialization(Predicates.alwaysTrue());
107 }
108
109
110
111
112
113 public void testAlwaysFalse_apply() throws Exception {
114 assertEvalsToFalse(Predicates.alwaysFalse());
115 }
116
117 public void testAlwaysFalse_equality() throws Exception {
118 new EqualsTester()
119 .addEqualityGroup(FALSE, Predicates.alwaysFalse())
120 .addEqualityGroup(isOdd())
121 .addEqualityGroup(Predicates.alwaysTrue())
122 .testEquals();
123 }
124
125 @GwtIncompatible("SerializableTester")
126 public void testAlwaysFalse_serialization() {
127 checkSerialization(Predicates.alwaysFalse());
128 }
129
130
131
132
133
134 public void testNot_apply() {
135 assertEvalsToTrue(Predicates.not(FALSE));
136 assertEvalsToFalse(Predicates.not(TRUE));
137 assertEvalsLikeOdd(Predicates.not(Predicates.not(isOdd())));
138 }
139
140 public void testNot_equality() {
141 new EqualsTester()
142 .addEqualityGroup(Predicates.not(isOdd()), Predicates.not(isOdd()))
143 .addEqualityGroup(Predicates.not(TRUE))
144 .addEqualityGroup(isOdd())
145 .testEquals();
146 }
147
148 public void testNot_equalityForNotOfKnownValues() {
149 new EqualsTester()
150 .addEqualityGroup(TRUE, Predicates.alwaysTrue())
151 .addEqualityGroup(FALSE)
152 .addEqualityGroup(Predicates.not(TRUE))
153 .testEquals();
154
155 new EqualsTester()
156 .addEqualityGroup(FALSE, Predicates.alwaysFalse())
157 .addEqualityGroup(TRUE)
158 .addEqualityGroup(Predicates.not(FALSE))
159 .testEquals();
160
161 new EqualsTester()
162 .addEqualityGroup(Predicates.isNull(), Predicates.isNull())
163 .addEqualityGroup(Predicates.notNull())
164 .addEqualityGroup(Predicates.not(Predicates.isNull()))
165 .testEquals();
166
167 new EqualsTester()
168 .addEqualityGroup(Predicates.notNull(), Predicates.notNull())
169 .addEqualityGroup(Predicates.isNull())
170 .addEqualityGroup(Predicates.not(Predicates.notNull()))
171 .testEquals();
172 }
173
174 @GwtIncompatible("SerializableTester")
175 public void testNot_serialization() {
176 checkSerialization(Predicates.not(isOdd()));
177 }
178
179
180
181
182
183 @SuppressWarnings("unchecked")
184 public void testAnd_applyNoArgs() {
185 assertEvalsToTrue(Predicates.and());
186 }
187
188 @SuppressWarnings("unchecked")
189 public void testAnd_equalityNoArgs() {
190 new EqualsTester()
191 .addEqualityGroup(Predicates.and(), Predicates.and())
192 .addEqualityGroup(Predicates.and(FALSE))
193 .addEqualityGroup(Predicates.or())
194 .testEquals();
195 }
196
197 @GwtIncompatible("SerializableTester")
198 @SuppressWarnings("unchecked")
199 public void testAnd_serializationNoArgs() {
200 checkSerialization(Predicates.and());
201 }
202
203 @SuppressWarnings("unchecked")
204 public void testAnd_applyOneArg() {
205 assertEvalsLikeOdd(Predicates.and(isOdd()));
206 }
207
208 @SuppressWarnings("unchecked")
209 public void testAnd_equalityOneArg() {
210 Object[] notEqualObjects = {Predicates.and(NEVER_REACHED, FALSE)};
211 new EqualsTester()
212 .addEqualityGroup(
213 Predicates.and(NEVER_REACHED), Predicates.and(NEVER_REACHED))
214 .addEqualityGroup(notEqualObjects)
215 .addEqualityGroup(Predicates.and(isOdd()))
216 .addEqualityGroup(Predicates.and())
217 .addEqualityGroup(Predicates.or(NEVER_REACHED))
218 .testEquals();
219 }
220
221 @GwtIncompatible("SerializableTester")
222 @SuppressWarnings("unchecked")
223 public void testAnd_serializationOneArg() {
224 checkSerialization(Predicates.and(isOdd()));
225 }
226
227 public void testAnd_applyBinary() {
228 assertEvalsLikeOdd(Predicates.and(isOdd(), TRUE));
229 assertEvalsLikeOdd(Predicates.and(TRUE, isOdd()));
230 assertEvalsToFalse(Predicates.and(FALSE, NEVER_REACHED));
231 }
232
233 @SuppressWarnings("unchecked")
234 public void testAnd_equalityBinary() {
235 new EqualsTester()
236 .addEqualityGroup(
237 Predicates.and(TRUE, NEVER_REACHED),
238 Predicates.and(TRUE, NEVER_REACHED))
239 .addEqualityGroup(Predicates.and(NEVER_REACHED, TRUE))
240 .addEqualityGroup(Predicates.and(TRUE))
241 .addEqualityGroup(Predicates.or(TRUE, NEVER_REACHED))
242 .testEquals();
243 }
244
245 @GwtIncompatible("SerializableTester")
246 public void testAnd_serializationBinary() {
247 checkSerialization(Predicates.and(TRUE, isOdd()));
248 }
249
250 @SuppressWarnings("unchecked")
251 public void testAnd_applyTernary() {
252 assertEvalsLikeOdd(Predicates.and(isOdd(), TRUE, TRUE));
253 assertEvalsLikeOdd(Predicates.and(TRUE, isOdd(), TRUE));
254 assertEvalsLikeOdd(Predicates.and(TRUE, TRUE, isOdd()));
255 assertEvalsToFalse(Predicates.and(TRUE, FALSE, NEVER_REACHED));
256 }
257
258 @SuppressWarnings("unchecked")
259 public void testAnd_equalityTernary() {
260 new EqualsTester()
261 .addEqualityGroup(
262 Predicates.and(TRUE, isOdd(), NEVER_REACHED),
263 Predicates.and(TRUE, isOdd(), NEVER_REACHED))
264 .addEqualityGroup(Predicates.and(isOdd(), NEVER_REACHED, TRUE))
265 .addEqualityGroup(Predicates.and(TRUE))
266 .addEqualityGroup(Predicates.or(TRUE, isOdd(), NEVER_REACHED))
267 .testEquals();
268 }
269
270 @GwtIncompatible("SerializableTester")
271 @SuppressWarnings("unchecked")
272 public void testAnd_serializationTernary() {
273 checkSerialization(Predicates.and(TRUE, isOdd(), FALSE));
274 }
275
276 @SuppressWarnings("unchecked")
277 public void testAnd_applyIterable() {
278 Collection<Predicate<Integer>> empty = Arrays.asList();
279 assertEvalsToTrue(Predicates.and(empty));
280 assertEvalsLikeOdd(Predicates.and(Arrays.asList(isOdd())));
281 assertEvalsLikeOdd(Predicates.and(Arrays.asList(TRUE, isOdd())));
282 assertEvalsToFalse(Predicates.and(Arrays.asList(FALSE, NEVER_REACHED)));
283 }
284
285 @SuppressWarnings("unchecked")
286 public void testAnd_equalityIterable() {
287 new EqualsTester()
288 .addEqualityGroup(
289 Predicates.and(Arrays.asList(TRUE, NEVER_REACHED)),
290 Predicates.and(Arrays.asList(TRUE, NEVER_REACHED)),
291 Predicates.and(TRUE, NEVER_REACHED))
292 .addEqualityGroup(Predicates.and(FALSE, NEVER_REACHED))
293 .addEqualityGroup(Predicates.or(TRUE, NEVER_REACHED))
294 .testEquals();
295 }
296
297 @GwtIncompatible("SerializableTester")
298 @SuppressWarnings("unchecked")
299 public void testAnd_serializationIterable() {
300 checkSerialization(Predicates.and(Arrays.asList(TRUE, FALSE)));
301 }
302
303 @SuppressWarnings("unchecked")
304 public void testAnd_arrayDefensivelyCopied() {
305 Predicate[] array = {Predicates.alwaysFalse()};
306 Predicate<Object> predicate = Predicates.and(array);
307 assertFalse(predicate.apply(1));
308 array[0] = Predicates.alwaysTrue();
309 assertFalse(predicate.apply(1));
310 }
311
312 public void testAnd_listDefensivelyCopied() {
313 List<Predicate<Object>> list = newArrayList();
314 Predicate<Object> predicate = Predicates.and(list);
315 assertTrue(predicate.apply(1));
316 list.add(Predicates.alwaysFalse());
317 assertTrue(predicate.apply(1));
318 }
319
320 public void testAnd_iterableDefensivelyCopied() {
321 final List<Predicate<Object>> list = newArrayList();
322 Iterable<Predicate<Object>> iterable = new Iterable<Predicate<Object>>() {
323 @Override
324 public Iterator<Predicate<Object>> iterator() {
325 return list.iterator();
326 }
327 };
328 Predicate<Object> predicate = Predicates.and(iterable);
329 assertTrue(predicate.apply(1));
330 list.add(Predicates.alwaysFalse());
331 assertTrue(predicate.apply(1));
332 }
333
334
335
336
337
338 @SuppressWarnings("unchecked")
339 public void testOr_applyNoArgs() {
340 assertEvalsToFalse(Predicates.or());
341 }
342
343 @SuppressWarnings("unchecked")
344 public void testOr_equalityNoArgs() {
345 new EqualsTester()
346 .addEqualityGroup(Predicates.or(), Predicates.or())
347 .addEqualityGroup(Predicates.or(TRUE))
348 .addEqualityGroup(Predicates.and())
349 .testEquals();
350 }
351
352 @GwtIncompatible("SerializableTester")
353 @SuppressWarnings("unchecked")
354 public void testOr_serializationNoArgs() {
355 checkSerialization(Predicates.or());
356 }
357
358 @SuppressWarnings("unchecked")
359 public void testOr_applyOneArg() {
360 assertEvalsToTrue(Predicates.or(TRUE));
361 assertEvalsToFalse(Predicates.or(FALSE));
362 }
363
364 @SuppressWarnings("unchecked")
365 public void testOr_equalityOneArg() {
366 new EqualsTester()
367 .addEqualityGroup(
368 Predicates.or(NEVER_REACHED), Predicates.or(NEVER_REACHED))
369 .addEqualityGroup(Predicates.or(NEVER_REACHED, TRUE))
370 .addEqualityGroup(Predicates.or(TRUE))
371 .addEqualityGroup(Predicates.or())
372 .addEqualityGroup(Predicates.and(NEVER_REACHED))
373 .testEquals();
374 }
375
376 @GwtIncompatible("SerializableTester")
377 @SuppressWarnings("unchecked")
378 public void testOr_serializationOneArg() {
379 checkSerialization(Predicates.or(isOdd()));
380 }
381
382 public void testOr_applyBinary() {
383 Predicate<Integer> falseOrFalse = Predicates.or(FALSE, FALSE);
384 Predicate<Integer> falseOrTrue = Predicates.or(FALSE, TRUE);
385 Predicate<Integer> trueOrAnything = Predicates.or(TRUE, NEVER_REACHED);
386
387 assertEvalsToFalse(falseOrFalse);
388 assertEvalsToTrue(falseOrTrue);
389 assertEvalsToTrue(trueOrAnything);
390 }
391
392 @SuppressWarnings("unchecked")
393 public void testOr_equalityBinary() {
394 new EqualsTester()
395 .addEqualityGroup(
396 Predicates.or(FALSE, NEVER_REACHED),
397 Predicates.or(FALSE, NEVER_REACHED))
398 .addEqualityGroup(Predicates.or(NEVER_REACHED, FALSE))
399 .addEqualityGroup(Predicates.or(TRUE))
400 .addEqualityGroup(Predicates.and(FALSE, NEVER_REACHED))
401 .testEquals();
402 }
403
404 @GwtIncompatible("SerializableTester")
405 public void testOr_serializationBinary() {
406 checkSerialization(Predicates.or(isOdd(), TRUE));
407 }
408
409 @SuppressWarnings("unchecked")
410 public void testOr_applyTernary() {
411 assertEvalsLikeOdd(Predicates.or(isOdd(), FALSE, FALSE));
412 assertEvalsLikeOdd(Predicates.or(FALSE, isOdd(), FALSE));
413 assertEvalsLikeOdd(Predicates.or(FALSE, FALSE, isOdd()));
414 assertEvalsToTrue(Predicates.or(FALSE, TRUE, NEVER_REACHED));
415 }
416
417 @SuppressWarnings("unchecked")
418 public void testOr_equalityTernary() {
419 new EqualsTester()
420 .addEqualityGroup(
421 Predicates.or(FALSE, NEVER_REACHED, TRUE),
422 Predicates.or(FALSE, NEVER_REACHED, TRUE))
423 .addEqualityGroup(Predicates.or(TRUE, NEVER_REACHED, FALSE))
424 .addEqualityGroup(Predicates.or(TRUE))
425 .addEqualityGroup(Predicates.and(FALSE, NEVER_REACHED, TRUE))
426 .testEquals();
427 }
428
429 @GwtIncompatible("SerializableTester")
430 @SuppressWarnings("unchecked")
431 public void testOr_serializationTernary() {
432 checkSerialization(Predicates.or(FALSE, isOdd(), TRUE));
433 }
434
435 @SuppressWarnings("unchecked")
436 public void testOr_applyIterable() {
437 Predicate<Integer> vacuouslyFalse =
438 Predicates.or(Collections.<Predicate<Integer>>emptyList());
439 Predicate<Integer> troo = Predicates.or(Collections.singletonList(TRUE));
440
441
442
443
444 Predicate<Integer> trueAndFalse = Predicates.or(Arrays.asList(TRUE, FALSE));
445
446 assertEvalsToFalse(vacuouslyFalse);
447 assertEvalsToTrue(troo);
448 assertEvalsToTrue(trueAndFalse);
449 }
450
451 @SuppressWarnings("unchecked")
452 public void testOr_equalityIterable() {
453 new EqualsTester()
454 .addEqualityGroup(
455 Predicates.or(Arrays.asList(FALSE, NEVER_REACHED)),
456 Predicates.or(Arrays.asList(FALSE, NEVER_REACHED)),
457 Predicates.or(FALSE, NEVER_REACHED))
458 .addEqualityGroup(Predicates.or(TRUE, NEVER_REACHED))
459 .addEqualityGroup(Predicates.and(FALSE, NEVER_REACHED))
460 .testEquals();
461 }
462
463 @GwtIncompatible("SerializableTester")
464 @SuppressWarnings("unchecked")
465 public void testOr_serializationIterable() {
466 Predicate<Integer> pre = Predicates.or(Arrays.asList(TRUE, FALSE));
467 Predicate<Integer> post = SerializableTester.reserializeAndAssert(pre);
468 assertEquals(pre.apply(0), post.apply(0));
469 }
470
471 @SuppressWarnings("unchecked")
472 public void testOr_arrayDefensivelyCopied() {
473 Predicate[] array = {Predicates.alwaysFalse()};
474 Predicate<Object> predicate = Predicates.or(array);
475 assertFalse(predicate.apply(1));
476 array[0] = Predicates.alwaysTrue();
477 assertFalse(predicate.apply(1));
478 }
479
480 public void testOr_listDefensivelyCopied() {
481 List<Predicate<Object>> list = newArrayList();
482 Predicate<Object> predicate = Predicates.or(list);
483 assertFalse(predicate.apply(1));
484 list.add(Predicates.alwaysTrue());
485 assertFalse(predicate.apply(1));
486 }
487
488 public void testOr_iterableDefensivelyCopied() {
489 final List<Predicate<Object>> list = newArrayList();
490 Iterable<Predicate<Object>> iterable = new Iterable<Predicate<Object>>() {
491 @Override
492 public Iterator<Predicate<Object>> iterator() {
493 return list.iterator();
494 }
495 };
496 Predicate<Object> predicate = Predicates.or(iterable);
497 assertFalse(predicate.apply(1));
498 list.add(Predicates.alwaysTrue());
499 assertFalse(predicate.apply(1));
500 }
501
502
503
504
505
506 public void testIsEqualTo_apply() {
507 Predicate<Integer> isOne = Predicates.equalTo(1);
508
509 assertTrue(isOne.apply(1));
510 assertFalse(isOne.apply(2));
511 assertFalse(isOne.apply(null));
512 }
513
514 public void testIsEqualTo_equality() {
515 new EqualsTester()
516 .addEqualityGroup(Predicates.equalTo(1), Predicates.equalTo(1))
517 .addEqualityGroup(Predicates.equalTo(2))
518 .addEqualityGroup(Predicates.equalTo(null))
519 .testEquals();
520 }
521
522 @GwtIncompatible("SerializableTester")
523 public void testIsEqualTo_serialization() {
524 checkSerialization(Predicates.equalTo(1));
525 }
526
527 public void testIsEqualToNull_apply() {
528 Predicate<Integer> isNull = Predicates.equalTo(null);
529 assertTrue(isNull.apply(null));
530 assertFalse(isNull.apply(1));
531 }
532
533 public void testIsEqualToNull_equality() {
534 new EqualsTester()
535 .addEqualityGroup(Predicates.equalTo(null), Predicates.equalTo(null))
536 .addEqualityGroup(Predicates.equalTo(1))
537 .addEqualityGroup(Predicates.equalTo("null"))
538 .testEquals();
539 }
540
541 @GwtIncompatible("SerializableTester")
542 public void testIsEqualToNull_serialization() {
543 checkSerialization(Predicates.equalTo(null));
544 }
545
546
547
548
549
550
551
552
553 @GwtIncompatible("Predicates.instanceOf")
554 public void testIsInstanceOf_apply() {
555 Predicate<Object> isInteger = Predicates.instanceOf(Integer.class);
556
557 assertTrue(isInteger.apply(1));
558 assertFalse(isInteger.apply(2.0f));
559 assertFalse(isInteger.apply(""));
560 assertFalse(isInteger.apply(null));
561 }
562
563 @GwtIncompatible("Predicates.instanceOf")
564 public void testIsInstanceOf_subclass() {
565 Predicate<Object> isNumber = Predicates.instanceOf(Number.class);
566
567 assertTrue(isNumber.apply(1));
568 assertTrue(isNumber.apply(2.0f));
569 assertFalse(isNumber.apply(""));
570 assertFalse(isNumber.apply(null));
571 }
572
573 @GwtIncompatible("Predicates.instanceOf")
574 public void testIsInstanceOf_interface() {
575 Predicate<Object> isComparable = Predicates.instanceOf(Comparable.class);
576
577 assertTrue(isComparable.apply(1));
578 assertTrue(isComparable.apply(2.0f));
579 assertTrue(isComparable.apply(""));
580 assertFalse(isComparable.apply(null));
581 }
582
583 @GwtIncompatible("Predicates.instanceOf")
584 public void testIsInstanceOf_equality() {
585 new EqualsTester()
586 .addEqualityGroup(
587 Predicates.instanceOf(Integer.class),
588 Predicates.instanceOf(Integer.class))
589 .addEqualityGroup(Predicates.instanceOf(Number.class))
590 .addEqualityGroup(Predicates.instanceOf(Float.class))
591 .testEquals();
592 }
593
594 @GwtIncompatible("Predicates.instanceOf, SerializableTester")
595 public void testIsInstanceOf_serialization() {
596 checkSerialization(Predicates.instanceOf(Integer.class));
597 }
598
599 @GwtIncompatible("Predicates.assignableFrom")
600 public void testIsAssignableFrom_apply() {
601 Predicate<Class<?>> isInteger = Predicates.assignableFrom(Integer.class);
602
603 assertTrue(isInteger.apply(Integer.class));
604 assertFalse(isInteger.apply(Float.class));
605
606 try {
607 isInteger.apply(null);
608 fail();
609 } catch (NullPointerException expected) {}
610 }
611
612 @GwtIncompatible("Predicates.assignableFrom")
613 public void testIsAssignableFrom_subclass() {
614 Predicate<Class<?>> isNumber = Predicates.assignableFrom(Number.class);
615
616 assertTrue(isNumber.apply(Integer.class));
617 assertTrue(isNumber.apply(Float.class));
618 }
619
620 @GwtIncompatible("Predicates.assignableFrom")
621 public void testIsAssignableFrom_interface() {
622 Predicate<Class<?>> isComparable =
623 Predicates.assignableFrom(Comparable.class);
624
625 assertTrue(isComparable.apply(Integer.class));
626 assertTrue(isComparable.apply(Float.class));
627 }
628
629 @GwtIncompatible("Predicates.assignableFrom")
630 public void testIsAssignableFrom_equality() {
631 new EqualsTester()
632 .addEqualityGroup(
633 Predicates.assignableFrom(Integer.class),
634 Predicates.assignableFrom(Integer.class))
635 .addEqualityGroup(Predicates.assignableFrom(Number.class))
636 .addEqualityGroup(Predicates.assignableFrom(Float.class))
637 .testEquals();
638 }
639
640 @GwtIncompatible("Predicates.assignableFrom, SerializableTester")
641 public void testIsAssignableFrom_serialization() {
642 Predicate<Class<?>> predicate =
643 Predicates.assignableFrom(Integer.class);
644 Predicate<Class<?>> reserialized =
645 SerializableTester.reserializeAndAssert(predicate);
646
647 assertEvalsLike(predicate, reserialized, Integer.class);
648 assertEvalsLike(predicate, reserialized, Float.class);
649 assertEvalsLike(predicate, reserialized, null);
650 }
651
652
653
654
655
656 public void testIsNull_apply() {
657 Predicate<Integer> isNull = Predicates.isNull();
658 assertTrue(isNull.apply(null));
659 assertFalse(isNull.apply(1));
660 }
661
662 public void testIsNull_equality() {
663 new EqualsTester()
664 .addEqualityGroup(Predicates.isNull(), Predicates.isNull())
665 .addEqualityGroup(Predicates.notNull())
666 .testEquals();
667 }
668
669 @GwtIncompatible("SerializableTester")
670 public void testIsNull_serialization() {
671 Predicate<String> pre = Predicates.isNull();
672 Predicate<String> post = SerializableTester.reserializeAndAssert(pre);
673 assertEquals(pre.apply("foo"), post.apply("foo"));
674 assertEquals(pre.apply(null), post.apply(null));
675 }
676
677 public void testNotNull_apply() {
678 Predicate<Integer> notNull = Predicates.notNull();
679 assertFalse(notNull.apply(null));
680 assertTrue(notNull.apply(1));
681 }
682
683 public void testNotNull_equality() {
684 new EqualsTester()
685 .addEqualityGroup(Predicates.notNull(), Predicates.notNull())
686 .addEqualityGroup(Predicates.isNull())
687 .testEquals();
688 }
689
690 @GwtIncompatible("SerializableTester")
691 public void testNotNull_serialization() {
692 checkSerialization(Predicates.notNull());
693 }
694
695 public void testIn_apply() {
696 Collection<Integer> nums = Arrays.asList(1, 5);
697 Predicate<Integer> isOneOrFive = Predicates.in(nums);
698
699 assertTrue(isOneOrFive.apply(1));
700 assertTrue(isOneOrFive.apply(5));
701 assertFalse(isOneOrFive.apply(3));
702 assertFalse(isOneOrFive.apply(null));
703 }
704
705 public void testIn_equality() {
706 Collection<Integer> nums = ImmutableSet.of(1, 5);
707 Collection<Integer> sameOrder = ImmutableSet.of(1, 5);
708 Collection<Integer> differentOrder = ImmutableSet.of(5, 1);
709 Collection<Integer> differentNums = ImmutableSet.of(1, 3, 5);
710
711 new EqualsTester()
712 .addEqualityGroup(Predicates.in(nums), Predicates.in(nums),
713 Predicates.in(sameOrder), Predicates.in(differentOrder))
714 .addEqualityGroup(Predicates.in(differentNums))
715 .testEquals();
716 }
717
718 @GwtIncompatible("SerializableTester")
719 public void testIn_serialization() {
720 checkSerialization(Predicates.in(Arrays.asList(1, 2, 3, null)));
721 }
722
723 public void testIn_handlesNullPointerException() {
724 class CollectionThatThrowsNPE<T> extends ArrayList<T> {
725 private static final long serialVersionUID = 1L;
726
727 @Override public boolean contains(Object element) {
728 Preconditions.checkNotNull(element);
729 return super.contains(element);
730 }
731 }
732 Collection<Integer> nums = new CollectionThatThrowsNPE<Integer>();
733 Predicate<Integer> isFalse = Predicates.in(nums);
734 assertFalse(isFalse.apply(null));
735 }
736
737 public void testIn_handlesClassCastException() {
738 class CollectionThatThrowsCCE<T> extends ArrayList<T> {
739 private static final long serialVersionUID = 1L;
740
741 @Override public boolean contains(Object element) {
742 throw new ClassCastException("");
743 }
744 }
745 Collection<Integer> nums = new CollectionThatThrowsCCE<Integer>();
746 nums.add(3);
747 Predicate<Integer> isThree = Predicates.in(nums);
748 assertFalse(isThree.apply(3));
749 }
750
751
752
753
754 @SuppressWarnings("unused")
755 public void testIn_compilesWithExplicitSupertype() {
756 Collection<Number> nums = ImmutableSet.of();
757 Predicate<Number> p1 = Predicates.in(nums);
758 Predicate<Object> p2 = Predicates.<Object>in(nums);
759
760
761
762 }
763
764 @GwtIncompatible("NullPointerTester")
765 public void testNullPointerExceptions() {
766 NullPointerTester tester = new NullPointerTester();
767 tester.testAllPublicStaticMethods(Predicates.class);
768 }
769
770 @SuppressWarnings("unchecked")
771 @GwtIncompatible("SerializbleTester")
772 public void testCascadingSerialization() throws Exception {
773
774 Predicate<? super Integer> nasty = Predicates.not(Predicates.and(
775 Predicates.or(
776 Predicates.equalTo((Object) 1), Predicates.equalTo(null),
777 Predicates.alwaysFalse(), Predicates.alwaysTrue(),
778 Predicates.isNull(), Predicates.notNull(),
779 Predicates.in(Arrays.asList(1)))));
780 assertEvalsToFalse(nasty);
781
782 Predicate<? super Integer> stillNasty =
783 SerializableTester.reserializeAndAssert(nasty);
784
785 assertEvalsToFalse(stillNasty);
786 }
787
788
789 private enum TrimStringFunction implements Function<String, String> {
790 INSTANCE;
791
792 @Override
793 public String apply(String string) {
794 return WHITESPACE.trimFrom(string);
795 }
796 }
797
798 public void testCompose() {
799 Function<String, String> trim = TrimStringFunction.INSTANCE;
800 Predicate<String> equalsFoo = Predicates.equalTo("Foo");
801 Predicate<String> equalsBar = Predicates.equalTo("Bar");
802 Predicate<String> trimEqualsFoo = Predicates.compose(equalsFoo, trim);
803 Function<String, String> identity = Functions.identity();
804
805 assertTrue(trimEqualsFoo.apply("Foo"));
806 assertTrue(trimEqualsFoo.apply(" Foo "));
807 assertFalse(trimEqualsFoo.apply("Foo-b-que"));
808
809 new EqualsTester()
810 .addEqualityGroup(trimEqualsFoo, Predicates.compose(equalsFoo, trim))
811 .addEqualityGroup(equalsFoo)
812 .addEqualityGroup(trim)
813 .addEqualityGroup(Predicates.compose(equalsFoo, identity))
814 .addEqualityGroup(Predicates.compose(equalsBar, trim))
815 .testEquals();
816 }
817
818 @GwtIncompatible("SerializableTester")
819 public void testComposeSerialization() {
820 Function<String, String> trim = TrimStringFunction.INSTANCE;
821 Predicate<String> equalsFoo = Predicates.equalTo("Foo");
822 Predicate<String> trimEqualsFoo = Predicates.compose(equalsFoo, trim);
823 SerializableTester.reserializeAndAssert(trimEqualsFoo);
824 }
825
826
827
828
829
830
831
832
833 @GwtIncompatible("Predicates.containsPattern")
834 public void testContainsPattern_apply() {
835 Predicate<CharSequence> isFoobar =
836 Predicates.containsPattern("^Fo.*o.*bar$");
837 assertTrue(isFoobar.apply("Foxyzoabcbar"));
838 assertFalse(isFoobar.apply("Foobarx"));
839 }
840
841 @GwtIncompatible("Predicates.containsPattern")
842 public void testContains_apply() {
843 Predicate<CharSequence> isFoobar =
844 Predicates.contains(Pattern.compile("^Fo.*o.*bar$"));
845
846 assertTrue(isFoobar.apply("Foxyzoabcbar"));
847 assertFalse(isFoobar.apply("Foobarx"));
848 }
849
850 @GwtIncompatible("NullPointerTester")
851 public void testContainsPattern_nulls() throws Exception {
852 NullPointerTester tester = new NullPointerTester();
853 Predicate<CharSequence> isWooString = Predicates.containsPattern("Woo");
854
855 tester.testAllPublicInstanceMethods(isWooString);
856 }
857
858 @GwtIncompatible("NullPointerTester")
859 public void testContains_nulls() throws Exception {
860 NullPointerTester tester = new NullPointerTester();
861 Predicate<CharSequence> isWooPattern =
862 Predicates.contains(Pattern.compile("Woo"));
863
864 tester.testAllPublicInstanceMethods(isWooPattern);
865 }
866
867 @GwtIncompatible("SerializableTester")
868 public void testContainsPattern_serialization() {
869 Predicate<CharSequence> pre = Predicates.containsPattern("foo");
870 Predicate<CharSequence> post = SerializableTester.reserializeAndAssert(pre);
871 assertEquals(pre.apply("foo"), post.apply("foo"));
872 }
873
874 @GwtIncompatible("java.util.regex.Pattern")
875 public void testContains_equals() {
876 new EqualsTester()
877 .addEqualityGroup(
878 Predicates.contains(Pattern.compile("foo")),
879 Predicates.containsPattern("foo"))
880 .addEqualityGroup(
881 Predicates.contains(
882 Pattern.compile("foo", Pattern.CASE_INSENSITIVE)))
883 .addEqualityGroup(
884 Predicates.containsPattern("bar"))
885 .testEquals();
886 }
887
888 public void assertEqualHashCode(
889 Predicate<? super Integer> expected, Predicate<? super Integer> actual) {
890 assertEquals(actual + " should hash like " + expected, expected.hashCode(), actual.hashCode());
891 }
892
893 public void testHashCodeForBooleanOperations() {
894 Predicate<Integer> p1 = Predicates.isNull();
895 Predicate<Integer> p2 = isOdd();
896
897
898 assertEqualHashCode(
899 Predicates.not(p1),
900 Predicates.not(p1));
901
902 assertEqualHashCode(
903 Predicates.and(p1, p2),
904 Predicates.and(p1, p2));
905
906 assertEqualHashCode(
907 Predicates.or(p1, p2),
908 Predicates.or(p1, p2));
909
910
911
912 assertTrue(Predicates.and(p1, p2).hashCode() != Predicates.or(p1, p2).hashCode());
913 }
914
915 @GwtIncompatible("reflection")
916 public void testNulls() throws Exception {
917 new ClassSanityTester().forAllPublicStaticMethods(Predicates.class).testNulls();
918 }
919
920 @GwtIncompatible("reflection")
921 public void testEqualsAndSerializable() throws Exception {
922 new ClassSanityTester().forAllPublicStaticMethods(Predicates.class).testEqualsAndSerializable();
923 }
924
925 private static void assertEvalsToTrue(Predicate<? super Integer> predicate) {
926 assertTrue(predicate.apply(0));
927 assertTrue(predicate.apply(1));
928 assertTrue(predicate.apply(null));
929 }
930
931 private static void assertEvalsToFalse(Predicate<? super Integer> predicate) {
932 assertFalse(predicate.apply(0));
933 assertFalse(predicate.apply(1));
934 assertFalse(predicate.apply(null));
935 }
936
937 private static void assertEvalsLikeOdd(Predicate<? super Integer> predicate) {
938 assertEvalsLike(isOdd(), predicate);
939 }
940
941 private static void assertEvalsLike(
942 Predicate<? super Integer> expected,
943 Predicate<? super Integer> actual) {
944 assertEvalsLike(expected, actual, 0);
945 assertEvalsLike(expected, actual, 1);
946 assertEvalsLike(expected, actual, null);
947 }
948
949 private static <T> void assertEvalsLike(
950 Predicate<? super T> expected,
951 Predicate<? super T> actual,
952 T input) {
953 Boolean expectedResult = null;
954 RuntimeException expectedRuntimeException = null;
955 try {
956 expectedResult = expected.apply(input);
957 } catch (RuntimeException e) {
958 expectedRuntimeException = e;
959 }
960
961 Boolean actualResult = null;
962 RuntimeException actualRuntimeException = null;
963 try {
964 actualResult = actual.apply(input);
965 } catch (RuntimeException e) {
966 actualRuntimeException = e;
967 }
968
969 assertEquals(expectedResult, actualResult);
970 if (expectedRuntimeException != null) {
971 assertNotNull(actualRuntimeException);
972 assertEquals(
973 expectedRuntimeException.getClass(),
974 actualRuntimeException.getClass());
975 }
976 }
977
978 @GwtIncompatible("SerializableTester")
979 private static void checkSerialization(Predicate<? super Integer> predicate) {
980 Predicate<? super Integer> reserialized =
981 SerializableTester.reserializeAndAssert(predicate);
982 assertEvalsLike(predicate, reserialized);
983 }
984 }