Path: blob/master/test/jdk/java/util/Hashtable/SimpleSerialization.java
41149 views
/*1* Copyright (c) 2010, 2012, 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*22* -------------------------------------------23*24* Portions Copyright (c) 2010, 2011 IBM Corporation25*/2627/*28* @test29* @bug 692748630* @summary A serialized Hashtable can be de-serialized properly.31* @author Neil Richards <[email protected]>, <[email protected]>32*/3334import java.io.ByteArrayInputStream;35import java.io.ByteArrayOutputStream;36import java.io.ObjectInputStream;37import java.io.ObjectOutputStream;38import java.io.PrintWriter;39import java.io.StringWriter;40import java.util.Hashtable;41import java.util.Map;4243public class SimpleSerialization {44public static void main(final String[] args) throws Exception {45Hashtable<String, String> h1 = new Hashtable<>();4647System.err.println("*** BEGIN TEST ***");4849h1.put("key", "value");5051final ByteArrayOutputStream baos = new ByteArrayOutputStream();52try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {53oos.writeObject(h1);54}5556final byte[] data = baos.toByteArray();57final ByteArrayInputStream bais = new ByteArrayInputStream(data);58final Object deserializedObject;59try (ObjectInputStream ois = new ObjectInputStream(bais)) {60deserializedObject = ois.readObject();61}6263if(!h1.getClass().isInstance(deserializedObject)) {64throw new RuntimeException("Result not assignable to type of h1");65}6667if (false == h1.equals(deserializedObject)) {68Hashtable<String, String> d1 = (Hashtable<String, String>) h1;69for(Map.Entry entry: h1.entrySet()) {70System.err.println("h1.key::" + entry.getKey() + " d1.containsKey()::" + d1.containsKey((String) entry.getKey()));71System.err.println("h1.value::" + entry.getValue() + " d1.contains()::" + d1.contains(entry.getValue()));72System.err.println("h1.value == d1.value " + entry.getValue().equals(d1.get((String) entry.getKey())));73}7475throw new RuntimeException(getFailureText(h1, deserializedObject));76}77}7879private static String getFailureText(final Object orig, final Object copy) {80final StringWriter sw = new StringWriter();81try (PrintWriter pw = new PrintWriter(sw)) {82pw.println("Test FAILED: Deserialized object is not equal to the original object");83pw.print("\tOriginal: ");84printObject(pw, orig).println();85pw.print("\tCopy: ");86printObject(pw, copy).println();87}88return sw.toString();89}9091private static PrintWriter printObject(final PrintWriter pw, final Object o) {92pw.printf("%s@%08x", o.getClass().getName(), System.identityHashCode(o));93return pw;94}95}969798