View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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); // different object
62          assertPredicateTrue(predicate, FALSE_OBJECT); // the same object
63          assertPredicateFalse(predicate, new EqualsTestObject(false)); // different object but always returns false from equals()
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  }