Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Collections/SyncSubMutexes.java
41149 views
1
/*
2
* Copyright (c) 2014, 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.
8
*
9
* This code is distributed in the hope that it will be useful, but WITHOUT
10
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12
* version 2 for more details (a copy is included in the LICENSE file that
13
* accompanied this code).
14
*
15
* You should have received a copy of the GNU General Public License version
16
* 2 along with this work; if not, write to the Free Software Foundation,
17
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18
*
19
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20
* or visit www.oracle.com if you need additional information or have any
21
* questions.
22
*/
23
24
/**
25
* @test
26
* @bug 8048209
27
* @summary Check that Collections.synchronizedNavigableSet().tailSet() is using
28
* the same lock object as it's source.
29
* @modules java.base/java.util:open
30
* @run testng SyncSubMutexes
31
*/
32
import java.lang.reflect.Field;
33
import java.util.*;
34
import java.util.Set;
35
import java.util.Arrays;
36
37
import org.testng.annotations.Test;
38
import org.testng.annotations.DataProvider;
39
import static org.testng.Assert.assertSame;
40
41
public class SyncSubMutexes {
42
43
@Test(dataProvider = "Collections")
44
public void testCollections(Collection<String> instance) {
45
// nothing to test, no subset methods
46
}
47
48
@Test(dataProvider = "Lists")
49
public void testLists(List<String> instance) {
50
assertSame(getSyncCollectionMutex(instance.subList(0, 1)), getSyncCollectionMutex(instance));
51
}
52
53
@Test(dataProvider = "Sets")
54
public void testSets(Set<String> instance) {
55
// nothing to test, no subset methods
56
57
}
58
59
@Test(dataProvider = "SortedSets")
60
public void testSortedSets(SortedSet<String> instance) {
61
assertSame(getSyncCollectionMutex(instance.headSet("Echo")), getSyncCollectionMutex(instance));
62
assertSame(getSyncCollectionMutex(instance.tailSet("Charlie")), getSyncCollectionMutex(instance));
63
assertSame(getSyncCollectionMutex(instance.subSet("Charlie", "Echo")), getSyncCollectionMutex(instance));
64
65
}
66
67
@Test(dataProvider = "NavigableSets")
68
public void testNavigableSets(NavigableSet<String> instance) {
69
assertSame(getSyncCollectionMutex(instance.descendingSet()), getSyncCollectionMutex(instance));
70
assertSame(getSyncCollectionMutex(instance.headSet("Echo")), getSyncCollectionMutex(instance));
71
assertSame(getSyncCollectionMutex(instance.headSet("Echo", true)), getSyncCollectionMutex(instance));
72
assertSame(getSyncCollectionMutex(instance.tailSet("Charlie")), getSyncCollectionMutex(instance));
73
assertSame(getSyncCollectionMutex(instance.tailSet("Charlie", true)), getSyncCollectionMutex(instance));
74
assertSame(getSyncCollectionMutex(instance.subSet("Charlie", "Echo")), getSyncCollectionMutex(instance));
75
assertSame(getSyncCollectionMutex(instance.subSet("Charlie", true, "Echo", true)), getSyncCollectionMutex(instance));
76
}
77
78
@Test(dataProvider = "Maps")
79
public void testMaps(Map<String, String> instance) {
80
assertSame(getSyncCollectionMutex(instance.entrySet()), getSyncMapMutex(instance));
81
assertSame(getSyncCollectionMutex(instance.keySet()), getSyncMapMutex(instance));
82
assertSame(getSyncCollectionMutex(instance.values()), getSyncMapMutex(instance));
83
}
84
85
@Test(dataProvider = "SortedMaps")
86
public void testSortedMaps(SortedMap<String, String> instance) {
87
assertSame(getSyncCollectionMutex(instance.entrySet()), getSyncMapMutex(instance));
88
assertSame(getSyncCollectionMutex(instance.keySet()), getSyncMapMutex(instance));
89
assertSame(getSyncCollectionMutex(instance.values()), getSyncMapMutex(instance));
90
assertSame(getSyncMapMutex(instance.headMap("Echo")), getSyncMapMutex(instance));
91
assertSame(getSyncMapMutex(instance.tailMap("Charlie")), getSyncMapMutex(instance));
92
assertSame(getSyncMapMutex(instance.subMap("Charlie", "Echo")), getSyncMapMutex(instance));
93
}
94
95
@Test(dataProvider = "NavigableMaps")
96
public void testNavigableMaps(NavigableMap<String, String> instance) {
97
assertSame(getSyncMapMutex(instance.descendingMap()), getSyncMapMutex(instance));
98
assertSame(getSyncCollectionMutex(instance.entrySet()), getSyncMapMutex(instance));
99
assertSame(getSyncCollectionMutex(instance.keySet()), getSyncMapMutex(instance));
100
assertSame(getSyncCollectionMutex(instance.descendingKeySet()), getSyncMapMutex(instance));
101
assertSame(getSyncCollectionMutex(instance.values()), getSyncMapMutex(instance));
102
assertSame(getSyncMapMutex(instance.headMap("Echo")), getSyncMapMutex(instance));
103
assertSame(getSyncMapMutex(instance.headMap("Echo", true)), getSyncMapMutex(instance));
104
assertSame(getSyncMapMutex(instance.tailMap("Charlie")), getSyncMapMutex(instance));
105
assertSame(getSyncMapMutex(instance.tailMap("Charlie", true)), getSyncMapMutex(instance));
106
assertSame(getSyncMapMutex(instance.subMap("Charlie", true, "Echo", true)), getSyncMapMutex(instance));
107
assertSame(getSyncMapMutex(instance.subMap("Charlie", true, "Echo", true)), getSyncMapMutex(instance));
108
}
109
110
@DataProvider(name = "Collections", parallel = true)
111
public static Iterator<Object[]> collectionProvider() {
112
return makeCollections().iterator();
113
}
114
115
@DataProvider(name = "Lists", parallel = true)
116
public static Iterator<Object[]> listProvider() {
117
return makeLists().iterator();
118
}
119
120
@DataProvider(name = "Sets", parallel = true)
121
public static Iterator<Object[]> setProvider() {
122
return makeSets().iterator();
123
}
124
125
@DataProvider(name = "SortedSets", parallel = true)
126
public static Iterator<Object[]> sortedsetProvider() {
127
return makeSortedSets().iterator();
128
}
129
130
@DataProvider(name = "NavigableSets", parallel = true)
131
public static Iterator<Object[]> navigablesetProvider() {
132
return makeNavigableSets().iterator();
133
}
134
135
@DataProvider(name = "Maps", parallel = true)
136
public static Iterator<Object[]> mapProvider() {
137
return makeMaps().iterator();
138
}
139
140
@DataProvider(name = "SortedMaps", parallel = true)
141
public static Iterator<Object[]> sortedmapProvider() {
142
return makeSortedMaps().iterator();
143
}
144
145
@DataProvider(name = "NavigableMaps", parallel = true)
146
public static Iterator<Object[]> navigablemapProvider() {
147
return makeNavigableMaps().iterator();
148
}
149
150
private static final Collection<String> BASE_COLLECTION = Collections.unmodifiableCollection(
151
Arrays.asList("Alpha", "Bravo", "Charlie", "Delta", "Echo", "Foxtrot", "Golf")
152
);
153
private static final Map<String, String> BASE_MAP;
154
155
static {
156
Map<String, String> map = new HashMap<>();
157
for(String each : BASE_COLLECTION) {
158
map.put(each, "*" + each + "*");
159
}
160
BASE_MAP = Collections.unmodifiableMap(map);
161
}
162
163
public static Collection<Object[]> makeCollections() {
164
Collection<Object[]> instances = new ArrayList<>();
165
instances.add(new Object[] {Collections.synchronizedCollection(new ArrayList<>(BASE_COLLECTION))});
166
instances.addAll(makeLists());
167
168
return instances;
169
}
170
171
public static Collection<Object[]> makeLists() {
172
Collection<Object[]> instances = new ArrayList<>();
173
instances.add(new Object[] {Collections.synchronizedList(new ArrayList<>(BASE_COLLECTION))});
174
instances.add(new Object[] {Collections.synchronizedList(new ArrayList<>(BASE_COLLECTION)).subList(1, 2)});
175
176
return instances;
177
}
178
179
public static Collection<Object[]> makeSets() {
180
Collection<Object[]> instances = new ArrayList<>();
181
182
instances.add(new Object[] {Collections.synchronizedSet(new TreeSet<>(BASE_COLLECTION))});
183
instances.addAll(makeSortedSets());
184
return instances;
185
}
186
187
public static Collection<Object[]> makeSortedSets() {
188
Collection<Object[]> instances = new ArrayList<>();
189
instances.add(new Object[] {Collections.synchronizedSortedSet(new TreeSet<>(BASE_COLLECTION))});
190
instances.add(new Object[] {Collections.synchronizedSortedSet(new TreeSet<>(BASE_COLLECTION)).headSet("Foxtrot")});
191
instances.add(new Object[] {Collections.synchronizedSortedSet(new TreeSet<>(BASE_COLLECTION)).tailSet("Bravo")});
192
instances.add(new Object[] {Collections.synchronizedSortedSet(new TreeSet<>(BASE_COLLECTION)).subSet("Bravo", "Foxtrot")});
193
instances.addAll(makeNavigableSets());
194
195
return instances;
196
}
197
198
public static Collection<Object[]> makeNavigableSets() {
199
Collection<Object[]> instances = new ArrayList<>();
200
201
instances.add(new Object[] {Collections.synchronizedNavigableSet(new TreeSet<>(BASE_COLLECTION))});
202
instances.add(new Object[] {Collections.synchronizedNavigableSet(new TreeSet<>(BASE_COLLECTION)).descendingSet().descendingSet()});
203
instances.add(new Object[] {Collections.synchronizedNavigableSet(new TreeSet<>(BASE_COLLECTION)).headSet("Foxtrot")});
204
instances.add(new Object[] {Collections.synchronizedNavigableSet(new TreeSet<>(BASE_COLLECTION)).headSet("Foxtrot", true)});
205
instances.add(new Object[] {Collections.synchronizedNavigableSet(new TreeSet<>(BASE_COLLECTION)).tailSet("Bravo")});
206
instances.add(new Object[] {Collections.synchronizedNavigableSet(new TreeSet<>(BASE_COLLECTION)).tailSet("Bravo", true)});
207
instances.add(new Object[] {Collections.synchronizedNavigableSet(new TreeSet<>(BASE_COLLECTION)).subSet("Bravo", "Foxtrot")});
208
instances.add(new Object[] {Collections.synchronizedNavigableSet(new TreeSet<>(BASE_COLLECTION)).subSet("Bravo", true, "Foxtrot", true)});
209
210
return instances;
211
}
212
213
public static Collection<Object[]> makeMaps() {
214
Collection<Object[]> instances = new ArrayList<>();
215
216
instances.add(new Object[] {Collections.synchronizedMap(new HashMap<>(BASE_MAP))});
217
instances.addAll(makeSortedMaps());
218
219
return instances;
220
}
221
222
public static Collection<Object[]> makeSortedMaps() {
223
Collection<Object[]> instances = new ArrayList<>();
224
225
instances.add(new Object[] {Collections.synchronizedSortedMap(new TreeMap<>(BASE_MAP))});
226
instances.add(new Object[] {Collections.synchronizedSortedMap(new TreeMap<>(BASE_MAP)).headMap("Foxtrot")});
227
instances.add(new Object[] {Collections.synchronizedSortedMap(new TreeMap<>(BASE_MAP)).tailMap("Bravo")});
228
instances.add(new Object[] {Collections.synchronizedSortedMap(new TreeMap<>(BASE_MAP)).subMap("Bravo", "Foxtrot")});
229
instances.addAll(makeNavigableMaps());
230
231
return instances;
232
}
233
234
public static Collection<Object[]> makeNavigableMaps() {
235
Collection<Object[]> instances = new ArrayList<>();
236
237
instances.add(new Object[] {Collections.synchronizedNavigableMap(new TreeMap<>(BASE_MAP))});
238
instances.add(new Object[] {Collections.synchronizedNavigableMap(new TreeMap<>(BASE_MAP).descendingMap().descendingMap())});
239
instances.add(new Object[] {Collections.synchronizedNavigableMap(new TreeMap<>(BASE_MAP)).headMap("Foxtrot")});
240
instances.add(new Object[] {Collections.synchronizedNavigableMap(new TreeMap<>(BASE_MAP)).headMap("Foxtrot", true)});
241
instances.add(new Object[] {Collections.synchronizedNavigableMap(new TreeMap<>(BASE_MAP)).tailMap("Bravo")});
242
instances.add(new Object[] {Collections.synchronizedNavigableMap(new TreeMap<>(BASE_MAP)).tailMap("Bravo", true)});
243
instances.add(new Object[] {Collections.synchronizedNavigableMap(new TreeMap<>(BASE_MAP)).subMap("Bravo", "Foxtrot")});
244
instances.add(new Object[] {Collections.synchronizedNavigableMap(new TreeMap<>(BASE_MAP)).subMap("Bravo", true, "Foxtrot", true)});
245
246
return instances;
247
}
248
249
private static Object getSyncCollectionMutex(Collection<?> from) {
250
try {
251
Class<?> synchronizedCollectionClazz = Class.forName("java.util.Collections$SynchronizedCollection");
252
Field f = synchronizedCollectionClazz.getDeclaredField("mutex");
253
f.setAccessible(true);
254
return f.get(from);
255
} catch ( ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {
256
throw new RuntimeException("Unable to get mutex field.", e);
257
}
258
}
259
260
private static Object getSyncMapMutex(Map<?,?> from) {
261
try {
262
Class<?> synchronizedMapClazz = Class.forName("java.util.Collections$SynchronizedMap");
263
Field f = synchronizedMapClazz.getDeclaredField("mutex");
264
f.setAccessible(true);
265
return f.get(from);
266
} catch ( ClassNotFoundException | NoSuchFieldException | IllegalAccessException e) {
267
throw new RuntimeException("Unable to get mutex field.", e);
268
}
269
}
270
271
}
272
273