1 /* 2 * Copyright (c) 2002, 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 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 { 43 // Serialize 44 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 45 ObjectOutputStream oos = new ObjectOutputStream(bos); 46 oos.writeObject(o); 47 byte[] serializedForm = bos.toByteArray(); 48 49 // Deserialize 50 InputStream is = new ByteArrayInputStream(serializedForm); 51 ObjectInputStream ois = new ObjectInputStream(is); 52 return ois.readObject(); 53 } 54 55 @Test(dataProvider="SerializableSingletons") 56 public static void serializableSingletons(String description, Supplier<Object> o) { 57 try { 58 assertSame(patheticDeepCopy(o.get()), o.get(), description + ": not a singleton"); 59 } catch(Exception all) { 60 fail(description + ": Unexpected Exception", all); 61 } 62 } 63 64 @DataProvider(name = "SerializableSingletons", parallel = true) 65 public static Iterator<Object[]> navigableMapProvider() { 66 return makeSingletons().iterator(); 67 } 68 69 public static Collection<Object[]> makeSingletons() { 70 return Arrays.asList( 71 new Object[]{"Collections.EMPTY_LIST", 72 (Supplier) () -> {return Collections.EMPTY_LIST;}}, 73 new Object[]{"Collections.EMPTY_MAP", 74 (Supplier) () -> {return Collections.EMPTY_MAP;}}, 75 new Object[]{"Collections.EMPTY_SET", 76 (Supplier) () -> {return Collections.EMPTY_SET;}}, 77 new Object[]{"Collections.singletonMap()", 78 (Supplier) () -> {return Collections.emptyList();}}, 79 new Object[]{"Collections.emptyMap()", 80 (Supplier) () -> {return Collections.emptyMap();}}, 81 new Object[]{"Collections.emptySet()", 82 (Supplier) () -> {return Collections.emptySet();}}, 83 new Object[]{"Collections.emptySortedSet()", 84 (Supplier) () -> {return Collections.emptySortedSet();}}, 85 new Object[]{"Collections.emptySortedMap()", 86 (Supplier) () -> {return Collections.emptySortedMap();}}, 87 new Object[]{"Collections.emptyNavigableSet()", 88 (Supplier) () -> {return Collections.emptyNavigableSet();}}, 89 new Object[]{"Collections.emptyNavigableMap()", 90 (Supplier) () -> {return Collections.emptyNavigableMap();}} 91 ); 92 } 93 }