1 /*
   2  * Copyright (c) 2000, 2001, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 import java.io.*;
  25 import java.util.*;
  26 
  27 /*
  28  * @test
  29  * @bug     4202914 4363318
  30  * @summary Basic test of serialization of stack trace information
  31  * @author  Josh Bloch
  32  */
  33 
  34 public class StackTraceSerialization {
  35     public static void main(String args[]) throws Exception {
  36         Throwable original = null;
  37         try {
  38             a();
  39         } catch(HighLevelException e) {
  40             original = e;
  41         }
  42 
  43         ByteArrayOutputStream bout = new ByteArrayOutputStream();
  44         ObjectOutputStream out = new ObjectOutputStream(bout);
  45         out.writeObject(original);
  46         out.flush();
  47         ByteArrayInputStream bin =
  48             new ByteArrayInputStream(bout.toByteArray());
  49         ObjectInputStream in = new ObjectInputStream(bin);
  50         Throwable clone = (Throwable) in.readObject();
  51 
  52         if (!equal(original, clone))
  53             throw new Exception();
  54     }
  55 
  56     /**
  57      * Returns true if e1 and e2 have equal stack traces and their causes
  58      * are recursively equal (by the same definition).  Returns false
  59      * or throws NullPointerExeption otherwise.
  60      */
  61     private static boolean equal(Throwable t1, Throwable t2) {
  62         return t1==t2 || (Arrays.equals(t1.getStackTrace(), t2.getStackTrace())
  63                           && equal(t1.getCause(), t2.getCause()));
  64     }
  65 
  66     static void a() throws HighLevelException {
  67         try {
  68             b();
  69         } catch(MidLevelException e) {
  70             throw new HighLevelException(e);
  71         }
  72     }
  73     static void b() throws MidLevelException {
  74         c();
  75     }
  76     static void c() throws MidLevelException {
  77         try {
  78             d();
  79         } catch(LowLevelException e) {
  80             throw new MidLevelException(e);
  81         }
  82     }
  83     static void d() throws LowLevelException {
  84        e();
  85     }
  86     static void e() throws LowLevelException {
  87         throw new LowLevelException();
  88     }
  89 
  90     private static final String OUR_CLASS  = StackTraceSerialization.class.getName();
  91     private static final String OUR_FILE_NAME = "StackTraceSerialization.java";
  92 
  93     private static void check(StackTraceElement e, String methodName, int n) {
  94         if (!e.getClassName().equals(OUR_CLASS))
  95             throw new RuntimeException("Class: " + e);
  96         if (!e.getMethodName().equals(methodName))
  97             throw new RuntimeException("Method name: " + e);
  98         if (!e.getFileName().equals(OUR_FILE_NAME))
  99             throw new RuntimeException("File name: " + e);
 100         if (e.getLineNumber() != n)
 101             throw new RuntimeException("Line number: " + e);
 102     }
 103 }
 104 
 105 class HighLevelException extends Exception {
 106     HighLevelException(Throwable cause) { super(cause); }
 107 }
 108 
 109 class MidLevelException extends Exception {
 110     MidLevelException(Throwable cause)  { super(cause); }
 111 }
 112 
 113 class LowLevelException extends Exception {
 114 }