Path: blob/master/test/jdk/java/util/Map/MapFactories.java
41149 views
/*1* Copyright (c) 2015, 2021, 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*/2223import java.io.ByteArrayInputStream;24import java.io.ByteArrayOutputStream;25import java.io.IOException;26import java.io.ObjectInputStream;27import java.io.ObjectOutputStream;28import java.util.AbstractMap;29import java.util.ArrayList;30import java.util.Arrays;31import java.util.Collections;32import java.util.Iterator;33import java.util.HashMap;34import java.util.List;35import java.util.Map;36import java.util.stream.IntStream;3738import org.testng.annotations.DataProvider;39import org.testng.annotations.Test;4041import static org.testng.Assert.assertEquals;42import static org.testng.Assert.assertFalse;43import static org.testng.Assert.assertNotEquals;44import static org.testng.Assert.assertNotSame;45import static org.testng.Assert.assertSame;46import static org.testng.Assert.assertThrows;47import static org.testng.Assert.assertTrue;4849/*50* @test51* @bug 8048330 822192452* @summary Test convenience static factory methods on Map.53* @run testng MapFactories54*/5556public class MapFactories {5758static final int MAX_ENTRIES = 20; // should be larger than the largest fixed-arg overload59static String valueFor(int i) {60// the String literal below should be of length MAX_ENTRIES61return "abcdefghijklmnopqrst".substring(i, i+1);62}6364// for "expected" values65Map<Integer,String> genMap(int n) {66Map<Integer,String> result = new HashMap<>();67for (int i = 0; i < n; i++) {68result.put(i, valueFor(i));69}70return result;71}7273// for varargs Map.Entry methods7475@SuppressWarnings("unchecked")76Map.Entry<Integer,String>[] genEmptyEntryArray1() {77return (Map.Entry<Integer,String>[])new Map.Entry<?,?>[1];78}7980@SuppressWarnings("unchecked")81Map.Entry<Integer,String>[] genEntries(int n) {82return IntStream.range(0, n)83.mapToObj(i -> Map.entry(i, valueFor(i)))84.toArray(Map.Entry[]::new);85}8687// returns array of [actual, expected]88static Object[] a(Map<Integer,String> act, Map<Integer,String> exp) {89return new Object[] { act, exp };90}9192@DataProvider(name="empty")93public Iterator<Object[]> empty() {94return Collections.singletonList(95a(Map.of(), genMap(0))96).iterator();97}9899@DataProvider(name="nonempty")100@SuppressWarnings("unchecked")101public Iterator<Object[]> nonempty() {102return Arrays.asList(103a(Map.of(0, "a"), genMap(1)),104a(Map.of(0, "a", 1, "b"), genMap(2)),105a(Map.of(0, "a", 1, "b", 2, "c"), genMap(3)),106a(Map.of(0, "a", 1, "b", 2, "c", 3, "d"), genMap(4)),107a(Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e"), genMap(5)),108a(Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e", 5, "f"), genMap(6)),109a(Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e", 5, "f", 6, "g"), genMap(7)),110a(Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e", 5, "f", 6, "g", 7, "h"), genMap(8)),111a(Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e", 5, "f", 6, "g", 7, "h", 8, "i"), genMap(9)),112a(Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e", 5, "f", 6, "g", 7, "h", 8, "i", 9, "j"), genMap(10)),113a(Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e", 5, "f", 6, "g", 7, "h", 8, "i", 9, "j"),114Map.of(4, "e", 5, "f", 6, "g", 7, "h", 8, "i", 9, "j", 0, "a", 1, "b", 2, "c", 3, "d")),115a(Map.ofEntries(genEntries(MAX_ENTRIES)), genMap(MAX_ENTRIES))116).iterator();117}118119@DataProvider(name="all")120public Iterator<Object[]> all() {121List<Object[]> all = new ArrayList<>();122empty().forEachRemaining(all::add);123nonempty().forEachRemaining(all::add);124return all.iterator();125}126127@Test(dataProvider="all", expectedExceptions=UnsupportedOperationException.class)128public void cannotPutNew(Map<Integer,String> act, Map<Integer,String> exp) {129act.put(-1, "xyzzy");130}131132@Test(dataProvider="nonempty", expectedExceptions=UnsupportedOperationException.class)133public void cannotPutOld(Map<Integer,String> act, Map<Integer,String> exp) {134act.put(0, "a");135}136137@Test(dataProvider="nonempty", expectedExceptions=UnsupportedOperationException.class)138public void cannotRemove(Map<Integer,String> act, Map<Integer,String> exp) {139act.remove(act.keySet().iterator().next());140}141142@Test(dataProvider="all")143public void contentsMatch(Map<Integer,String> act, Map<Integer,String> exp) {144assertEquals(act, exp);145}146147@Test(dataProvider="all")148public void containsAllKeys(Map<Integer,String> act, Map<Integer,String> exp) {149assertTrue(act.keySet().containsAll(exp.keySet()));150assertTrue(exp.keySet().containsAll(act.keySet()));151}152153@Test(dataProvider="all")154public void containsAllValues(Map<Integer,String> act, Map<Integer,String> exp) {155assertTrue(act.values().containsAll(exp.values()));156assertTrue(exp.values().containsAll(act.values()));157}158159@Test(expectedExceptions=IllegalArgumentException.class)160public void dupKeysDisallowed2() {161Map<Integer, String> map = Map.of(0, "a", 0, "b");162}163164@Test(expectedExceptions=IllegalArgumentException.class)165public void dupKeysDisallowed3() {166Map<Integer, String> map = Map.of(0, "a", 1, "b", 0, "c");167}168169@Test(expectedExceptions=IllegalArgumentException.class)170public void dupKeysDisallowed4() {171Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 0, "d");172}173174@Test(expectedExceptions=IllegalArgumentException.class)175public void dupKeysDisallowed5() {176Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 0, "e");177}178179@Test(expectedExceptions=IllegalArgumentException.class)180public void dupKeysDisallowed6() {181Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",1820, "f");183}184185@Test(expectedExceptions=IllegalArgumentException.class)186public void dupKeysDisallowed7() {187Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",1885, "f", 0, "g");189}190191@Test(expectedExceptions=IllegalArgumentException.class)192public void dupKeysDisallowed8() {193Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",1945, "f", 6, "g", 0, "h");195}196197@Test(expectedExceptions=IllegalArgumentException.class)198public void dupKeysDisallowed9() {199Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",2005, "f", 6, "g", 7, "h", 0, "i");201}202203@Test(expectedExceptions=IllegalArgumentException.class)204public void dupKeysDisallowed10() {205Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",2065, "f", 6, "g", 7, "h", 8, "i", 0, "j");207}208209@Test(expectedExceptions=IllegalArgumentException.class)210public void dupKeysDisallowedN() {211Map.Entry<Integer,String>[] entries = genEntries(MAX_ENTRIES);212entries[MAX_ENTRIES-1] = Map.entry(0, "xxx");213Map<Integer, String> map = Map.ofEntries(entries);214}215216@Test(dataProvider="all")217public void hashCodeEquals(Map<Integer,String> act, Map<Integer,String> exp) {218assertEquals(act.hashCode(), exp.hashCode());219}220221@Test(expectedExceptions=NullPointerException.class)222public void nullKeyDisallowed1() {223Map<Integer, String> map = Map.of(null, "a");224}225226@Test(expectedExceptions=NullPointerException.class)227public void nullValueDisallowed1() {228Map<Integer, String> map = Map.of(0, null);229}230231@Test(expectedExceptions=NullPointerException.class)232public void nullKeyDisallowed2() {233Map<Integer, String> map = Map.of(0, "a", null, "b");234}235236@Test(expectedExceptions=NullPointerException.class)237public void nullValueDisallowed2() {238Map<Integer, String> map = Map.of(0, "a", 1, null);239}240241@Test(expectedExceptions=NullPointerException.class)242public void nullKeyDisallowed3() {243Map<Integer, String> map = Map.of(0, "a", 1, "b", null, "c");244}245246@Test(expectedExceptions=NullPointerException.class)247public void nullValueDisallowed3() {248Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, null);249}250251@Test(expectedExceptions=NullPointerException.class)252public void nullKeyDisallowed4() {253Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", null, "d");254}255256@Test(expectedExceptions=NullPointerException.class)257public void nullValueDisallowed4() {258Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, null);259}260261@Test(expectedExceptions=NullPointerException.class)262public void nullKeyDisallowed5() {263Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", null, "e");264}265266@Test(expectedExceptions=NullPointerException.class)267public void nullValueDisallowed5() {268Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, null);269}270271@Test(expectedExceptions=NullPointerException.class)272public void nullKeyDisallowed6() {273Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",274null, "f");275}276277@Test(expectedExceptions=NullPointerException.class)278public void nullValueDisallowed6() {279Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",2805, null);281}282283@Test(expectedExceptions=NullPointerException.class)284public void nullKeyDisallowed7() {285Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",2865, "f", null, "g");287}288289@Test(expectedExceptions=NullPointerException.class)290public void nullValueDisallowed7() {291Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",2925, "f", 6, null);293}294295@Test(expectedExceptions=NullPointerException.class)296public void nullKeyDisallowed8() {297Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",2985, "f", 6, "g", null, "h");299}300301@Test(expectedExceptions=NullPointerException.class)302public void nullValueDisallowed8() {303Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",3045, "f", 6, "g", 7, null);305}306307@Test(expectedExceptions=NullPointerException.class)308public void nullKeyDisallowed9() {309Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",3105, "f", 6, "g", 7, "h", null, "i");311}312313@Test(expectedExceptions=NullPointerException.class)314public void nullValueDisallowed9() {315Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",3165, "f", 6, "g", 7, "h", 8, null);317}318319@Test(expectedExceptions=NullPointerException.class)320public void nullKeyDisallowed10() {321Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",3225, "f", 6, "g", 7, "h", 8, "i", null, "j");323}324325@Test(expectedExceptions=NullPointerException.class)326public void nullValueDisallowed10() {327Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",3285, "f", 6, "g", 7, "h", 8, "i", 9, null);329}330331@Test(expectedExceptions=NullPointerException.class)332public void nullKeyDisallowedVar1() {333Map.Entry<Integer,String>[] entries = genEmptyEntryArray1();334entries[0] = new AbstractMap.SimpleImmutableEntry<>(null, "a");335Map<Integer, String> map = Map.ofEntries(entries);336}337338@Test(expectedExceptions=NullPointerException.class)339public void nullValueDisallowedVar1() {340Map.Entry<Integer,String>[] entries = genEmptyEntryArray1();341entries[0] = new AbstractMap.SimpleImmutableEntry<>(0, null);342Map<Integer, String> map = Map.ofEntries(entries);343}344345@Test(expectedExceptions=NullPointerException.class)346public void nullEntryDisallowedVar1() {347Map.Entry<Integer,String>[] entries = genEmptyEntryArray1();348Map<Integer, String> map = Map.ofEntries(entries);349}350351@Test(expectedExceptions=NullPointerException.class)352public void nullKeyDisallowedVarN() {353Map.Entry<Integer,String>[] entries = genEntries(MAX_ENTRIES);354entries[0] = new AbstractMap.SimpleImmutableEntry<>(null, "a");355Map<Integer, String> map = Map.ofEntries(entries);356}357358@Test(expectedExceptions=NullPointerException.class)359public void nullValueDisallowedVarN() {360Map.Entry<Integer,String>[] entries = genEntries(MAX_ENTRIES);361entries[0] = new AbstractMap.SimpleImmutableEntry<>(0, null);362Map<Integer, String> map = Map.ofEntries(entries);363}364365@Test(expectedExceptions=NullPointerException.class)366public void nullEntryDisallowedVarN() {367Map.Entry<Integer,String>[] entries = genEntries(MAX_ENTRIES);368entries[5] = null;369Map<Integer, String> map = Map.ofEntries(entries);370}371372@Test(expectedExceptions=NullPointerException.class)373public void nullArrayDisallowed() {374Map.ofEntries((Map.Entry<?,?>[])null);375}376377@Test(dataProvider="all", expectedExceptions=NullPointerException.class)378public void containsValueNullShouldThrowNPE(Map<Integer,String> act, Map<Integer,String> exp) {379act.containsValue(null);380}381382@Test(dataProvider="all", expectedExceptions=NullPointerException.class)383public void containsKeyNullShouldThrowNPE(Map<Integer,String> act, Map<Integer,String> exp) {384act.containsKey(null);385}386387@Test(dataProvider="all", expectedExceptions=NullPointerException.class)388public void getNullShouldThrowNPE(Map<Integer,String> act, Map<Integer,String> exp) {389act.get(null);390}391392@Test(dataProvider="all")393public void serialEquality(Map<Integer, String> act, Map<Integer, String> exp) {394// assume that act.equals(exp) tested elsewhere395Map<Integer, String> copy = serialClone(act);396assertEquals(act, copy);397assertEquals(copy, exp);398}399400@SuppressWarnings("unchecked")401static <T> T serialClone(T obj) {402try {403ByteArrayOutputStream baos = new ByteArrayOutputStream();404try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {405oos.writeObject(obj);406}407ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());408ObjectInputStream ois = new ObjectInputStream(bais);409return (T) ois.readObject();410} catch (IOException | ClassNotFoundException e) {411throw new AssertionError(e);412}413}414415Map<Integer, String> genMap() {416Map<Integer, String> map = new HashMap<>();417map.put(1, "a");418map.put(2, "b");419map.put(3, "c");420return map;421}422423@Test424public void copyOfResultsEqual() {425Map<Integer, String> orig = genMap();426Map<Integer, String> copy = Map.copyOf(orig);427428assertEquals(orig, copy);429assertEquals(copy, orig);430}431432@Test433public void copyOfModifiedUnequal() {434Map<Integer, String> orig = genMap();435Map<Integer, String> copy = Map.copyOf(orig);436orig.put(4, "d");437438assertNotEquals(orig, copy);439assertNotEquals(copy, orig);440}441442@Test443public void copyOfIdentity() {444Map<Integer, String> orig = genMap();445Map<Integer, String> copy1 = Map.copyOf(orig);446Map<Integer, String> copy2 = Map.copyOf(copy1);447448assertNotSame(orig, copy1);449assertSame(copy1, copy2);450}451452@Test(expectedExceptions=NullPointerException.class)453public void copyOfRejectsNullMap() {454Map<Integer, String> map = Map.copyOf(null);455}456457@Test(expectedExceptions=NullPointerException.class)458public void copyOfRejectsNullKey() {459Map<Integer, String> map = genMap();460map.put(null, "x");461Map<Integer, String> copy = Map.copyOf(map);462}463464@Test(expectedExceptions=NullPointerException.class)465public void copyOfRejectsNullValue() {466Map<Integer, String> map = genMap();467map.put(-1, null);468Map<Integer, String> copy = Map.copyOf(map);469}470471// Map::entry tests472473@Test(expectedExceptions=NullPointerException.class)474public void entryWithNullKeyDisallowed() {475Map.Entry<Integer,String> e = Map.entry(null, "x");476}477478@Test(expectedExceptions=NullPointerException.class)479public void entryWithNullValueDisallowed() {480Map.Entry<Integer,String> e = Map.entry(0, null);481}482483@Test484public void entrySetValueDisallowed() {485var e = Map.entry("a", "b");486assertThrows(UnsupportedOperationException.class, () -> e.setValue("x"));487}488489@Test490public void entryBasicTests() {491Map.Entry<String,String> kvh1 = Map.entry("xyzzy", "plugh");492Map.Entry<String,String> kvh2 = Map.entry("foobar", "blurfl");493Map.Entry<String,String> sie = new AbstractMap.SimpleImmutableEntry<>("xyzzy", "plugh");494495assertTrue(kvh1.equals(sie));496assertTrue(sie.equals(kvh1));497assertFalse(kvh2.equals(sie));498assertFalse(sie.equals(kvh2));499assertEquals(kvh1.hashCode(), sie.hashCode());500assertEquals(kvh1.toString(), sie.toString());501}502503// Map.Entry::copyOf tests504505@Test(expectedExceptions=NullPointerException.class)506public void entryCopyNullDisallowed() {507Map.Entry.copyOf(null);508}509510@Test511public void entryCopyWithNullKeyDisallowed() {512var e = new AbstractMap.SimpleEntry<>(null, "b");513assertThrows(NullPointerException.class, () -> Map.Entry.copyOf(e));514}515516@Test517public void entryCopyWithNullValueDisallowed() {518var e = new AbstractMap.SimpleEntry<>("a", null);519assertThrows(NullPointerException.class, () -> Map.Entry.copyOf(e));520}521522@Test523public void entryCopySetValueDisallowed() {524var e = new AbstractMap.SimpleEntry<>("a", "b");525var c = Map.Entry.copyOf(e);526assertThrows(UnsupportedOperationException.class, () -> c.setValue("x"));527}528529@Test530public void entryCopyBasicTests() {531Map.Entry<String,String> orig = new AbstractMap.SimpleImmutableEntry<>("xyzzy", "plugh");532Map.Entry<String,String> copy1 = Map.Entry.copyOf(orig);533Map.Entry<String,String> copy2 = Map.Entry.copyOf(copy1);534535assertEquals(orig, copy1);536assertEquals(copy1, orig);537assertEquals(orig, copy2);538assertEquals(copy2, orig);539assertEquals(copy1, copy2);540assertEquals(copy2, copy1);541542assertNotSame(orig, copy1);543assertSame(copy1, copy2);544545assertEquals(copy1.hashCode(), orig.hashCode());546assertEquals(copy1.toString(), orig.toString());547}548549// compile-time test of wildcards550@Test551public void entryWildcardTests() {552Map.Entry<Integer,Double> e1 = Map.entry(1, 2.0);553Map.Entry<Float,Long> e2 = Map.entry(3.0f, 4L);554Map<Number,Number> map = Map.ofEntries(e1, e2);555assertEquals(map.size(), 2);556}557}558559560