Path: blob/master/test/jdk/java/util/Map/EntryComparators.java
41149 views
/*1* Copyright (c) 2012, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 8009736 801027926* @run testng EntryComparators27*/28import java.util.function.Function;29import java.util.Comparator;30import java.util.AbstractMap;31import java.util.Map;32import org.testng.annotations.Test;3334import static org.testng.Assert.assertTrue;35import static org.testng.Assert.fail;3637/**38* Unit tests for Map.Entry.comparing39*/40@Test(groups = "unit")41public class EntryComparators {4243private <K, V> void assertPairComparison(K k1, V v1, K k2, V v2,44Comparator<Map.Entry<K, V>> ck,45Comparator<Map.Entry<K, V>> cv) {46final Map.Entry<K, V> p11 = new AbstractMap.SimpleImmutableEntry<>(k1, v1);47final Map.Entry<K, V> p12 = new AbstractMap.SimpleImmutableEntry<>(k1, v2);48final Map.Entry<K, V> p21 = new AbstractMap.SimpleImmutableEntry<>(k2, v1);49final Map.Entry<K, V> p22 = new AbstractMap.SimpleImmutableEntry<>(k2, v2);5051assertTrue(ck.compare(p11, p11) == 0);52assertTrue(ck.compare(p12, p11) == 0);53assertTrue(ck.compare(p11, p12) == 0);54assertTrue(ck.compare(p12, p22) < 0);55assertTrue(ck.compare(p12, p21) < 0);56assertTrue(ck.compare(p21, p11) > 0);57assertTrue(ck.compare(p21, p12) > 0);5859assertTrue(cv.compare(p11, p11) == 0);60assertTrue(cv.compare(p12, p11) > 0);61assertTrue(cv.compare(p11, p12) < 0);62assertTrue(cv.compare(p12, p22) == 0);63assertTrue(cv.compare(p12, p21) > 0);64assertTrue(cv.compare(p21, p11) == 0);65assertTrue(cv.compare(p21, p12) < 0);6667Comparator<Map.Entry<K, V>> cmp = ck.thenComparing(cv);68assertTrue(cmp.compare(p11, p11) == 0);69assertTrue(cmp.compare(p12, p11) > 0);70assertTrue(cmp.compare(p11, p12) < 0);71assertTrue(cmp.compare(p12, p22) < 0);72assertTrue(cmp.compare(p12, p21) < 0);73assertTrue(cmp.compare(p21, p11) > 0);74assertTrue(cmp.compare(p21, p12) > 0);7576cmp = cv.thenComparing(ck);77assertTrue(cmp.compare(p11, p11) == 0);78assertTrue(cmp.compare(p12, p11) > 0);79assertTrue(cmp.compare(p11, p12) < 0);80assertTrue(cmp.compare(p12, p22) < 0);81assertTrue(cmp.compare(p12, p21) > 0);82assertTrue(cmp.compare(p21, p11) > 0);83assertTrue(cmp.compare(p21, p12) < 0);84}8586public void testKVComparables() {87assertPairComparison(1, "ABC", 2, "XYZ",88Map.Entry.<Integer, String>comparingByKey(),89Map.Entry.<Integer, String>comparingByValue());90}9192private static class People {93final String firstName;94final String lastName;95final int age;9697People(String first, String last, int age) {98firstName = first;99lastName = last;100this.age = age;101}102103String getFirstName() { return firstName; }104String getLastName() { return lastName; }105int getAge() { return age; }106}107108private final People people[] = {109new People("John", "Doe", 34),110new People("Mary", "Doe", 30),111};112113public void testKVComparators() {114// Comparator<People> cmp = Comparator.naturalOrder(); // Should fail to compiler as People is not comparable115// We can use simple comparator, but those have been tested above.116// Thus choose to do compose for some level of interation.117Comparator<People> cmp1 = Comparator.comparing(People::getFirstName);118Comparator<People> cmp2 = Comparator.comparing(People::getLastName);119Comparator<People> cmp = cmp1.thenComparing(cmp2);120121assertPairComparison(people[0], people[0], people[1], people[1],122Map.Entry.<People, People>comparingByKey(cmp),123Map.Entry.<People, People>comparingByValue(cmp));124125}126127public void testNulls() {128try {129Comparator<Map.Entry<String, String>> cmp = Map.Entry.comparingByKey(null);130fail("comparingByKey(null) should throw NPE");131} catch (NullPointerException npe) {}132133try {134Comparator<Map.Entry<String, String>> cmp = Map.Entry.comparingByValue(null);135fail("comparingByValue(null) should throw NPE");136} catch (NullPointerException npe) {}137}138}139140141