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.io.*; |
32 |
> |
import org.testng.annotations.DataProvider; |
33 |
> |
import org.testng.annotations.Test; |
34 |
> |
|
35 |
> |
import java.io.ByteArrayInputStream; |
36 |
> |
import java.io.ByteArrayOutputStream; |
37 |
> |
import java.io.InputStream; |
38 |
> |
import java.io.ObjectInputStream; |
39 |
> |
import java.io.ObjectOutputStream; |
40 |
> |
import java.util.Arrays; |
41 |
> |
import java.util.Collection; |
42 |
> |
import java.util.Collections; |
43 |
> |
import java.util.Iterator; |
44 |
> |
import java.util.function.Supplier; |
45 |
> |
|
46 |
> |
import static org.testng.Assert.assertSame; |
47 |
> |
import static org.testng.Assert.fail; |
48 |
|
|
49 |
|
public class EmptyCollectionSerialization { |
50 |
|
private static Object patheticDeepCopy(Object o) throws Exception { |
60 |
|
return ois.readObject(); |
61 |
|
} |
62 |
|
|
63 |
< |
private static boolean isSingleton(Object o) throws Exception { |
64 |
< |
return patheticDeepCopy(o) == o; |
63 |
> |
@Test(dataProvider="SerializableSingletons") |
64 |
> |
public static void serializableSingletons(String description, Supplier<Object> o) { |
65 |
> |
try { |
66 |
> |
Object singleton = o.get(); |
67 |
> |
assertSame(o.get(), singleton, description + ": broken Supplier not returning singleton"); |
68 |
> |
Object copy = patheticDeepCopy(singleton); |
69 |
> |
assertSame(copy, singleton, description + ": " + |
70 |
> |
copy.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(copy)) + |
71 |
> |
" is not the singleton " + |
72 |
> |
singleton.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(singleton))); |
73 |
> |
} catch (Exception all) { |
74 |
> |
fail(description + ": Unexpected Exception", all); |
75 |
> |
} |
76 |
> |
} |
77 |
> |
|
78 |
> |
@DataProvider(name = "SerializableSingletons", parallel = true) |
79 |
> |
public static Iterator<Object[]> navigableMapProvider() { |
80 |
> |
return makeSingletons().iterator(); |
81 |
|
} |
82 |
|
|
83 |
< |
public static void main(String[] args) throws Exception { |
84 |
< |
if (!isSingleton(Collections.EMPTY_SET)) |
85 |
< |
throw new Exception("EMPTY_SET"); |
86 |
< |
if (!isSingleton(Collections.EMPTY_LIST)) |
87 |
< |
throw new Exception("EMPTY_LIST"); |
88 |
< |
if (!isSingleton(Collections.EMPTY_MAP)) |
89 |
< |
throw new Exception("EMPTY_MAP"); |
83 |
> |
public static Collection<Object[]> makeSingletons() { |
84 |
> |
Object[][] params = { |
85 |
> |
{"Collections.EMPTY_LIST", |
86 |
> |
(Supplier) () -> Collections.EMPTY_LIST}, |
87 |
> |
{"Collections.EMPTY_MAP", |
88 |
> |
(Supplier) () -> Collections.EMPTY_MAP}, |
89 |
> |
{"Collections.EMPTY_SET", |
90 |
> |
(Supplier) () -> Collections.EMPTY_SET}, |
91 |
> |
{"Collections.emptyList()", |
92 |
> |
(Supplier) () -> Collections.emptyList()}, |
93 |
> |
{"Collections.emptyMap()", |
94 |
> |
(Supplier) () -> Collections.emptyMap()}, |
95 |
> |
{"Collections.emptySet()", |
96 |
> |
(Supplier) () -> Collections.emptySet()}, |
97 |
> |
{"Collections.emptySortedSet()", |
98 |
> |
(Supplier) () -> Collections.emptySortedSet()}, |
99 |
> |
{"Collections.emptySortedMap()", |
100 |
> |
(Supplier) () -> Collections.emptySortedMap()}, |
101 |
> |
{"Collections.emptyNavigableSet()", |
102 |
> |
(Supplier) () -> Collections.emptyNavigableSet()}, |
103 |
> |
{"Collections.emptyNavigableMap()", |
104 |
> |
(Supplier) () -> Collections.emptyNavigableMap()}, |
105 |
> |
}; |
106 |
> |
return Arrays.asList(params); |
107 |
|
} |
108 |
|
} |