1 import static org.testng.Assert.assertTrue;
   2 import static org.testng.Assert.assertEquals;
   3 
   4 import org.testng.annotations.Test;
   5 
   6 import java.io.PrintWriter;
   7 import java.io.StringWriter;
   8 
   9 /*
  10  * @test
  11  * @bug 5050783
  12  * @summary Basic test presenting of stack trace information as String
  13  * @author Bulgarian JUG (dalexandrov, ngochev, iivanov, mtoshev, mstoynov)
  14  * @run testng GetStackTraceString
  15  */
  16 public class GetStackTraceString {
  17     @Test
  18     public void basicTest() {
  19         try {
  20             double a = 100 / 0;
  21         } catch (Throwable t) {
  22             assertTrue(t.getStackTraceString().length() > 0);
  23         }
  24     }
  25 
  26     @Test
  27     public void reasonTest() {
  28         try {
  29             double a = 100 / 0;
  30         } catch (Throwable t) {
  31             assertTrue(t.getStackTraceString().contains("java.lang.ArithmeticException: / by zero"));
  32         }
  33     }
  34 
  35     @Test
  36     public void nestedExceptionTest() {
  37         try {
  38             nestedExceptionHelper();
  39         } catch (Throwable t) {
  40             assertTrue(t.getStackTraceString().contains("java.lang.ArithmeticException: / by zero"));
  41             assertTrue(t.getStackTraceString().contains("Nested exception"));
  42         }
  43     }
  44 
  45     @Test
  46     public void printStackTraceComparissonTest() {
  47         try {
  48             nestedExceptionHelper();
  49         } catch (Throwable t) {
  50             StringWriter sw = new StringWriter();
  51             t.printStackTrace(new PrintWriter(sw));
  52             assertEquals(sw.toString(), t.getStackTraceString());
  53         }
  54     }
  55 
  56     private void nestedExceptionHelper() throws Exception {
  57         try {
  58             double a = 100 / 0;
  59         } catch (Throwable t) {
  60             throw new RuntimeException("Nested exception", t);
  61         }
  62     }
  63 }