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.Collectors;
38 import java.util.stream.IntStream;
39
40 import org.testng.annotations.DataProvider;
41 import org.testng.annotations.Test;
42
43 import static org.testng.Assert.assertEquals;
44 import static org.testng.Assert.assertFalse;
45 import static org.testng.Assert.assertTrue;
46 import static org.testng.Assert.fail;
47
48 /*
49 * @test
50 * @bug 8048330
51 * @summary Test convenience static factory methods on Map.
52 * @run testng MapFactories
53 */
54
55 public class MapFactories {
56
57 static final int MAX_ENTRIES = 20; // should be larger than the largest fixed-arg overload
58 static String valueFor(int i) {
59 // the String literal below should be of length MAX_ENTRIES
60 return "abcdefghijklmnopqrst".substring(i, i+1);
61 }
62
63 // for "expected" values
64 Map<Integer,String> genMap(int n) {
65 Map<Integer,String> result = new HashMap<>();
66 for (int i = 0; i < n; i++) {
67 result.put(i, valueFor(i));
68 }
69 return result;
70 }
71
72 // for varargs Map.Entry methods
73 @SuppressWarnings("unchecked")
74 Map.Entry<Integer,String>[] genEntries(int n) {
75 return IntStream.range(0, n)
76 .mapToObj(i -> Map.entry(i, valueFor(i)))
77 .toArray(Map.Entry[]::new);
78 }
79
80 // returns array of [actual, expected]
81 static Object[] a(Map<Integer,String> act, Map<Integer,String> exp) {
82 return new Object[] { act, exp };
83 }
84
85 @DataProvider(name="empty")
86 public Iterator<Object[]> empty() {
87 return Collections.singletonList(
88 a(Map.of(), genMap(0))
89 ).iterator();
90 }
91
92 @DataProvider(name="nonempty")
305
306 @Test(expectedExceptions=NullPointerException.class)
307 public void nullValueDisallowed9() {
308 Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",
309 5, "f", 6, "g", 7, "h", 8, null);
310 }
311
312 @Test(expectedExceptions=NullPointerException.class)
313 public void nullKeyDisallowed10() {
314 Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",
315 5, "f", 6, "g", 7, "h", 8, "i", null, "j");
316 }
317
318 @Test(expectedExceptions=NullPointerException.class)
319 public void nullValueDisallowed10() {
320 Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",
321 5, "f", 6, "g", 7, "h", 8, "i", 9, null);
322 }
323
324 @Test(expectedExceptions=NullPointerException.class)
325 public void nullKeyDisallowedN() {
326 Map.Entry<Integer,String>[] entries = genEntries(MAX_ENTRIES);
327 entries[0] = new AbstractMap.SimpleImmutableEntry(null, "a");
328 Map<Integer, String> map = Map.ofEntries(entries);
329 }
330
331 @Test(expectedExceptions=NullPointerException.class)
332 public void nullValueDisallowedN() {
333 Map.Entry<Integer,String>[] entries = genEntries(MAX_ENTRIES);
334 entries[0] = new AbstractMap.SimpleImmutableEntry(0, null);
335 Map<Integer, String> map = Map.ofEntries(entries);
336 }
337
338 @Test(expectedExceptions=NullPointerException.class)
339 public void nullEntryDisallowedN() {
340 Map.Entry<Integer,String>[] entries = genEntries(MAX_ENTRIES);
341 entries[5] = null;
342 Map<Integer, String> map = Map.ofEntries(entries);
343 }
344
345 @Test(expectedExceptions=NullPointerException.class)
346 public void nullArrayDisallowed() {
347 Map.ofEntries(null);
348 }
349
350 @Test(dataProvider="all")
351 public void serialEquality(Map<Integer, String> act, Map<Integer, String> exp) {
352 // assume that act.equals(exp) tested elsewhere
353 Map<Integer, String> copy = serialClone(act);
354 assertEquals(act, copy);
355 assertEquals(copy, exp);
356 }
357
358 @SuppressWarnings("unchecked")
359 static <T> T serialClone(T obj) {
360 try {
361 ByteArrayOutputStream baos = new ByteArrayOutputStream();
362 ObjectOutputStream oos = new ObjectOutputStream(baos);
363 oos.writeObject(obj);
364 oos.close();
365 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
366 ObjectInputStream ois = new ObjectInputStream(bais);
367 return (T) ois.readObject();
368 } catch (IOException | ClassNotFoundException e) {
369 throw new AssertionError(e);
370 }
371 }
372
373 // Map.entry() tests
374
375 @Test(expectedExceptions=NullPointerException.class)
376 public void entryWithNullKeyDisallowed() {
377 Map.Entry<Integer,String> e = Map.entry(null, "x");
378 }
379
380 @Test(expectedExceptions=NullPointerException.class)
381 public void entryWithNullValueDisallowed() {
382 Map.Entry<Integer,String> e = Map.entry(0, null);
383 }
384
385 @Test
386 public void entryBasicTests() {
387 Map.Entry<String,String> kvh1 = Map.entry("xyzzy", "plugh");
388 Map.Entry<String,String> kvh2 = Map.entry("foobar", "blurfl");
389 Map.Entry<String,String> sie = new AbstractMap.SimpleImmutableEntry("xyzzy", "plugh");
390
391 assertTrue(kvh1.equals(sie));
392 assertTrue(sie.equals(kvh1));
393 assertFalse(kvh2.equals(sie));
394 assertFalse(sie.equals(kvh2));
395 assertEquals(sie.hashCode(), kvh1.hashCode());
396 assertEquals(sie.toString(), kvh1.toString());
397 }
398
399 // compile-time test of wildcards
400 @Test
401 public void entryWildcardTests() {
402 Map.Entry<Integer,Double> e1 = Map.entry(1, 2.0);
403 Map.Entry<Float,Long> e2 = Map.entry(3.0f, 4L);
404 Map<Number,Number> map = Map.ofEntries(e1, e2);
405 assertEquals(map.size(), 2);
406 }
407
408 }
|
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.Collectors;
38 import java.util.stream.IntStream;
39
40 import org.testng.annotations.DataProvider;
41 import org.testng.annotations.Test;
42
43 import static org.testng.Assert.assertEquals;
44 import static org.testng.Assert.assertFalse;
45 import static org.testng.Assert.assertNotEquals;
46 import static org.testng.Assert.assertNotSame;
47 import static org.testng.Assert.assertSame;
48 import static org.testng.Assert.assertTrue;
49 import static org.testng.Assert.fail;
50
51 /*
52 * @test
53 * @bug 8048330
54 * @summary Test convenience static factory methods on Map.
55 * @run testng MapFactories
56 */
57
58 public class MapFactories {
59
60 static final int MAX_ENTRIES = 20; // should be larger than the largest fixed-arg overload
61 static String valueFor(int i) {
62 // the String literal below should be of length MAX_ENTRIES
63 return "abcdefghijklmnopqrst".substring(i, i+1);
64 }
65
66 // for "expected" values
67 Map<Integer,String> genMap(int n) {
68 Map<Integer,String> result = new HashMap<>();
69 for (int i = 0; i < n; i++) {
70 result.put(i, valueFor(i));
71 }
72 return result;
73 }
74
75 // for varargs Map.Entry methods
76
77 @SuppressWarnings("unchecked")
78 Map.Entry<Integer,String>[] genEmptyEntryArray1() {
79 return (Map.Entry<Integer,String>[])new Map.Entry<?,?>[1];
80 }
81
82 @SuppressWarnings("unchecked")
83 Map.Entry<Integer,String>[] genEntries(int n) {
84 return IntStream.range(0, n)
85 .mapToObj(i -> Map.entry(i, valueFor(i)))
86 .toArray(Map.Entry[]::new);
87 }
88
89 // returns array of [actual, expected]
90 static Object[] a(Map<Integer,String> act, Map<Integer,String> exp) {
91 return new Object[] { act, exp };
92 }
93
94 @DataProvider(name="empty")
95 public Iterator<Object[]> empty() {
96 return Collections.singletonList(
97 a(Map.of(), genMap(0))
98 ).iterator();
99 }
100
101 @DataProvider(name="nonempty")
314
315 @Test(expectedExceptions=NullPointerException.class)
316 public void nullValueDisallowed9() {
317 Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",
318 5, "f", 6, "g", 7, "h", 8, null);
319 }
320
321 @Test(expectedExceptions=NullPointerException.class)
322 public void nullKeyDisallowed10() {
323 Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",
324 5, "f", 6, "g", 7, "h", 8, "i", null, "j");
325 }
326
327 @Test(expectedExceptions=NullPointerException.class)
328 public void nullValueDisallowed10() {
329 Map<Integer, String> map = Map.of(0, "a", 1, "b", 2, "c", 3, "d", 4, "e",
330 5, "f", 6, "g", 7, "h", 8, "i", 9, null);
331 }
332
333 @Test(expectedExceptions=NullPointerException.class)
334 public void nullKeyDisallowedVar1() {
335 Map.Entry<Integer,String>[] entries = genEmptyEntryArray1();
336 entries[0] = new AbstractMap.SimpleImmutableEntry<>(null, "a");
337 Map<Integer, String> map = Map.ofEntries(entries);
338 }
339
340 @Test(expectedExceptions=NullPointerException.class)
341 public void nullValueDisallowedVar1() {
342 Map.Entry<Integer,String>[] entries = genEmptyEntryArray1();
343 entries[0] = new AbstractMap.SimpleImmutableEntry<>(0, null);
344 Map<Integer, String> map = Map.ofEntries(entries);
345 }
346
347 @Test(expectedExceptions=NullPointerException.class)
348 public void nullEntryDisallowedVar1() {
349 Map.Entry<Integer,String>[] entries = genEmptyEntryArray1();
350 Map<Integer, String> map = Map.ofEntries(entries);
351 }
352
353 @Test(expectedExceptions=NullPointerException.class)
354 public void nullKeyDisallowedVarN() {
355 Map.Entry<Integer,String>[] entries = genEntries(MAX_ENTRIES);
356 entries[0] = new AbstractMap.SimpleImmutableEntry<>(null, "a");
357 Map<Integer, String> map = Map.ofEntries(entries);
358 }
359
360 @Test(expectedExceptions=NullPointerException.class)
361 public void nullValueDisallowedVarN() {
362 Map.Entry<Integer,String>[] entries = genEntries(MAX_ENTRIES);
363 entries[0] = new AbstractMap.SimpleImmutableEntry<>(0, null);
364 Map<Integer, String> map = Map.ofEntries(entries);
365 }
366
367 @Test(expectedExceptions=NullPointerException.class)
368 public void nullEntryDisallowedVarN() {
369 Map.Entry<Integer,String>[] entries = genEntries(MAX_ENTRIES);
370 entries[5] = null;
371 Map<Integer, String> map = Map.ofEntries(entries);
372 }
373
374 @Test(expectedExceptions=NullPointerException.class)
375 public void nullArrayDisallowed() {
376 Map.ofEntries((Map.Entry<?,?>[])null);
377 }
378
379 @Test(dataProvider="all")
380 public void serialEquality(Map<Integer, String> act, Map<Integer, String> exp) {
381 // assume that act.equals(exp) tested elsewhere
382 Map<Integer, String> copy = serialClone(act);
383 assertEquals(act, copy);
384 assertEquals(copy, exp);
385 }
386
387 @SuppressWarnings("unchecked")
388 static <T> T serialClone(T obj) {
389 try {
390 ByteArrayOutputStream baos = new ByteArrayOutputStream();
391 try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
392 oos.writeObject(obj);
393 }
394 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray());
395 ObjectInputStream ois = new ObjectInputStream(bais);
396 return (T) ois.readObject();
397 } catch (IOException | ClassNotFoundException e) {
398 throw new AssertionError(e);
399 }
400 }
401
402 Map<Integer, String> genMap() {
403 Map<Integer, String> map = new HashMap<>();
404 map.put(1, "a");
405 map.put(2, "b");
406 map.put(3, "c");
407 return map;
408 }
409
410 @Test
411 public void copyOfResultsEqual() {
412 Map<Integer, String> orig = genMap();
413 Map<Integer, String> copy = Map.copyOf(orig);
414
415 assertEquals(orig, copy);
416 assertEquals(copy, orig);
417 }
418
419 @Test
420 public void copyOfModifiedUnequal() {
421 Map<Integer, String> orig = genMap();
422 Map<Integer, String> copy = Map.copyOf(orig);
423 orig.put(4, "d");
424
425 assertNotEquals(orig, copy);
426 assertNotEquals(copy, orig);
427 }
428
429 @Test
430 public void copyOfIdentity() {
431 Map<Integer, String> orig = genMap();
432 Map<Integer, String> copy1 = Map.copyOf(orig);
433 Map<Integer, String> copy2 = Map.copyOf(copy1);
434
435 assertNotSame(orig, copy1);
436 assertSame(copy1, copy2);
437 }
438
439 @Test(expectedExceptions=NullPointerException.class)
440 public void copyOfRejectsNullMap() {
441 Map<Integer, String> map = Map.copyOf(null);
442 }
443
444 @Test(expectedExceptions=NullPointerException.class)
445 public void copyOfRejectsNullKey() {
446 Map<Integer, String> map = genMap();
447 map.put(null, "x");
448 Map<Integer, String> copy = Map.copyOf(map);
449 }
450
451 @Test(expectedExceptions=NullPointerException.class)
452 public void copyOfRejectsNullValue() {
453 Map<Integer, String> map = genMap();
454 map.put(-1, null);
455 Map<Integer, String> copy = Map.copyOf(map);
456 }
457
458 // Map.entry() tests
459
460 @Test(expectedExceptions=NullPointerException.class)
461 public void entryWithNullKeyDisallowed() {
462 Map.Entry<Integer,String> e = Map.entry(null, "x");
463 }
464
465 @Test(expectedExceptions=NullPointerException.class)
466 public void entryWithNullValueDisallowed() {
467 Map.Entry<Integer,String> e = Map.entry(0, null);
468 }
469
470 @Test
471 public void entryBasicTests() {
472 Map.Entry<String,String> kvh1 = Map.entry("xyzzy", "plugh");
473 Map.Entry<String,String> kvh2 = Map.entry("foobar", "blurfl");
474 Map.Entry<String,String> sie = new AbstractMap.SimpleImmutableEntry<>("xyzzy", "plugh");
475
476 assertTrue(kvh1.equals(sie));
477 assertTrue(sie.equals(kvh1));
478 assertFalse(kvh2.equals(sie));
479 assertFalse(sie.equals(kvh2));
480 assertEquals(sie.hashCode(), kvh1.hashCode());
481 assertEquals(sie.toString(), kvh1.toString());
482 }
483
484 // compile-time test of wildcards
485 @Test
486 public void entryWildcardTests() {
487 Map.Entry<Integer,Double> e1 = Map.entry(1, 2.0);
488 Map.Entry<Float,Long> e2 = Map.entry(3.0f, 4L);
489 Map<Number,Number> map = Map.ofEntries(e1, e2);
490 assertEquals(map.size(), 2);
491 }
492 }
|