1 /*
  2  * Copyright (c) 1999, 2026, 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 4190323
 27  * @summary EMPTY_SET, EMPTY_LIST, and the collections returned by
 28  *          nCopies and singleton were spec'd to be serializable, but weren't.
 29  * @library /test/lib
 30  */
 31 
 32 import jdk.test.lib.valueclass.AsValueClass;
 33 import java.io.ByteArrayInputStream;
 34 import java.io.ByteArrayOutputStream;
 35 import java.io.ObjectInputStream;
 36 import java.io.ObjectOutputStream;
 37 import java.io.Serializable;
 38 import java.util.Collections;
 39 import java.util.List;
 40 import java.util.Set;
 41 
 42 public class Ser {
 43 
 44     @AsValueClass
 45     static final class SerV implements Serializable {
 46         int x;
 47         SerV(int x) { this.x = x; }
 48         public boolean equals(Object o) { return o instanceof SerV v && x == v.x; }
 49         public int hashCode() { return x; }
 50         private Object writeReplace() { return new SerProxy(x); }
 51         private static class SerProxy implements Serializable {
 52             int x;
 53             SerProxy(int x) { this.x = x; }
 54             private Object readResolve() { return new SerV(x); }
 55         }
 56     }
 57 
 58     public static void main(String[] args) throws Exception {
 59 
 60         try {
 61             ByteArrayOutputStream bos = new ByteArrayOutputStream();
 62             ObjectOutputStream out = new ObjectOutputStream(bos);
 63             out.writeObject(Collections.EMPTY_SET);
 64             out.flush();
 65             ObjectInputStream in = new ObjectInputStream(
 66                     new ByteArrayInputStream(bos.toByteArray()));
 67 
 68             if (!Collections.EMPTY_SET.equals(in.readObject()))
 69                 throw new RuntimeException("empty set Ser/Deser failure.");
 70         } catch (Exception e) {
 71             throw new RuntimeException("Failed to serialize empty set:" + e);
 72         }
 73 
 74         try {
 75             ByteArrayOutputStream bos = new ByteArrayOutputStream();
 76             ObjectOutputStream out = new ObjectOutputStream(bos);
 77             out.writeObject(Collections.EMPTY_LIST);
 78             out.flush();
 79             ObjectInputStream in = new ObjectInputStream(
 80                     new ByteArrayInputStream(bos.toByteArray()));
 81 
 82             if (!Collections.EMPTY_LIST.equals(in.readObject()))
 83                 throw new RuntimeException("empty list Ser/Deser failure.");
 84         } catch (Exception e) {
 85             throw new RuntimeException("Failed to serialize empty list:" + e);
 86         }
 87 
 88         try {
 89             ByteArrayOutputStream bos = new ByteArrayOutputStream();
 90             ObjectOutputStream out = new ObjectOutputStream(bos);
 91             Set gumby = Collections.singleton("gumby");
 92             out.writeObject(gumby);
 93             out.flush();
 94             ObjectInputStream in = new ObjectInputStream(
 95                     new ByteArrayInputStream(bos.toByteArray()));
 96 
 97             if (!gumby.equals(in.readObject()))
 98                 throw new RuntimeException("Singleton Ser/Deser failure.");
 99         } catch (Exception e) {
100             throw new RuntimeException("Failed to serialize singleton:" + e);
101         }
102 
103         try {
104             ByteArrayOutputStream bos = new ByteArrayOutputStream();
105             ObjectOutputStream out = new ObjectOutputStream(bos);
106             List gumbies = Collections.nCopies(50, "gumby");
107             out.writeObject(gumbies);
108             out.flush();
109             ObjectInputStream in = new ObjectInputStream(
110                     new ByteArrayInputStream(bos.toByteArray()));
111 
112             if (!gumbies.equals(in.readObject()))
113                 throw new RuntimeException("nCopies Ser/Deser failure.");
114         } catch (Exception e) {
115             throw new RuntimeException("Failed to serialize nCopies:" + e);
116         }
117 
118         try {
119             ByteArrayOutputStream bos = new ByteArrayOutputStream();
120             ObjectOutputStream out = new ObjectOutputStream(bos);
121             Set<SerV> one = Collections.singleton(new SerV(1));
122             out.writeObject(one);
123             out.flush();
124             ObjectInputStream in = new ObjectInputStream(
125                     new ByteArrayInputStream(bos.toByteArray()));
126             if (!one.equals(in.readObject()))
127                 throw new RuntimeException("value singleton Ser/Deser failure");
128         } catch (Exception e) {
129             throw new RuntimeException("Failed to serialize value singleton: " + e);
130         }
131 
132         try {
133             ByteArrayOutputStream bos = new ByteArrayOutputStream();
134             ObjectOutputStream out = new ObjectOutputStream(bos);
135             List<SerV> many = Collections.nCopies(5, new SerV(2));
136             out.writeObject(many);
137             out.flush();
138             ObjectInputStream in = new ObjectInputStream(
139                     new ByteArrayInputStream(bos.toByteArray()));
140             if (!many.equals(in.readObject()))
141                 throw new RuntimeException("value nCopies Ser/Deser failure");
142         } catch (Exception e) {
143             throw new RuntimeException("Failed to serialize value nCopies: " + e);
144         }
145     }
146 }
--- EOF ---