1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
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
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
72 }
73
74 try {
75 ComparatorUtils.max(null, 10, null);
76 fail("expecting NullPointerException");
77 } catch (final NullPointerException npe) {
78
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
98 }
99
100 try {
101 ComparatorUtils.min(null, 10, null);
102 fail("expecting NullPointerException");
103 } catch (final NullPointerException npe) {
104
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 }