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.Assert.assertEquals;
20  import static org.junit.Assert.assertTrue;
21  import static org.junit.Assert.fail;
22  
23  import java.util.Comparator;
24  
25  import org.junit.Test;
26  
27  /**
28   * Tests ComparatorUtils.
29   *
30   */
31  public class ComparatorUtilsTest {
32  
33      @Test
34      public void booleanComparator() {
35          Comparator<Boolean> comp = ComparatorUtils.booleanComparator(true);
36          assertTrue(comp.compare(Boolean.TRUE, Boolean.FALSE) < 0);
37          assertTrue(comp.compare(Boolean.TRUE, Boolean.TRUE) == 0);
38          assertTrue(comp.compare(Boolean.FALSE, Boolean.TRUE) > 0);
39  
40          comp = ComparatorUtils.booleanComparator(false);
41          assertTrue(comp.compare(Boolean.TRUE, Boolean.FALSE) > 0);
42          assertTrue(comp.compare(Boolean.TRUE, Boolean.TRUE) == 0);
43          assertTrue(comp.compare(Boolean.FALSE, Boolean.TRUE) < 0);
44      }
45  
46      @Test
47      public void chainedComparator() {
48          // simple test: chain 2 natural comparators
49          final Comparator<Integer> comp = ComparatorUtils.chainedComparator(ComparatorUtils.<Integer>naturalComparator(),
50                                                                       ComparatorUtils.<Integer>naturalComparator());
51          assertTrue(comp.compare(1, 2) < 0);
52          assertTrue(comp.compare(1, 1) == 0);
53          assertTrue(comp.compare(2, 1) > 0);
54      }
55  
56      @Test
57      public void max() {
58          final Comparator<Integer> reversed =
59                  ComparatorUtils.reversedComparator(ComparatorUtils.<Integer>naturalComparator());
60  
61          assertEquals(Integer.valueOf(10), ComparatorUtils.max(1, 10, null));
62          assertEquals(Integer.valueOf(10), ComparatorUtils.max(10, -10, null));
63  
64          assertEquals(Integer.valueOf(1), ComparatorUtils.max(1, 10, reversed));
65          assertEquals(Integer.valueOf(-10), ComparatorUtils.max(10, -10, reversed));
66  
67          try {
68              ComparatorUtils.max(1, null, null);
69              fail("expecting NullPointerException");
70          } catch (final NullPointerException npe) {
71              // expected
72          }
73  
74          try {
75              ComparatorUtils.max(null, 10, null);
76              fail("expecting NullPointerException");
77          } catch (final NullPointerException npe) {
78              // expected
79          }
80      }
81  
82      @Test
83      public void min() {
84          final Comparator<Integer> reversed =
85                  ComparatorUtils.reversedComparator(ComparatorUtils.<Integer>naturalComparator());
86  
87          assertEquals(Integer.valueOf(1), ComparatorUtils.min(1, 10, null));
88          assertEquals(Integer.valueOf(-10), ComparatorUtils.min(10, -10, null));
89  
90          assertEquals(Integer.valueOf(10), ComparatorUtils.min(1, 10, reversed));
91          assertEquals(Integer.valueOf(10), ComparatorUtils.min(10, -10, reversed));
92  
93          try {
94              ComparatorUtils.min(1, null, null);
95              fail("expecting NullPointerException");
96          } catch (final NullPointerException npe) {
97              // expected
98          }
99  
100         try {
101             ComparatorUtils.min(null, 10, null);
102             fail("expecting NullPointerException");
103         } catch (final NullPointerException npe) {
104             // expected
105         }
106     }
107 
108     @Test
109     public void nullLowComparator() {
110         final Comparator<Integer> comp = ComparatorUtils.nullLowComparator(null);
111         assertTrue(comp.compare(null, 10) < 0);
112         assertTrue(comp.compare(null, null) == 0);
113         assertTrue(comp.compare(10, null) > 0);
114     }
115 
116     @Test
117     public void nullHighComparator() {
118         final Comparator<Integer> comp = ComparatorUtils.nullHighComparator(null);
119         assertTrue(comp.compare(null, 10) > 0);
120         assertTrue(comp.compare(null, null) == 0);
121         assertTrue(comp.compare(10, null) < 0);
122     }
123 
124 }