1 /* 2 * Copyright (C) 2005 The Guava Authors 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 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 * Unit test for {@link Predicates}. 44 * 45 * @author Kevin Bourrillion 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 /** Instantiable predicate with reasonable hashCode() and equals() methods. */ 61 static class IsOdd implements Predicate<Integer>, Serializable { 62 private static final long serialVersionUID = 0x150ddL; 63 @Override apply(Integer i)64 public boolean apply(Integer i) { 65 return (i.intValue() & 1) == 1; 66 } hashCode()67 @Override public int hashCode() { 68 return 0x150dd; 69 } equals(Object obj)70 @Override public boolean equals(Object obj) { 71 return obj instanceof IsOdd; 72 } toString()73 @Override public String toString() { 74 return "IsOdd"; 75 } 76 } 77 78 /** 79 * Generates a new Predicate per call. 80 * 81 * <p>Creating a new Predicate each time helps catch cases where code is 82 * using {@code x == y} instead of {@code x.equals(y)}. 83 */ isOdd()84 private static IsOdd isOdd() { 85 return new IsOdd(); 86 } 87 88 /* 89 * Tests for Predicates.alwaysTrue(). 90 */ 91 testAlwaysTrue_apply()92 public void testAlwaysTrue_apply() { 93 assertEvalsToTrue(Predicates.alwaysTrue()); 94 } 95 testAlwaysTrue_equality()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") testAlwaysTrue_serialization()105 public void testAlwaysTrue_serialization() { 106 checkSerialization(Predicates.alwaysTrue()); 107 } 108 109 /* 110 * Tests for Predicates.alwaysFalse(). 111 */ 112 testAlwaysFalse_apply()113 public void testAlwaysFalse_apply() throws Exception { 114 assertEvalsToFalse(Predicates.alwaysFalse()); 115 } 116 testAlwaysFalse_equality()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") testAlwaysFalse_serialization()126 public void testAlwaysFalse_serialization() { 127 checkSerialization(Predicates.alwaysFalse()); 128 } 129 130 /* 131 * Tests for Predicates.not(predicate). 132 */ 133 testNot_apply()134 public void testNot_apply() { 135 assertEvalsToTrue(Predicates.not(FALSE)); 136 assertEvalsToFalse(Predicates.not(TRUE)); 137 assertEvalsLikeOdd(Predicates.not(Predicates.not(isOdd()))); 138 } 139 testNot_equality()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 testNot_equalityForNotOfKnownValues()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") testNot_serialization()175 public void testNot_serialization() { 176 checkSerialization(Predicates.not(isOdd())); 177 } 178 179 /* 180 * Tests for all the different flavors of Predicates.and(). 181 */ 182 183 @SuppressWarnings("unchecked") // varargs testAnd_applyNoArgs()184 public void testAnd_applyNoArgs() { 185 assertEvalsToTrue(Predicates.and()); 186 } 187 188 @SuppressWarnings("unchecked") // varargs testAnd_equalityNoArgs()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") // varargs testAnd_serializationNoArgs()199 public void testAnd_serializationNoArgs() { 200 checkSerialization(Predicates.and()); 201 } 202 203 @SuppressWarnings("unchecked") // varargs testAnd_applyOneArg()204 public void testAnd_applyOneArg() { 205 assertEvalsLikeOdd(Predicates.and(isOdd())); 206 } 207 208 @SuppressWarnings("unchecked") // varargs testAnd_equalityOneArg()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") // varargs testAnd_serializationOneArg()223 public void testAnd_serializationOneArg() { 224 checkSerialization(Predicates.and(isOdd())); 225 } 226 testAnd_applyBinary()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") // varargs testAnd_equalityBinary()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") testAnd_serializationBinary()246 public void testAnd_serializationBinary() { 247 checkSerialization(Predicates.and(TRUE, isOdd())); 248 } 249 250 @SuppressWarnings("unchecked") // varargs testAnd_applyTernary()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") // varargs testAnd_equalityTernary()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") // varargs testAnd_serializationTernary()272 public void testAnd_serializationTernary() { 273 checkSerialization(Predicates.and(TRUE, isOdd(), FALSE)); 274 } 275 276 @SuppressWarnings("unchecked") // varargs testAnd_applyIterable()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") // varargs testAnd_equalityIterable()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") // varargs testAnd_serializationIterable()299 public void testAnd_serializationIterable() { 300 checkSerialization(Predicates.and(Arrays.asList(TRUE, FALSE))); 301 } 302 303 @SuppressWarnings("unchecked") // varargs testAnd_arrayDefensivelyCopied()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 testAnd_listDefensivelyCopied()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 testAnd_iterableDefensivelyCopied()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 * Tests for all the different flavors of Predicates.or(). 336 */ 337 338 @SuppressWarnings("unchecked") // varargs testOr_applyNoArgs()339 public void testOr_applyNoArgs() { 340 assertEvalsToFalse(Predicates.or()); 341 } 342 343 @SuppressWarnings("unchecked") // varargs testOr_equalityNoArgs()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") // varargs testOr_serializationNoArgs()354 public void testOr_serializationNoArgs() { 355 checkSerialization(Predicates.or()); 356 } 357 358 @SuppressWarnings("unchecked") // varargs testOr_applyOneArg()359 public void testOr_applyOneArg() { 360 assertEvalsToTrue(Predicates.or(TRUE)); 361 assertEvalsToFalse(Predicates.or(FALSE)); 362 } 363 364 @SuppressWarnings("unchecked") // varargs testOr_equalityOneArg()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") // varargs testOr_serializationOneArg()378 public void testOr_serializationOneArg() { 379 checkSerialization(Predicates.or(isOdd())); 380 } 381 testOr_applyBinary()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") // varargs testOr_equalityBinary()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") testOr_serializationBinary()405 public void testOr_serializationBinary() { 406 checkSerialization(Predicates.or(isOdd(), TRUE)); 407 } 408 409 @SuppressWarnings("unchecked") // varargs testOr_applyTernary()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") // varargs testOr_equalityTernary()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") // varargs testOr_serializationTernary()431 public void testOr_serializationTernary() { 432 checkSerialization(Predicates.or(FALSE, isOdd(), TRUE)); 433 } 434 435 @SuppressWarnings("unchecked") // varargs testOr_applyIterable()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 * newLinkedList() takes varargs. TRUE and FALSE are both instances of 442 * Predicate<Integer>, so the call is safe. 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") // varargs testOr_equalityIterable()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") // varargs testOr_serializationIterable()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") // varargs testOr_arrayDefensivelyCopied()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 testOr_listDefensivelyCopied()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 testOr_iterableDefensivelyCopied()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 * Tests for Predicates.equalTo(x). 504 */ 505 testIsEqualTo_apply()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 testIsEqualTo_equality()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") testIsEqualTo_serialization()523 public void testIsEqualTo_serialization() { 524 checkSerialization(Predicates.equalTo(1)); 525 } 526 testIsEqualToNull_apply()527 public void testIsEqualToNull_apply() { 528 Predicate<Integer> isNull = Predicates.equalTo(null); 529 assertTrue(isNull.apply(null)); 530 assertFalse(isNull.apply(1)); 531 } 532 testIsEqualToNull_equality()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") testIsEqualToNull_serialization()542 public void testIsEqualToNull_serialization() { 543 checkSerialization(Predicates.equalTo(null)); 544 } 545 546 /** 547 * Tests for Predicates.instanceOf(x). 548 * TODO: Fix the comment style after fixing annotation stripper to remove 549 * comments properly. Currently, all tests before the comments are removed 550 * as well. 551 */ 552 553 @GwtIncompatible("Predicates.instanceOf") testIsInstanceOf_apply()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") testIsInstanceOf_subclass()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") testIsInstanceOf_interface()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") testIsInstanceOf_equality()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") testIsInstanceOf_serialization()595 public void testIsInstanceOf_serialization() { 596 checkSerialization(Predicates.instanceOf(Integer.class)); 597 } 598 599 @GwtIncompatible("Predicates.assignableFrom") testIsAssignableFrom_apply()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") testIsAssignableFrom_subclass()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") testIsAssignableFrom_interface()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") testIsAssignableFrom_equality()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") testIsAssignableFrom_serialization()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 * Tests for Predicates.isNull() 654 */ 655 testIsNull_apply()656 public void testIsNull_apply() { 657 Predicate<Integer> isNull = Predicates.isNull(); 658 assertTrue(isNull.apply(null)); 659 assertFalse(isNull.apply(1)); 660 } 661 testIsNull_equality()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") testIsNull_serialization()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 testNotNull_apply()677 public void testNotNull_apply() { 678 Predicate<Integer> notNull = Predicates.notNull(); 679 assertFalse(notNull.apply(null)); 680 assertTrue(notNull.apply(1)); 681 } 682 testNotNull_equality()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") testNotNull_serialization()691 public void testNotNull_serialization() { 692 checkSerialization(Predicates.notNull()); 693 } 694 testIn_apply()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 testIn_equality()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") testIn_serialization()719 public void testIn_serialization() { 720 checkSerialization(Predicates.in(Arrays.asList(1, 2, 3, null))); 721 } 722 testIn_handlesNullPointerException()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 testIn_handlesClassCastException()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 * Tests that compilation will work when applying explicit types. 753 */ 754 @SuppressWarnings("unused") // compilation test testIn_compilesWithExplicitSupertype()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 // The next two lines are not expected to compile. 760 // Predicate<Integer> p3 = Predicates.in(nums); 761 // Predicate<Integer> p4 = Predicates.<Integer>in(nums); 762 } 763 764 @GwtIncompatible("NullPointerTester") testNullPointerExceptions()765 public void testNullPointerExceptions() { 766 NullPointerTester tester = new NullPointerTester(); 767 tester.testAllPublicStaticMethods(Predicates.class); 768 } 769 770 @SuppressWarnings("unchecked") // varargs 771 @GwtIncompatible("SerializbleTester") testCascadingSerialization()772 public void testCascadingSerialization() throws Exception { 773 // Eclipse says Predicate<Integer>; javac says Predicate<Object>. 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 // enum singleton pattern 789 private enum TrimStringFunction implements Function<String, String> { 790 INSTANCE; 791 792 @Override apply(String string)793 public String apply(String string) { 794 return WHITESPACE.trimFrom(string); 795 } 796 } 797 testCompose()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") testComposeSerialization()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 * Tests for Predicates.contains(Pattern) and .containsPattern(String). 828 * We assume the regex level works, so there are only trivial tests of that 829 * aspect. 830 * TODO: Fix comment style once annotation stripper is fixed. 831 */ 832 833 @GwtIncompatible("Predicates.containsPattern") testContainsPattern_apply()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") testContains_apply()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") testContainsPattern_nulls()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") testContains_nulls()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") testContainsPattern_serialization()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") testContains_equals()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 assertEqualHashCode( Predicate<? super Integer> expected, Predicate<? super Integer> actual)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 testHashCodeForBooleanOperations()893 public void testHashCodeForBooleanOperations() { 894 Predicate<Integer> p1 = Predicates.isNull(); 895 Predicate<Integer> p2 = isOdd(); 896 897 // Make sure that hash codes are not computed per-instance. 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 // While not a contractual requirement, we'd like the hash codes for ands 911 // & ors of the same predicates to not collide. 912 assertTrue(Predicates.and(p1, p2).hashCode() != Predicates.or(p1, p2).hashCode()); 913 } 914 915 @GwtIncompatible("reflection") testNulls()916 public void testNulls() throws Exception { 917 new ClassSanityTester().forAllPublicStaticMethods(Predicates.class).testNulls(); 918 } 919 920 @GwtIncompatible("reflection") testEqualsAndSerializable()921 public void testEqualsAndSerializable() throws Exception { 922 new ClassSanityTester().forAllPublicStaticMethods(Predicates.class).testEqualsAndSerializable(); 923 } 924 assertEvalsToTrue(Predicate<? super Integer> predicate)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 assertEvalsToFalse(Predicate<? super Integer> predicate)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 assertEvalsLikeOdd(Predicate<? super Integer> predicate)937 private static void assertEvalsLikeOdd(Predicate<? super Integer> predicate) { 938 assertEvalsLike(isOdd(), predicate); 939 } 940 assertEvalsLike( Predicate<? super Integer> expected, Predicate<? super Integer> actual)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 assertEvalsLike( Predicate<? super T> expected, Predicate<? super T> actual, T input)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") checkSerialization(Predicate<? super Integer> predicate)979 private static void checkSerialization(Predicate<? super Integer> predicate) { 980 Predicate<? super Integer> reserialized = 981 SerializableTester.reserializeAndAssert(predicate); 982 assertEvalsLike(predicate, reserialized); 983 } 984 } 985