1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.collections4.functors;
18
19 import static org.junit.jupiter.api.Assertions.assertSame;
20
21 import org.apache.commons.collections4.Predicate;
22 import org.junit.jupiter.api.Test;
23
24 public class EqualPredicateTest extends AbstractPredicateTest {
25 public static class EqualsTestObject {
26 private final boolean b;
27
28 public EqualsTestObject(final boolean b) {
29 this.b = b;
30 }
31
32 @Override
33 public boolean equals(final Object obj) {
34 return b;
35 }
36
37 @Override
38 public int hashCode() {
39 return Boolean.hashCode(b);
40 }
41 }
42
43 private static final EqualsTestObject FALSE_OBJECT = new EqualsTestObject(false);
44
45 private static final EqualsTestObject TRUE_OBJECT = new EqualsTestObject(true);
46
47 @Override
48 protected Predicate<Object> generatePredicate() {
49 return EqualPredicate.equalPredicate(null);
50 }
51
52 @Test
53 public void testNullArgumentEqualsNullPredicate() throws Exception {
54 assertSame(NullPredicate.nullPredicate(), EqualPredicate.equalPredicate(null));
55 }
56
57 @Test
58 public void testObjectFactoryUsesEqualsForTest() throws Exception {
59 final Predicate<EqualsTestObject> predicate = EqualPredicate.equalPredicate(FALSE_OBJECT);
60 assertPredicateFalse(predicate, null);
61 assertPredicateFalse(predicate, TRUE_OBJECT);
62 assertPredicateTrue(predicate, FALSE_OBJECT);
63 assertPredicateFalse(predicate, new EqualsTestObject(false));
64 assertPredicateTrue(EqualPredicate.equalPredicate(TRUE_OBJECT), TRUE_OBJECT);
65 }
66
67 @SuppressWarnings("boxing")
68 @Test
69 public void testPredicateTypeCanBeSuperClassOfObject() throws Exception {
70 final Predicate<Number> predicate = EqualPredicate.equalPredicate((Number) 4);
71 assertPredicateTrue(predicate, 4);
72 }
73 }