Path: blob/master/test/jdk/java/util/Collections/Ser.java
41152 views
/*1* Copyright (c) 1999, 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 419032326* @summary EMPTY_SET, EMPTY_LIST, and the collections returned by27* nCopies and singleton were spec'd to be serializable, but weren't.28*/2930import java.io.ByteArrayInputStream;31import java.io.ByteArrayOutputStream;32import java.io.ObjectInputStream;33import java.io.ObjectOutputStream;34import java.util.Collections;35import java.util.List;36import java.util.Set;3738public class Ser {39public static void main(String[] args) throws Exception {4041try {42ByteArrayOutputStream bos = new ByteArrayOutputStream();43ObjectOutputStream out = new ObjectOutputStream(bos);44out.writeObject(Collections.EMPTY_SET);45out.flush();46ObjectInputStream in = new ObjectInputStream(47new ByteArrayInputStream(bos.toByteArray()));4849if (!Collections.EMPTY_SET.equals(in.readObject()))50throw new RuntimeException("empty set Ser/Deser failure.");51} catch (Exception e) {52throw new RuntimeException("Failed to serialize empty set:" + e);53}5455try {56ByteArrayOutputStream bos = new ByteArrayOutputStream();57ObjectOutputStream out = new ObjectOutputStream(bos);58out.writeObject(Collections.EMPTY_LIST);59out.flush();60ObjectInputStream in = new ObjectInputStream(61new ByteArrayInputStream(bos.toByteArray()));6263if (!Collections.EMPTY_LIST.equals(in.readObject()))64throw new RuntimeException("empty list Ser/Deser failure.");65} catch (Exception e) {66throw new RuntimeException("Failed to serialize empty list:" + e);67}6869try {70ByteArrayOutputStream bos = new ByteArrayOutputStream();71ObjectOutputStream out = new ObjectOutputStream(bos);72Set gumby = Collections.singleton("gumby");73out.writeObject(gumby);74out.flush();75ObjectInputStream in = new ObjectInputStream(76new ByteArrayInputStream(bos.toByteArray()));7778if (!gumby.equals(in.readObject()))79throw new RuntimeException("Singleton Ser/Deser failure.");80} catch (Exception e) {81throw new RuntimeException("Failed to serialize singleton:" + e);82}8384try {85ByteArrayOutputStream bos = new ByteArrayOutputStream();86ObjectOutputStream out = new ObjectOutputStream(bos);87List gumbies = Collections.nCopies(50, "gumby");88out.writeObject(gumbies);89out.flush();90ObjectInputStream in = new ObjectInputStream(91new ByteArrayInputStream(bos.toByteArray()));9293if (!gumbies.equals(in.readObject()))94throw new RuntimeException("nCopies Ser/Deser failure.");95} catch (Exception e) {96throw new RuntimeException("Failed to serialize nCopies:" + e);97}98}99}100101102