test/jdk/java/io/Serializable/oldTests/CheckForException.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) 2005, 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
 * @summary it is new version of old test which was
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 *          /src/share/test/serialization/psiotest.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *          Test pickling and unpickling an object with derived classes
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          and using a read special to serialize the "middle" class,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *          which raises NotSerializableException inside writeObject()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 *          and readObject() methods.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class CheckForException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    public static void main (String argv[]) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
        System.err.println("\nRegression test of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
                           "serialization/deserialization of " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
                           "complex objects which raise " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
                           "NotSerializableException inside " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
                           "writeObject() and readObject() methods.\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        FileInputStream istream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
            FileOutputStream ostream = new FileOutputStream("psiotest3.tmp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            ObjectOutputStream p = new ObjectOutputStream(ostream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            /* Catch the expected exception and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
             * complain if it does not occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            TryPickleClass npc = new TryPickleClass();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            NotSerializableException we = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
                // Two objects of the same class that are used
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                // in cleanup test below
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
                p.writeObject("test");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
                p.writeObject("test2");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
                p.writeObject(npc);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
            } catch (NotSerializableException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
                we = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
            if (we == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                System.err.println("\nTEST FAILED: Write of NoPickleClass " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                    "should have raised an exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
                throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            p.flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
            ostream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            istream = new FileInputStream("psiotest3.tmp");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
            ObjectInputStream q = new ObjectInputStream(istream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            /* Catch the expected exception and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
             * complain if it does not occur.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
             */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            TryPickleClass npc_u;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            NotSerializableException re = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                // Read the two objects, neither has a cleanup method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                q.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                q.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                npc_u = (TryPickleClass)q.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            } catch (NotSerializableException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                re = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            if (re == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                System.err.println("\nTEST FAILED: Read of NoPickleClass " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                   "should have raised an exception");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
                throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
            istream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
            System.err.println("\nTEST PASSED");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            System.err.print("TEST FAILED: ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
class PickleClass implements java.io.Serializable {
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
   108
    private static final long serialVersionUID = 1L;
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
   109
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    int ii = 17;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    transient int tmp[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    private void writeObject(ObjectOutputStream pw) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        pw.writeUTF("PickleClass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        pw.writeInt(ii);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    private void readObject(ObjectInputStream pr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        tmp = new int[32];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        pr.readUTF();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        ii = pr.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private void readObjectCleanup(ObjectInputStream pr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        System.err.println("\nPickleClass cleanup correctly called on abort.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        if (tmp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            tmp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
class NoPickleClass extends PickleClass {
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
   134
    private static final long serialVersionUID = 1L;
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
   135
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    private void writeObject(ObjectOutputStream pw)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        throws NotSerializableException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        throw new NotSerializableException("NoPickleClass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
    private void readObject(ObjectInputStream pr)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            throws NotSerializableException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            throw new NotSerializableException("NoPickleClass");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
class TryPickleClass  extends NoPickleClass {
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
   150
    private static final long serialVersionUID = 1L;
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
   151
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
    int i = 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    transient int tmp[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    private void writeObject(ObjectOutputStream pw) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
            pw.writeInt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    private void readObject(ObjectInputStream pr) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            tmp = new int[32];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            i = pr.readInt();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
    private void readObjectCleanup(ObjectInputStream pr) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            System.err.println("\nCleanup called on abort");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
            if (tmp != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
                tmp = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
}