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 package test.java.util.Collections;
32 
33 import java.io.ByteArrayInputStream;
34 import java.io.ByteArrayOutputStream;
35 import java.io.ObjectInputStream;
36 import java.io.ObjectOutputStream;
37 import java.util.Collections;
38 import java.util.List;
39 import java.util.Set;
40 
41 public class Ser {
main(String[] args)42     public static void main(String[] args) throws Exception {
43 
44         try {
45             ByteArrayOutputStream bos = new ByteArrayOutputStream();
46             ObjectOutputStream out = new ObjectOutputStream(bos);
47             out.writeObject(Collections.EMPTY_SET);
48             out.flush();
49             ObjectInputStream in = new ObjectInputStream(
50                     new ByteArrayInputStream(bos.toByteArray()));
51 
52             if (!Collections.EMPTY_SET.equals(in.readObject()))
53                 throw new RuntimeException("empty set Ser/Deser failure.");
54         } catch (Exception e) {
55             throw new RuntimeException("Failed to serialize empty set:" + e);
56         }
57 
58         try {
59             ByteArrayOutputStream bos = new ByteArrayOutputStream();
60             ObjectOutputStream out = new ObjectOutputStream(bos);
61             out.writeObject(Collections.EMPTY_LIST);
62             out.flush();
63             ObjectInputStream in = new ObjectInputStream(
64                     new ByteArrayInputStream(bos.toByteArray()));
65 
66             if (!Collections.EMPTY_LIST.equals(in.readObject()))
67                 throw new RuntimeException("empty list Ser/Deser failure.");
68         } catch (Exception e) {
69             throw new RuntimeException("Failed to serialize empty list:" + e);
70         }
71 
72         try {
73             ByteArrayOutputStream bos = new ByteArrayOutputStream();
74             ObjectOutputStream out = new ObjectOutputStream(bos);
75             Set gumby = Collections.singleton("gumby");
76             out.writeObject(gumby);
77             out.flush();
78             ObjectInputStream in = new ObjectInputStream(
79                     new ByteArrayInputStream(bos.toByteArray()));
80 
81             if (!gumby.equals(in.readObject()))
82                 throw new RuntimeException("Singleton Ser/Deser failure.");
83         } catch (Exception e) {
84             throw new RuntimeException("Failed to serialize singleton:" + e);
85         }
86 
87         try {
88             ByteArrayOutputStream bos = new ByteArrayOutputStream();
89             ObjectOutputStream out = new ObjectOutputStream(bos);
90             List gumbies = Collections.nCopies(50, "gumby");
91             out.writeObject(gumbies);
92             out.flush();
93             ObjectInputStream in = new ObjectInputStream(
94                     new ByteArrayInputStream(bos.toByteArray()));
95 
96             if (!gumbies.equals(in.readObject()))
97                 throw new RuntimeException("nCopies Ser/Deser failure.");
98         } catch (Exception e) {
99             throw new RuntimeException("Failed to serialize nCopies:" + e);
100         }
101     }
102 }
103