3577N/A/*
4248N/A * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
3577N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3577N/A *
3577N/A * This code is free software; you can redistribute it and/or modify it
3577N/A * under the terms of the GNU General Public License version 2 only, as
3577N/A * published by the Free Software Foundation.
3577N/A *
3577N/A * This code is distributed in the hope that it will be useful, but WITHOUT
3577N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3577N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
3577N/A * version 2 for more details (a copy is included in the LICENSE file that
3577N/A * accompanied this code).
3577N/A *
3577N/A * You should have received a copy of the GNU General Public License version
3577N/A * 2 along with this work; if not, write to the Free Software Foundation,
3577N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3577N/A *
3577N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3577N/A * or visit www.oracle.com if you need additional information or have any
3577N/A * questions.
3577N/A *
3577N/A * -------------------------------------------
3577N/A *
3577N/A * Portions Copyright (c) 2010, 2011 IBM Corporation
3577N/A */
3577N/A
3577N/A/*
3577N/A * @test
3577N/A * @bug 6927486
3577N/A * @summary A serialized Hashtable can be de-serialized properly.
3577N/A * @author Neil Richards <neil.richards@ngmr.net>, <neil_richards@uk.ibm.com>
3577N/A */
3577N/A
3577N/Aimport java.io.ByteArrayInputStream;
3577N/Aimport java.io.ByteArrayOutputStream;
3577N/Aimport java.io.ObjectInputStream;
3577N/Aimport java.io.ObjectOutputStream;
3577N/Aimport java.io.PrintWriter;
3577N/Aimport java.io.StringWriter;
3577N/Aimport java.util.Hashtable;
5047N/Aimport java.util.Map;
3577N/A
3577N/Apublic class SimpleSerialization {
3577N/A public static void main(final String[] args) throws Exception {
3577N/A Hashtable<String, String> h1 = new Hashtable<>();
3577N/A
5047N/A System.err.println("*** BEGIN TEST ***");
5047N/A
3577N/A h1.put("key", "value");
3577N/A
3577N/A final ByteArrayOutputStream baos = new ByteArrayOutputStream();
5047N/A try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
5047N/A oos.writeObject(h1);
5047N/A }
3577N/A
3577N/A final byte[] data = baos.toByteArray();
3577N/A final ByteArrayInputStream bais = new ByteArrayInputStream(data);
5047N/A final Object deserializedObject;
5047N/A try (ObjectInputStream ois = new ObjectInputStream(bais)) {
5047N/A deserializedObject = ois.readObject();
5047N/A }
3577N/A
5047N/A if(!h1.getClass().isInstance(deserializedObject)) {
5047N/A throw new RuntimeException("Result not assignable to type of h1");
5047N/A }
3577N/A
3577N/A if (false == h1.equals(deserializedObject)) {
5047N/A Hashtable<String, String> d1 = (Hashtable<String, String>) h1;
5047N/A for(Map.Entry entry: h1.entrySet()) {
5047N/A System.err.println("h1.key::" + entry.getKey() + " d1.containsKey()::" + d1.containsKey((String) entry.getKey()));
5047N/A System.err.println("h1.value::" + entry.getValue() + " d1.contains()::" + d1.contains(entry.getValue()));
5047N/A System.err.println("h1.value == d1.value " + entry.getValue().equals(d1.get((String) entry.getKey())));
5047N/A }
5047N/A
3577N/A throw new RuntimeException(getFailureText(h1, deserializedObject));
3577N/A }
3577N/A }
3577N/A
3577N/A private static String getFailureText(final Object orig, final Object copy) {
3577N/A final StringWriter sw = new StringWriter();
5047N/A try (PrintWriter pw = new PrintWriter(sw)) {
5047N/A pw.println("Test FAILED: Deserialized object is not equal to the original object");
5047N/A pw.print("\tOriginal: ");
5047N/A printObject(pw, orig).println();
5047N/A pw.print("\tCopy: ");
5047N/A printObject(pw, copy).println();
5047N/A }
3577N/A return sw.toString();
3577N/A }
3577N/A
3577N/A private static PrintWriter printObject(final PrintWriter pw, final Object o) {
3577N/A pw.printf("%s@%08x", o.getClass().getName(), System.identityHashCode(o));
3577N/A return pw;
3577N/A }
3577N/A}