0N/A/*
4116N/A * Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/A
0N/A/*
0N/A * @test
4116N/A * @bug 4202914 4363318 6991528 6998871
0N/A * @summary Basic test of serialization of stack trace information
0N/A * @author Josh Bloch
0N/A */
0N/A
0N/Apublic class StackTraceSerialization {
0N/A public static void main(String args[]) throws Exception {
3057N/A testWithSetStackTrace();
3057N/A testWithFillInStackTrace();
3057N/A }
3057N/A
4116N/A private static void testWithSetStackTrace() {
4116N/A StackTraceElement[] stackTrace = {new StackTraceElement("foo", "bar", "baz", -1)};
4116N/A
4116N/A Throwable t = new TestThrowable(true, false); // Immutable and empty stack
4116N/A assertEmptyStackTrace(t);
4116N/A
4116N/A // Verify fillInStackTrace is now a no-op.
4116N/A t.fillInStackTrace();
4116N/A assertEmptyStackTrace(t);
4116N/A
4116N/A // Verify setStackTrace is now a no-op.
4116N/A t.setStackTrace(stackTrace);
4116N/A assertEmptyStackTrace(t);
3057N/A
4116N/A // Verify null-handling
4116N/A try {
4116N/A t.setStackTrace(null);
4116N/A throw new RuntimeException("No NPE on a null stack trace.");
4116N/A } catch(NullPointerException npe) {
4116N/A assertEmptyStackTrace(t);
4116N/A }
4116N/A
4116N/A try {
4116N/A t.setStackTrace(new StackTraceElement[]{null});
4116N/A throw new RuntimeException("No NPE on a null stack trace element.");
4116N/A } catch(NullPointerException npe) {
4116N/A assertEmptyStackTrace(t);
4116N/A }
3057N/A
3057N/A if (!equal(t, reconstitute(t)))
4116N/A throw new RuntimeException("Unequal Throwables with set stacktrace");
4116N/A
4116N/A Throwable t2 = new Throwable();
4116N/A t2.setStackTrace(stackTrace);
4116N/A if (!equal(t2, reconstitute(t2)))
4116N/A throw new RuntimeException("Unequal Throwables with set stacktrace");
4116N/A
4116N/A }
4116N/A
4116N/A private static class TestThrowable extends Throwable {
4116N/A public TestThrowable(boolean enableSuppression,
4116N/A boolean writableStackTrace) {
4116N/A super("the medium", null,
4116N/A enableSuppression,
4116N/A writableStackTrace);
4116N/A }
3057N/A }
3057N/A
3057N/A private static void assertEmptyStackTrace(Throwable t) {
3057N/A if (t.getStackTrace().length != 0)
3057N/A throw new AssertionError("Nonempty stacktrace.");
3057N/A }
3057N/A
4116N/A private static void testWithFillInStackTrace() {
0N/A Throwable original = null;
0N/A try {
0N/A a();
0N/A } catch(HighLevelException e) {
0N/A original = e;
0N/A }
0N/A
3057N/A if (!equal(original, reconstitute(original)))
4116N/A throw new RuntimeException("Unequal Throwables with filled-in stacktrace");
3057N/A }
3057N/A
3057N/A /**
3057N/A * Serialize the argument and return the deserialized result.
3057N/A */
4116N/A private static Throwable reconstitute(Throwable t) {
3057N/A Throwable result = null;
3057N/A try(ByteArrayOutputStream bout = new ByteArrayOutputStream();
3057N/A ObjectOutputStream out = new ObjectOutputStream(bout)) {
3057N/A out.writeObject(t);
3057N/A out.flush();
3057N/A try(ByteArrayInputStream bin =
3057N/A new ByteArrayInputStream(bout.toByteArray());
3057N/A ObjectInputStream in = new ObjectInputStream(bin)) {
3057N/A result = (Throwable) in.readObject();
3057N/A }
4116N/A } catch(IOException | ClassNotFoundException e) {
4116N/A throw new RuntimeException(e);
3057N/A }
3057N/A return result;
0N/A }
0N/A
0N/A /**
3057N/A * Returns true if e1 and e2 have equal stack traces and their
3057N/A * causes are recursively equal (by the same definition) and their
3057N/A * suppressed exception information is equals. Returns false or
3057N/A * throws NullPointerExeption otherwise.
0N/A */
0N/A private static boolean equal(Throwable t1, Throwable t2) {
3057N/A return t1==t2 ||
3057N/A (Arrays.equals(t1.getStackTrace(), t2.getStackTrace()) &&
3057N/A equal(t1.getCause(), t2.getCause()) &&
3057N/A Objects.equals(t1.getSuppressed(), t2.getSuppressed()));
0N/A }
0N/A
0N/A static void a() throws HighLevelException {
0N/A try {
0N/A b();
0N/A } catch(MidLevelException e) {
0N/A throw new HighLevelException(e);
0N/A }
0N/A }
0N/A static void b() throws MidLevelException {
0N/A c();
0N/A }
0N/A static void c() throws MidLevelException {
0N/A try {
0N/A d();
0N/A } catch(LowLevelException e) {
0N/A throw new MidLevelException(e);
0N/A }
0N/A }
0N/A static void d() throws LowLevelException {
0N/A e();
0N/A }
0N/A static void e() throws LowLevelException {
0N/A throw new LowLevelException();
0N/A }
0N/A
0N/A private static final String OUR_CLASS = StackTraceSerialization.class.getName();
0N/A private static final String OUR_FILE_NAME = "StackTraceSerialization.java";
0N/A
0N/A private static void check(StackTraceElement e, String methodName, int n) {
0N/A if (!e.getClassName().equals(OUR_CLASS))
0N/A throw new RuntimeException("Class: " + e);
0N/A if (!e.getMethodName().equals(methodName))
0N/A throw new RuntimeException("Method name: " + e);
0N/A if (!e.getFileName().equals(OUR_FILE_NAME))
0N/A throw new RuntimeException("File name: " + e);
0N/A if (e.getLineNumber() != n)
0N/A throw new RuntimeException("Line number: " + e);
0N/A }
0N/A}
0N/A
0N/Aclass HighLevelException extends Exception {
0N/A HighLevelException(Throwable cause) { super(cause); }
0N/A}
0N/A
0N/Aclass MidLevelException extends Exception {
0N/A MidLevelException(Throwable cause) { super(cause); }
0N/A}
0N/A
0N/Aclass LowLevelException extends Exception {
0N/A}