FunctionalCMEs.java revision 12745:f068a4ffddd2
1/* 2 * Copyright (c) 2015 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. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25import java.util.Arrays; 26import java.util.ConcurrentModificationException; 27import java.util.HashMap; 28import java.util.Hashtable; 29import java.util.Iterator; 30import java.util.LinkedHashMap; 31import java.util.Map; 32import java.util.function.BiFunction; 33 34import org.testng.annotations.Test; 35import org.testng.annotations.DataProvider; 36 37/** 38 * @test 39 * @bug 8071667 40 * @summary Ensure that ConcurrentModificationExceptions are thrown as specified from Map methods that accept Functions 41 * @author bchristi 42 * @build Defaults 43 * @run testng FunctionalCMEs 44 */ 45public class FunctionalCMEs { 46 static final String KEY = "key"; 47 48 @DataProvider(name = "Maps", parallel = true) 49 private static Iterator<Object[]> makeMaps() { 50 return Arrays.asList( 51 // Test maps that CME 52 new Object[]{new HashMap<>(), true}, 53 new Object[]{new Hashtable<>(), true}, 54 new Object[]{new LinkedHashMap<>(), true}, 55 // Test default Map methods - no CME 56 new Object[]{new Defaults.ExtendsAbstractMap<>(), false} 57 ).iterator(); 58 } 59 60 @Test(dataProvider = "Maps") 61 public void testComputeIfAbsent(Map<String,String> map, boolean expectCME) { 62 checkCME(() -> { 63 map.computeIfAbsent(KEY, k -> { 64 putToForceRehash(map); 65 return "computedValue"; 66 }); 67 }, expectCME); 68 } 69 70 @Test(dataProvider = "Maps") 71 public void testCompute(Map<String,String> map, boolean expectCME) { 72 checkCME(() -> { 73 map.compute(KEY, mkBiFunc(map)); 74 }, expectCME); 75 } 76 77 @Test(dataProvider = "Maps") 78 public void testComputeWithKeyMapped(Map<String,String> map, boolean expectCME) { 79 map.put(KEY, "firstValue"); 80 checkCME(() -> { 81 map.compute(KEY, mkBiFunc(map)); 82 }, expectCME); 83 } 84 85 @Test(dataProvider = "Maps") 86 public void testComputeIfPresent(Map<String,String> map, boolean expectCME) { 87 map.put(KEY, "firstValue"); 88 checkCME(() -> { 89 map.computeIfPresent(KEY, mkBiFunc(map)); 90 }, expectCME); 91 } 92 93 @Test(dataProvider = "Maps") 94 public void testMerge(Map<String,String> map, boolean expectCME) { 95 map.put(KEY, "firstValue"); 96 checkCME(() -> { 97 map.merge(KEY, "nextValue", mkBiFunc(map)); 98 }, expectCME); 99 } 100 101 @Test(dataProvider = "Maps") 102 public void testForEach(Map<String,String> map, boolean ignored) { 103 checkCME(() -> { 104 map.put(KEY, "firstValue"); 105 putToForceRehash(map); 106 map.forEach((k,v) -> { 107 map.remove(KEY); 108 }); 109 }, true); 110 } 111 112 @Test(dataProvider = "Maps") 113 public void testReplaceAll(Map<String,String> map, boolean ignored) { 114 checkCME(() -> { 115 map.put(KEY, "firstValue"); 116 putToForceRehash(map); 117 map.replaceAll((k,v) -> { 118 map.remove(KEY); 119 return "computedValue"; 120 }); 121 },true); 122 } 123 124 private static void checkCME(Runnable code, boolean expectCME) { 125 try { 126 code.run(); 127 } catch (ConcurrentModificationException cme) { 128 if (expectCME) { return; } else { throw cme; } 129 } 130 if (expectCME) { 131 throw new RuntimeException("Expected CME, but wasn't thrown"); 132 } 133 } 134 135 private static BiFunction<String,String,String> mkBiFunc(Map<String,String> map) { 136 return (k,v) -> { 137 putToForceRehash(map); 138 return "computedValue"; 139 }; 140 } 141 142 private static void putToForceRehash(Map<String,String> map) { 143 for (int i = 0; i < 64; ++i) { 144 map.put(i + "", "value"); 145 } 146 } 147} 148