ThrowableTest.java revision 89c1feb0a69a7707b271086e749975b3f7acacf7
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 */ 17package org.apache.harmony.luni.tests.java.lang; 18 19import dalvik.annotation.TestInfo; 20import dalvik.annotation.TestLevel; 21import dalvik.annotation.TestTarget; 22import dalvik.annotation.TestTargetClass; 23 24import junit.framework.TestCase; 25 26import java.io.ByteArrayOutputStream; 27import java.io.PrintStream; 28import java.io.PrintWriter; 29 30@TestTargetClass(Throwable.class) 31public class ThrowableTest extends TestCase { 32 33 /** 34 * @tests java.lang.Throwable#Throwable() 35 */ 36 @TestInfo( 37 level = TestLevel.COMPLETE, 38 purpose = "", 39 targets = { 40 @TestTarget( 41 methodName = "Throwable", 42 methodArgs = {} 43 ) 44 }) 45 public void test_Constructor() { 46 Throwable e = new Throwable(); 47 assertNull(e.getMessage()); 48 assertNull(e.getLocalizedMessage()); 49 assertNull(e.getCause()); 50 } 51 52 /** 53 * @tests java.lang.Throwable#Throwable(java.lang.String) 54 */ 55 @TestInfo( 56 level = TestLevel.COMPLETE, 57 purpose = "", 58 targets = { 59 @TestTarget( 60 methodName = "Throwable", 61 methodArgs = {java.lang.String.class} 62 ) 63 }) 64 public void test_ConstructorLjava_lang_String() { 65 Throwable e = new Throwable("fixture"); 66 assertEquals("fixture", e.getMessage()); 67 assertNull(e.getCause()); 68 } 69 70 /** 71 * @tests java.lang.Throwable#fillInStackTrace() 72 */ 73 @TestInfo( 74 level = TestLevel.COMPLETE, 75 purpose = "", 76 targets = { 77 @TestTarget( 78 methodName = "fillInStackTrace", 79 methodArgs = {} 80 ) 81 }) 82 public void test_fillInStackTrace() { 83 // Test for method java.lang.Throwable 84 // java.lang.Throwable.fillInStackTrace() 85 class Test implements Runnable { 86 public int x; 87 88 public Test(int x) { 89 this.x = x; 90 } 91 92 public void anotherMethod() { 93 if (true) 94 throw new IndexOutOfBoundsException(); 95 } 96 97 public void run() { 98 if (x == 0) 99 throw new IndexOutOfBoundsException(); 100 try { 101 anotherMethod(); 102 } catch (IndexOutOfBoundsException e) { 103 e.fillInStackTrace(); 104 throw e; 105 } 106 } 107 } 108 ByteArrayOutputStream bao = new ByteArrayOutputStream(); 109 PrintStream ps = new PrintStream(bao); 110 try { 111 new Test(0).run(); 112 } catch (Throwable e) { 113 e.printStackTrace(ps); 114 } 115 ps.flush(); 116 String s = fixStacktrace(new String(bao.toByteArray(), 0, bao.size())); 117 118 bao.reset(); 119 try { 120 new Test(1).run(); 121 } catch (Throwable e) { 122 e.printStackTrace(ps); 123 } 124 ps.close(); 125 String s2 = fixStacktrace(new String(bao.toByteArray(), 0, bao.size())); 126 assertTrue("Invalid stackTrace? length: " + s2.length() + "\n" + s2, s2 127 .length() > 300); 128 assertTrue("Incorrect stackTrace printed: \n" + s2 129 + "\n\nCompared with:\n" + s, s2.equals(s)); 130 } 131 132 private String fixStacktrace(String trace) { 133 // remove linenumbers 134 StringBuffer sb = new StringBuffer(); 135 int lastIndex = 0; 136 while (lastIndex < trace.length()) { 137 int index = trace.indexOf('\n', lastIndex); 138 if (index == -1) 139 index = trace.length(); 140 String line = trace.substring(lastIndex, index); 141 lastIndex = index + 1; 142 143 index = line.indexOf("("); 144 if (index > -1) { 145 line = line.substring(0, index); 146 } 147 // Usually the construction of the exception is removed 148 // however if running with the JIT, it may not be removed 149 if (line.indexOf("java.lang.Throwable") > -1) 150 continue; 151 sb.append(line); 152 sb.append('\n'); 153 } 154 return sb.toString(); 155 } 156 157 /** 158 * @tests java.lang.Throwable#printStackTrace() 159 */ 160 @TestInfo( 161 level = TestLevel.COMPLETE, 162 purpose = "", 163 targets = { 164 @TestTarget( 165 methodName = "printStackTrace", 166 methodArgs = {} 167 ) 168 }) 169 public void test_printStackTrace() { 170 // Test for method void java.lang.Throwable.printStackTrace() 171 Throwable x = new ClassNotFoundException("A Test Message"); 172 ByteArrayOutputStream bao = new ByteArrayOutputStream(); 173 PrintStream ps = new PrintStream(bao); 174 PrintStream err = System.err; 175 System.setErr(ps); 176 x.printStackTrace(); 177 System.setErr(err); 178 ps.close(); 179 String s = new String(bao.toByteArray(), 0, bao.size()); 180 assertTrue("Incorrect stackTrace printed:\n" + s, s != null 181 && s.length() > 400); 182 } 183 184 /** 185 * @tests java.lang.Throwable#printStackTrace(java.io.PrintStream) 186 */ 187 @TestInfo( 188 level = TestLevel.COMPLETE, 189 purpose = "", 190 targets = { 191 @TestTarget( 192 methodName = "printStackTrace", 193 methodArgs = {java.io.PrintStream.class} 194 ) 195 }) 196 public void test_printStackTraceLjava_io_PrintStream() { 197 // Test for method void 198 // java.lang.Throwable.printStackTrace(java.io.PrintStream) 199 ByteArrayOutputStream bao = new ByteArrayOutputStream(); 200 PrintStream ps = new PrintStream(bao); 201 Throwable x = new java.net.UnknownHostException("A Message"); 202 x.printStackTrace(ps); 203 ps.close(); 204 String s = new String(bao.toByteArray(), 0, bao.size()); 205 assertTrue("Incorrect stackTrace printed:\n" + s, s != null 206 && s.length() > 400); 207 } 208 209 /** 210 * @tests java.lang.Throwable#printStackTrace(java.io.PrintWriter) 211 */ 212 @TestInfo( 213 level = TestLevel.COMPLETE, 214 purpose = "", 215 targets = { 216 @TestTarget( 217 methodName = "printStackTrace", 218 methodArgs = {java.io.PrintWriter.class} 219 ) 220 }) 221 public void test_printStackTraceLjava_io_PrintWriter() { 222 // Test for method void 223 // java.lang.Throwable.printStackTrace(java.io.PrintWriter) 224 // SM 225 ByteArrayOutputStream bao = new ByteArrayOutputStream(); 226 PrintWriter pw = new PrintWriter(bao); 227 Throwable x = new java.net.UnknownHostException("A Message"); 228 x.printStackTrace(pw); 229 pw.close(); 230 String s = new String(bao.toByteArray(), 0, bao.size()); 231 assertTrue("Incorrect stackTrace printed:\n" + s, s != null 232 && s.length() > 400); 233 } 234 235 /** 236 * @tests java.lang.Throwable#toString() 237 */ 238 @TestInfo( 239 level = TestLevel.COMPLETE, 240 purpose = "", 241 targets = { 242 @TestTarget( 243 methodName = "toString", 244 methodArgs = {} 245 ) 246 }) 247 public void test_toString() { 248 Throwable e = new Throwable("Throw"); 249 assertEquals("java.lang.Throwable: Throw", e.toString()); 250 251 } 252} 253