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.apache.commons.collections4.functors.EqualPredicate.equalPredicate;
20  import static org.apache.commons.collections4.functors.NullPredicate.nullPredicate;
21  import static org.junit.jupiter.api.Assertions.assertSame;
22  
23  import org.apache.commons.collections4.Predicate;
24  import org.junit.jupiter.api.Test;
25  
26  public class EqualPredicateTest extends AbstractPredicateTest {
27      public static class EqualsTestObject {
28          private final boolean b;
29  
30          public EqualsTestObject(final boolean b) {
31              this.b = b;
32          }
33  
34          @Override
35          public boolean equals(final Object obj) {
36              return b;
37          }
38      }
39      private static final EqualsTestObject FALSE_OBJECT = new EqualsTestObject(false);
40  
41      private static final EqualsTestObject TRUE_OBJECT = new EqualsTestObject(true);
42  
43      @Override
44      protected Predicate<Object> generatePredicate() {
45          return equalPredicate(null);
46      }
47  
48      @Test
49      public void testNullArgumentEqualsNullPredicate() throws Exception {
50          assertSame(nullPredicate(), equalPredicate(null));
51      }
52  
53      @Test
54      public void testObjectFactoryUsesEqualsForTest() throws Exception {
55          final Predicate<EqualsTestObject> predicate = equalPredicate(FALSE_OBJECT);
56          assertPredicateFalse(predicate, FALSE_OBJECT);
57          assertPredicateTrue(equalPredicate(TRUE_OBJECT), TRUE_OBJECT);
58      }
59  
60      @SuppressWarnings("boxing")
61      @Test
62      public void testPredicateTypeCanBeSuperClassOfObject() throws Exception {
63          final Predicate<Number> predicate = equalPredicate((Number) 4);
64          assertPredicateTrue(predicate, 4);
65      }
66  }