test/java/lang/Throwable/SuppressedExceptions.java

Print this page




   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     6911258 6962571
  30  * @summary Basic tests of suppressed exceptions
  31  * @author  Joseph D. Darcy
  32  */
  33 
  34 public class SuppressedExceptions {
  35     private static String message = "Bad suppressed exception information";
  36 
  37     public static void main(String... args) throws Exception {

  38         basicSupressionTest();
  39         serializationTest();
  40         selfReference();
  41     }
  42 










  43     private static void basicSupressionTest() {
  44         Throwable throwable = new Throwable();
  45         RuntimeException suppressed = new RuntimeException("A suppressed exception.");
  46         AssertionError repressed  = new AssertionError("A repressed error.");
  47 
  48         Throwable[] t0 = throwable.getSuppressedExceptions();
  49         if (t0.length != 0) {
  50             throw new RuntimeException(message);
  51         }
  52         throwable.printStackTrace();
  53 
  54         throwable.addSuppressedException(suppressed);
  55         Throwable[] t1 = throwable.getSuppressedExceptions();
  56         if (t1.length != 1 ||
  57             t1[0] != suppressed) {throw new RuntimeException(message);
  58         }
  59         throwable.printStackTrace();
  60 
  61         throwable.addSuppressedException(repressed);
  62         Throwable[] t2 = throwable.getSuppressedExceptions();


 139         Object o = ois.readObject();
 140         Throwable throwable = (Throwable) o;
 141 
 142         System.err.println("TESTING SERIALIZED EXCEPTION");
 143 
 144         Throwable[] t0 = throwable.getSuppressedExceptions();
 145         if (t0.length != 0) { // Will fail if t0 is null.
 146             throw new RuntimeException(message);
 147         }
 148         throwable.printStackTrace();
 149     }
 150 
 151     private static void selfReference() {
 152         Throwable throwable1 = new RuntimeException();
 153         Throwable throwable2 = new AssertionError();
 154         throwable1.initCause(throwable2);
 155         throwable2.initCause(throwable1);
 156 
 157         throwable1.printStackTrace();
 158 
 159 
 160         throwable1.addSuppressedException(throwable1);
 161         throwable1.addSuppressedException(throwable2);

 162 
 163         throwable1.printStackTrace();
 164     }
 165 }


   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     6911258 6962571 6963622
  30  * @summary Basic tests of suppressed exceptions
  31  * @author  Joseph D. Darcy
  32  */
  33 
  34 public class SuppressedExceptions {
  35     private static String message = "Bad suppressed exception information";
  36 
  37     public static void main(String... args) throws Exception {
  38         noSelfSuppression();
  39         basicSupressionTest();
  40         serializationTest();
  41         selfReference();
  42     }
  43 
  44     private static void noSelfSuppression() {
  45         Throwable throwable = new Throwable();
  46         try {
  47             throwable.addSuppressedException(throwable);
  48             throw new RuntimeException("IllegalArgumentException for self-suppresion not thrown.");
  49         } catch (IllegalArgumentException iae) {
  50             ; // Expected
  51         }
  52     }
  53 
  54     private static void basicSupressionTest() {
  55         Throwable throwable = new Throwable();
  56         RuntimeException suppressed = new RuntimeException("A suppressed exception.");
  57         AssertionError repressed  = new AssertionError("A repressed error.");
  58 
  59         Throwable[] t0 = throwable.getSuppressedExceptions();
  60         if (t0.length != 0) {
  61             throw new RuntimeException(message);
  62         }
  63         throwable.printStackTrace();
  64 
  65         throwable.addSuppressedException(suppressed);
  66         Throwable[] t1 = throwable.getSuppressedExceptions();
  67         if (t1.length != 1 ||
  68             t1[0] != suppressed) {throw new RuntimeException(message);
  69         }
  70         throwable.printStackTrace();
  71 
  72         throwable.addSuppressedException(repressed);
  73         Throwable[] t2 = throwable.getSuppressedExceptions();


 150         Object o = ois.readObject();
 151         Throwable throwable = (Throwable) o;
 152 
 153         System.err.println("TESTING SERIALIZED EXCEPTION");
 154 
 155         Throwable[] t0 = throwable.getSuppressedExceptions();
 156         if (t0.length != 0) { // Will fail if t0 is null.
 157             throw new RuntimeException(message);
 158         }
 159         throwable.printStackTrace();
 160     }
 161 
 162     private static void selfReference() {
 163         Throwable throwable1 = new RuntimeException();
 164         Throwable throwable2 = new AssertionError();
 165         throwable1.initCause(throwable2);
 166         throwable2.initCause(throwable1);
 167 
 168         throwable1.printStackTrace();
 169 


 170         throwable1.addSuppressedException(throwable2);
 171         throwable2.addSuppressedException(throwable1);
 172 
 173         throwable1.printStackTrace();
 174     }
 175 }