jdk/test/java/util/Hashtable/SimpleSerialization.java
author ctornqvi
Thu, 16 May 2013 15:31:00 +0200
changeset 17598 f78d22d3c6c2
parent 14342 8435a30053c1
permissions -rw-r--r--
8008169: test/runtime/7158804/Test7158804.sh has bad copyright header Summary: Re-wrote test in Java in addition to fixing the Copyright notice. Also reviewed by leonid.mesnik@oracle.com Reviewed-by: coleenp, ctornqvi Contributed-by: Mikhailo Seledtsov <mikhailo.seledtsov@oracle.com>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8394
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 12859
diff changeset
     2
 * Copyright (c) 2010, 2012, Oracle and/or its affiliates. All rights reserved.
8394
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
     4
 *
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
     7
 * published by the Free Software Foundation.
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
     8
 *
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    13
 * accompanied this code).
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    14
 *
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    18
 *
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    21
 * questions.
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    22
 *
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    23
 * -------------------------------------------
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    24
 *
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    25
 * Portions Copyright (c) 2010, 2011 IBM Corporation
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    26
 */
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    27
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    28
/*
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    29
 * @test
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    30
 * @bug 6927486
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    31
 * @summary A serialized Hashtable can be de-serialized properly.
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    32
 * @author Neil Richards <neil.richards@ngmr.net>, <neil_richards@uk.ibm.com>
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    33
 */
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    34
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    35
import java.io.ByteArrayInputStream;
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    36
import java.io.ByteArrayOutputStream;
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    37
import java.io.ObjectInputStream;
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    38
import java.io.ObjectOutputStream;
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    39
import java.io.PrintWriter;
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    40
import java.io.StringWriter;
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    41
import java.util.Hashtable;
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 9734
diff changeset
    42
import java.util.Map;
8394
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    43
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    44
public class SimpleSerialization {
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    45
    public static void main(final String[] args) throws Exception {
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    46
        Hashtable<String, String> h1 = new Hashtable<>();
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    47
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 9734
diff changeset
    48
        System.err.println("*** BEGIN TEST ***");
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 9734
diff changeset
    49
8394
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    50
        h1.put("key", "value");
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    51
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    52
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 9734
diff changeset
    53
        try (ObjectOutputStream oos = new ObjectOutputStream(baos)) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 9734
diff changeset
    54
            oos.writeObject(h1);
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 9734
diff changeset
    55
        }
8394
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    56
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    57
        final byte[] data = baos.toByteArray();
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    58
        final ByteArrayInputStream bais = new ByteArrayInputStream(data);
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 9734
diff changeset
    59
        final Object deserializedObject;
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 9734
diff changeset
    60
        try (ObjectInputStream ois = new ObjectInputStream(bais)) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 9734
diff changeset
    61
            deserializedObject = ois.readObject();
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 9734
diff changeset
    62
        }
8394
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    63
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 9734
diff changeset
    64
        if(!h1.getClass().isInstance(deserializedObject)) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 9734
diff changeset
    65
             throw new RuntimeException("Result not assignable to type of h1");
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 9734
diff changeset
    66
        }
8394
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    67
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    68
        if (false == h1.equals(deserializedObject)) {
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 9734
diff changeset
    69
             Hashtable<String, String> d1 = (Hashtable<String, String>) h1;
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 9734
diff changeset
    70
            for(Map.Entry entry: h1.entrySet()) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 9734
diff changeset
    71
                System.err.println("h1.key::" + entry.getKey() + " d1.containsKey()::" + d1.containsKey((String) entry.getKey()));
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 9734
diff changeset
    72
                System.err.println("h1.value::" + entry.getValue() + " d1.contains()::" + d1.contains(entry.getValue()));
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 9734
diff changeset
    73
                System.err.println("h1.value == d1.value " + entry.getValue().equals(d1.get((String) entry.getKey())));
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 9734
diff changeset
    74
            }
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 9734
diff changeset
    75
8394
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    76
            throw new RuntimeException(getFailureText(h1, deserializedObject));
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    77
        }
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    78
    }
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    79
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    80
    private static String getFailureText(final Object orig, final Object copy) {
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    81
        final StringWriter sw = new StringWriter();
12859
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 9734
diff changeset
    82
        try (PrintWriter pw = new PrintWriter(sw)) {
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 9734
diff changeset
    83
            pw.println("Test FAILED: Deserialized object is not equal to the original object");
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 9734
diff changeset
    84
            pw.print("\tOriginal: ");
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 9734
diff changeset
    85
            printObject(pw, orig).println();
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 9734
diff changeset
    86
            pw.print("\tCopy:     ");
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 9734
diff changeset
    87
            printObject(pw, copy).println();
c44b88bb9b5e 7126277: Alternative String hashing implementation
mduigou
parents: 9734
diff changeset
    88
        }
8394
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    89
        return sw.toString();
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    90
    }
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    91
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    92
    private static PrintWriter printObject(final PrintWriter pw, final Object o) {
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    93
        pw.printf("%s@%08x", o.getClass().getName(), System.identityHashCode(o));
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    94
        return pw;
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    95
    }
49215a09f282 6927486: Hashtable writeObject() may deadlock
mduigou
parents:
diff changeset
    96
}