1 /* 2 * Copyright (c) 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 * @run testng UnmodifiableMapEntrySet 27 * @summary Unit tests for wrapping classes should delegate to default methods 28 */ 29 30 package test.java.util.Collections; 31 32 import java.util.ArrayList; 33 import java.util.Collections; 34 import java.util.HashMap; 35 import java.util.List; 36 import java.util.Map; 37 import java.util.Spliterator; 38 import java.util.TreeMap; 39 import java.util.function.Consumer; 40 import java.util.function.Function; 41 import java.util.function.Supplier; 42 43 import org.testng.annotations.Test; 44 import org.testng.annotations.DataProvider; 45 46 import static org.testng.Assert.assertEquals; 47 48 @Test(groups = "unit") 49 public class UnmodifiableMapEntrySet { 50 static Object[][] collections; 51 fillMap(int size, M m)52 static <M extends Map<Integer, Integer>> M fillMap(int size, M m) { 53 for (int i = 0; i < size; i++) { 54 m.put(i, i); 55 } 56 return m; 57 } 58 59 @DataProvider(name="maps") mapCases()60 static Object[][] mapCases() { 61 if (collections != null) { 62 return collections; 63 } 64 65 List<Object[]> cases = new ArrayList<>(); 66 for (int size : new int[] {1, 2, 16}) { 67 cases.add(new Object[] { 68 String.format("new HashMap(%d)", size), 69 (Supplier<Map<Integer, Integer>>) 70 () -> Collections.unmodifiableMap(fillMap(size, new HashMap<>())) }); 71 cases.add(new Object[] { 72 String.format("new TreeMap(%d)", size), 73 (Supplier<Map<Integer, Integer>>) 74 () -> Collections.unmodifiableSortedMap(fillMap(size, new TreeMap<>())) }); 75 } 76 77 return cases.toArray(new Object[0][]); 78 } 79 80 static class EntryConsumer implements Consumer<Map.Entry<Integer, Integer>> { 81 int updates; 82 @Override accept(Map.Entry<Integer, Integer> me)83 public void accept(Map.Entry<Integer, Integer> me) { 84 try { 85 me.setValue(Integer.MAX_VALUE); 86 updates++; 87 } catch (UnsupportedOperationException e) { 88 } 89 } 90 assertNoUpdates()91 void assertNoUpdates() { 92 assertEquals(updates, 0, "Updates to entries"); 93 } 94 } 95 testWithEntryConsumer(Consumer<EntryConsumer> c)96 void testWithEntryConsumer(Consumer<EntryConsumer> c) { 97 EntryConsumer ec = new EntryConsumer(); 98 c.accept(ec); 99 ec.assertNoUpdates(); 100 } 101 102 @Test(dataProvider = "maps") testForEach(String d, Supplier<Map<Integer, Integer>> ms)103 public void testForEach(String d, Supplier<Map<Integer, Integer>> ms) { 104 testWithEntryConsumer( 105 ec -> ms.get().entrySet().forEach(ec)); 106 } 107 108 @Test(dataProvider = "maps") testIteratorForEachRemaining(String d, Supplier<Map<Integer, Integer>> ms)109 public void testIteratorForEachRemaining(String d, Supplier<Map<Integer, Integer>> ms) { 110 testWithEntryConsumer( 111 ec -> ms.get().entrySet().iterator().forEachRemaining(ec)); 112 } 113 114 @Test(dataProvider = "maps") testIteratorNext(String d, Supplier<Map<Integer, Integer>> ms)115 public void testIteratorNext(String d, Supplier<Map<Integer, Integer>> ms) { 116 testWithEntryConsumer(ec -> { 117 for (Map.Entry<Integer, Integer> me : ms.get().entrySet()) { 118 ec.accept(me); 119 } 120 }); 121 } 122 123 @Test(dataProvider = "maps") testSpliteratorForEachRemaining(String d, Supplier<Map<Integer, Integer>> ms)124 public void testSpliteratorForEachRemaining(String d, Supplier<Map<Integer, Integer>> ms) { 125 testSpliterator( 126 ms.get().entrySet()::spliterator, 127 // Higher order function returning a consumer that 128 // traverses all spliterator elements using an EntryConsumer 129 s -> ec -> s.forEachRemaining(ec)); 130 } 131 132 @Test(dataProvider = "maps") testSpliteratorTryAdvance(String d, Supplier<Map<Integer, Integer>> ms)133 public void testSpliteratorTryAdvance(String d, Supplier<Map<Integer, Integer>> ms) { 134 testSpliterator( 135 ms.get().entrySet()::spliterator, 136 // Higher order function returning a consumer that 137 // traverses all spliterator elements using an EntryConsumer 138 s -> ec -> { while (s.tryAdvance(ec)); }); 139 } 140 testSpliterator(Supplier<Spliterator<Map.Entry<Integer, Integer>>> ss, Function<Spliterator<Map.Entry<Integer, Integer>>, Consumer<EntryConsumer>> sc)141 void testSpliterator(Supplier<Spliterator<Map.Entry<Integer, Integer>>> ss, 142 // Higher order function that given a spliterator returns a 143 // consumer for that spliterator which traverses elements 144 // using an EntryConsumer 145 Function<Spliterator<Map.Entry<Integer, Integer>>, Consumer<EntryConsumer>> sc) { 146 testWithEntryConsumer(sc.apply(ss.get())); 147 148 Spliterator<Map.Entry<Integer, Integer>> s = ss.get(); 149 Spliterator<Map.Entry<Integer, Integer>> split = s.trySplit(); 150 if (split != null) { 151 testWithEntryConsumer(sc.apply(split)); 152 testWithEntryConsumer(sc.apply(s)); 153 } 154 } 155 156 @Test(dataProvider = "maps") testStreamForEach(String d, Supplier<Map<Integer, Integer>> ms)157 public void testStreamForEach(String d, Supplier<Map<Integer, Integer>> ms) { 158 testWithEntryConsumer(ec -> ms.get().entrySet().stream().forEach(ec)); 159 } 160 161 @Test(dataProvider = "maps") testParallelStreamForEach(String d, Supplier<Map<Integer, Integer>> ms)162 public void testParallelStreamForEach(String d, Supplier<Map<Integer, Integer>> ms) { 163 testWithEntryConsumer(ec -> ms.get().entrySet().parallelStream().forEach(ec)); 164 } 165 } 166 167