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;
18  
19  import static org.junit.jupiter.api.Assertions.assertEquals;
20  import static org.junit.jupiter.api.Assertions.assertThrows;
21  import static org.junit.jupiter.api.Assertions.assertTrue;
22  
23  import java.util.Comparator;
24  
25  import org.junit.jupiter.api.Test;
26  
27  /**
28   * Tests ComparatorUtils.
29   */
30  public class ComparatorUtilsTest {
31  
32      @Test
33      public void testBooleanComparator() {
34          Comparator<Boolean> comp = ComparatorUtils.booleanComparator(true);
35          assertTrue(comp.compare(Boolean.TRUE, Boolean.FALSE) < 0);
36          assertEquals(0, comp.compare(Boolean.TRUE, Boolean.TRUE));
37          assertTrue(comp.compare(Boolean.FALSE, Boolean.TRUE) > 0);
38  
39          comp = ComparatorUtils.booleanComparator(false);
40          assertTrue(comp.compare(Boolean.TRUE, Boolean.FALSE) > 0);
41          assertEquals(0, comp.compare(Boolean.TRUE, Boolean.TRUE));
42          assertTrue(comp.compare(Boolean.FALSE, Boolean.TRUE) < 0);
43      }
44  
45      @Test
46      public void testChainedComparator() {
47          // simple test: chain 2 natural comparators
48          final Comparator<Integer> comp = ComparatorUtils.chainedComparator(ComparatorUtils.<Integer>naturalComparator(),
49                  ComparatorUtils.naturalComparator());
50          assertTrue(comp.compare(1, 2) < 0);
51          assertEquals(0, comp.compare(1, 1));
52          assertTrue(comp.compare(2, 1) > 0);
53      }
54  
55      @Test
56      public void testMax() {
57          final Comparator<Integer> reversed =
58                  ComparatorUtils.reversedComparator(ComparatorUtils.<Integer>naturalComparator());
59  
60          assertEquals(Integer.valueOf(10), ComparatorUtils.max(1, 10, null));
61          assertEquals(Integer.valueOf(10), ComparatorUtils.max(10, -10, null));
62  
63          assertEquals(Integer.valueOf(1), ComparatorUtils.max(1, 10, reversed));
64          assertEquals(Integer.valueOf(-10), ComparatorUtils.max(10, -10, reversed));
65  
66          assertThrows(NullPointerException.class, () -> ComparatorUtils.max(1, null, null));
67          assertThrows(NullPointerException.class, () -> ComparatorUtils.max(null, 10, null));
68      }
69  
70      @Test
71      public void testMin() {
72          final Comparator<Integer> reversed =
73                  ComparatorUtils.reversedComparator(ComparatorUtils.<Integer>naturalComparator());
74  
75          assertEquals(Integer.valueOf(1), ComparatorUtils.min(1, 10, null));
76          assertEquals(Integer.valueOf(-10), ComparatorUtils.min(10, -10, null));
77  
78          assertEquals(Integer.valueOf(10), ComparatorUtils.min(1, 10, reversed));
79          assertEquals(Integer.valueOf(10), ComparatorUtils.min(10, -10, reversed));
80  
81          assertThrows(NullPointerException.class, () -> ComparatorUtils.min(1, null, null));
82          assertThrows(NullPointerException.class, () -> ComparatorUtils.min(null, 10, null));
83      }
84  
85      @Test
86      public void testNullHighComparator() {
87          final Comparator<Integer> comp = ComparatorUtils.nullHighComparator(null);
88          assertTrue(comp.compare(null, 10) > 0);
89          assertEquals(0, comp.compare(null, null));
90          assertTrue(comp.compare(10, null) < 0);
91      }
92  
93      @Test
94      public void testNullLowComparator() {
95          final Comparator<Integer> comp = ComparatorUtils.nullLowComparator(null);
96          assertTrue(comp.compare(null, 10) < 0);
97          assertEquals(0, comp.compare(null, null));
98          assertTrue(comp.compare(10, null) > 0);
99      }
100 }