jdk/test/java/lang/Throwable/StackTraceSerialization.java
changeset 7186 7f5fe8985263
parent 5506 202f599c92aa
child 9513 1079ae7ada52
equal deleted inserted replaced
7185:0ca9d322c703 7186:7f5fe8985263
     1 /*
     1 /*
     2  * Copyright (c) 2000, 2001, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    24 import java.io.*;
    24 import java.io.*;
    25 import java.util.*;
    25 import java.util.*;
    26 
    26 
    27 /*
    27 /*
    28  * @test
    28  * @test
    29  * @bug     4202914 4363318
    29  * @bug     4202914 4363318 6991528
    30  * @summary Basic test of serialization of stack trace information
    30  * @summary Basic test of serialization of stack trace information
    31  * @author  Josh Bloch
    31  * @author  Josh Bloch
    32  */
    32  */
    33 
    33 
    34 public class StackTraceSerialization {
    34 public class StackTraceSerialization {
    35     public static void main(String args[]) throws Exception {
    35     public static void main(String args[]) throws Exception {
       
    36         testWithSetStackTrace();
       
    37         testWithFillInStackTrace();
       
    38     }
       
    39 
       
    40     private static void testWithSetStackTrace() throws Exception {
       
    41         Throwable t = new Throwable();
       
    42 
       
    43         t.setStackTrace(new StackTraceElement[]
       
    44             {new StackTraceElement("foo", "bar", "baz", -1)});
       
    45 
       
    46         if (!equal(t, reconstitute(t)))
       
    47             throw new Exception("Unequal Throwables with set stacktrace");
       
    48     }
       
    49 
       
    50     private static void assertEmptyStackTrace(Throwable t) {
       
    51         if (t.getStackTrace().length != 0)
       
    52             throw new AssertionError("Nonempty stacktrace.");
       
    53     }
       
    54 
       
    55     private static void testWithFillInStackTrace() throws Exception {
    36         Throwable original = null;
    56         Throwable original = null;
    37         try {
    57         try {
    38             a();
    58             a();
    39         } catch(HighLevelException e) {
    59         } catch(HighLevelException e) {
    40             original = e;
    60             original = e;
    41         }
    61         }
    42 
    62 
    43         ByteArrayOutputStream bout = new ByteArrayOutputStream();
    63         if (!equal(original, reconstitute(original)))
    44         ObjectOutputStream out = new ObjectOutputStream(bout);
    64             throw new Exception("Unequal Throwables with filled-in stacktrace");
    45         out.writeObject(original);
    65     }
    46         out.flush();
       
    47         ByteArrayInputStream bin =
       
    48             new ByteArrayInputStream(bout.toByteArray());
       
    49         ObjectInputStream in = new ObjectInputStream(bin);
       
    50         Throwable clone = (Throwable) in.readObject();
       
    51 
    66 
    52         if (!equal(original, clone))
    67 
    53             throw new Exception();
    68     /**
       
    69      * Serialize the argument and return the deserialized result.
       
    70      */
       
    71     private static Throwable reconstitute(Throwable t) throws Exception {
       
    72         Throwable result = null;
       
    73 
       
    74         try(ByteArrayOutputStream bout = new ByteArrayOutputStream();
       
    75             ObjectOutputStream out = new ObjectOutputStream(bout)) {
       
    76             out.writeObject(t);
       
    77             out.flush();
       
    78             try(ByteArrayInputStream bin =
       
    79                 new ByteArrayInputStream(bout.toByteArray());
       
    80                 ObjectInputStream in = new ObjectInputStream(bin)) {
       
    81                 result = (Throwable) in.readObject();
       
    82             }
       
    83         }
       
    84 
       
    85         return result;
    54     }
    86     }
    55 
    87 
    56     /**
    88     /**
    57      * Returns true if e1 and e2 have equal stack traces and their causes
    89      * Returns true if e1 and e2 have equal stack traces and their
    58      * are recursively equal (by the same definition).  Returns false
    90      * causes are recursively equal (by the same definition) and their
    59      * or throws NullPointerExeption otherwise.
    91      * suppressed exception information is equals.  Returns false or
       
    92      * throws NullPointerExeption otherwise.
    60      */
    93      */
    61     private static boolean equal(Throwable t1, Throwable t2) {
    94     private static boolean equal(Throwable t1, Throwable t2) {
    62         return t1==t2 || (Arrays.equals(t1.getStackTrace(), t2.getStackTrace())
    95         return t1==t2 ||
    63                           && equal(t1.getCause(), t2.getCause()));
    96             (Arrays.equals(t1.getStackTrace(), t2.getStackTrace()) &&
       
    97              equal(t1.getCause(), t2.getCause()) &&
       
    98              Objects.equals(t1.getSuppressed(), t2.getSuppressed()));
    64     }
    99     }
    65 
   100 
    66     static void a() throws HighLevelException {
   101     static void a() throws HighLevelException {
    67         try {
   102         try {
    68             b();
   103             b();