jdk/test/java/io/Serializable/expectedStackTrace/ExpectedStackTrace.java
author smarks
Thu, 17 Nov 2011 15:04:46 -0800
changeset 11895 77896d5ad69b
parent 11894 c7af6deeffd8
child 14342 8435a30053c1
permissions -rw-r--r--
7112267: clean up fix for 7110700 Reviewed-by: dmeetry
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) 2005, 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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
class SerializableObject extends NotSerializableObject
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    implements Serializable
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    public SerializableObject(String m_str, Integer m_int) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        super(m_str, m_int);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
    public SerializableObject() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        super("test", 10);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
public class ExpectedStackTrace {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    private static final String SER_METHOD_NAME = "checkSerializable";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
    public static final void main(String[] args) throws Exception {
11895
77896d5ad69b 7112267: clean up fix for 7110700
smarks
parents: 11894
diff changeset
    62
        System.err.println("\nRegression test for CRs 6317435, 7110700");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        checkSerializable(getObject());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    private static Object getObject() throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        ObjectStreamClass osc =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            ObjectStreamClass.lookup(SerializableObject.class);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        SerializableObject initObj =
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
            (SerializableObject) osc.forClass().newInstance();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
        return initObj;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    private static void checkSerializable(Object initObj) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            // Serialize to a byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
            ByteArrayOutputStream bos = new ByteArrayOutputStream() ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            ObjectOutputStream out = new ObjectOutputStream(bos) ;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
            out.writeObject(initObj);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
            out.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            // Get the bytes of the serialized object
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            byte[] buf = bos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            // Deserialize from a byte array
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
            ByteArrayInputStream bais = new ByteArrayInputStream(buf);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            ObjectInputStream in = new ObjectInputStream(bais);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            SerializableObject finObj = (SerializableObject) in.readObject();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
            in.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            throw new Error();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        } catch(ObjectStreamException ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
            StackTraceElement[] stes = ex.getStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
            boolean found = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            for (int i = 0; i<stes.length-1; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
                StackTraceElement ste = stes[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                String nme = ste.getMethodName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
                if (nme.equals(SER_METHOD_NAME)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
                    found = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            if (found) {
11894
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 5506
diff changeset
   102
                if (ex.getCause() != null) {
11895
77896d5ad69b 7112267: clean up fix for 7110700
smarks
parents: 11894
diff changeset
   103
                    throw new Error("\nTest for CR 7110700 FAILED");
11894
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 5506
diff changeset
   104
                }
11895
77896d5ad69b 7112267: clean up fix for 7110700
smarks
parents: 11894
diff changeset
   105
                System.err.println("\nTEST PASSED");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
            } else {
11894
c7af6deeffd8 7110700: Enhance exception throwing mechanism in ObjectStreamClass
smarks
parents: 5506
diff changeset
   107
                throw new Error("\nTest for CR 6317435 FAILED");
2
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
}