jdk/test/java/lang/Throwable/SuppressedExceptions.java

Print this page

        

*** 24,34 **** import java.io.*; import java.util.*; /* * @test ! * @bug 6911258 6962571 6963622 * @summary Basic tests of suppressed exceptions * @author Joseph D. Darcy */ public class SuppressedExceptions { --- 24,34 ---- import java.io.*; import java.util.*; /* * @test ! * @bug 6911258 6962571 6963622 6991528 * @summary Basic tests of suppressed exceptions * @author Joseph D. Darcy */ public class SuppressedExceptions {
*** 37,52 **** public static void main(String... args) throws Exception { noSelfSuppression(); basicSupressionTest(); serializationTest(); selfReference(); } private static void noSelfSuppression() { Throwable throwable = new Throwable(); try { ! throwable.addSuppressedException(throwable); throw new RuntimeException("IllegalArgumentException for self-suppresion not thrown."); } catch (IllegalArgumentException iae) { ; // Expected } } --- 37,61 ---- public static void main(String... args) throws Exception { noSelfSuppression(); basicSupressionTest(); serializationTest(); selfReference(); + noModification(); } private static void noSelfSuppression() { Throwable throwable = new Throwable(); try { ! throwable.addSuppressed(throwable); ! throw new RuntimeException("IllegalArgumentException for self-suppresion not thrown."); ! } catch (IllegalArgumentException iae) { ! ; // Expected ! } ! ! throwable.addSuppressed(null); // Immutable suppression list ! try { ! throwable.addSuppressed(throwable); throw new RuntimeException("IllegalArgumentException for self-suppresion not thrown."); } catch (IllegalArgumentException iae) { ; // Expected } }
*** 54,78 **** private static void basicSupressionTest() { Throwable throwable = new Throwable(); RuntimeException suppressed = new RuntimeException("A suppressed exception."); AssertionError repressed = new AssertionError("A repressed error."); ! Throwable[] t0 = throwable.getSuppressedExceptions(); if (t0.length != 0) { throw new RuntimeException(message); } throwable.printStackTrace(); ! throwable.addSuppressedException(suppressed); ! Throwable[] t1 = throwable.getSuppressedExceptions(); if (t1.length != 1 || t1[0] != suppressed) {throw new RuntimeException(message); } throwable.printStackTrace(); ! throwable.addSuppressedException(repressed); ! Throwable[] t2 = throwable.getSuppressedExceptions(); if (t2.length != 2 || t2[0] != suppressed || t2[1] != repressed) { throw new RuntimeException(message); } --- 63,87 ---- private static void basicSupressionTest() { Throwable throwable = new Throwable(); RuntimeException suppressed = new RuntimeException("A suppressed exception."); AssertionError repressed = new AssertionError("A repressed error."); ! Throwable[] t0 = throwable.getSuppressed(); if (t0.length != 0) { throw new RuntimeException(message); } throwable.printStackTrace(); ! throwable.addSuppressed(suppressed); ! Throwable[] t1 = throwable.getSuppressed(); if (t1.length != 1 || t1[0] != suppressed) {throw new RuntimeException(message); } throwable.printStackTrace(); ! throwable.addSuppressed(repressed); ! Throwable[] t2 = throwable.getSuppressed(); if (t2.length != 2 || t2[0] != suppressed || t2[1] != repressed) { throw new RuntimeException(message); }
*** 150,160 **** Object o = ois.readObject(); Throwable throwable = (Throwable) o; System.err.println("TESTING SERIALIZED EXCEPTION"); ! Throwable[] t0 = throwable.getSuppressedExceptions(); if (t0.length != 0) { // Will fail if t0 is null. throw new RuntimeException(message); } throwable.printStackTrace(); } --- 159,169 ---- Object o = ois.readObject(); Throwable throwable = (Throwable) o; System.err.println("TESTING SERIALIZED EXCEPTION"); ! Throwable[] t0 = throwable.getSuppressed(); if (t0.length != 0) { // Will fail if t0 is null. throw new RuntimeException(message); } throwable.printStackTrace(); }
*** 165,175 **** throwable1.initCause(throwable2); throwable2.initCause(throwable1); throwable1.printStackTrace(); ! throwable1.addSuppressedException(throwable2); ! throwable2.addSuppressedException(throwable1); throwable1.printStackTrace(); } } --- 174,200 ---- throwable1.initCause(throwable2); throwable2.initCause(throwable1); throwable1.printStackTrace(); ! throwable1.addSuppressed(throwable2); ! throwable2.addSuppressed(throwable1); throwable1.printStackTrace(); } + + private static void noModification() { + Throwable t = new Throwable(); + t.addSuppressed(null); + + Throwable[] t0 = t.getSuppressed(); + if (t0.length != 0) + throw new RuntimeException("Bad nonzero length of suppressed exceptions."); + + t.addSuppressed(new ArithmeticException()); + + // Make sure a suppressed exception did *not* get added. + t0 = t.getSuppressed(); + if (t0.length != 0) + throw new RuntimeException("Bad nonzero length of suppressed exceptions."); + } }