1 /* 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. 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 6991528 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 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 { 56 Throwable original = null; 57 try { 58 a(); 59 } catch(HighLevelException e) { 60 original = e; 61 } 62 63 if (!equal(original, reconstitute(original))) 64 throw new Exception("Unequal Throwables with filled-in stacktrace"); 65 } 66 67 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; 86 } 87 88 /** 89 * Returns true if e1 and e2 have equal stack traces and their 90 * causes are recursively equal (by the same definition) and their 91 * suppressed exception information is equals. Returns false or 92 * throws NullPointerExeption otherwise. 93 */ 94 private static boolean equal(Throwable t1, Throwable t2) { 95 return t1==t2 || 96 (Arrays.equals(t1.getStackTrace(), t2.getStackTrace()) && 97 equal(t1.getCause(), t2.getCause()) && 98 Objects.equals(t1.getSuppressed(), t2.getSuppressed())); 99 } 100 101 static void a() throws HighLevelException { 102 try { 103 b(); 104 } catch(MidLevelException e) { | 1 /* 2 * Copyright (c) 2000, 2011, 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 6991528 6998871 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 testWithSetStackTrace(); 37 testWithFillInStackTrace(); 38 } 39 40 private static void testWithSetStackTrace() { 41 StackTraceElement[] stackTrace = {new StackTraceElement("foo", "bar", "baz", -1)}; 42 43 Throwable t = new TestThrowable(true, false); // Immutable and empty stack 44 assertEmptyStackTrace(t); 45 46 // Verify fillInStackTrace is now a no-op. 47 t.fillInStackTrace(); 48 assertEmptyStackTrace(t); 49 50 // Verify setStackTrace is now a no-op. 51 t.setStackTrace(stackTrace); 52 assertEmptyStackTrace(t); 53 54 if (!equal(t, reconstitute(t))) 55 throw new RuntimeException("Unequal Throwables with set stacktrace"); 56 57 Throwable t2 = new Throwable(); 58 t2.setStackTrace(stackTrace); 59 if (!equal(t2, reconstitute(t2))) 60 throw new RuntimeException("Unequal Throwables with set stacktrace"); 61 62 } 63 64 private static class TestThrowable extends Throwable { 65 public TestThrowable(boolean enableSuppression, 66 boolean writableStackTrace) { 67 super("the medium", null, 68 enableSuppression, 69 writableStackTrace); 70 } 71 } 72 73 private static void assertEmptyStackTrace(Throwable t) { 74 if (t.getStackTrace().length != 0) 75 throw new AssertionError("Nonempty stacktrace."); 76 } 77 78 private static void testWithFillInStackTrace() { 79 Throwable original = null; 80 try { 81 a(); 82 } catch(HighLevelException e) { 83 original = e; 84 } 85 86 if (!equal(original, reconstitute(original))) 87 throw new RuntimeException("Unequal Throwables with filled-in stacktrace"); 88 } 89 90 /** 91 * Serialize the argument and return the deserialized result. 92 */ 93 private static Throwable reconstitute(Throwable t) { 94 Throwable result = null; 95 try(ByteArrayOutputStream bout = new ByteArrayOutputStream(); 96 ObjectOutputStream out = new ObjectOutputStream(bout)) { 97 out.writeObject(t); 98 out.flush(); 99 try(ByteArrayInputStream bin = 100 new ByteArrayInputStream(bout.toByteArray()); 101 ObjectInputStream in = new ObjectInputStream(bin)) { 102 result = (Throwable) in.readObject(); 103 } 104 } catch(IOException | ClassNotFoundException e) { 105 throw new RuntimeException(e); 106 } 107 return result; 108 } 109 110 /** 111 * Returns true if e1 and e2 have equal stack traces and their 112 * causes are recursively equal (by the same definition) and their 113 * suppressed exception information is equals. Returns false or 114 * throws NullPointerExeption otherwise. 115 */ 116 private static boolean equal(Throwable t1, Throwable t2) { 117 return t1==t2 || 118 (Arrays.equals(t1.getStackTrace(), t2.getStackTrace()) && 119 equal(t1.getCause(), t2.getCause()) && 120 Objects.equals(t1.getSuppressed(), t2.getSuppressed())); 121 } 122 123 static void a() throws HighLevelException { 124 try { 125 b(); 126 } catch(MidLevelException e) { |