• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2011, 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  * Portions Copyright (c) 2011 IBM Corporation
26  */
27 
28 package test.java.util.EnumMap;
29 
30 /*
31  * @test
32  * @bug 6312706
33  * @summary A serialized EnumMap can be successfully de-serialized.
34  * @author Neil Richards <neil.richards@ngmr.net>, <neil_richards@uk.ibm.com>
35  */
36 
37 import java.io.ByteArrayInputStream;
38 import java.io.ByteArrayOutputStream;
39 import java.io.IOException;
40 import java.io.ObjectInputStream;
41 import java.io.ObjectOutputStream;
42 import java.io.PrintWriter;
43 import java.io.StringWriter;
44 import java.util.EnumMap;
45 
46 public class SimpleSerialization {
47     private enum TestEnum { e00, e01, e02, e03, e04, e05, e06, e07 }
main(final String[] args)48     public static void main(final String[] args) throws Exception {
49         final EnumMap<TestEnum, String> enumMap = new EnumMap<>(TestEnum.class);
50 
51         enumMap.put(TestEnum.e01, TestEnum.e01.name());
52         enumMap.put(TestEnum.e04, TestEnum.e04.name());
53         enumMap.put(TestEnum.e05, TestEnum.e05.name());
54 
55         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
56         final ObjectOutputStream oos = new ObjectOutputStream(baos);
57 
58         oos.writeObject(enumMap);
59         oos.close();
60 
61         final byte[] data = baos.toByteArray();
62         final ByteArrayInputStream bais = new ByteArrayInputStream(data);
63         final ObjectInputStream ois = new ObjectInputStream(bais);
64 
65         final Object deserializedObject = ois.readObject();
66         ois.close();
67 
68         if (false == enumMap.equals(deserializedObject)) {
69             throw new RuntimeException(getFailureText(enumMap, deserializedObject));
70         }
71     }
72 
getFailureText(final Object orig, final Object copy)73     private static String getFailureText(final Object orig, final Object copy) {
74         final StringWriter sw = new StringWriter();
75         final PrintWriter pw = new PrintWriter(sw);
76 
77         pw.println("Test FAILED: Deserialized object is not equal to the original object");
78         pw.print("\tOriginal: ");
79         printObject(pw, orig).println();
80         pw.print("\tCopy:     ");
81         printObject(pw, copy).println();
82 
83         pw.close();
84         return sw.toString();
85     }
86 
printObject(final PrintWriter pw, final Object o)87     private static PrintWriter printObject(final PrintWriter pw, final Object o) {
88         pw.printf("%s@%08x", o.getClass().getName(), System.identityHashCode(o));
89         return pw;
90     }
91 }
92