Path: blob/master/test/jdk/java/util/Map/FunctionalCMEs.java
41149 views
/*1* Copyright (c) 2015, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425import 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.TreeMap;33import java.util.function.BiFunction;3435import org.testng.annotations.Test;36import org.testng.annotations.DataProvider;3738/**39* @test40* @bug 807166741* @summary Ensure that ConcurrentModificationExceptions are thrown as specified from Map methods that accept Functions42* @author bchristi43* @build Defaults44* @run testng FunctionalCMEs45*/46public class FunctionalCMEs {47static final String KEY = "key";4849@DataProvider(name = "Maps", parallel = true)50private static Iterator<Object[]> makeMaps() {51return Arrays.asList(52// Test maps that CME53new Object[]{new HashMap<>(), true},54new Object[]{new Hashtable<>(), true},55new Object[]{new LinkedHashMap<>(), true},56new Object[]{new TreeMap<>(), true},57// Test default Map methods - no CME58new Object[]{new Defaults.ExtendsAbstractMap<>(), false}59).iterator();60}6162@Test(dataProvider = "Maps")63public void testComputeIfAbsent(Map<String,String> map, boolean expectCME) {64checkCME(() -> {65map.computeIfAbsent(KEY, k -> {66putToForceRehash(map);67return "computedValue";68});69}, expectCME);70}7172@Test(dataProvider = "Maps")73public void testCompute(Map<String,String> map, boolean expectCME) {74checkCME(() -> {75map.compute(KEY, mkBiFunc(map));76}, expectCME);77}7879@Test(dataProvider = "Maps")80public void testComputeWithKeyMapped(Map<String,String> map, boolean expectCME) {81map.put(KEY, "firstValue");82checkCME(() -> {83map.compute(KEY, mkBiFunc(map));84}, expectCME);85}8687@Test(dataProvider = "Maps")88public void testComputeIfPresent(Map<String,String> map, boolean expectCME) {89map.put(KEY, "firstValue");90checkCME(() -> {91map.computeIfPresent(KEY, mkBiFunc(map));92}, expectCME);93}9495@Test(dataProvider = "Maps")96public void testMerge(Map<String,String> map, boolean expectCME) {97map.put(KEY, "firstValue");98checkCME(() -> {99map.merge(KEY, "nextValue", mkBiFunc(map));100}, expectCME);101}102103@Test(dataProvider = "Maps")104public void testForEach(Map<String,String> map, boolean ignored) {105checkCME(() -> {106map.put(KEY, "firstValue");107putToForceRehash(map);108map.forEach((k,v) -> {109map.remove(KEY);110});111}, true);112}113114@Test(dataProvider = "Maps")115public void testReplaceAll(Map<String,String> map, boolean ignored) {116checkCME(() -> {117map.put(KEY, "firstValue");118putToForceRehash(map);119map.replaceAll((k,v) -> {120map.remove(KEY);121return "computedValue";122});123},true);124}125126private static void checkCME(Runnable code, boolean expectCME) {127try {128code.run();129} catch (ConcurrentModificationException cme) {130if (expectCME) { return; } else { throw cme; }131}132if (expectCME) {133throw new RuntimeException("Expected CME, but wasn't thrown");134}135}136137private static BiFunction<String,String,String> mkBiFunc(Map<String,String> map) {138return (k,v) -> {139putToForceRehash(map);140return "computedValue";141};142}143144private static void putToForceRehash(Map<String,String> map) {145for (int i = 0; i < 64; ++i) {146map.put(i + "", "value");147}148}149}150151152