Home | History | Annotate | Download | only in lang
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 package org.apache.harmony.tests.java.lang;
     18 
     19 import java.io.ByteArrayOutputStream;
     20 import java.io.PrintStream;
     21 import java.io.PrintWriter;
     22 
     23 import junit.framework.TestCase;
     24 
     25 public class ThrowableTest extends TestCase {
     26 
     27     /**
     28      * java.lang.Throwable#Throwable()
     29      */
     30     public void test_Constructor() {
     31         Throwable e = new Throwable();
     32         assertNull(e.getMessage());
     33         assertNull(e.getLocalizedMessage());
     34         assertNull(e.getCause());
     35     }
     36 
     37     /**
     38      * java.lang.Throwable#Throwable(java.lang.String)
     39      */
     40     public void test_ConstructorLjava_lang_String() {
     41         Throwable e = new Throwable("fixture");
     42         assertEquals("fixture", e.getMessage());
     43         assertNull(e.getCause());
     44     }
     45 
     46     /**
     47      * java.lang.Throwable#fillInStackTrace()
     48      */
     49     public void test_fillInStackTrace() {
     50         // Test for method java.lang.Throwable
     51         // java.lang.Throwable.fillInStackTrace()
     52         class Test implements Runnable {
     53             public int x;
     54 
     55             public Test(int x) {
     56                 this.x = x;
     57             }
     58 
     59             public void anotherMethod() {
     60                 if (true)
     61                     throw new IndexOutOfBoundsException();
     62             }
     63 
     64             public void run() {
     65                 if (x == 0)
     66                     throw new IndexOutOfBoundsException();
     67                 try {
     68                     anotherMethod();
     69                 } catch (IndexOutOfBoundsException e) {
     70                     e.fillInStackTrace();
     71                     throw e;
     72                 }
     73             }
     74         }
     75         ByteArrayOutputStream bao = new ByteArrayOutputStream();
     76         PrintStream ps = new PrintStream(bao);
     77         try {
     78             new Test(0).run();
     79         } catch (Throwable e) {
     80             e.printStackTrace(ps);
     81         }
     82         ps.flush();
     83         String s = fixStacktrace(new String(bao.toByteArray(), 0, bao.size()));
     84 
     85         bao.reset();
     86         try {
     87             new Test(1).run();
     88         } catch (Throwable e) {
     89             e.printStackTrace(ps);
     90         }
     91         ps.close();
     92         String s2 = fixStacktrace(new String(bao.toByteArray(), 0, bao.size()));
     93         assertTrue("Invalid stackTrace? length: " + s2.length() + "\n" + s2, s2
     94                 .length() > 300);
     95         assertTrue("Incorrect stackTrace printed: \n" + s2
     96                 + "\n\nCompared with:\n" + s, s2.equals(s));
     97     }
     98 
     99     private String fixStacktrace(String trace) {
    100         // remove linenumbers
    101         StringBuffer sb = new StringBuffer();
    102         int lastIndex = 0;
    103         while (lastIndex < trace.length()) {
    104             int index = trace.indexOf('\n', lastIndex);
    105             if (index == -1)
    106                 index = trace.length();
    107             String line = trace.substring(lastIndex, index);
    108             lastIndex = index + 1;
    109 
    110             index = line.indexOf("(");
    111             if (index > -1) {
    112                 line = line.substring(0, index);
    113             }
    114             // Usually the construction of the exception is removed
    115             // however if running with the JIT, it may not be removed
    116             if (line.indexOf("java.lang.Throwable") > -1)
    117                 continue;
    118             sb.append(line);
    119             sb.append('\n');
    120         }
    121         return sb.toString();
    122     }
    123 
    124     /**
    125      * java.lang.Throwable#printStackTrace()
    126      */
    127     public void test_printStackTrace() {
    128         // Test for method void java.lang.Throwable.printStackTrace()
    129         Throwable x = new ClassNotFoundException("A Test Message");
    130         ByteArrayOutputStream bao = new ByteArrayOutputStream();
    131         PrintStream ps = new PrintStream(bao);
    132         PrintStream err = System.err;
    133         System.setErr(ps);
    134         x.printStackTrace();
    135         System.setErr(err);
    136         ps.close();
    137         String s = new String(bao.toByteArray(), 0, bao.size());
    138         assertTrue("Incorrect stackTrace printed:\n" + s, s != null
    139                 && s.length() > 400);
    140     }
    141 
    142     /**
    143      * java.lang.Throwable#printStackTrace(java.io.PrintStream)
    144      */
    145     public void test_printStackTraceLjava_io_PrintStream() {
    146         // Test for method void
    147         // java.lang.Throwable.printStackTrace(java.io.PrintStream)
    148         ByteArrayOutputStream bao = new ByteArrayOutputStream();
    149         PrintStream ps = new PrintStream(bao);
    150         Throwable x = new java.net.UnknownHostException("A Message");
    151         x.printStackTrace(ps);
    152         ps.close();
    153         String s = new String(bao.toByteArray(), 0, bao.size());
    154         assertTrue("Incorrect stackTrace printed:\n" + s, s != null
    155                 && s.length() > 400);
    156     }
    157 
    158     /**
    159      * java.lang.Throwable#printStackTrace(java.io.PrintWriter)
    160      */
    161     public void test_printStackTraceLjava_io_PrintWriter() {
    162         // Test for method void
    163         // java.lang.Throwable.printStackTrace(java.io.PrintWriter)
    164         // SM
    165         ByteArrayOutputStream bao = new ByteArrayOutputStream();
    166         PrintWriter pw = new PrintWriter(bao);
    167         Throwable x = new java.net.UnknownHostException("A Message");
    168         x.printStackTrace(pw);
    169         pw.close();
    170         String s = new String(bao.toByteArray(), 0, bao.size());
    171         assertTrue("Incorrect stackTrace printed:\n" + s, s != null
    172                 && s.length() > 400);
    173     }
    174 
    175     /**
    176      * java.lang.Throwable#toString()
    177      */
    178     public void test_toString() {
    179         Throwable e = new Throwable("Throw");
    180         assertEquals("java.lang.Throwable: Throw", e.toString());
    181 
    182     }
    183 }
    184