Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Map/FunctionalCMEs.java
41149 views
1
/*
2
* Copyright (c) 2015, 2020, 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);
78
}
79
80
@Test(dataProvider = "Maps")
81
public void testComputeWithKeyMapped(Map<String,String> map, boolean expectCME) {
82
map.put(KEY, "firstValue");
83
checkCME(() -> {
84
map.compute(KEY, mkBiFunc(map));
85
}, expectCME);
86
}
87
88
@Test(dataProvider = "Maps")
89
public void testComputeIfPresent(Map<String,String> map, boolean expectCME) {
90
map.put(KEY, "firstValue");
91
checkCME(() -> {
92
map.computeIfPresent(KEY, mkBiFunc(map));
93
}, expectCME);
94
}
95
96
@Test(dataProvider = "Maps")
97
public void testMerge(Map<String,String> map, boolean expectCME) {
98
map.put(KEY, "firstValue");
99
checkCME(() -> {
100
map.merge(KEY, "nextValue", mkBiFunc(map));
101
}, expectCME);
102
}
103
104
@Test(dataProvider = "Maps")
105
public void testForEach(Map<String,String> map, boolean ignored) {
106
checkCME(() -> {
107
map.put(KEY, "firstValue");
108
putToForceRehash(map);
109
map.forEach((k,v) -> {
110
map.remove(KEY);
111
});
112
}, true);
113
}
114
115
@Test(dataProvider = "Maps")
116
public void testReplaceAll(Map<String,String> map, boolean ignored) {
117
checkCME(() -> {
118
map.put(KEY, "firstValue");
119
putToForceRehash(map);
120
map.replaceAll((k,v) -> {
121
map.remove(KEY);
122
return "computedValue";
123
});
124
},true);
125
}
126
127
private static void checkCME(Runnable code, boolean expectCME) {
128
try {
129
code.run();
130
} catch (ConcurrentModificationException cme) {
131
if (expectCME) { return; } else { throw cme; }
132
}
133
if (expectCME) {
134
throw new RuntimeException("Expected CME, but wasn't thrown");
135
}
136
}
137
138
private static BiFunction<String,String,String> mkBiFunc(Map<String,String> map) {
139
return (k,v) -> {
140
putToForceRehash(map);
141
return "computedValue";
142
};
143
}
144
145
private static void putToForceRehash(Map<String,String> map) {
146
for (int i = 0; i < 64; ++i) {
147
map.put(i + "", "value");
148
}
149
}
150
}
151
152