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 * @bug 8025173 27 * @summary Verify that replacing the value for an existing key does not 28 * corrupt active iterators, in particular due to a resize() occurring and 29 * not updating modCount. 30 * @run main ReplaceExisting 31 */ 32 package test.java.util.HashMap; 33 34 import java.util.ConcurrentModificationException; 35 import java.util.HashMap; 36 import java.util.HashSet; 37 import java.util.Iterator; 38 39 public class ReplaceExisting { 40 /* Number of entries required to trigger a resize for cap=16, load=0.75*/ 41 private static int ENTRIES = 13; 42 main(String[] args)43 public static void main(String[] args) { 44 for (int i = 0; i <= ENTRIES; i++) { 45 HashMap<Integer,Integer> hm = prepHashMap(); 46 testItr(hm, i); 47 } 48 } 49 50 /* Prepare a HashMap that will resize on next put() */ prepHashMap()51 private static HashMap<Integer,Integer> prepHashMap() { 52 HashMap<Integer,Integer> hm = new HashMap<>(16, 0.75f); 53 // Add items to one more than the resize threshold 54 for (int i = 0; i < ENTRIES; i++) { 55 hm.put(i*10, i*10); 56 } 57 return hm; 58 } 59 60 /* Iterate hm for elemBeforePut elements, then call put() to replace value 61 * for existing key. With bug 8025173, this will also cause a resize, but 62 * not increase the modCount. 63 * Finish the iteration to check for a corrupt iterator. 64 */ testItr(HashMap<Integer,Integer> hm, int elemBeforePut)65 private static void testItr(HashMap<Integer,Integer> hm, int elemBeforePut) { 66 if (elemBeforePut > hm.size()) { 67 throw new IllegalArgumentException("Error in test: elemBeforePut must be <= HashMap size"); 68 } 69 // Create a copy of the keys 70 HashSet<Integer> keys = new HashSet<>(hm.size()); 71 keys.addAll(hm.keySet()); 72 73 HashSet<Integer> collected = new HashSet<>(hm.size()); 74 75 // Run itr for elemBeforePut items, collecting returned elems 76 Iterator<Integer> itr = hm.keySet().iterator(); 77 for (int i = 0; i < elemBeforePut; i++) { 78 Integer retVal = itr.next(); 79 if (!collected.add(retVal)) { 80 throw new RuntimeException("Corrupt iterator: key " + retVal + " already encountered"); 81 } 82 } 83 84 // Do put() to replace entry (and resize table when bug present) 85 if (null == hm.put(0, 100)) { 86 throw new RuntimeException("Error in test: expected key 0 to be in the HashMap"); 87 } 88 89 // Finish itr + collecting returned elems 90 while(itr.hasNext()) { 91 Integer retVal = itr.next(); 92 if (!collected.add(retVal)) { 93 throw new RuntimeException("Corrupt iterator: key " + retVal + " already encountered"); 94 } 95 } 96 97 // Compare returned elems to original copy of keys 98 if (!keys.equals(collected)) { 99 throw new RuntimeException("Collected keys do not match original set of keys"); 100 } 101 } 102 } 103