001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.harmony.luni.tests.java.lang;
018:
019: import java.io.ByteArrayOutputStream;
020: import java.io.PrintStream;
021: import java.io.PrintWriter;
022:
023: import junit.framework.TestCase;
024:
025: public class ThrowableTest extends TestCase {
026:
027: /**
028: * @tests java.lang.Throwable#Throwable()
029: */
030: public void test_Constructor() {
031: Throwable e = new Throwable();
032: assertNull(e.getMessage());
033: assertNull(e.getLocalizedMessage());
034: assertNull(e.getCause());
035: }
036:
037: /**
038: * @tests java.lang.Throwable#Throwable(java.lang.String)
039: */
040: public void test_ConstructorLjava_lang_String() {
041: Throwable e = new Throwable("fixture");
042: assertEquals("fixture", e.getMessage());
043: assertNull(e.getCause());
044: }
045:
046: /**
047: * @tests java.lang.Throwable#fillInStackTrace()
048: */
049: public void test_fillInStackTrace() {
050: // Test for method java.lang.Throwable
051: // java.lang.Throwable.fillInStackTrace()
052: class Test implements Runnable {
053: public int x;
054:
055: public Test(int x) {
056: this .x = x;
057: }
058:
059: public void anotherMethod() {
060: if (true)
061: throw new IndexOutOfBoundsException();
062: }
063:
064: public void run() {
065: if (x == 0)
066: throw new IndexOutOfBoundsException();
067: try {
068: anotherMethod();
069: } catch (IndexOutOfBoundsException e) {
070: e.fillInStackTrace();
071: throw e;
072: }
073: }
074: }
075: ByteArrayOutputStream bao = new ByteArrayOutputStream();
076: PrintStream ps = new PrintStream(bao);
077: try {
078: new Test(0).run();
079: } catch (Throwable e) {
080: e.printStackTrace(ps);
081: }
082: ps.flush();
083: String s = fixStacktrace(new String(bao.toByteArray(), 0, bao
084: .size()));
085:
086: bao.reset();
087: try {
088: new Test(1).run();
089: } catch (Throwable e) {
090: e.printStackTrace(ps);
091: }
092: ps.close();
093: String s2 = fixStacktrace(new String(bao.toByteArray(), 0, bao
094: .size()));
095: assertTrue("Invalid stackTrace? length: " + s2.length() + "\n"
096: + s2, s2.length() > 300);
097: assertTrue("Incorrect stackTrace printed: \n" + s2
098: + "\n\nCompared with:\n" + s, s2.equals(s));
099: }
100:
101: private String fixStacktrace(String trace) {
102: // remove linenumbers
103: StringBuffer sb = new StringBuffer();
104: int lastIndex = 0;
105: while (lastIndex < trace.length()) {
106: int index = trace.indexOf('\n', lastIndex);
107: if (index == -1)
108: index = trace.length();
109: String line = trace.substring(lastIndex, index);
110: lastIndex = index + 1;
111:
112: index = line.indexOf("(");
113: if (index > -1) {
114: line = line.substring(0, index);
115: }
116: // Usually the construction of the exception is removed
117: // however if running with the JIT, it may not be removed
118: if (line.indexOf("java.lang.Throwable") > -1)
119: continue;
120: sb.append(line);
121: sb.append('\n');
122: }
123: return sb.toString();
124: }
125:
126: /**
127: * @tests java.lang.Throwable#printStackTrace()
128: */
129: public void test_printStackTrace() {
130: // Test for method void java.lang.Throwable.printStackTrace()
131: Throwable x = new ClassNotFoundException("A Test Message");
132: ByteArrayOutputStream bao = new ByteArrayOutputStream();
133: PrintStream ps = new PrintStream(bao);
134: PrintStream err = System.err;
135: System.setErr(ps);
136: x.printStackTrace();
137: System.setErr(err);
138: ps.close();
139: String s = new String(bao.toByteArray(), 0, bao.size());
140: assertTrue("Incorrect stackTrace printed:\n" + s, s != null
141: && s.length() > 400);
142: }
143:
144: /**
145: * @tests java.lang.Throwable#printStackTrace(java.io.PrintStream)
146: */
147: public void test_printStackTraceLjava_io_PrintStream() {
148: // Test for method void
149: // java.lang.Throwable.printStackTrace(java.io.PrintStream)
150: ByteArrayOutputStream bao = new ByteArrayOutputStream();
151: PrintStream ps = new PrintStream(bao);
152: Throwable x = new java.net.UnknownHostException("A Message");
153: x.printStackTrace(ps);
154: ps.close();
155: String s = new String(bao.toByteArray(), 0, bao.size());
156: assertTrue("Incorrect stackTrace printed:\n" + s, s != null
157: && s.length() > 400);
158: }
159:
160: /**
161: * @tests java.lang.Throwable#printStackTrace(java.io.PrintWriter)
162: */
163: public void test_printStackTraceLjava_io_PrintWriter() {
164: // Test for method void
165: // java.lang.Throwable.printStackTrace(java.io.PrintWriter)
166: // SM
167: ByteArrayOutputStream bao = new ByteArrayOutputStream();
168: PrintWriter pw = new PrintWriter(bao);
169: Throwable x = new java.net.UnknownHostException("A Message");
170: x.printStackTrace(pw);
171: pw.close();
172: String s = new String(bao.toByteArray(), 0, bao.size());
173: assertTrue("Incorrect stackTrace printed:\n" + s, s != null
174: && s.length() > 400);
175: }
176:
177: /**
178: * @tests java.lang.Throwable#toString()
179: */
180: public void test_toString() {
181: Throwable e = new Throwable("Throw");
182: assertEquals("java.lang.Throwable: Throw", e.toString());
183:
184: }
185: }
|