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 */
25
26 import java.util.Arrays;
27 import java.util.ConcurrentModificationException;
28 import java.util.HashMap;
29 import java.util.Hashtable;
30 import java.util.Iterator;
31 import java.util.LinkedHashMap;
32 import java.util.Map;
33 import java.util.function.BiFunction;
34
35 import org.testng.annotations.Test;
36 import org.testng.annotations.DataProvider;
37
38 /**
39 * @test
40 * @bug 8071667
41 * @summary Ensure that ConcurrentModificationExceptions are thrown as specified from Map methods that accept Functions
42 * @author bchristi
43 * @build Defaults
44 * @run testng FunctionalCMEs
45 */
46 public class FunctionalCMEs {
47 static final String KEY = "key";
48
49 @DataProvider(name = "Maps", parallel = true)
50 private static Iterator<Object[]> makeMaps() {
51 return Arrays.asList(
52 // Test maps that CME
53 new Object[]{new HashMap<>(), true},
54 new Object[]{new Hashtable<>(), true},
55 new Object[]{new LinkedHashMap<>(), true},
56 // Test default Map methods - no CME
57 new Object[]{new Defaults.ExtendsAbstractMap<>(), false}
58 ).iterator();
59 }
60
61 @Test(dataProvider = "Maps")
62 public void testComputeIfAbsent(Map<String,String> map, boolean expectCME) {
63 checkCME(() -> {
64 map.computeIfAbsent(KEY, k -> {
65 putToForceRehash(map);
66 return "computedValue";
67 });
68 }, expectCME);
69 }
70
71 @Test(dataProvider = "Maps")
72 public void testCompute(Map<String,String> map, boolean expectCME) {
73 checkCME(() -> {
74 map.compute(KEY, mkBiFunc(map));
75 }, expectCME);
|
1 /*
2 * Copyright (c) 2015, 2019, 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 */
25
26 import java.util.Arrays;
27 import java.util.ConcurrentModificationException;
28 import java.util.HashMap;
29 import java.util.Hashtable;
30 import java.util.Iterator;
31 import java.util.LinkedHashMap;
32 import java.util.Map;
33 import java.util.TreeMap;
34 import java.util.function.BiFunction;
35
36 import org.testng.annotations.Test;
37 import org.testng.annotations.DataProvider;
38
39 /**
40 * @test
41 * @bug 8071667
42 * @summary Ensure that ConcurrentModificationExceptions are thrown as specified from Map methods that accept Functions
43 * @author bchristi
44 * @build Defaults
45 * @run testng FunctionalCMEs
46 */
47 public class FunctionalCMEs {
48 static final String KEY = "key";
49
50 @DataProvider(name = "Maps", parallel = true)
51 private static Iterator<Object[]> makeMaps() {
52 return Arrays.asList(
53 // Test maps that CME
54 new Object[]{new HashMap<>(), true},
55 new Object[]{new Hashtable<>(), true},
56 new Object[]{new LinkedHashMap<>(), true},
57 new Object[]{new TreeMap<>(), true},
58 // Test default Map methods - no CME
59 new Object[]{new Defaults.ExtendsAbstractMap<>(), false}
60 ).iterator();
61 }
62
63 @Test(dataProvider = "Maps")
64 public void testComputeIfAbsent(Map<String,String> map, boolean expectCME) {
65 checkCME(() -> {
66 map.computeIfAbsent(KEY, k -> {
67 putToForceRehash(map);
68 return "computedValue";
69 });
70 }, expectCME);
71 }
72
73 @Test(dataProvider = "Maps")
74 public void testCompute(Map<String,String> map, boolean expectCME) {
75 checkCME(() -> {
76 map.compute(KEY, mkBiFunc(map));
77 }, expectCME);
|