test/jdk/java/io/Serializable/typeSafeEnum/TypeSafeEnum.java
author rriggs
Fri, 11 Oct 2019 13:11:56 -0400
changeset 58565 baa5969ecf34
parent 47216 71c04702a3d5
permissions -rw-r--r--
8231427: Warning cleanup in tests of java.io.Serializable Reviewed-by: darcy, lancea
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
     2
 * Copyright (c) 1998, 2019, 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 {
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    33
    private static final long serialVersionUID = 1L;
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    34
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    private static int numWriteObject = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private static int numReadObject = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    private String value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
    private TypeSafeEnum(String value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        this.value = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
32649
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 5506
diff changeset
    44
    public static final TypeSafeEnum FIRST = new TypeSafeEnum("First");
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 5506
diff changeset
    45
    public static final TypeSafeEnum SECOND = new TypeSafeEnum("Second");
2ee9017c7597 8136583: Core libraries should use blessed modifier order
martin
parents: 5506
diff changeset
    46
    public static final TypeSafeEnum THIRD = new TypeSafeEnum("Third");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    static int numReadResolve = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    static int numWriteReplace = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    static boolean verbose = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    52
    private Object writeReplace() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        numWriteReplace++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            System.out.println("TypeSafeEnum.writeReplace() " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                               this.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
        return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    61
    private Object readResolve() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        numReadResolve++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            System.out.println("readResolve called on " + this.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        if (value.equals(FIRST.value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
            return FIRST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        } else if (value.equals(SECOND.value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            return SECOND;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        } else if (value.equals(THIRD.value)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            return THIRD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
            //unknown type safe enum
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    private void readObject(ObjectInputStream in)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        numReadObject++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        in.defaultReadObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            System.out.println("TypeSafeEnum.readObject() " + this.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        if (value == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            in.registerValidation(this, 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    public void validateObject() throws InvalidObjectException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        // only top level case has null for value, validate.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        if (numWriteObject != 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            throw new Error("Expected 4 calls to writeObject, only " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                            numWriteObject + " made");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        if (numReadObject != 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            throw new Error("Expected 4 calls to readObject, only " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                            numReadObject + " made");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        if (numWriteReplace != 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throw new Error("Expected 4 calls to writeReplace, only " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
                            numWriteReplace + " made");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        if (numReadResolve != 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            throw new Error("Expected 4 calls to readResolve, only " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
                            numReadResolve + " made");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private void writeObject(ObjectOutputStream out) throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        numWriteObject++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        out.defaultWriteObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        if (verbose) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
            System.out.println("TypeSafeEnum.writeObject() " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
                               this.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    public String toString() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        return super.toString() + " value=" + value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    public static void main(String args[])
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        throws IOException, ClassNotFoundException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        if (args.length > 0 && args[0].equals("-verbose"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            verbose = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        TypeSafeEnum[] writeArray = new TypeSafeEnum[7];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        writeArray[0] = FIRST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        writeArray[1] = SECOND;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        writeArray[2] = THIRD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        writeArray[3] = FIRST;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        writeArray[4] = SECOND;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        writeArray[5] = THIRD;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
        writeArray[6] = new TypeSafeEnum("Third");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
        ObjectOutputStream os = new ObjectOutputStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        os.writeObject(writeArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        os.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        TypeSafeEnum[] readArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        ObjectInputStream in =
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
           new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        readArray = (TypeSafeEnum[])in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
        in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        for (int i= 0; i < writeArray.length - 1 ; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
            if (writeArray[i] != readArray[i]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
                throw new Error("Serializa/deserialize did not preserve " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
                                "singleton for " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
                                readArray[i].toString() +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
                                " and " + writeArray[i].toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
};