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