2548N/A/*
6141N/A * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
2548N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2548N/A *
2548N/A * This code is free software; you can redistribute it and/or modify it
2548N/A * under the terms of the GNU General Public License version 2 only, as
2548N/A * published by the Free Software Foundation.
2548N/A *
2548N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2548N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2548N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2548N/A * version 2 for more details (a copy is included in the LICENSE file that
2548N/A * accompanied this code).
2548N/A *
2548N/A * You should have received a copy of the GNU General Public License version
2548N/A * 2 along with this work; if not, write to the Free Software Foundation,
2548N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2548N/A *
2548N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2548N/A * or visit www.oracle.com if you need additional information or have any
2548N/A * questions.
2548N/A */
2548N/A
2548N/Aimport java.io.*;
2548N/Aimport java.util.*;
2548N/A
2548N/A/*
2548N/A * @test
6141N/A * @bug 6911258 6962571 6963622 6991528 7005628 8012044
2548N/A * @summary Basic tests of suppressed exceptions
2548N/A * @author Joseph D. Darcy
2548N/A */
2548N/A
2548N/Apublic class SuppressedExceptions {
2548N/A private static String message = "Bad suppressed exception information";
2548N/A
2548N/A public static void main(String... args) throws Exception {
2607N/A noSelfSuppression();
2548N/A basicSupressionTest();
2548N/A serializationTest();
2548N/A selfReference();
3057N/A noModification();
6141N/A initCausePlumbing();
2548N/A }
2548N/A
2607N/A private static void noSelfSuppression() {
2607N/A Throwable throwable = new Throwable();
2607N/A try {
3057N/A throwable.addSuppressed(throwable);
3057N/A throw new RuntimeException("IllegalArgumentException for self-suppresion not thrown.");
3057N/A } catch (IllegalArgumentException iae) {
6141N/A // Expected to be here
6141N/A if (iae.getCause() != throwable)
6141N/A throw new RuntimeException("Bad cause after self-suppresion.");
3057N/A }
2607N/A }
2607N/A
2548N/A private static void basicSupressionTest() {
2548N/A Throwable throwable = new Throwable();
2548N/A RuntimeException suppressed = new RuntimeException("A suppressed exception.");
2548N/A AssertionError repressed = new AssertionError("A repressed error.");
2548N/A
3057N/A Throwable[] t0 = throwable.getSuppressed();
2548N/A if (t0.length != 0) {
2548N/A throw new RuntimeException(message);
2548N/A }
2548N/A throwable.printStackTrace();
2548N/A
3057N/A throwable.addSuppressed(suppressed);
3057N/A Throwable[] t1 = throwable.getSuppressed();
2548N/A if (t1.length != 1 ||
2548N/A t1[0] != suppressed) {throw new RuntimeException(message);
2548N/A }
2548N/A throwable.printStackTrace();
2548N/A
3057N/A throwable.addSuppressed(repressed);
3057N/A Throwable[] t2 = throwable.getSuppressed();
2548N/A if (t2.length != 2 ||
2548N/A t2[0] != suppressed ||
2548N/A t2[1] != repressed) {
2548N/A throw new RuntimeException(message);
2548N/A }
2548N/A throwable.printStackTrace();
2548N/A }
2548N/A
2548N/A private static void serializationTest() throws Exception {
2548N/A /*
2548N/A * Bytes of the serial form of
2548N/A *
2548N/A * (new Throwable())setStackTrace(new StackTraceElement[0])
2548N/A *
2548N/A * from JDK 6; suppressedException field will be missing and
2548N/A * thus default to null upon deserialization.
2548N/A */
2548N/A byte[] bytes = {
2548N/A (byte)0xac, (byte)0xed, (byte)0x00, (byte)0x05, (byte)0x73, (byte)0x72, (byte)0x00, (byte)0x13,
2548N/A (byte)0x6a, (byte)0x61, (byte)0x76, (byte)0x61, (byte)0x2e, (byte)0x6c, (byte)0x61, (byte)0x6e,
2548N/A (byte)0x67, (byte)0x2e, (byte)0x54, (byte)0x68, (byte)0x72, (byte)0x6f, (byte)0x77, (byte)0x61,
2548N/A (byte)0x62, (byte)0x6c, (byte)0x65, (byte)0xd5, (byte)0xc6, (byte)0x35, (byte)0x27, (byte)0x39,
2548N/A (byte)0x77, (byte)0xb8, (byte)0xcb, (byte)0x03, (byte)0x00, (byte)0x03, (byte)0x4c, (byte)0x00,
2548N/A (byte)0x05, (byte)0x63, (byte)0x61, (byte)0x75, (byte)0x73, (byte)0x65, (byte)0x74, (byte)0x00,
2548N/A (byte)0x15, (byte)0x4c, (byte)0x6a, (byte)0x61, (byte)0x76, (byte)0x61, (byte)0x2f, (byte)0x6c,
2548N/A (byte)0x61, (byte)0x6e, (byte)0x67, (byte)0x2f, (byte)0x54, (byte)0x68, (byte)0x72, (byte)0x6f,
2548N/A (byte)0x77, (byte)0x61, (byte)0x62, (byte)0x6c, (byte)0x65, (byte)0x3b, (byte)0x4c, (byte)0x00,
2548N/A (byte)0x0d, (byte)0x64, (byte)0x65, (byte)0x74, (byte)0x61, (byte)0x69, (byte)0x6c, (byte)0x4d,
2548N/A (byte)0x65, (byte)0x73, (byte)0x73, (byte)0x61, (byte)0x67, (byte)0x65, (byte)0x74, (byte)0x00,
2548N/A (byte)0x12, (byte)0x4c, (byte)0x6a, (byte)0x61, (byte)0x76, (byte)0x61, (byte)0x2f, (byte)0x6c,
2548N/A (byte)0x61, (byte)0x6e, (byte)0x67, (byte)0x2f, (byte)0x53, (byte)0x74, (byte)0x72, (byte)0x69,
2548N/A (byte)0x6e, (byte)0x67, (byte)0x3b, (byte)0x5b, (byte)0x00, (byte)0x0a, (byte)0x73, (byte)0x74,
2548N/A (byte)0x61, (byte)0x63, (byte)0x6b, (byte)0x54, (byte)0x72, (byte)0x61, (byte)0x63, (byte)0x65,
2548N/A (byte)0x74, (byte)0x00, (byte)0x1e, (byte)0x5b, (byte)0x4c, (byte)0x6a, (byte)0x61, (byte)0x76,
2548N/A (byte)0x61, (byte)0x2f, (byte)0x6c, (byte)0x61, (byte)0x6e, (byte)0x67, (byte)0x2f, (byte)0x53,
2548N/A (byte)0x74, (byte)0x61, (byte)0x63, (byte)0x6b, (byte)0x54, (byte)0x72, (byte)0x61, (byte)0x63,
2548N/A (byte)0x65, (byte)0x45, (byte)0x6c, (byte)0x65, (byte)0x6d, (byte)0x65, (byte)0x6e, (byte)0x74,
2548N/A (byte)0x3b, (byte)0x78, (byte)0x70, (byte)0x71, (byte)0x00, (byte)0x7e, (byte)0x00, (byte)0x04,
2548N/A (byte)0x70, (byte)0x75, (byte)0x72, (byte)0x00, (byte)0x1e, (byte)0x5b, (byte)0x4c, (byte)0x6a,
2548N/A (byte)0x61, (byte)0x76, (byte)0x61, (byte)0x2e, (byte)0x6c, (byte)0x61, (byte)0x6e, (byte)0x67,
2548N/A (byte)0x2e, (byte)0x53, (byte)0x74, (byte)0x61, (byte)0x63, (byte)0x6b, (byte)0x54, (byte)0x72,
2548N/A (byte)0x61, (byte)0x63, (byte)0x65, (byte)0x45, (byte)0x6c, (byte)0x65, (byte)0x6d, (byte)0x65,
2548N/A (byte)0x6e, (byte)0x74, (byte)0x3b, (byte)0x02, (byte)0x46, (byte)0x2a, (byte)0x3c, (byte)0x3c,
2548N/A (byte)0xfd, (byte)0x22, (byte)0x39, (byte)0x02, (byte)0x00, (byte)0x00, (byte)0x78, (byte)0x70,
2548N/A (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x78, (byte)0xac, (byte)0xed, (byte)0x00,
2548N/A (byte)0x05, (byte)0x73, (byte)0x72, (byte)0x00, (byte)0x13, (byte)0x6a, (byte)0x61, (byte)0x76,
2548N/A (byte)0x61, (byte)0x2e, (byte)0x6c, (byte)0x61, (byte)0x6e, (byte)0x67, (byte)0x2e, (byte)0x54,
2548N/A (byte)0x68, (byte)0x72, (byte)0x6f, (byte)0x77, (byte)0x61, (byte)0x62, (byte)0x6c, (byte)0x65,
2548N/A (byte)0xd5, (byte)0xc6, (byte)0x35, (byte)0x27, (byte)0x39, (byte)0x77, (byte)0xb8, (byte)0xcb,
2548N/A (byte)0x03, (byte)0x00, (byte)0x03, (byte)0x4c, (byte)0x00, (byte)0x05, (byte)0x63, (byte)0x61,
2548N/A (byte)0x75, (byte)0x73, (byte)0x65, (byte)0x74, (byte)0x00, (byte)0x15, (byte)0x4c, (byte)0x6a,
2548N/A (byte)0x61, (byte)0x76, (byte)0x61, (byte)0x2f, (byte)0x6c, (byte)0x61, (byte)0x6e, (byte)0x67,
2548N/A (byte)0x2f, (byte)0x54, (byte)0x68, (byte)0x72, (byte)0x6f, (byte)0x77, (byte)0x61, (byte)0x62,
2548N/A (byte)0x6c, (byte)0x65, (byte)0x3b, (byte)0x4c, (byte)0x00, (byte)0x0d, (byte)0x64, (byte)0x65,
2548N/A (byte)0x74, (byte)0x61, (byte)0x69, (byte)0x6c, (byte)0x4d, (byte)0x65, (byte)0x73, (byte)0x73,
2548N/A (byte)0x61, (byte)0x67, (byte)0x65, (byte)0x74, (byte)0x00, (byte)0x12, (byte)0x4c, (byte)0x6a,
2548N/A (byte)0x61, (byte)0x76, (byte)0x61, (byte)0x2f, (byte)0x6c, (byte)0x6e, (byte)0x67, (byte)0x3b,
2548N/A (byte)0x61, (byte)0x6e, (byte)0x67, (byte)0x2f, (byte)0x53, (byte)0x74, (byte)0x72, (byte)0x69,
2548N/A (byte)0x5b, (byte)0x00, (byte)0x0a, (byte)0x73, (byte)0x74, (byte)0x61, (byte)0x63, (byte)0x6b,
2548N/A (byte)0x54, (byte)0x72, (byte)0x61, (byte)0x63, (byte)0x65, (byte)0x74, (byte)0x00, (byte)0x1e,
2548N/A (byte)0x5b, (byte)0x4c, (byte)0x6a, (byte)0x61, (byte)0x76, (byte)0x61, (byte)0x2f, (byte)0x6c,
2548N/A (byte)0x61, (byte)0x6e, (byte)0x67, (byte)0x2f, (byte)0x53, (byte)0x74, (byte)0x61, (byte)0x63,
2548N/A (byte)0x6b, (byte)0x54, (byte)0x72, (byte)0x61, (byte)0x63, (byte)0x65, (byte)0x45, (byte)0x6c,
2548N/A (byte)0x65, (byte)0x6d, (byte)0x65, (byte)0x6e, (byte)0x74, (byte)0x3b, (byte)0x78, (byte)0x70,
2548N/A (byte)0x71, (byte)0x00, (byte)0x7e, (byte)0x00, (byte)0x04, (byte)0x70, (byte)0x75, (byte)0x72,
2548N/A (byte)0x00, (byte)0x1e, (byte)0x5b, (byte)0x4c, (byte)0x6a, (byte)0x61, (byte)0x76, (byte)0x61,
2548N/A (byte)0x2e, (byte)0x6c, (byte)0x61, (byte)0x6e, (byte)0x67, (byte)0x2e, (byte)0x53, (byte)0x74,
2548N/A (byte)0x61, (byte)0x63, (byte)0x6b, (byte)0x54, (byte)0x72, (byte)0x61, (byte)0x63, (byte)0x65,
2548N/A (byte)0x45, (byte)0x6c, (byte)0x65, (byte)0x6d, (byte)0x65, (byte)0x6e, (byte)0x74, (byte)0x3b,
2548N/A (byte)0x02, (byte)0x46, (byte)0x2a, (byte)0x3c, (byte)0x3c, (byte)0xfd, (byte)0x22, (byte)0x39,
2548N/A (byte)0x02, (byte)0x00, (byte)0x00, (byte)0x78, (byte)0x70,
2548N/A };
2548N/A
3894N/A try(ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
3894N/A ObjectInputStream ois = new ObjectInputStream(bais)) {
3894N/A Object o = ois.readObject();
3894N/A Throwable throwable = (Throwable) o;
2548N/A
3894N/A System.err.println("TESTING SERIALIZED EXCEPTION");
2548N/A
3894N/A Throwable[] t0 = throwable.getSuppressed();
3894N/A if (t0.length != 0) { // Will fail if t0 is null.
3894N/A throw new RuntimeException(message);
3894N/A }
3894N/A throwable.printStackTrace();
2548N/A }
2548N/A }
2548N/A
2548N/A private static void selfReference() {
2548N/A Throwable throwable1 = new RuntimeException();
2548N/A Throwable throwable2 = new AssertionError();
2548N/A throwable1.initCause(throwable2);
2548N/A throwable2.initCause(throwable1);
2548N/A
2548N/A throwable1.printStackTrace();
2548N/A
3057N/A throwable1.addSuppressed(throwable2);
3057N/A throwable2.addSuppressed(throwable1);
2548N/A
2548N/A throwable1.printStackTrace();
2548N/A }
3057N/A
3057N/A private static void noModification() {
3894N/A Throwable t = new NoSuppression(false);
3057N/A
3057N/A Throwable[] t0 = t.getSuppressed();
3057N/A if (t0.length != 0)
3057N/A throw new RuntimeException("Bad nonzero length of suppressed exceptions.");
3057N/A
3057N/A t.addSuppressed(new ArithmeticException());
3057N/A
3057N/A // Make sure a suppressed exception did *not* get added.
3057N/A t0 = t.getSuppressed();
3057N/A if (t0.length != 0)
3057N/A throw new RuntimeException("Bad nonzero length of suppressed exceptions.");
3894N/A
3894N/A Throwable suppressed = new ArithmeticException();
3894N/A t = new NoSuppression(true); // Suppression enabled
3894N/A // Make sure addSuppressed(null) throws an NPE
3894N/A try {
3894N/A t.addSuppressed(null);
4116N/A throw new RuntimeException("NPE not thrown!");
3894N/A } catch(NullPointerException e) {
3894N/A ; // Expected
3894N/A }
3894N/A t.addSuppressed(suppressed);
3894N/A t0 = t.getSuppressed();
3894N/A if (t0.length != 1 || t0[0] != suppressed)
3894N/A throw new RuntimeException("Expected suppression did not occur.");
3894N/A }
3894N/A
3894N/A private static class NoSuppression extends Throwable {
3894N/A public NoSuppression(boolean enableSuppression) {
4116N/A super("The medium.", null, enableSuppression, true);
3894N/A }
3057N/A }
6141N/A
6141N/A private static void initCausePlumbing() {
6141N/A Throwable t1 = new Throwable();
6141N/A Throwable t2 = new Throwable("message", t1);
6141N/A Throwable t3 = new Throwable();
6141N/A
6141N/A try {
6141N/A t2.initCause(t3);
6141N/A throw new RuntimeException("Shouldn't reach.");
6141N/A } catch (IllegalStateException ise) {
6141N/A if (ise.getCause() != t2)
6141N/A throw new RuntimeException("Unexpected cause in ISE", ise);
6141N/A Throwable[] suppressed = ise.getSuppressed();
6141N/A if (suppressed.length != 0)
6141N/A throw new RuntimeException("Bad suppression in ISE", ise);
6141N/A }
6141N/A
6141N/A try {
6141N/A t2.initCause(null);
6141N/A throw new RuntimeException("Shouldn't reach.");
6141N/A } catch (IllegalStateException ise) {
6141N/A ; // Expected; don't want an NPE.
6141N/A }
6141N/A
6141N/A try {
6141N/A t3.initCause(t3);
6141N/A throw new RuntimeException("Shouldn't reach.");
6141N/A } catch (IllegalArgumentException iae) {
6141N/A if (iae.getCause() != t3)
6141N/A throw new RuntimeException("Unexpected cause in ISE", iae);
6141N/A }
6141N/A }
2548N/A}