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