1 /*
   2  * Copyright (c) 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   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 /*
  25  * @test
  26  * @bug 8146851
  27  * @summary Test exception depths, and code to get stack traces
  28  * @library /testlibrary
  29  * @run main/othervm -XX:MaxJavaStackTraceDepth=1024 TestThrowable
  30  */
  31 
  32 import java.lang.reflect.Method;
  33 import jdk.test.lib.Asserts;
  34 
  35 public class TestThrowable {
  36 
  37   // Inner class that throws a lot of exceptions
  38   static class Thrower {
  39     static int MaxJavaStackTraceDepth = 1024; // as above
  40     int[] depths = {10, 34, 100, 1024, 2042};
  41     int count = 0;
  42 
  43     int getDepth(Throwable t) throws Exception {
  44       for (Method m : Throwable.class.getDeclaredMethods()) {
  45         if (m.getName().equals("getStackTraceDepth")) {
  46           m.setAccessible(true); // it's private
  47           return (int)m.invoke(t);
  48         }
  49       }
  50       return 0;
  51     }
  52 
  53     void callThrow(int depth) {
  54       if (++count < depth) {
  55         callThrow(depth);
  56       } else {
  57         throw new RuntimeException("depth tested " + depth);
  58       }
  59     }
  60     void testThrow() throws Exception {
  61       for (int d : depths) {
  62         try {
  63           count = getDepth(new Throwable());
  64           callThrow(d);
  65         } catch(Exception e) {
  66           e.getStackTrace();
  67           System.out.println(e.getMessage());
  68           int throwableDepth = getDepth(e);
  69           Asserts.assertTrue(throwableDepth == d ||
  70                              (d > MaxJavaStackTraceDepth && throwableDepth == MaxJavaStackTraceDepth),
  71                              "depth should return the correct value: depth tested=" +
  72                              d + " throwableDepth=" + throwableDepth);
  73         }
  74       }
  75     }
  76   }
  77 
  78   public static void main(java.lang.String[] unused) throws Exception {
  79     Thrower t = new Thrower();
  80     t.testThrow();
  81   }
  82 }