Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Collections/EmptyCollectionSerialization.java
38812 views
/*1* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 4684279 712918526* @summary Empty utility collections should be singletons27* @author Josh Bloch28* @run testng EmptyCollectionSerialization29*/3031import java.util.*;32import java.util.function.Supplier;33import java.io.*;34import org.testng.annotations.Test;35import org.testng.annotations.DataProvider;3637import static org.testng.Assert.fail;38import static org.testng.Assert.assertSame;3940public class EmptyCollectionSerialization {41private static Object patheticDeepCopy(Object o) throws Exception {42// Serialize43ByteArrayOutputStream bos = new ByteArrayOutputStream();44ObjectOutputStream oos = new ObjectOutputStream(bos);45oos.writeObject(o);46byte[] serializedForm = bos.toByteArray();4748// Deserialize49InputStream is = new ByteArrayInputStream(serializedForm);50ObjectInputStream ois = new ObjectInputStream(is);51return ois.readObject();52}5354@Test(dataProvider="SerializableSingletons")55public static void serializableSingletons(String description, Supplier<Object> o) {56try {57Object singleton = o.get();58assertSame(o.get(), singleton, description + ": broken Supplier not returning singleton");59Object copy = patheticDeepCopy(singleton);60assertSame( copy, singleton, description + ": " +61copy.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(copy)) +62" is not the singleton " +63singleton.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(singleton)));64} catch(Exception all) {65fail(description + ": Unexpected Exception", all);66}67}6869@DataProvider(name = "SerializableSingletons", parallel = true)70public static Iterator<Object[]> navigableMapProvider() {71return makeSingletons().iterator();72}7374public static Collection<Object[]> makeSingletons() {75return Arrays.asList(76new Object[]{"Collections.EMPTY_LIST",77(Supplier) () -> {return Collections.EMPTY_LIST;}},78new Object[]{"Collections.EMPTY_MAP",79(Supplier) () -> {return Collections.EMPTY_MAP;}},80new Object[]{"Collections.EMPTY_SET",81(Supplier) () -> {return Collections.EMPTY_SET;}},82new Object[]{"Collections.singletonMap()",83(Supplier) () -> {return Collections.emptyList();}},84new Object[]{"Collections.emptyMap()",85(Supplier) () -> {return Collections.emptyMap();}},86new Object[]{"Collections.emptySet()",87(Supplier) () -> {return Collections.emptySet();}},88new Object[]{"Collections.emptySortedSet()",89(Supplier) () -> {return Collections.emptySortedSet();}},90new Object[]{"Collections.emptySortedMap()",91(Supplier) () -> {return Collections.emptySortedMap();}},92new Object[]{"Collections.emptyNavigableSet()",93(Supplier) () -> {return Collections.emptyNavigableSet();}},94new Object[]{"Collections.emptyNavigableMap()",95(Supplier) () -> {return Collections.emptyNavigableMap();}}96);97}98}99100101