< prev index next >

test/jdk/java/util/Collections/Ser.java

Print this page

  1 /*
  2  * Copyright (c) 1999, 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  */
 30 

 31 import java.io.ByteArrayInputStream;
 32 import java.io.ByteArrayOutputStream;
 33 import java.io.ObjectInputStream;
 34 import java.io.ObjectOutputStream;

 35 import java.util.Collections;
 36 import java.util.List;
 37 import java.util.Set;
 38 
 39 public class Ser {















 40     public static void main(String[] args) throws Exception {
 41 
 42         try {
 43             ByteArrayOutputStream bos = new ByteArrayOutputStream();
 44             ObjectOutputStream out = new ObjectOutputStream(bos);
 45             out.writeObject(Collections.EMPTY_SET);
 46             out.flush();
 47             ObjectInputStream in = new ObjectInputStream(
 48                     new ByteArrayInputStream(bos.toByteArray()));
 49 
 50             if (!Collections.EMPTY_SET.equals(in.readObject()))
 51                 throw new RuntimeException("empty set Ser/Deser failure.");
 52         } catch (Exception e) {
 53             throw new RuntimeException("Failed to serialize empty set:" + e);
 54         }
 55 
 56         try {
 57             ByteArrayOutputStream bos = new ByteArrayOutputStream();
 58             ObjectOutputStream out = new ObjectOutputStream(bos);
 59             out.writeObject(Collections.EMPTY_LIST);

 79             if (!gumby.equals(in.readObject()))
 80                 throw new RuntimeException("Singleton Ser/Deser failure.");
 81         } catch (Exception e) {
 82             throw new RuntimeException("Failed to serialize singleton:" + e);
 83         }
 84 
 85         try {
 86             ByteArrayOutputStream bos = new ByteArrayOutputStream();
 87             ObjectOutputStream out = new ObjectOutputStream(bos);
 88             List gumbies = Collections.nCopies(50, "gumby");
 89             out.writeObject(gumbies);
 90             out.flush();
 91             ObjectInputStream in = new ObjectInputStream(
 92                     new ByteArrayInputStream(bos.toByteArray()));
 93 
 94             if (!gumbies.equals(in.readObject()))
 95                 throw new RuntimeException("nCopies Ser/Deser failure.");
 96         } catch (Exception e) {
 97             throw new RuntimeException("Failed to serialize nCopies:" + e);
 98         }




























 99     }
100 }

  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);

 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 }
< prev index next >