test/java/lang/Throwable/SuppressedExceptions.java

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -24,11 +24,11 @@
 import java.io.*;
 import java.util.*;
 
 /*
  * @test
- * @bug     6911258 6962571 6963622 6991528
+ * @bug     6911258 6962571 6963622 6991528 7005628
  * @summary Basic tests of suppressed exceptions
  * @author  Joseph D. Darcy
  */
 
 public class SuppressedExceptions {

@@ -48,18 +48,10 @@
             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
-        }
     }
 
     private static void basicSupressionTest() {
         Throwable throwable = new Throwable();
         RuntimeException suppressed = new RuntimeException("A suppressed exception.");

@@ -151,13 +143,12 @@
             (byte)0x45, (byte)0x6c, (byte)0x65, (byte)0x6d, (byte)0x65, (byte)0x6e, (byte)0x74, (byte)0x3b,
             (byte)0x02, (byte)0x46, (byte)0x2a, (byte)0x3c, (byte)0x3c, (byte)0xfd, (byte)0x22, (byte)0x39,
             (byte)0x02, (byte)0x00, (byte)0x00, (byte)0x78, (byte)0x70,
         };
 
-        ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
-        ObjectInputStream ois = new ObjectInputStream(bais);
-
+        try(ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
+            ObjectInputStream ois = new ObjectInputStream(bais)) {
         Object o = ois.readObject();
         Throwable throwable = (Throwable) o;
 
         System.err.println("TESTING SERIALIZED EXCEPTION");
 

@@ -165,10 +156,11 @@
         if (t0.length != 0) { // Will fail if t0 is null.
             throw new RuntimeException(message);
         }
         throwable.printStackTrace();
     }
+    }
 
     private static void selfReference() {
         Throwable throwable1 = new RuntimeException();
         Throwable throwable2 = new AssertionError();
         throwable1.initCause(throwable2);

@@ -181,12 +173,11 @@
 
         throwable1.printStackTrace();
     }
 
     private static void noModification() {
-        Throwable t = new Throwable();
-        t.addSuppressed(null);
+        Throwable t = new NoSuppression(false);
 
         Throwable[] t0 = t.getSuppressed();
         if (t0.length != 0)
             throw new RuntimeException("Bad nonzero length of suppressed exceptions.");
 

@@ -194,7 +185,26 @@
 
         // 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.");
+
+        Throwable suppressed = new ArithmeticException();
+        t = new NoSuppression(true); // Suppression enabled
+        // Make sure addSuppressed(null) throws an NPE
+        try {
+            t.addSuppressed(null);
+        } catch(NullPointerException e) {
+            ; // Expected
+        }
+        t.addSuppressed(suppressed);
+        t0 = t.getSuppressed();
+        if (t0.length != 1 || t0[0] != suppressed)
+            throw new RuntimeException("Expected suppression did not occur.");
+    }
+
+    private static class NoSuppression extends Throwable {
+        public NoSuppression(boolean enableSuppression) {
+            super("The medium.", null, enableSuppression);
+        }
     }
 }