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 import org.testng.Assert; 40 import org.testng.annotations.Test; 41 42 public class ReplaceExisting { 43 /* Number of entries required to trigger a resize for cap=16, load=0.75*/ 44 private static int ENTRIES = 13; 45 46 @Test test()47 public void test() { 48 for (int i = 0; i <= ENTRIES; i++) { 49 HashMap<Integer,Integer> hm = prepHashMap(); 50 testItr(hm, i); 51 } 52 } 53 54 /* Prepare a HashMap that will resize on next put() */ prepHashMap()55 private static HashMap<Integer,Integer> prepHashMap() { 56 HashMap<Integer,Integer> hm = new HashMap<>(16, 0.75f); 57 // Add items to one more than the resize threshold 58 for (int i = 0; i < ENTRIES; i++) { 59 hm.put(i*10, i*10); 60 } 61 return hm; 62 } 63 64 /* Iterate hm for elemBeforePut elements, then call put() to replace value 65 * for existing key. With bug 8025173, this will also cause a resize, but 66 * not increase the modCount. 67 * Finish the iteration to check for a corrupt iterator. 68 */ testItr(HashMap<Integer,Integer> hm, int elemBeforePut)69 private static void testItr(HashMap<Integer,Integer> hm, int elemBeforePut) { 70 if (elemBeforePut > hm.size()) { 71 Assert.fail("elemBeforePut must be <= HashMap size. elemBeforePut:" + 72 elemBeforePut + " " + "HashMap.size:" + hm.size()); 73 } 74 // Create a copy of the keys 75 HashSet<Integer> keys = new HashSet<>(hm.size()); 76 keys.addAll(hm.keySet()); 77 78 HashSet<Integer> collected = new HashSet<>(hm.size()); 79 80 // Run itr for elemBeforePut items, collecting returned elems 81 Iterator<Integer> itr = hm.keySet().iterator(); 82 for (int i = 0; i < elemBeforePut; i++) { 83 Integer retVal = itr.next(); 84 Assert.assertTrue(collected.add(retVal), 85 "Corrupt iterator: key " + retVal + " already encountered"); 86 } 87 88 // Do put() to replace entry (and resize table when bug present) 89 Assert.assertNotNull(hm.put(0, 100), 90 "Error in test: expected key 0 to be in the HashMap"); 91 92 // Finish itr + collecting returned elems 93 while(itr.hasNext()) { 94 Integer retVal = itr.next(); 95 Assert.assertTrue(collected.add(retVal), 96 "Corrupt iterator: key " + retVal + " already encountered"); 97 } 98 99 // Compare returned elems to original copy of keys 100 Assert.assertEquals (collected, keys, 101 "Collected keys do not match original set of keys"); 102 } 103 } 104