StackTraceSerialization.java revision 0
0N/A/*
2362N/A * Copyright 2000-2001 Sun Microsystems, Inc. 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
2362N/A * published by the Free Software Foundation.
0N/A *
2362N/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 *
0N/A * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
0N/A * CA 95054 USA or visit www.sun.com if you need additional information or
2362N/A * have any questions.
2362N/A */
2362N/A
0N/Aimport java.io.*;
0N/Aimport java.util.*;
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 4202914 4363318
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 {
0N/A Throwable original = null;
0N/A try {
0N/A a();
0N/A } catch(HighLevelException e) {
0N/A original = e;
0N/A }
0N/A
0N/A ByteArrayOutputStream bout = new ByteArrayOutputStream();
0N/A ObjectOutputStream out = new ObjectOutputStream(bout);
0N/A out.writeObject(original);
0N/A out.flush();
0N/A ByteArrayInputStream bin =
0N/A new ByteArrayInputStream(bout.toByteArray());
0N/A ObjectInputStream in = new ObjectInputStream(bin);
0N/A Throwable clone = (Throwable) in.readObject();
0N/A
0N/A if (!equal(original, clone))
0N/A throw new Exception();
0N/A }
0N/A
0N/A /**
28N/A * Returns true if e1 and e2 have equal stack traces and their causes
28N/A * are recursively equal (by the same definition). Returns false
28N/A * or throws NullPointerExeption otherwise.
0N/A */
0N/A private static boolean equal(Throwable t1, Throwable t2) {
0N/A return t1==t2 || (Arrays.equals(t1.getStackTrace(), t2.getStackTrace())
0N/A && equal(t1.getCause(), t2.getCause()));
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();
}
static void c() throws MidLevelException {
try {
d();
} catch(LowLevelException e) {
throw new MidLevelException(e);
}
}
static void d() throws LowLevelException {
e();
}
static void e() throws LowLevelException {
throw new LowLevelException();
}
private static final String OUR_CLASS = StackTraceSerialization.class.getName();
private static final String OUR_FILE_NAME = "StackTraceSerialization.java";
private static void check(StackTraceElement e, String methodName, int n) {
if (!e.getClassName().equals(OUR_CLASS))
throw new RuntimeException("Class: " + e);
if (!e.getMethodName().equals(methodName))
throw new RuntimeException("Method name: " + e);
if (!e.getFileName().equals(OUR_FILE_NAME))
throw new RuntimeException("File name: " + e);
if (e.getLineNumber() != n)
throw new RuntimeException("Line number: " + e);
}
}
class HighLevelException extends Exception {
HighLevelException(Throwable cause) { super(cause); }
}
class MidLevelException extends Exception {
MidLevelException(Throwable cause) { super(cause); }
}
class LowLevelException extends Exception {
}