1 |
|
/* |
2 |
< |
* Copyright (c) 2002, Oracle and/or its affiliates. All rights reserved. |
2 |
> |
* Copyright (c) 2002, 2013, 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 |
23 |
|
|
24 |
|
/* |
25 |
|
* @test |
26 |
< |
* @bug 4684279 |
26 |
> |
* @bug 4684279 7129185 |
27 |
|
* @summary Empty utility collections should be singletons |
28 |
|
* @author Josh Bloch |
29 |
+ |
* @run testng EmptyCollectionSerialization |
30 |
|
*/ |
31 |
|
|
32 |
|
import java.util.*; |
33 |
+ |
import java.util.function.Supplier; |
34 |
|
import java.io.*; |
35 |
+ |
import org.testng.annotations.Test; |
36 |
+ |
import org.testng.annotations.DataProvider; |
37 |
+ |
|
38 |
+ |
import static org.testng.Assert.fail; |
39 |
+ |
import static org.testng.Assert.assertSame; |
40 |
|
|
41 |
|
public class EmptyCollectionSerialization { |
42 |
|
private static Object patheticDeepCopy(Object o) throws Exception { |
52 |
|
return ois.readObject(); |
53 |
|
} |
54 |
|
|
55 |
< |
private static boolean isSingleton(Object o) throws Exception { |
56 |
< |
return patheticDeepCopy(o) == o; |
55 |
> |
@Test(dataProvider="SerializableSingletons") |
56 |
> |
public static void serializableSingletons(String description, Supplier<Object> o) { |
57 |
> |
try { |
58 |
> |
Object singleton = o.get(); |
59 |
> |
assertSame(o.get(), singleton, description + ": broken Supplier not returning singleton"); |
60 |
> |
Object copy = patheticDeepCopy(singleton); |
61 |
> |
assertSame( copy, singleton, description + ": " + |
62 |
> |
copy.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(copy)) + |
63 |
> |
" is not the singleton " + |
64 |
> |
singleton.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(singleton))); |
65 |
> |
} catch (Exception all) { |
66 |
> |
fail(description + ": Unexpected Exception", all); |
67 |
> |
} |
68 |
> |
} |
69 |
> |
|
70 |
> |
@DataProvider(name = "SerializableSingletons", parallel = true) |
71 |
> |
public static Iterator<Object[]> navigableMapProvider() { |
72 |
> |
return makeSingletons().iterator(); |
73 |
|
} |
74 |
|
|
75 |
< |
public static void main(String[] args) throws Exception { |
76 |
< |
if (!isSingleton(Collections.EMPTY_SET)) |
77 |
< |
throw new Exception("EMPTY_SET"); |
78 |
< |
if (!isSingleton(Collections.EMPTY_LIST)) |
79 |
< |
throw new Exception("EMPTY_LIST"); |
80 |
< |
if (!isSingleton(Collections.EMPTY_MAP)) |
81 |
< |
throw new Exception("EMPTY_MAP"); |
75 |
> |
public static Collection<Object[]> makeSingletons() { |
76 |
> |
return Arrays.asList( |
77 |
> |
new Object[]{"Collections.EMPTY_LIST", |
78 |
> |
(Supplier) () -> {return Collections.EMPTY_LIST;}}, |
79 |
> |
new Object[]{"Collections.EMPTY_MAP", |
80 |
> |
(Supplier) () -> {return Collections.EMPTY_MAP;}}, |
81 |
> |
new Object[]{"Collections.EMPTY_SET", |
82 |
> |
(Supplier) () -> {return Collections.EMPTY_SET;}}, |
83 |
> |
new Object[]{"Collections.singletonMap()", |
84 |
> |
(Supplier) () -> {return Collections.emptyList();}}, |
85 |
> |
new Object[]{"Collections.emptyMap()", |
86 |
> |
(Supplier) () -> {return Collections.emptyMap();}}, |
87 |
> |
new Object[]{"Collections.emptySet()", |
88 |
> |
(Supplier) () -> {return Collections.emptySet();}}, |
89 |
> |
new Object[]{"Collections.emptySortedSet()", |
90 |
> |
(Supplier) () -> {return Collections.emptySortedSet();}}, |
91 |
> |
new Object[]{"Collections.emptySortedMap()", |
92 |
> |
(Supplier) () -> {return Collections.emptySortedMap();}}, |
93 |
> |
new Object[]{"Collections.emptyNavigableSet()", |
94 |
> |
(Supplier) () -> {return Collections.emptyNavigableSet();}}, |
95 |
> |
new Object[]{"Collections.emptyNavigableMap()", |
96 |
> |
(Supplier) () -> {return Collections.emptyNavigableMap();}} |
97 |
> |
); |
98 |
|
} |
99 |
|
} |