1 /* 2 * Copyright (c) 2012, 2016, 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 package org.openjdk.tests.java.util.stream; 24 25 import java.util.concurrent.ArrayBlockingQueue; 26 import java.util.concurrent.ConcurrentHashMap; 27 import java.util.concurrent.ConcurrentLinkedDeque; 28 import java.util.concurrent.ConcurrentLinkedQueue; 29 import java.util.concurrent.ConcurrentSkipListMap; 30 import java.util.concurrent.ConcurrentSkipListSet; 31 import java.util.concurrent.CopyOnWriteArrayList; 32 import java.util.concurrent.CopyOnWriteArraySet; 33 import java.util.concurrent.LinkedBlockingDeque; 34 import java.util.concurrent.LinkedBlockingQueue; 35 import java.util.concurrent.LinkedTransferQueue; 36 import java.util.concurrent.PriorityBlockingQueue; 37 import java.util.function.Supplier; 38 import java.util.stream.LambdaTestHelpers; 39 import org.testng.annotations.DataProvider; 40 import org.testng.annotations.Test; 41 42 import java.util.*; 43 import java.util.stream.Stream; 44 45 import static org.testng.Assert.assertEquals; 46 import static org.testng.Assert.assertTrue; 47 48 /** 49 * Tests laziness of stream operations -- mutations to the source after the stream() but prior to terminal operations 50 * are reflected in the stream contents. 51 */ 52 @Test 53 public class CollectionAndMapModifyStreamTest { 54 55 @DataProvider(name = "collections") createCollections()56 public Object[][] createCollections() { 57 List<Integer> content = LambdaTestHelpers.countTo(10); 58 59 List<Collection<Integer>> collections = new ArrayList<>(); 60 collections.add(new ArrayList<>(content)); 61 collections.add(new LinkedList<>(content)); 62 collections.add(new Vector<>(content)); 63 64 collections.add(new HashSet<>(content)); 65 collections.add(new LinkedHashSet<>(content)); 66 collections.add(new TreeSet<>(content)); 67 68 Stack<Integer> stack = new Stack<>(); 69 stack.addAll(content); 70 collections.add(stack); 71 collections.add(new PriorityQueue<>(content)); 72 collections.add(new ArrayDeque<>(content)); 73 74 // Concurrent collections 75 76 collections.add(new ConcurrentSkipListSet<>(content)); 77 78 ArrayBlockingQueue<Integer> arrayBlockingQueue = new ArrayBlockingQueue<>(content.size()); 79 for (Integer i : content) 80 arrayBlockingQueue.add(i); 81 collections.add(arrayBlockingQueue); 82 collections.add(new PriorityBlockingQueue<>(content)); 83 collections.add(new LinkedBlockingQueue<>(content)); 84 collections.add(new LinkedTransferQueue<>(content)); 85 collections.add(new ConcurrentLinkedQueue<>(content)); 86 collections.add(new LinkedBlockingDeque<>(content)); 87 collections.add(new ConcurrentLinkedDeque<>(content)); 88 89 Object[][] params = new Object[collections.size()][]; 90 for (int i = 0; i < collections.size(); i++) { 91 params[i] = new Object[]{collections.get(i).getClass().getName(), collections.get(i)}; 92 } 93 94 return params; 95 } 96 97 @Test(dataProvider = "collections") testCollectionSizeRemove(String name, Collection<Integer> c)98 public void testCollectionSizeRemove(String name, Collection<Integer> c) { 99 assertTrue(c.remove(1)); 100 Stream<Integer> s = c.stream(); 101 assertTrue(c.remove(2)); 102 Object[] result = s.toArray(); 103 assertEquals(result.length, c.size()); 104 } 105 106 @DataProvider(name = "maps") createMaps()107 public Object[][] createMaps() { 108 Map<Integer, Integer> content = new HashMap<>(); 109 for (int i = 0; i < 10; i++) { 110 content.put(i, i); 111 } 112 113 Map<String, Supplier<Map<Integer, Integer>>> maps = new HashMap<>(); 114 115 maps.put(HashMap.class.getName(), () -> new HashMap<>(content)); 116 maps.put(LinkedHashMap.class.getName(), () -> new LinkedHashMap<>(content)); 117 maps.put(IdentityHashMap.class.getName(), () -> new IdentityHashMap<>(content)); 118 maps.put(WeakHashMap.class.getName(), () -> new WeakHashMap<>(content)); 119 120 maps.put(TreeMap.class.getName(), () -> new TreeMap<>(content)); 121 maps.put(TreeMap.class.getName() + ".descendingMap()", () -> new TreeMap<>(content).descendingMap()); 122 123 // The following are not lazy 124 // maps.put(TreeMap.class.getName() + ".descendingMap().descendingMap()", () -> new TreeMap<>(content).descendingMap().descendingMap()); 125 // maps.put(TreeMap.class.getName() + ".headMap()", () -> new TreeMap<>(content).headMap(content.size() - 1)); 126 // maps.put(TreeMap.class.getName() + ".descendingMap().headMap()", () -> new TreeMap<>(content).descendingMap().tailMap(content.size() - 1, false)); 127 128 // Concurrent collections 129 130 maps.put(ConcurrentHashMap.class.getName(), () -> new ConcurrentHashMap<>(content)); 131 maps.put(ConcurrentSkipListMap.class.getName(), () -> new ConcurrentSkipListMap<>(content)); 132 133 Object[][] params = new Object[maps.size()][]; 134 int i = 0; 135 for (Map.Entry<String, Supplier<Map<Integer, Integer>>> e : maps.entrySet()) { 136 params[i++] = new Object[]{e.getKey(), e.getValue()}; 137 138 } 139 140 return params; 141 } 142 143 @Test(dataProvider = "maps", groups = { "serialization-hostile" }) testMapKeysSizeRemove(String name, Supplier<Map<Integer, Integer>> c)144 public void testMapKeysSizeRemove(String name, Supplier<Map<Integer, Integer>> c) { 145 testCollectionSizeRemove(name + " key set", c.get().keySet()); 146 } 147 148 @Test(dataProvider = "maps", groups = { "serialization-hostile" }) testMapValuesSizeRemove(String name, Supplier<Map<Integer, Integer>> c)149 public void testMapValuesSizeRemove(String name, Supplier<Map<Integer, Integer>> c) { 150 testCollectionSizeRemove(name + " value set", c.get().values()); 151 } 152 153 @Test(dataProvider = "maps") testMapEntriesSizeRemove(String name, Supplier<Map<Integer, Integer>> c)154 public void testMapEntriesSizeRemove(String name, Supplier<Map<Integer, Integer>> c) { 155 testEntrySetSizeRemove(name + " entry set", c.get().entrySet()); 156 } 157 testEntrySetSizeRemove(String name, Set<Map.Entry<Integer, Integer>> c)158 private void testEntrySetSizeRemove(String name, Set<Map.Entry<Integer, Integer>> c) { 159 Map.Entry<Integer, Integer> first = c.iterator().next(); 160 assertTrue(c.remove(first)); 161 Stream<Map.Entry<Integer, Integer>> s = c.stream(); 162 Map.Entry<Integer, Integer> second = c.iterator().next(); 163 assertTrue(c.remove(second)); 164 Object[] result = s.toArray(); 165 assertEquals(result.length, c.size()); 166 } 167 } 168