Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/util/Collections/Ser.java
38812 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.*;31import java.util.*;3233public class Ser {34public static void main(String[] args) throws Exception {3536try {37ByteArrayOutputStream bos = new ByteArrayOutputStream();38ObjectOutputStream out = new ObjectOutputStream(bos);39out.writeObject(Collections.EMPTY_SET);40out.flush();41ObjectInputStream in = new ObjectInputStream(42new ByteArrayInputStream(bos.toByteArray()));4344if (!Collections.EMPTY_SET.equals(in.readObject()))45throw new RuntimeException("empty set Ser/Deser failure.");46} catch (Exception e) {47throw new RuntimeException("Failed to serialize empty set:" + e);48}4950try {51ByteArrayOutputStream bos = new ByteArrayOutputStream();52ObjectOutputStream out = new ObjectOutputStream(bos);53out.writeObject(Collections.EMPTY_LIST);54out.flush();55ObjectInputStream in = new ObjectInputStream(56new ByteArrayInputStream(bos.toByteArray()));5758if (!Collections.EMPTY_LIST.equals(in.readObject()))59throw new RuntimeException("empty list Ser/Deser failure.");60} catch (Exception e) {61throw new RuntimeException("Failed to serialize empty list:" + e);62}6364try {65ByteArrayOutputStream bos = new ByteArrayOutputStream();66ObjectOutputStream out = new ObjectOutputStream(bos);67Set gumby = Collections.singleton("gumby");68out.writeObject(gumby);69out.flush();70ObjectInputStream in = new ObjectInputStream(71new ByteArrayInputStream(bos.toByteArray()));7273if (!gumby.equals(in.readObject()))74throw new RuntimeException("Singleton Ser/Deser failure.");75} catch (Exception e) {76throw new RuntimeException("Failed to serialize singleton:" + e);77}7879try {80ByteArrayOutputStream bos = new ByteArrayOutputStream();81ObjectOutputStream out = new ObjectOutputStream(bos);82List gumbies = Collections.nCopies(50, "gumby");83out.writeObject(gumbies);84out.flush();85ObjectInputStream in = new ObjectInputStream(86new ByteArrayInputStream(bos.toByteArray()));8788if (!gumbies.equals(in.readObject()))89throw new RuntimeException("nCopies Ser/Deser failure.");90} catch (Exception e) {91throw new RuntimeException("Failed to serialize nCopies:" + e);92}93}94}959697