• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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
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     4684279 7129185
27  * @summary Empty utility collections should be singletons
28  * @author  Josh Bloch
29  * @run testng EmptyCollectionSerialization
30  */
31 
32 package test.java.util.Collections;
33 
34 import org.testng.annotations.DataProvider;
35 import org.testng.annotations.Test;
36 
37 import java.io.ByteArrayInputStream;
38 import java.io.ByteArrayOutputStream;
39 import java.io.InputStream;
40 import java.io.ObjectInputStream;
41 import java.io.ObjectOutputStream;
42 import java.util.Arrays;
43 import java.util.Collection;
44 import java.util.Collections;
45 import java.util.Iterator;
46 import java.util.function.Supplier;
47 
48 import static org.testng.Assert.assertSame;
49 import static org.testng.Assert.fail;
50 
51 public class EmptyCollectionSerialization {
patheticDeepCopy(Object o)52     private static Object patheticDeepCopy(Object o) throws Exception {
53         // Serialize
54         ByteArrayOutputStream bos = new ByteArrayOutputStream();
55         ObjectOutputStream oos = new ObjectOutputStream(bos);
56         oos.writeObject(o);
57         byte[] serializedForm = bos.toByteArray();
58 
59         // Deserialize
60         InputStream is = new ByteArrayInputStream(serializedForm);
61         ObjectInputStream ois = new ObjectInputStream(is);
62         return ois.readObject();
63     }
64 
65     @Test(dataProvider="SerializableSingletons")
serializableSingletons(String description, Supplier<Object> o)66     public static void serializableSingletons(String description, Supplier<Object> o) {
67         try {
68             Object singleton = o.get();
69             assertSame(o.get(), singleton, description + ": broken Supplier not returning singleton");
70             Object copy = patheticDeepCopy(singleton);
71             assertSame(copy, singleton, description + ": " +
72                 copy.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(copy)) +
73                 " is not the singleton " +
74                 singleton.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(singleton)));
75         } catch (Exception all) {
76             fail(description + ": Unexpected Exception", all);
77         }
78     }
79 
80     @DataProvider(name = "SerializableSingletons", parallel = true)
navigableMapProvider()81     public static Iterator<Object[]> navigableMapProvider() {
82         return makeSingletons().iterator();
83     }
84 
makeSingletons()85     public static Collection<Object[]> makeSingletons() {
86         Object[][] params = {
87             {"Collections.EMPTY_LIST",
88              (Supplier) () -> Collections.EMPTY_LIST},
89             {"Collections.EMPTY_MAP",
90              (Supplier) () -> Collections.EMPTY_MAP},
91             {"Collections.EMPTY_SET",
92              (Supplier) () -> Collections.EMPTY_SET},
93             {"Collections.emptyList()",
94              (Supplier) () -> Collections.emptyList()},
95             {"Collections.emptyMap()",
96              (Supplier) () -> Collections.emptyMap()},
97             {"Collections.emptySet()",
98              (Supplier) () -> Collections.emptySet()},
99             {"Collections.emptySortedSet()",
100              (Supplier) () -> Collections.emptySortedSet()},
101             {"Collections.emptySortedMap()",
102              (Supplier) () -> Collections.emptySortedMap()},
103             {"Collections.emptyNavigableSet()",
104              (Supplier) () -> Collections.emptyNavigableSet()},
105             {"Collections.emptyNavigableMap()",
106              (Supplier) () -> Collections.emptyNavigableMap()},
107         };
108         return Arrays.asList(params);
109     }
110 }
111