test/jdk/java/io/Serializable/expectedStackTrace/ExpectedStackTrace.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
11895
77896d5ad69b 7112267: clean up fix for 7110700
smarks
parents: 11894
diff changeset
    25
 * @bug 6317435 7110700
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
 * @summary Verify that stack trace contains a proper cause of
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
 *          InvalidClassException (methods: checkSerialize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 *          checkDeserialize or checkDefaultSerialize)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @author Andrey Ozerov
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
class NotSerializableObject {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private String m_str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private Integer m_int;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    public NotSerializableObject(String m_str, Integer m_int) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
        this.m_str = m_str;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
        this.m_int = m_int;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    45
@SuppressWarnings("serial") /* Incorrect declarations are being tested */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
class SerializableObject extends NotSerializableObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
    implements Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
{
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    49
    private static final long serialVersionUID = 1L;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    public SerializableObject(String m_str, Integer m_int) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        super(m_str, m_int);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    public SerializableObject() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        super("test", 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
public class ExpectedStackTrace {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    private static final String SER_METHOD_NAME = "checkSerializable";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    public static final void main(String[] args) throws Exception {
11895
77896d5ad69b 7112267: clean up fix for 7110700
smarks
parents: 11894
diff changeset
    64
        System.err.println("\nRegression test for CRs 6317435, 7110700");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        checkSerializable(getObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    private static Object getObject() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        ObjectStreamClass osc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            ObjectStreamClass.lookup(SerializableObject.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        SerializableObject initObj =
58565
baa5969ecf34 8231427: Warning cleanup in tests of java.io.Serializable
rriggs
parents: 47216
diff changeset
    72
            (SerializableObject) osc.forClass().getConstructor().newInstance();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        return initObj;
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 static void checkSerializable(Object initObj) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            // Serialize to a byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            ObjectOutputStream out = new ObjectOutputStream(bos) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            out.writeObject(initObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            // Get the bytes of the serialized object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            byte[] buf = bos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            // Deserialize from a byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            ByteArrayInputStream bais = new ByteArrayInputStream(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            ObjectInputStream in = new ObjectInputStream(bais);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            SerializableObject finObj = (SerializableObject) in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
        } catch(ObjectStreamException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            StackTraceElement[] stes = ex.getStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
            for (int i = 0; i<stes.length-1; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                StackTraceElement ste = stes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                String nme = ste.getMethodName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                if (nme.equals(SER_METHOD_NAME)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
                    found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            if (found) {
11894
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 5506
diff changeset
   104
                if (ex.getCause() != null) {
11895
77896d5ad69b 7112267: clean up fix for 7110700
smarks
parents: 11894
diff changeset
   105
                    throw new Error("\nTest for CR 7110700 FAILED");
11894
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 5506
diff changeset
   106
                }
11895
77896d5ad69b 7112267: clean up fix for 7110700
smarks
parents: 11894
diff changeset
   107
                System.err.println("\nTEST PASSED");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
            } else {
11894
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 5506
diff changeset
   109
                throw new Error("\nTest for CR 6317435 FAILED");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
}