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