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 Throwable(); 44 t.setStackTrace(null); // Immutable and empty stack 45 assertEmptyStackTrace(t); 46 47 // Verify fillInStackTrace is now a no-op. 48 t.fillInStackTrace(); 49 assertEmptyStackTrace(t); 50 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 void assertEmptyStackTrace(Throwable t) { 65 if (t.getStackTrace().length != 0) 66 throw new AssertionError("Nonempty stacktrace."); 67 } 68 69 private static void testWithFillInStackTrace() { 70 Throwable original = null; 71 try { 72 a(); 73 } catch(HighLevelException e) { 74 original = e; 75 } 76 77 if (!equal(original, reconstitute(original))) 78 throw new RuntimeException("Unequal Throwables with filled-in stacktrace"); 79 } 80 81 /** 82 * Serialize the argument and return the deserialized result. 83 */ 84 private static Throwable reconstitute(Throwable t) { 85 Throwable result = null; 86 try(ByteArrayOutputStream bout = new ByteArrayOutputStream(); 87 ObjectOutputStream out = new ObjectOutputStream(bout)) { 88 out.writeObject(t); 89 out.flush(); 90 try(ByteArrayInputStream bin = 91 new ByteArrayInputStream(bout.toByteArray()); 92 ObjectInputStream in = new ObjectInputStream(bin)) { 93 result = (Throwable) in.readObject(); 94 } 95 } catch(IOException | ClassNotFoundException e) { 96 throw new RuntimeException(e); 97 } 98 return result; 99 } 100 101 /** 102 * Returns true if e1 and e2 have equal stack traces and their 103 * causes are recursively equal (by the same definition) and their 104 * suppressed exception information is equals. Returns false or 105 * throws NullPointerExeption otherwise. 106 */ 107 private static boolean equal(Throwable t1, Throwable t2) { 108 return t1==t2 || 109 (Arrays.equals(t1.getStackTrace(), t2.getStackTrace()) && 110 equal(t1.getCause(), t2.getCause()) && 111 Objects.equals(t1.getSuppressed(), t2.getSuppressed())); 112 } 113 114 static void a() throws HighLevelException { 115 try { 116 b(); 117 } catch(MidLevelException e) { 118 throw new HighLevelException(e); 119 } 120 } 121 static void b() throws MidLevelException { 122 c(); 123 } 124 static void c() throws MidLevelException { 125 try { 126 d(); 127 } catch(LowLevelException e) { 128 throw new MidLevelException(e); 129 } 130 } 131 static void d() throws LowLevelException { 132 e(); 133 } 134 static void e() throws LowLevelException { 135 throw new LowLevelException(); 136 } 137 138 private static final String OUR_CLASS = StackTraceSerialization.class.getName(); 139 private static final String OUR_FILE_NAME = "StackTraceSerialization.java"; 140 141 private static void check(StackTraceElement e, String methodName, int n) { 142 if (!e.getClassName().equals(OUR_CLASS)) 143 throw new RuntimeException("Class: " + e); 144 if (!e.getMethodName().equals(methodName)) 145 throw new RuntimeException("Method name: " + e); 146 if (!e.getFileName().equals(OUR_FILE_NAME)) 147 throw new RuntimeException("File name: " + e); 148 if (e.getLineNumber() != n) 149 throw new RuntimeException("Line number: " + e); 150 } 151 } 152 153 class HighLevelException extends Exception { 154 HighLevelException(Throwable cause) { super(cause); } 155 } 156 157 class MidLevelException extends Exception { 158 MidLevelException(Throwable cause) { super(cause); } 159 } 160 161 class LowLevelException extends Exception { 162 }