FunctionalCMEs.java revision 15491:6f390eafc676
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
26import java.util.Arrays;
27import java.util.ConcurrentModificationException;
28import java.util.HashMap;
29import java.util.Hashtable;
30import java.util.Iterator;
31import java.util.LinkedHashMap;
32import java.util.Map;
33import java.util.function.BiFunction;
34
35import org.testng.annotations.Test;
36import 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 */
46public 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);
76    }
77
78    @Test(dataProvider = "Maps")
79    public void testComputeWithKeyMapped(Map<String,String> map, boolean expectCME) {
80        map.put(KEY, "firstValue");
81        checkCME(() -> {
82            map.compute(KEY, mkBiFunc(map));
83        }, expectCME);
84    }
85
86    @Test(dataProvider = "Maps")
87    public void testComputeIfPresent(Map<String,String> map, boolean expectCME) {
88        map.put(KEY, "firstValue");
89        checkCME(() -> {
90           map.computeIfPresent(KEY, mkBiFunc(map));
91        }, expectCME);
92    }
93
94    @Test(dataProvider = "Maps")
95    public void testMerge(Map<String,String> map, boolean expectCME) {
96        map.put(KEY, "firstValue");
97        checkCME(() -> {
98            map.merge(KEY, "nextValue", mkBiFunc(map));
99        }, expectCME);
100    }
101
102    @Test(dataProvider = "Maps")
103    public void testForEach(Map<String,String> map, boolean ignored) {
104        checkCME(() -> {
105            map.put(KEY, "firstValue");
106            putToForceRehash(map);
107            map.forEach((k,v) -> {
108                map.remove(KEY);
109            });
110        }, true);
111    }
112
113    @Test(dataProvider = "Maps")
114    public void testReplaceAll(Map<String,String> map, boolean ignored) {
115        checkCME(() -> {
116            map.put(KEY, "firstValue");
117            putToForceRehash(map);
118            map.replaceAll((k,v) -> {
119                map.remove(KEY);
120                return "computedValue";
121            });
122        },true);
123    }
124
125    private static void checkCME(Runnable code, boolean expectCME) {
126        try {
127            code.run();
128        } catch (ConcurrentModificationException cme) {
129            if (expectCME) { return; } else { throw cme; }
130        }
131        if (expectCME) {
132            throw new RuntimeException("Expected CME, but wasn't thrown");
133        }
134    }
135
136    private static BiFunction<String,String,String> mkBiFunc(Map<String,String> map) {
137        return (k,v) -> {
138            putToForceRehash(map);
139            return "computedValue";
140        };
141    }
142
143    private static void putToForceRehash(Map<String,String> map) {
144        for (int i = 0; i < 64; ++i) {
145            map.put(i + "", "value");
146        }
147    }
148}
149