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 // Verify null-handling 55 try { 56 t.setStackTrace(null); 57 throw new RuntimeException("No NPE on a null stack trace."); 58 } catch(NullPointerException npe) { 59 assertEmptyStackTrace(t); 60 } 61 62 try { 63 t.setStackTrace(new StackTraceElement[]{null}); 64 throw new RuntimeException("No NPE on a null stack trace element."); 65 } catch(NullPointerException npe) { 66 assertEmptyStackTrace(t); 67 } 68 69 if (!equal(t, reconstitute(t))) 70 throw new RuntimeException("Unequal Throwables with set stacktrace"); 71 72 Throwable t2 = new Throwable(); 73 t2.setStackTrace(stackTrace); 74 if (!equal(t2, reconstitute(t2))) 75 throw new RuntimeException("Unequal Throwables with set stacktrace"); 76 77 } 78 79 private static class TestThrowable extends Throwable { 80 public TestThrowable(boolean enableSuppression, 81 boolean writableStackTrace) { 82 super("the medium", null, 83 enableSuppression, 84 writableStackTrace); 85 } 86 } 87 88 private static void assertEmptyStackTrace(Throwable t) { 89 if (t.getStackTrace().length != 0) 90 throw new AssertionError("Nonempty stacktrace."); 91 } 92 93 private static void testWithFillInStackTrace() { 94 Throwable original = null; 95 try { 96 a(); 97 } catch(HighLevelException e) { 98 original = e; 99 } 100 101 if (!equal(original, reconstitute(original))) 102 throw new RuntimeException("Unequal Throwables with filled-in stacktrace"); 103 } 104 105 /** 106 * Serialize the argument and return the deserialized result. 107 */ 108 private static Throwable reconstitute(Throwable t) { 109 Throwable result = null; 110 try(ByteArrayOutputStream bout = new ByteArrayOutputStream(); 111 ObjectOutputStream out = new ObjectOutputStream(bout)) { 112 out.writeObject(t); 113 out.flush(); 114 try(ByteArrayInputStream bin = 115 new ByteArrayInputStream(bout.toByteArray()); 116 ObjectInputStream in = new ObjectInputStream(bin)) { 117 result = (Throwable) in.readObject(); 118 } 119 } catch(IOException | ClassNotFoundException e) { 120 throw new RuntimeException(e); 121 } 122 return result; 123 } 124 125 /** 126 * Returns true if e1 and e2 have equal stack traces and their 127 * causes are recursively equal (by the same definition) and their 128 * suppressed exception information is equals. Returns false or 129 * throws NullPointerExeption otherwise. 130 */ 131 private static boolean equal(Throwable t1, Throwable t2) { 132 return t1==t2 || 133 (Arrays.equals(t1.getStackTrace(), t2.getStackTrace()) && 134 equal(t1.getCause(), t2.getCause()) && 135 Objects.equals(t1.getSuppressed(), t2.getSuppressed())); 136 } 137 138 static void a() throws HighLevelException { 139 try { 140 b(); 141 } catch(MidLevelException e) { 142 throw new HighLevelException(e); 143 } 144 } 145 static void b() throws MidLevelException { 146 c(); 147 } 148 static void c() throws MidLevelException { 149 try { 150 d(); 151 } catch(LowLevelException e) { 152 throw new MidLevelException(e); 153 } 154 } 155 static void d() throws LowLevelException { 156 e(); 157 } 158 static void e() throws LowLevelException { 159 throw new LowLevelException(); 160 } 161 162 private static final String OUR_CLASS = StackTraceSerialization.class.getName(); 163 private static final String OUR_FILE_NAME = "StackTraceSerialization.java"; 164 165 private static void check(StackTraceElement e, String methodName, int n) { 166 if (!e.getClassName().equals(OUR_CLASS)) 167 throw new RuntimeException("Class: " + e); 168 if (!e.getMethodName().equals(methodName)) 169 throw new RuntimeException("Method name: " + e); 170 if (!e.getFileName().equals(OUR_FILE_NAME)) 171 throw new RuntimeException("File name: " + e); 172 if (e.getLineNumber() != n) 173 throw new RuntimeException("Line number: " + e); 174 } 175 } 176 177 class HighLevelException extends Exception { 178 HighLevelException(Throwable cause) { super(cause); } 179 } 180 181 class MidLevelException extends Exception { 182 MidLevelException(Throwable cause) { super(cause); } 183 } 184 185 class LowLevelException extends Exception { 186 }