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 java.util.Comparator;
20  
21  import org.apache.commons.collections4.Predicate;
22  import org.apache.commons.collections4.functors.ComparatorPredicate.Criterion;
23  import org.junit.jupiter.api.Test;
24  
25  public class ComparatorPredicateTest extends AbstractPredicateTest {
26      private static final class TestComparator<T extends Comparable<T>> implements Comparator<T> {
27          @Override
28          public int compare(final T first, final T second) {
29              return first.compareTo(second);
30          }
31      }
32  
33      @Override
34      protected Predicate<?> generatePredicate() {
35          return ComparatorPredicate.comparatorPredicate(Integer.valueOf(10), new TestComparator<>());
36      }
37  
38      @Test
39      public void testCompareEquals() {
40          final Integer value = Integer.valueOf(10);
41          final Predicate<Integer> p = ComparatorPredicate.comparatorPredicate(value, new TestComparator<>());
42          assertPredicateFalse(p, Integer.valueOf(value.intValue() - 1));
43          assertPredicateTrue(p, Integer.valueOf(value.intValue()));
44          assertPredicateFalse(p, Integer.valueOf(value.intValue() + 1));
45      }
46  
47      @Test
48      public void testCompareGreater() {
49          final Integer value = Integer.valueOf(10);
50          final Predicate<Integer> p = ComparatorPredicate.comparatorPredicate(value, new TestComparator<>(), Criterion.GREATER);
51          assertPredicateTrue(p, Integer.valueOf(value.intValue() - 1));
52          assertPredicateFalse(p, Integer.valueOf(value.intValue()));
53          assertPredicateFalse(p, Integer.valueOf(value.intValue() + 1));
54      }
55  
56      @Test
57      public void testCompareGreaterOrEqual() {
58          final Integer value = Integer.valueOf(10);
59          final Predicate<Integer> p = ComparatorPredicate.comparatorPredicate(value, new TestComparator<>(), Criterion.GREATER_OR_EQUAL);
60          assertPredicateTrue(p, Integer.valueOf(value.intValue() - 1));
61          assertPredicateTrue(p, Integer.valueOf(value.intValue()));
62          assertPredicateFalse(p, Integer.valueOf(value.intValue() + 1));
63      }
64  
65      @Test
66      public void testCompareLess() {
67          final Integer value = Integer.valueOf(10);
68          final Predicate<Integer> p = ComparatorPredicate.comparatorPredicate(value, new TestComparator<>(), Criterion.LESS);
69          assertPredicateFalse(p, Integer.valueOf(value.intValue() - 1));
70          assertPredicateFalse(p, Integer.valueOf(value.intValue()));
71          assertPredicateTrue(p, Integer.valueOf(value.intValue() + 1));
72      }
73  
74      @Test
75      public void testCompareLessOrEqual() {
76          final Integer value = Integer.valueOf(10);
77          final Predicate<Integer> p = ComparatorPredicate.comparatorPredicate(value, new TestComparator<>(), Criterion.LESS_OR_EQUAL);
78          assertPredicateFalse(p, Integer.valueOf(value.intValue() - 1));
79          assertPredicateTrue(p, Integer.valueOf(value.intValue()));
80          assertPredicateTrue(p, Integer.valueOf(value.intValue() + 1));
81      }
82  }