Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Map/MapFactories.java
41149 views
1
/*
2
* Copyright (c) 2015, 2021, 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
import java.io.ByteArrayInputStream;
25
import java.io.ByteArrayOutputStream;
26
import java.io.IOException;
27
import java.io.ObjectInputStream;
28
import java.io.ObjectOutputStream;
29
import java.util.AbstractMap;
30
import java.util.ArrayList;
31
import java.util.Arrays;
32
import java.util.Collections;
33
import java.util.Iterator;
34
import java.util.HashMap;
35
import java.util.List;
36
import java.util.Map;
37
import java.util.stream.IntStream;
38
39
import org.testng.annotations.DataProvider;
40
import org.testng.annotations.Test;
41
42
import static org.testng.Assert.assertEquals;
43
import static org.testng.Assert.assertFalse;
44
import static org.testng.Assert.assertNotEquals;
45
import static org.testng.Assert.assertNotSame;
46
import static org.testng.Assert.assertSame;
47
import static org.testng.Assert.assertThrows;
48
import static org.testng.Assert.assertTrue;
49
50
/*
51
* @test
52
* @bug 8048330 8221924
53
* @summary Test convenience static factory methods on Map.
54
* @run testng MapFactories
55
*/
56
57
public class MapFactories {
58
59
static final int MAX_ENTRIES = 20; // should be larger than the largest fixed-arg overload
60
static String valueFor(int i) {
61
// the String literal below should be of length MAX_ENTRIES
62
return "abcdefghijklmnopqrst".substring(i, i+1);
63
}
64
65
// for "expected" values
66
Map<Integer,String> genMap(int n) {
67
Map<Integer,String> result = new HashMap<>();
68
for (int i = 0; i < n; i++) {
69
result.put(i, valueFor(i));
70
}
71
return result;
72
}
73
74
// for varargs Map.Entry methods
75
76
@SuppressWarnings("unchecked")
77
Map.Entry<Integer,String>[] genEmptyEntryArray1() {
78
return (Map.Entry<Integer,String>[])new Map.Entry<?,?>[1];
79
}
80
81
@SuppressWarnings("unchecked")
82
Map.Entry<Integer,String>[] genEntries(int n) {
83
return IntStream.range(0, n)
84
.mapToObj(i -> Map.entry(i, valueFor(i)))
85
.toArray(Map.Entry[]::new);
86
}
87
88
// returns array of [actual, expected]
89
static Object[] a(Map<Integer,String> act, Map<Integer,String> exp) {
90
return new Object[] { act, exp };
91
}
92
93
@DataProvider(name="empty")
94
public Iterator<Object[]> empty() {
95
return Collections.singletonList(
96
a(Map.of(), genMap(0))
97
).iterator();
98
}
99
100
@DataProvider(name="nonempty")
101
@SuppressWarnings("unchecked")
102
public Iterator<Object[]> nonempty() {
103
return Arrays.asList(
104
a(Map.of(0, "a"), genMap(1)),
105
a(Map.of(0, "a", 1, "b"), genMap(2)),
106
a(Map.of(0, "a", 1, "b", 2, "c"), genMap(3)),
107
a(Map.of(0, "a", 1, "b", 2, "c", 3, "d"), genMap(4)),
108
a(Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e"), genMap(5)),
109
a(Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e", 5, "f"), genMap(6)),
110
a(Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e", 5, "f", 6, "g"), genMap(7)),
111
a(Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e", 5, "f", 6, "g", 7, "h"), genMap(8)),
112
a(Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e", 5, "f", 6, "g", 7, "h", 8, "i"), genMap(9)),
113
a(Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e", 5, "f", 6, "g", 7, "h", 8, "i", 9, "j"), genMap(10)),
114
a(Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e", 5, "f", 6, "g", 7, "h", 8, "i", 9, "j"),
115
Map.of(4, "e", 5, "f", 6, "g", 7, "h", 8, "i", 9, "j", 0, "a", 1, "b", 2, "c", 3, "d")),
116
a(Map.ofEntries(genEntries(MAX_ENTRIES)), genMap(MAX_ENTRIES))
117
).iterator();
118
}
119
120
@DataProvider(name="all")
121
public Iterator<Object[]> all() {
122
List<Object[]> all = new ArrayList<>();
123
empty().forEachRemaining(all::add);
124
nonempty().forEachRemaining(all::add);
125
return all.iterator();
126
}
127
128
@Test(dataProvider="all", expectedExceptions=UnsupportedOperationException.class)
129
public void cannotPutNew(Map<Integer,String> act, Map<Integer,String> exp) {
130
act.put(-1, "xyzzy");
131
}
132
133
@Test(dataProvider="nonempty", expectedExceptions=UnsupportedOperationException.class)
134
public void cannotPutOld(Map<Integer,String> act, Map<Integer,String> exp) {
135
act.put(0, "a");
136
}
137
138
@Test(dataProvider="nonempty", expectedExceptions=UnsupportedOperationException.class)
139
public void cannotRemove(Map<Integer,String> act, Map<Integer,String> exp) {
140
act.remove(act.keySet().iterator().next());
141
}
142
143
@Test(dataProvider="all")
144
public void contentsMatch(Map<Integer,String> act, Map<Integer,String> exp) {
145
assertEquals(act, exp);
146
}
147
148
@Test(dataProvider="all")
149
public void containsAllKeys(Map<Integer,String> act, Map<Integer,String> exp) {
150
assertTrue(act.keySet().containsAll(exp.keySet()));
151
assertTrue(exp.keySet().containsAll(act.keySet()));
152
}
153
154
@Test(dataProvider="all")
155
public void containsAllValues(Map<Integer,String> act, Map<Integer,String> exp) {
156
assertTrue(act.values().containsAll(exp.values()));
157
assertTrue(exp.values().containsAll(act.values()));
158
}
159
160
@Test(expectedExceptions=IllegalArgumentException.class)
161
public void dupKeysDisallowed2() {
162
Map<Integer, String> map = Map.of(0, "a", 0, "b");
163
}
164
165
@Test(expectedExceptions=IllegalArgumentException.class)
166
public void dupKeysDisallowed3() {
167
Map<Integer, String> map = Map.of(0, "a", 1, "b", 0, "c");
168
}
169
170
@Test(expectedExceptions=IllegalArgumentException.class)
171
public void dupKeysDisallowed4() {
172
Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 0, "d");
173
}
174
175
@Test(expectedExceptions=IllegalArgumentException.class)
176
public void dupKeysDisallowed5() {
177
Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 0, "e");
178
}
179
180
@Test(expectedExceptions=IllegalArgumentException.class)
181
public void dupKeysDisallowed6() {
182
Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",
183
0, "f");
184
}
185
186
@Test(expectedExceptions=IllegalArgumentException.class)
187
public void dupKeysDisallowed7() {
188
Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",
189
5, "f", 0, "g");
190
}
191
192
@Test(expectedExceptions=IllegalArgumentException.class)
193
public void dupKeysDisallowed8() {
194
Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",
195
5, "f", 6, "g", 0, "h");
196
}
197
198
@Test(expectedExceptions=IllegalArgumentException.class)
199
public void dupKeysDisallowed9() {
200
Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",
201
5, "f", 6, "g", 7, "h", 0, "i");
202
}
203
204
@Test(expectedExceptions=IllegalArgumentException.class)
205
public void dupKeysDisallowed10() {
206
Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",
207
5, "f", 6, "g", 7, "h", 8, "i", 0, "j");
208
}
209
210
@Test(expectedExceptions=IllegalArgumentException.class)
211
public void dupKeysDisallowedN() {
212
Map.Entry<Integer,String>[] entries = genEntries(MAX_ENTRIES);
213
entries[MAX_ENTRIES-1] = Map.entry(0, "xxx");
214
Map<Integer, String> map = Map.ofEntries(entries);
215
}
216
217
@Test(dataProvider="all")
218
public void hashCodeEquals(Map<Integer,String> act, Map<Integer,String> exp) {
219
assertEquals(act.hashCode(), exp.hashCode());
220
}
221
222
@Test(expectedExceptions=NullPointerException.class)
223
public void nullKeyDisallowed1() {
224
Map<Integer, String> map = Map.of(null, "a");
225
}
226
227
@Test(expectedExceptions=NullPointerException.class)
228
public void nullValueDisallowed1() {
229
Map<Integer, String> map = Map.of(0, null);
230
}
231
232
@Test(expectedExceptions=NullPointerException.class)
233
public void nullKeyDisallowed2() {
234
Map<Integer, String> map = Map.of(0, "a", null, "b");
235
}
236
237
@Test(expectedExceptions=NullPointerException.class)
238
public void nullValueDisallowed2() {
239
Map<Integer, String> map = Map.of(0, "a", 1, null);
240
}
241
242
@Test(expectedExceptions=NullPointerException.class)
243
public void nullKeyDisallowed3() {
244
Map<Integer, String> map = Map.of(0, "a", 1, "b", null, "c");
245
}
246
247
@Test(expectedExceptions=NullPointerException.class)
248
public void nullValueDisallowed3() {
249
Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, null);
250
}
251
252
@Test(expectedExceptions=NullPointerException.class)
253
public void nullKeyDisallowed4() {
254
Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", null, "d");
255
}
256
257
@Test(expectedExceptions=NullPointerException.class)
258
public void nullValueDisallowed4() {
259
Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, null);
260
}
261
262
@Test(expectedExceptions=NullPointerException.class)
263
public void nullKeyDisallowed5() {
264
Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", null, "e");
265
}
266
267
@Test(expectedExceptions=NullPointerException.class)
268
public void nullValueDisallowed5() {
269
Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, null);
270
}
271
272
@Test(expectedExceptions=NullPointerException.class)
273
public void nullKeyDisallowed6() {
274
Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",
275
null, "f");
276
}
277
278
@Test(expectedExceptions=NullPointerException.class)
279
public void nullValueDisallowed6() {
280
Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",
281
5, null);
282
}
283
284
@Test(expectedExceptions=NullPointerException.class)
285
public void nullKeyDisallowed7() {
286
Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",
287
5, "f", null, "g");
288
}
289
290
@Test(expectedExceptions=NullPointerException.class)
291
public void nullValueDisallowed7() {
292
Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",
293
5, "f", 6, null);
294
}
295
296
@Test(expectedExceptions=NullPointerException.class)
297
public void nullKeyDisallowed8() {
298
Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",
299
5, "f", 6, "g", null, "h");
300
}
301
302
@Test(expectedExceptions=NullPointerException.class)
303
public void nullValueDisallowed8() {
304
Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",
305
5, "f", 6, "g", 7, null);
306
}
307
308
@Test(expectedExceptions=NullPointerException.class)
309
public void nullKeyDisallowed9() {
310
Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",
311
5, "f", 6, "g", 7, "h", null, "i");
312
}
313
314
@Test(expectedExceptions=NullPointerException.class)
315
public void nullValueDisallowed9() {
316
Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",
317
5, "f", 6, "g", 7, "h", 8, null);
318
}
319
320
@Test(expectedExceptions=NullPointerException.class)
321
public void nullKeyDisallowed10() {
322
Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",
323
5, "f", 6, "g", 7, "h", 8, "i", null, "j");
324
}
325
326
@Test(expectedExceptions=NullPointerException.class)
327
public void nullValueDisallowed10() {
328
Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",
329
5, "f", 6, "g", 7, "h", 8, "i", 9, null);
330
}
331
332
@Test(expectedExceptions=NullPointerException.class)
333
public void nullKeyDisallowedVar1() {
334
Map.Entry<Integer,String>[] entries = genEmptyEntryArray1();
335
entries[0] = new AbstractMap.SimpleImmutableEntry<>(null, "a");
336
Map<Integer, String> map = Map.ofEntries(entries);
337
}
338
339
@Test(expectedExceptions=NullPointerException.class)
340
public void nullValueDisallowedVar1() {
341
Map.Entry<Integer,String>[] entries = genEmptyEntryArray1();
342
entries[0] = new AbstractMap.SimpleImmutableEntry<>(0, null);
343
Map<Integer, String> map = Map.ofEntries(entries);
344
}
345
346
@Test(expectedExceptions=NullPointerException.class)
347
public void nullEntryDisallowedVar1() {
348
Map.Entry<Integer,String>[] entries = genEmptyEntryArray1();
349
Map<Integer, String> map = Map.ofEntries(entries);
350
}
351
352
@Test(expectedExceptions=NullPointerException.class)
353
public void nullKeyDisallowedVarN() {
354
Map.Entry<Integer,String>[] entries = genEntries(MAX_ENTRIES);
355
entries[0] = new AbstractMap.SimpleImmutableEntry<>(null, "a");
356
Map<Integer, String> map = Map.ofEntries(entries);
357
}
358
359
@Test(expectedExceptions=NullPointerException.class)
360
public void nullValueDisallowedVarN() {
361
Map.Entry<Integer,String>[] entries = genEntries(MAX_ENTRIES);
362
entries[0] = new AbstractMap.SimpleImmutableEntry<>(0, null);
363
Map<Integer, String> map = Map.ofEntries(entries);
364
}
365
366
@Test(expectedExceptions=NullPointerException.class)
367
public void nullEntryDisallowedVarN() {
368
Map.Entry<Integer,String>[] entries = genEntries(MAX_ENTRIES);
369
entries[5] = null;
370
Map<Integer, String> map = Map.ofEntries(entries);
371
}
372
373
@Test(expectedExceptions=NullPointerException.class)
374
public void nullArrayDisallowed() {
375
Map.ofEntries((Map.Entry<?,?>[])null);
376
}
377
378
@Test(dataProvider="all", expectedExceptions=NullPointerException.class)
379
public void containsValueNullShouldThrowNPE(Map<Integer,String> act, Map<Integer,String> exp) {
380
act.containsValue(null);
381
}
382
383
@Test(dataProvider="all", expectedExceptions=NullPointerException.class)
384
public void containsKeyNullShouldThrowNPE(Map<Integer,String> act, Map<Integer,String> exp) {
385
act.containsKey(null);
386
}
387
388
@Test(dataProvider="all", expectedExceptions=NullPointerException.class)
389
public void getNullShouldThrowNPE(Map<Integer,String> act, Map<Integer,String> exp) {
390
act.get(null);
391
}
392
393
@Test(dataProvider="all")
394
public void serialEquality(Map<Integer, String> act, Map<Integer, String> exp) {
395
// assume that act.equals(exp) tested elsewhere
396
Map<Integer, String> copy = serialClone(act);
397
assertEquals(act, copy);
398
assertEquals(copy, exp);
399
}
400
401
@SuppressWarnings("unchecked")
402
static <T> T serialClone(T obj) {
403
try {
404
ByteArrayOutputStream baos = new ByteArrayOutputStream();
405
try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
406
oos.writeObject(obj);
407
}
408
ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
409
ObjectInputStream ois = new ObjectInputStream(bais);
410
return (T) ois.readObject();
411
} catch (IOException | ClassNotFoundException e) {
412
throw new AssertionError(e);
413
}
414
}
415
416
Map<Integer, String> genMap() {
417
Map<Integer, String> map = new HashMap<>();
418
map.put(1, "a");
419
map.put(2, "b");
420
map.put(3, "c");
421
return map;
422
}
423
424
@Test
425
public void copyOfResultsEqual() {
426
Map<Integer, String> orig = genMap();
427
Map<Integer, String> copy = Map.copyOf(orig);
428
429
assertEquals(orig, copy);
430
assertEquals(copy, orig);
431
}
432
433
@Test
434
public void copyOfModifiedUnequal() {
435
Map<Integer, String> orig = genMap();
436
Map<Integer, String> copy = Map.copyOf(orig);
437
orig.put(4, "d");
438
439
assertNotEquals(orig, copy);
440
assertNotEquals(copy, orig);
441
}
442
443
@Test
444
public void copyOfIdentity() {
445
Map<Integer, String> orig = genMap();
446
Map<Integer, String> copy1 = Map.copyOf(orig);
447
Map<Integer, String> copy2 = Map.copyOf(copy1);
448
449
assertNotSame(orig, copy1);
450
assertSame(copy1, copy2);
451
}
452
453
@Test(expectedExceptions=NullPointerException.class)
454
public void copyOfRejectsNullMap() {
455
Map<Integer, String> map = Map.copyOf(null);
456
}
457
458
@Test(expectedExceptions=NullPointerException.class)
459
public void copyOfRejectsNullKey() {
460
Map<Integer, String> map = genMap();
461
map.put(null, "x");
462
Map<Integer, String> copy = Map.copyOf(map);
463
}
464
465
@Test(expectedExceptions=NullPointerException.class)
466
public void copyOfRejectsNullValue() {
467
Map<Integer, String> map = genMap();
468
map.put(-1, null);
469
Map<Integer, String> copy = Map.copyOf(map);
470
}
471
472
// Map::entry tests
473
474
@Test(expectedExceptions=NullPointerException.class)
475
public void entryWithNullKeyDisallowed() {
476
Map.Entry<Integer,String> e = Map.entry(null, "x");
477
}
478
479
@Test(expectedExceptions=NullPointerException.class)
480
public void entryWithNullValueDisallowed() {
481
Map.Entry<Integer,String> e = Map.entry(0, null);
482
}
483
484
@Test
485
public void entrySetValueDisallowed() {
486
var e = Map.entry("a", "b");
487
assertThrows(UnsupportedOperationException.class, () -> e.setValue("x"));
488
}
489
490
@Test
491
public void entryBasicTests() {
492
Map.Entry<String,String> kvh1 = Map.entry("xyzzy", "plugh");
493
Map.Entry<String,String> kvh2 = Map.entry("foobar", "blurfl");
494
Map.Entry<String,String> sie = new AbstractMap.SimpleImmutableEntry<>("xyzzy", "plugh");
495
496
assertTrue(kvh1.equals(sie));
497
assertTrue(sie.equals(kvh1));
498
assertFalse(kvh2.equals(sie));
499
assertFalse(sie.equals(kvh2));
500
assertEquals(kvh1.hashCode(), sie.hashCode());
501
assertEquals(kvh1.toString(), sie.toString());
502
}
503
504
// Map.Entry::copyOf tests
505
506
@Test(expectedExceptions=NullPointerException.class)
507
public void entryCopyNullDisallowed() {
508
Map.Entry.copyOf(null);
509
}
510
511
@Test
512
public void entryCopyWithNullKeyDisallowed() {
513
var e = new AbstractMap.SimpleEntry<>(null, "b");
514
assertThrows(NullPointerException.class, () -> Map.Entry.copyOf(e));
515
}
516
517
@Test
518
public void entryCopyWithNullValueDisallowed() {
519
var e = new AbstractMap.SimpleEntry<>("a", null);
520
assertThrows(NullPointerException.class, () -> Map.Entry.copyOf(e));
521
}
522
523
@Test
524
public void entryCopySetValueDisallowed() {
525
var e = new AbstractMap.SimpleEntry<>("a", "b");
526
var c = Map.Entry.copyOf(e);
527
assertThrows(UnsupportedOperationException.class, () -> c.setValue("x"));
528
}
529
530
@Test
531
public void entryCopyBasicTests() {
532
Map.Entry<String,String> orig = new AbstractMap.SimpleImmutableEntry<>("xyzzy", "plugh");
533
Map.Entry<String,String> copy1 = Map.Entry.copyOf(orig);
534
Map.Entry<String,String> copy2 = Map.Entry.copyOf(copy1);
535
536
assertEquals(orig, copy1);
537
assertEquals(copy1, orig);
538
assertEquals(orig, copy2);
539
assertEquals(copy2, orig);
540
assertEquals(copy1, copy2);
541
assertEquals(copy2, copy1);
542
543
assertNotSame(orig, copy1);
544
assertSame(copy1, copy2);
545
546
assertEquals(copy1.hashCode(), orig.hashCode());
547
assertEquals(copy1.toString(), orig.toString());
548
}
549
550
// compile-time test of wildcards
551
@Test
552
public void entryWildcardTests() {
553
Map.Entry<Integer,Double> e1 = Map.entry(1, 2.0);
554
Map.Entry<Float,Long> e2 = Map.entry(3.0f, 4L);
555
Map<Number,Number> map = Map.ofEntries(e1, e2);
556
assertEquals(map.size(), 2);
557
}
558
}
559
560