Path: blob/master/test/jdk/java/util/EnumMap/SimpleSerialization.java
41149 views
/*1* Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* Portions Copyright (c) 2011 IBM Corporation25*/2627/*28* @test29* @bug 631270630* @summary A serialized EnumMap can be successfully de-serialized.31* @author Neil Richards <[email protected]>, <[email protected]>32*/3334import java.io.ByteArrayInputStream;35import java.io.ByteArrayOutputStream;36import java.io.IOException;37import java.io.ObjectInputStream;38import java.io.ObjectOutputStream;39import java.io.PrintWriter;40import java.io.StringWriter;41import java.util.EnumMap;4243public class SimpleSerialization {44private enum TestEnum { e00, e01, e02, e03, e04, e05, e06, e07 }45public static void main(final String[] args) throws Exception {46final EnumMap<TestEnum, String> enumMap = new EnumMap<>(TestEnum.class);4748enumMap.put(TestEnum.e01, TestEnum.e01.name());49enumMap.put(TestEnum.e04, TestEnum.e04.name());50enumMap.put(TestEnum.e05, TestEnum.e05.name());5152final ByteArrayOutputStream baos = new ByteArrayOutputStream();53final ObjectOutputStream oos = new ObjectOutputStream(baos);5455oos.writeObject(enumMap);56oos.close();5758final byte[] data = baos.toByteArray();59final ByteArrayInputStream bais = new ByteArrayInputStream(data);60final ObjectInputStream ois = new ObjectInputStream(bais);6162final Object deserializedObject = ois.readObject();63ois.close();6465if (false == enumMap.equals(deserializedObject)) {66throw new RuntimeException(getFailureText(enumMap, deserializedObject));67}68}6970private static String getFailureText(final Object orig, final Object copy) {71final StringWriter sw = new StringWriter();72final PrintWriter pw = new PrintWriter(sw);7374pw.println("Test FAILED: Deserialized object is not equal to the original object");75pw.print("\tOriginal: ");76printObject(pw, orig).println();77pw.print("\tCopy: ");78printObject(pw, copy).println();7980pw.close();81return sw.toString();82}8384private static PrintWriter printObject(final PrintWriter pw, final Object o) {85pw.printf("%s@%08x", o.getClass().getName(), System.identityHashCode(o));86return pw;87}88}899091