Path: blob/master/test/jdk/java/util/Collections/EmptyCollectionSerialization.java
41152 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 org.testng.annotations.DataProvider;32import org.testng.annotations.Test;3334import java.io.ByteArrayInputStream;35import java.io.ByteArrayOutputStream;36import java.io.InputStream;37import java.io.ObjectInputStream;38import java.io.ObjectOutputStream;39import java.util.Arrays;40import java.util.Collection;41import java.util.Collections;42import java.util.Iterator;43import java.util.function.Supplier;4445import static org.testng.Assert.assertSame;46import static org.testng.Assert.fail;4748public class EmptyCollectionSerialization {49private static Object patheticDeepCopy(Object o) throws Exception {50// Serialize51ByteArrayOutputStream bos = new ByteArrayOutputStream();52ObjectOutputStream oos = new ObjectOutputStream(bos);53oos.writeObject(o);54byte[] serializedForm = bos.toByteArray();5556// Deserialize57InputStream is = new ByteArrayInputStream(serializedForm);58ObjectInputStream ois = new ObjectInputStream(is);59return ois.readObject();60}6162@Test(dataProvider="SerializableSingletons")63public static void serializableSingletons(String description, Supplier<Object> o) {64try {65Object singleton = o.get();66assertSame(o.get(), singleton, description + ": broken Supplier not returning singleton");67Object copy = patheticDeepCopy(singleton);68assertSame(copy, singleton, description + ": " +69copy.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(copy)) +70" is not the singleton " +71singleton.getClass().getName() + "@" + Integer.toHexString(System.identityHashCode(singleton)));72} catch (Exception all) {73fail(description + ": Unexpected Exception", all);74}75}7677@DataProvider(name = "SerializableSingletons", parallel = true)78public static Iterator<Object[]> navigableMapProvider() {79return makeSingletons().iterator();80}8182public static Collection<Object[]> makeSingletons() {83Object[][] params = {84{"Collections.EMPTY_LIST",85(Supplier) () -> Collections.EMPTY_LIST},86{"Collections.EMPTY_MAP",87(Supplier) () -> Collections.EMPTY_MAP},88{"Collections.EMPTY_SET",89(Supplier) () -> Collections.EMPTY_SET},90{"Collections.emptyList()",91(Supplier) () -> Collections.emptyList()},92{"Collections.emptyMap()",93(Supplier) () -> Collections.emptyMap()},94{"Collections.emptySet()",95(Supplier) () -> Collections.emptySet()},96{"Collections.emptySortedSet()",97(Supplier) () -> Collections.emptySortedSet()},98{"Collections.emptySortedMap()",99(Supplier) () -> Collections.emptySortedMap()},100{"Collections.emptyNavigableSet()",101(Supplier) () -> Collections.emptyNavigableSet()},102{"Collections.emptyNavigableMap()",103(Supplier) () -> Collections.emptyNavigableMap()},104};105return Arrays.asList(params);106}107}108109110