jdk/test/java/io/Serializable/typeSafeEnum/TypeSafeEnum.java
author martin
Tue, 15 Sep 2015 21:56:04 -0700
changeset 32649 2ee9017c7597
parent 5506 202f599c92aa
permissions -rw-r--r--
8136583: Core libraries should use blessed modifier order Summary: Run blessed-modifier-order script (see bug) Reviewed-by: psandoz, chegar, alanb, plevart
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 1998, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
 * @bug 4140729
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Verify that writeReplace & readResolve are called by serialization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *          readResolve is used to maintain the invariant that the enums
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          of TypeSafeEnum are singletons.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
public class TypeSafeEnum implements Serializable, ObjectInputValidation {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
    private static int numWriteObject = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
    private static int numReadObject = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private String value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
    private TypeSafeEnum(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 5506
diff changeset
    42
    public static final TypeSafeEnum FIRST = new TypeSafeEnum("First");
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 5506
diff changeset
    43
    public static final TypeSafeEnum SECOND = new TypeSafeEnum("Second");
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 5506
diff changeset
    44
    public static final TypeSafeEnum THIRD = new TypeSafeEnum("Third");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    static int numReadResolve = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static int numWriteReplace = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static boolean verbose = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    private Object writeReplace() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
        numWriteReplace++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            System.out.println("TypeSafeEnum.writeReplace() " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
                               this.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private Object readResolve() throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        numReadResolve++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            System.out.println("readResolve called on " + this.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        if (value.equals(FIRST.value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            return FIRST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        } else if (value.equals(SECOND.value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            return SECOND;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        } else if (value.equals(THIRD.value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            return THIRD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            //unknown type safe enum
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    private void readObject(ObjectInputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        numReadObject++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        in.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            System.out.println("TypeSafeEnum.readObject() " + this.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            in.registerValidation(this, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    public void validateObject() throws InvalidObjectException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        // only top level case has null for value, validate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        if (numWriteObject != 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            throw new Error("Expected 4 calls to writeObject, only " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                            numWriteObject + " made");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        if (numReadObject != 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            throw new Error("Expected 4 calls to readObject, only " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                            numReadObject + " made");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        if (numWriteReplace != 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            throw new Error("Expected 4 calls to writeReplace, only " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                            numWriteReplace + " made");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        if (numReadResolve != 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
            throw new Error("Expected 4 calls to readResolve, only " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
                            numReadResolve + " made");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private void writeObject(ObjectOutputStream out) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        numWriteObject++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        out.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
            System.out.println("TypeSafeEnum.writeObject() " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
                               this.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        return super.toString() + " value=" + value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    public static void main(String args[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (args.length > 0 && args[0].equals("-verbose"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            verbose = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        TypeSafeEnum[] writeArray = new TypeSafeEnum[7];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
        writeArray[0] = FIRST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        writeArray[1] = SECOND;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        writeArray[2] = THIRD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        writeArray[3] = FIRST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        writeArray[4] = SECOND;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        writeArray[5] = THIRD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        writeArray[6] = new TypeSafeEnum("Third");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        ObjectOutputStream os = new ObjectOutputStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        os.writeObject(writeArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        TypeSafeEnum[] readArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        ObjectInputStream in =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
           new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        readArray = (TypeSafeEnum[])in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        for (int i= 0; i < writeArray.length - 1 ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            if (writeArray[i] != readArray[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
                throw new Error("Serializa/deserialize did not preserve " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
                                "singleton for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                                readArray[i].toString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                                " and " + writeArray[i].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
};