jdk/test/java/lang/Throwable/StackTraceSerialization.java
author darcy
Sun, 14 Nov 2010 07:22:39 -0800
changeset 7186 7f5fe8985263
parent 5506 202f599c92aa
child 9513 1079ae7ada52
permissions -rw-r--r--
6991528: Support making Throwable.suppressedExceptions immutable Reviewed-by: mchung, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
7186
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
     2
 * Copyright (c) 2000, 2010, 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
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 * @test
7186
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    29
 * @bug     4202914 4363318 6991528
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
 * @summary Basic test of serialization of stack trace information
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * @author  Josh Bloch
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class StackTraceSerialization {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    public static void main(String args[]) throws Exception {
7186
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    36
        testWithSetStackTrace();
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    37
        testWithFillInStackTrace();
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    38
    }
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    39
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    40
    private static void testWithSetStackTrace() throws Exception {
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    41
        Throwable t = new Throwable();
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    42
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    43
        t.setStackTrace(new StackTraceElement[]
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    44
            {new StackTraceElement("foo", "bar", "baz", -1)});
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    45
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    46
        if (!equal(t, reconstitute(t)))
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    47
            throw new Exception("Unequal Throwables with set stacktrace");
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    48
    }
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    49
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    50
    private static void assertEmptyStackTrace(Throwable t) {
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    51
        if (t.getStackTrace().length != 0)
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    52
            throw new AssertionError("Nonempty stacktrace.");
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    53
    }
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    54
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    55
    private static void testWithFillInStackTrace() throws Exception {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        Throwable original = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            a();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        } catch(HighLevelException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            original = e;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
7186
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    63
        if (!equal(original, reconstitute(original)))
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    64
            throw new Exception("Unequal Throwables with filled-in stacktrace");
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    65
    }
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    66
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    67
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    68
    /**
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    69
     * Serialize the argument and return the deserialized result.
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    70
     */
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    71
    private static Throwable reconstitute(Throwable t) throws Exception {
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    72
        Throwable result = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
7186
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    74
        try(ByteArrayOutputStream bout = new ByteArrayOutputStream();
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    75
            ObjectOutputStream out = new ObjectOutputStream(bout)) {
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    76
            out.writeObject(t);
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    77
            out.flush();
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    78
            try(ByteArrayInputStream bin =
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    79
                new ByteArrayInputStream(bout.toByteArray());
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    80
                ObjectInputStream in = new ObjectInputStream(bin)) {
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    81
                result = (Throwable) in.readObject();
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    82
            }
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    83
        }
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    84
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    85
        return result;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
    /**
7186
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    89
     * Returns true if e1 and e2 have equal stack traces and their
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    90
     * causes are recursively equal (by the same definition) and their
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    91
     * suppressed exception information is equals.  Returns false or
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    92
     * throws NullPointerExeption otherwise.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    private static boolean equal(Throwable t1, Throwable t2) {
7186
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    95
        return t1==t2 ||
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    96
            (Arrays.equals(t1.getStackTrace(), t2.getStackTrace()) &&
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    97
             equal(t1.getCause(), t2.getCause()) &&
7f5fe8985263 6991528: Support making Throwable.suppressedExceptions immutable
darcy
parents: 5506
diff changeset
    98
             Objects.equals(t1.getSuppressed(), t2.getSuppressed()));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    static void a() throws HighLevelException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
            b();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        } catch(MidLevelException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            throw new HighLevelException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    static void b() throws MidLevelException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        c();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    static void c() throws MidLevelException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
            d();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        } catch(LowLevelException e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
            throw new MidLevelException(e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
    static void d() throws LowLevelException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
       e();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    static void e() throws LowLevelException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        throw new LowLevelException();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    private static final String OUR_CLASS  = StackTraceSerialization.class.getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    private static final String OUR_FILE_NAME = "StackTraceSerialization.java";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    private static void check(StackTraceElement e, String methodName, int n) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
        if (!e.getClassName().equals(OUR_CLASS))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            throw new RuntimeException("Class: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
        if (!e.getMethodName().equals(methodName))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            throw new RuntimeException("Method name: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        if (!e.getFileName().equals(OUR_FILE_NAME))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            throw new RuntimeException("File name: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
        if (e.getLineNumber() != n)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            throw new RuntimeException("Line number: " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
class HighLevelException extends Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
    HighLevelException(Throwable cause) { super(cause); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
class MidLevelException extends Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
    MidLevelException(Throwable cause)  { super(cause); }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
class LowLevelException extends Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
}