jdk/test/java/util/Vector/SimpleSerialization.java
author yhuang
Wed, 17 Apr 2013 01:04:45 -0700
changeset 16908 c8a0f2183fed
parent 9734 b33a24d77590
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8392
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
     1
/*
9734
b33a24d77590 7044486: open jdk repos have files with incorrect copyright headers, which can end up in src bundles
katleman
parents: 8392
diff changeset
     2
 * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
8392
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
     4
 *
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
     7
 * published by the Free Software Foundation.
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
     8
 *
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    13
 * accompanied this code).
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    14
 *
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    18
 *
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    21
 * questions.
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    22
 */
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    23
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    24
/*
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    25
 * Portions Copyright (c) 2010, 2011 IBM Corporation
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    26
 */
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    27
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    28
/*
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    29
 * @test
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    30
 * @bug 6934356
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    31
 * @summary A serialized Vector can be successfully de-serialized.
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    32
 * @author Neil Richards <neil.richards@ngmr.net>, <neil_richards@uk.ibm.com>
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    33
 */
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    34
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    35
import java.io.ByteArrayInputStream;
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    36
import java.io.ByteArrayOutputStream;
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    37
import java.io.IOException;
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    38
import java.io.ObjectInputStream;
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    39
import java.io.ObjectOutputStream;
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    40
import java.io.PrintWriter;
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    41
import java.io.StringWriter;
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    42
import java.util.Vector;
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    43
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    44
public class SimpleSerialization {
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    45
    public static void main(final String[] args) throws Exception {
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    46
        final Vector<String> v1 = new Vector<>();
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    47
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    48
        v1.add("entry1");
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    49
        v1.add("entry2");
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    50
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    51
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    52
        final ObjectOutputStream oos = new ObjectOutputStream(baos);
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    53
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    54
        oos.writeObject(v1);
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    55
        oos.close();
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    56
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    57
        final byte[] data = baos.toByteArray();
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    58
        final ByteArrayInputStream bais = new ByteArrayInputStream(data);
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    59
        final ObjectInputStream ois = new ObjectInputStream(bais);
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    60
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    61
        final Object deserializedObject = ois.readObject();
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    62
        ois.close();
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    63
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    64
        if (false == v1.equals(deserializedObject)) {
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    65
            throw new RuntimeException(getFailureText(v1, deserializedObject));
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    66
        }
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    67
    }
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    68
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    69
    private static String getFailureText(final Object orig, final Object copy) {
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    70
        final StringWriter sw = new StringWriter();
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    71
        final PrintWriter pw = new PrintWriter(sw);
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    72
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    73
        pw.println("Test FAILED: Deserialized object is not equal to the original object");
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    74
        pw.print("\tOriginal: ");
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    75
        printObject(pw, orig).println();
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    76
        pw.print("\tCopy:     ");
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    77
        printObject(pw, copy).println();
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    78
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    79
        pw.close();
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    80
        return sw.toString();
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    81
    }
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    82
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    83
    private static PrintWriter printObject(final PrintWriter pw, final Object o) {
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    84
        pw.printf("%s@%08x", o.getClass().getName(), System.identityHashCode(o));
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    85
        return pw;
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    86
    }
3e5784c9f73e 6934356: Vector.writeObject() serialization may deadlock
mduigou
parents:
diff changeset
    87
}