Path: blob/master/test/jdk/java/lang/Throwable/SuppressedExceptions.java
41152 views
/*1* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.*;24import java.util.*;2526/*27* @test28* @bug 6911258 6962571 6963622 6991528 7005628 801204429* @summary Basic tests of suppressed exceptions30* @author Joseph D. Darcy31*/3233public class SuppressedExceptions {34private static String message = "Bad suppressed exception information";3536public static void main(String... args) throws Exception {37noSelfSuppression();38basicSupressionTest();39serializationTest();40selfReference();41noModification();42initCausePlumbing();43}4445private static void noSelfSuppression() {46Throwable throwable = new Throwable();47try {48throwable.addSuppressed(throwable);49throw new RuntimeException("IllegalArgumentException for self-suppresion not thrown.");50} catch (IllegalArgumentException iae) {51// Expected to be here52if (iae.getCause() != throwable)53throw new RuntimeException("Bad cause after self-suppresion.");54}55}5657private static void basicSupressionTest() {58Throwable throwable = new Throwable();59RuntimeException suppressed = new RuntimeException("A suppressed exception.");60AssertionError repressed = new AssertionError("A repressed error.");6162Throwable[] t0 = throwable.getSuppressed();63if (t0.length != 0) {64throw new RuntimeException(message);65}66throwable.printStackTrace();6768throwable.addSuppressed(suppressed);69Throwable[] t1 = throwable.getSuppressed();70if (t1.length != 1 ||71t1[0] != suppressed) {throw new RuntimeException(message);72}73throwable.printStackTrace();7475throwable.addSuppressed(repressed);76Throwable[] t2 = throwable.getSuppressed();77if (t2.length != 2 ||78t2[0] != suppressed ||79t2[1] != repressed) {80throw new RuntimeException(message);81}82throwable.printStackTrace();83}8485private static void serializationTest() throws Exception {86/*87* Bytes of the serial form of88*89* (new Throwable())setStackTrace(new StackTraceElement[0])90*91* from JDK 6; suppressedException field will be missing and92* thus default to null upon deserialization.93*/94byte[] bytes = {95(byte)0xac, (byte)0xed, (byte)0x00, (byte)0x05, (byte)0x73, (byte)0x72, (byte)0x00, (byte)0x13,96(byte)0x6a, (byte)0x61, (byte)0x76, (byte)0x61, (byte)0x2e, (byte)0x6c, (byte)0x61, (byte)0x6e,97(byte)0x67, (byte)0x2e, (byte)0x54, (byte)0x68, (byte)0x72, (byte)0x6f, (byte)0x77, (byte)0x61,98(byte)0x62, (byte)0x6c, (byte)0x65, (byte)0xd5, (byte)0xc6, (byte)0x35, (byte)0x27, (byte)0x39,99(byte)0x77, (byte)0xb8, (byte)0xcb, (byte)0x03, (byte)0x00, (byte)0x03, (byte)0x4c, (byte)0x00,100(byte)0x05, (byte)0x63, (byte)0x61, (byte)0x75, (byte)0x73, (byte)0x65, (byte)0x74, (byte)0x00,101(byte)0x15, (byte)0x4c, (byte)0x6a, (byte)0x61, (byte)0x76, (byte)0x61, (byte)0x2f, (byte)0x6c,102(byte)0x61, (byte)0x6e, (byte)0x67, (byte)0x2f, (byte)0x54, (byte)0x68, (byte)0x72, (byte)0x6f,103(byte)0x77, (byte)0x61, (byte)0x62, (byte)0x6c, (byte)0x65, (byte)0x3b, (byte)0x4c, (byte)0x00,104(byte)0x0d, (byte)0x64, (byte)0x65, (byte)0x74, (byte)0x61, (byte)0x69, (byte)0x6c, (byte)0x4d,105(byte)0x65, (byte)0x73, (byte)0x73, (byte)0x61, (byte)0x67, (byte)0x65, (byte)0x74, (byte)0x00,106(byte)0x12, (byte)0x4c, (byte)0x6a, (byte)0x61, (byte)0x76, (byte)0x61, (byte)0x2f, (byte)0x6c,107(byte)0x61, (byte)0x6e, (byte)0x67, (byte)0x2f, (byte)0x53, (byte)0x74, (byte)0x72, (byte)0x69,108(byte)0x6e, (byte)0x67, (byte)0x3b, (byte)0x5b, (byte)0x00, (byte)0x0a, (byte)0x73, (byte)0x74,109(byte)0x61, (byte)0x63, (byte)0x6b, (byte)0x54, (byte)0x72, (byte)0x61, (byte)0x63, (byte)0x65,110(byte)0x74, (byte)0x00, (byte)0x1e, (byte)0x5b, (byte)0x4c, (byte)0x6a, (byte)0x61, (byte)0x76,111(byte)0x61, (byte)0x2f, (byte)0x6c, (byte)0x61, (byte)0x6e, (byte)0x67, (byte)0x2f, (byte)0x53,112(byte)0x74, (byte)0x61, (byte)0x63, (byte)0x6b, (byte)0x54, (byte)0x72, (byte)0x61, (byte)0x63,113(byte)0x65, (byte)0x45, (byte)0x6c, (byte)0x65, (byte)0x6d, (byte)0x65, (byte)0x6e, (byte)0x74,114(byte)0x3b, (byte)0x78, (byte)0x70, (byte)0x71, (byte)0x00, (byte)0x7e, (byte)0x00, (byte)0x04,115(byte)0x70, (byte)0x75, (byte)0x72, (byte)0x00, (byte)0x1e, (byte)0x5b, (byte)0x4c, (byte)0x6a,116(byte)0x61, (byte)0x76, (byte)0x61, (byte)0x2e, (byte)0x6c, (byte)0x61, (byte)0x6e, (byte)0x67,117(byte)0x2e, (byte)0x53, (byte)0x74, (byte)0x61, (byte)0x63, (byte)0x6b, (byte)0x54, (byte)0x72,118(byte)0x61, (byte)0x63, (byte)0x65, (byte)0x45, (byte)0x6c, (byte)0x65, (byte)0x6d, (byte)0x65,119(byte)0x6e, (byte)0x74, (byte)0x3b, (byte)0x02, (byte)0x46, (byte)0x2a, (byte)0x3c, (byte)0x3c,120(byte)0xfd, (byte)0x22, (byte)0x39, (byte)0x02, (byte)0x00, (byte)0x00, (byte)0x78, (byte)0x70,121(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x78, (byte)0xac, (byte)0xed, (byte)0x00,122(byte)0x05, (byte)0x73, (byte)0x72, (byte)0x00, (byte)0x13, (byte)0x6a, (byte)0x61, (byte)0x76,123(byte)0x61, (byte)0x2e, (byte)0x6c, (byte)0x61, (byte)0x6e, (byte)0x67, (byte)0x2e, (byte)0x54,124(byte)0x68, (byte)0x72, (byte)0x6f, (byte)0x77, (byte)0x61, (byte)0x62, (byte)0x6c, (byte)0x65,125(byte)0xd5, (byte)0xc6, (byte)0x35, (byte)0x27, (byte)0x39, (byte)0x77, (byte)0xb8, (byte)0xcb,126(byte)0x03, (byte)0x00, (byte)0x03, (byte)0x4c, (byte)0x00, (byte)0x05, (byte)0x63, (byte)0x61,127(byte)0x75, (byte)0x73, (byte)0x65, (byte)0x74, (byte)0x00, (byte)0x15, (byte)0x4c, (byte)0x6a,128(byte)0x61, (byte)0x76, (byte)0x61, (byte)0x2f, (byte)0x6c, (byte)0x61, (byte)0x6e, (byte)0x67,129(byte)0x2f, (byte)0x54, (byte)0x68, (byte)0x72, (byte)0x6f, (byte)0x77, (byte)0x61, (byte)0x62,130(byte)0x6c, (byte)0x65, (byte)0x3b, (byte)0x4c, (byte)0x00, (byte)0x0d, (byte)0x64, (byte)0x65,131(byte)0x74, (byte)0x61, (byte)0x69, (byte)0x6c, (byte)0x4d, (byte)0x65, (byte)0x73, (byte)0x73,132(byte)0x61, (byte)0x67, (byte)0x65, (byte)0x74, (byte)0x00, (byte)0x12, (byte)0x4c, (byte)0x6a,133(byte)0x61, (byte)0x76, (byte)0x61, (byte)0x2f, (byte)0x6c, (byte)0x6e, (byte)0x67, (byte)0x3b,134(byte)0x61, (byte)0x6e, (byte)0x67, (byte)0x2f, (byte)0x53, (byte)0x74, (byte)0x72, (byte)0x69,135(byte)0x5b, (byte)0x00, (byte)0x0a, (byte)0x73, (byte)0x74, (byte)0x61, (byte)0x63, (byte)0x6b,136(byte)0x54, (byte)0x72, (byte)0x61, (byte)0x63, (byte)0x65, (byte)0x74, (byte)0x00, (byte)0x1e,137(byte)0x5b, (byte)0x4c, (byte)0x6a, (byte)0x61, (byte)0x76, (byte)0x61, (byte)0x2f, (byte)0x6c,138(byte)0x61, (byte)0x6e, (byte)0x67, (byte)0x2f, (byte)0x53, (byte)0x74, (byte)0x61, (byte)0x63,139(byte)0x6b, (byte)0x54, (byte)0x72, (byte)0x61, (byte)0x63, (byte)0x65, (byte)0x45, (byte)0x6c,140(byte)0x65, (byte)0x6d, (byte)0x65, (byte)0x6e, (byte)0x74, (byte)0x3b, (byte)0x78, (byte)0x70,141(byte)0x71, (byte)0x00, (byte)0x7e, (byte)0x00, (byte)0x04, (byte)0x70, (byte)0x75, (byte)0x72,142(byte)0x00, (byte)0x1e, (byte)0x5b, (byte)0x4c, (byte)0x6a, (byte)0x61, (byte)0x76, (byte)0x61,143(byte)0x2e, (byte)0x6c, (byte)0x61, (byte)0x6e, (byte)0x67, (byte)0x2e, (byte)0x53, (byte)0x74,144(byte)0x61, (byte)0x63, (byte)0x6b, (byte)0x54, (byte)0x72, (byte)0x61, (byte)0x63, (byte)0x65,145(byte)0x45, (byte)0x6c, (byte)0x65, (byte)0x6d, (byte)0x65, (byte)0x6e, (byte)0x74, (byte)0x3b,146(byte)0x02, (byte)0x46, (byte)0x2a, (byte)0x3c, (byte)0x3c, (byte)0xfd, (byte)0x22, (byte)0x39,147(byte)0x02, (byte)0x00, (byte)0x00, (byte)0x78, (byte)0x70,148};149150try(ByteArrayInputStream bais = new ByteArrayInputStream(bytes);151ObjectInputStream ois = new ObjectInputStream(bais)) {152Object o = ois.readObject();153Throwable throwable = (Throwable) o;154155System.err.println("TESTING SERIALIZED EXCEPTION");156157Throwable[] t0 = throwable.getSuppressed();158if (t0.length != 0) { // Will fail if t0 is null.159throw new RuntimeException(message);160}161throwable.printStackTrace();162}163}164165private static void selfReference() {166Throwable throwable1 = new RuntimeException();167Throwable throwable2 = new AssertionError();168throwable1.initCause(throwable2);169throwable2.initCause(throwable1);170171throwable1.printStackTrace();172173throwable1.addSuppressed(throwable2);174throwable2.addSuppressed(throwable1);175176throwable1.printStackTrace();177}178179private static void noModification() {180Throwable t = new NoSuppression(false);181182Throwable[] t0 = t.getSuppressed();183if (t0.length != 0)184throw new RuntimeException("Bad nonzero length of suppressed exceptions.");185186t.addSuppressed(new ArithmeticException());187188// Make sure a suppressed exception did *not* get added.189t0 = t.getSuppressed();190if (t0.length != 0)191throw new RuntimeException("Bad nonzero length of suppressed exceptions.");192193Throwable suppressed = new ArithmeticException();194t = new NoSuppression(true); // Suppression enabled195// Make sure addSuppressed(null) throws an NPE196try {197t.addSuppressed(null);198throw new RuntimeException("NPE not thrown!");199} catch(NullPointerException e) {200; // Expected201}202t.addSuppressed(suppressed);203t0 = t.getSuppressed();204if (t0.length != 1 || t0[0] != suppressed)205throw new RuntimeException("Expected suppression did not occur.");206}207208private static class NoSuppression extends Throwable {209public NoSuppression(boolean enableSuppression) {210super("The medium.", null, enableSuppression, true);211}212}213214private static void initCausePlumbing() {215Throwable t1 = new Throwable();216Throwable t2 = new Throwable("message", t1);217Throwable t3 = new Throwable();218219try {220t2.initCause(t3);221throw new RuntimeException("Shouldn't reach.");222} catch (IllegalStateException ise) {223if (ise.getCause() != t2)224throw new RuntimeException("Unexpected cause in ISE", ise);225Throwable[] suppressed = ise.getSuppressed();226if (suppressed.length != 0)227throw new RuntimeException("Bad suppression in ISE", ise);228}229230try {231t2.initCause(null);232throw new RuntimeException("Shouldn't reach.");233} catch (IllegalStateException ise) {234; // Expected; don't want an NPE.235}236237try {238t3.initCause(t3);239throw new RuntimeException("Shouldn't reach.");240} catch (IllegalArgumentException iae) {241if (iae.getCause() != t3)242throw new RuntimeException("Unexpected cause in ISE", iae);243}244}245}246247248