1 /*
2  * Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 /*
25  * @test
26  * @bug 8009736 8010279
27  * @run testng EntryComparators
28  */
29 import java.util.function.Function;
30 import java.util.Comparator;
31 import java.util.AbstractMap;
32 import java.util.Map;
33 import org.testng.annotations.Test;
34 
35 import static org.testng.Assert.assertTrue;
36 import static org.testng.Assert.fail;
37 
38 /**
39  * Unit tests for Map.Entry.comparing
40  */
41 @Test(groups = "unit")
42 public class EntryComparators {
43 
assertPairComparison(K k1, V v1, K k2, V v2, Comparator<Map.Entry<K, V>> ck, Comparator<Map.Entry<K, V>> cv)44     private <K, V> void assertPairComparison(K k1, V v1, K k2, V v2,
45                                         Comparator<Map.Entry<K, V>> ck,
46                                         Comparator<Map.Entry<K, V>> cv) {
47         final Map.Entry<K, V> p11 = new AbstractMap.SimpleImmutableEntry<>(k1, v1);
48         final Map.Entry<K, V> p12 = new AbstractMap.SimpleImmutableEntry<>(k1, v2);
49         final Map.Entry<K, V> p21 = new AbstractMap.SimpleImmutableEntry<>(k2, v1);
50         final Map.Entry<K, V> p22 = new AbstractMap.SimpleImmutableEntry<>(k2, v2);
51 
52         assertTrue(ck.compare(p11, p11) == 0);
53         assertTrue(ck.compare(p12, p11) == 0);
54         assertTrue(ck.compare(p11, p12) == 0);
55         assertTrue(ck.compare(p12, p22) < 0);
56         assertTrue(ck.compare(p12, p21) < 0);
57         assertTrue(ck.compare(p21, p11) > 0);
58         assertTrue(ck.compare(p21, p12) > 0);
59 
60         assertTrue(cv.compare(p11, p11) == 0);
61         assertTrue(cv.compare(p12, p11) > 0);
62         assertTrue(cv.compare(p11, p12) < 0);
63         assertTrue(cv.compare(p12, p22) == 0);
64         assertTrue(cv.compare(p12, p21) > 0);
65         assertTrue(cv.compare(p21, p11) == 0);
66         assertTrue(cv.compare(p21, p12) < 0);
67 
68         Comparator<Map.Entry<K, V>> cmp = ck.thenComparing(cv);
69         assertTrue(cmp.compare(p11, p11) == 0);
70         assertTrue(cmp.compare(p12, p11) > 0);
71         assertTrue(cmp.compare(p11, p12) < 0);
72         assertTrue(cmp.compare(p12, p22) < 0);
73         assertTrue(cmp.compare(p12, p21) < 0);
74         assertTrue(cmp.compare(p21, p11) > 0);
75         assertTrue(cmp.compare(p21, p12) > 0);
76 
77         cmp = cv.thenComparing(ck);
78         assertTrue(cmp.compare(p11, p11) == 0);
79         assertTrue(cmp.compare(p12, p11) > 0);
80         assertTrue(cmp.compare(p11, p12) < 0);
81         assertTrue(cmp.compare(p12, p22) < 0);
82         assertTrue(cmp.compare(p12, p21) > 0);
83         assertTrue(cmp.compare(p21, p11) > 0);
84         assertTrue(cmp.compare(p21, p12) < 0);
85     }
86 
87     public void testKVComparables() {
88         assertPairComparison(1, "ABC", 2, "XYZ",
89                          Map.Entry.<Integer, String>comparingByKey(),
90                          Map.Entry.<Integer, String>comparingByValue());
91     }
92 
93     private static class People {
94         final String firstName;
95         final String lastName;
96         final int age;
97 
98         People(String first, String last, int age) {
99             firstName = first;
100             lastName = last;
101             this.age = age;
102         }
103 
104         String getFirstName() { return firstName; }
105         String getLastName() { return lastName; }
106         int getAge() { return age; }
107     }
108 
109     private final People people[] = {
110         new People("John", "Doe", 34),
111         new People("Mary", "Doe", 30),
112     };
113 
114     public void testKVComparators() {
115         // Comparator<People> cmp = Comparator.naturalOrder(); // Should fail to compiler as People is not comparable
116         // We can use simple comparator, but those have been tested above.
117         // Thus choose to do compose for some level of interation.
118         Comparator<People> cmp1 = Comparator.comparing(People::getFirstName);
119         Comparator<People> cmp2 = Comparator.comparing(People::getLastName);
120         Comparator<People> cmp = cmp1.thenComparing(cmp2);
121 
122         assertPairComparison(people[0], people[0], people[1], people[1],
123                          Map.Entry.<People, People>comparingByKey(cmp),
124                          Map.Entry.<People, People>comparingByValue(cmp));
125 
126     }
127 
128     public void testNulls() {
129         try {
130             Comparator<Map.Entry<String, String>> cmp = Map.Entry.comparingByKey(null);
131             fail("comparingByKey(null) should throw NPE");
132         } catch (NullPointerException npe) {}
133 
134         try {
135             Comparator<Map.Entry<String, String>> cmp = Map.Entry.comparingByValue(null);
136             fail("comparingByValue(null) should throw NPE");
137         } catch (NullPointerException npe) {}
138     }
139 }
140