Path: blob/master/test/jdk/java/util/Collections/UnmodifiableMapEntrySet.java
41149 views
/*1* Copyright (c) 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* @run testng UnmodifiableMapEntrySet26* @summary Unit tests for wrapping classes should delegate to default methods27*/2829import java.util.ArrayList;30import java.util.Collections;31import java.util.HashMap;32import java.util.List;33import java.util.Map;34import java.util.Spliterator;35import java.util.TreeMap;36import java.util.function.Consumer;37import java.util.function.Function;38import java.util.function.Supplier;3940import org.testng.annotations.Test;41import org.testng.annotations.DataProvider;4243import static org.testng.Assert.assertEquals;4445@Test(groups = "unit")46public class UnmodifiableMapEntrySet {47static Object[][] collections;4849static <M extends Map<Integer, Integer>> M fillMap(int size, M m) {50for (int i = 0; i < size; i++) {51m.put(i, i);52}53return m;54}5556@DataProvider(name="maps")57static Object[][] mapCases() {58if (collections != null) {59return collections;60}6162List<Object[]> cases = new ArrayList<>();63for (int size : new int[] {1, 2, 16}) {64cases.add(new Object[] {65String.format("new HashMap(%d)", size),66(Supplier<Map<Integer, Integer>>)67() -> Collections.unmodifiableMap(fillMap(size, new HashMap<>())) });68cases.add(new Object[] {69String.format("new TreeMap(%d)", size),70(Supplier<Map<Integer, Integer>>)71() -> Collections.unmodifiableSortedMap(fillMap(size, new TreeMap<>())) });72}7374return cases.toArray(new Object[0][]);75}7677static class EntryConsumer implements Consumer<Map.Entry<Integer, Integer>> {78int updates;79@Override80public void accept(Map.Entry<Integer, Integer> me) {81try {82me.setValue(Integer.MAX_VALUE);83updates++;84} catch (UnsupportedOperationException e) {85}86}8788void assertNoUpdates() {89assertEquals(updates, 0, "Updates to entries");90}91}9293void testWithEntryConsumer(Consumer<EntryConsumer> c) {94EntryConsumer ec = new EntryConsumer();95c.accept(ec);96ec.assertNoUpdates();97}9899@Test(dataProvider = "maps")100public void testForEach(String d, Supplier<Map<Integer, Integer>> ms) {101testWithEntryConsumer(102ec -> ms.get().entrySet().forEach(ec));103}104105@Test(dataProvider = "maps")106public void testIteratorForEachRemaining(String d, Supplier<Map<Integer, Integer>> ms) {107testWithEntryConsumer(108ec -> ms.get().entrySet().iterator().forEachRemaining(ec));109}110111@Test(dataProvider = "maps")112public void testIteratorNext(String d, Supplier<Map<Integer, Integer>> ms) {113testWithEntryConsumer(ec -> {114for (Map.Entry<Integer, Integer> me : ms.get().entrySet()) {115ec.accept(me);116}117});118}119120@Test(dataProvider = "maps")121public void testSpliteratorForEachRemaining(String d, Supplier<Map<Integer, Integer>> ms) {122testSpliterator(123ms.get().entrySet()::spliterator,124// Higher order function returning a consumer that125// traverses all spliterator elements using an EntryConsumer126s -> ec -> s.forEachRemaining(ec));127}128129@Test(dataProvider = "maps")130public void testSpliteratorTryAdvance(String d, Supplier<Map<Integer, Integer>> ms) {131testSpliterator(132ms.get().entrySet()::spliterator,133// Higher order function returning a consumer that134// traverses all spliterator elements using an EntryConsumer135s -> ec -> { while (s.tryAdvance(ec)); });136}137138void testSpliterator(Supplier<Spliterator<Map.Entry<Integer, Integer>>> ss,139// Higher order function that given a spliterator returns a140// consumer for that spliterator which traverses elements141// using an EntryConsumer142Function<Spliterator<Map.Entry<Integer, Integer>>, Consumer<EntryConsumer>> sc) {143testWithEntryConsumer(sc.apply(ss.get()));144145Spliterator<Map.Entry<Integer, Integer>> s = ss.get();146Spliterator<Map.Entry<Integer, Integer>> split = s.trySplit();147if (split != null) {148testWithEntryConsumer(sc.apply(split));149testWithEntryConsumer(sc.apply(s));150}151}152153@Test(dataProvider = "maps")154public void testStreamForEach(String d, Supplier<Map<Integer, Integer>> ms) {155testWithEntryConsumer(ec -> ms.get().entrySet().stream().forEach(ec));156}157158@Test(dataProvider = "maps")159public void testParallelStreamForEach(String d, Supplier<Map<Integer, Integer>> ms) {160testWithEntryConsumer(ec -> ms.get().entrySet().parallelStream().forEach(ec));161}162}163164165166