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 libcore.java.lang;
18
19import junit.framework.TestCase;
20
21public class OldThrowableTest extends TestCase {
22
23    public void test_ConstructorLStringLThrowable() {
24        String message = "Test message";
25        NullPointerException npe = new NullPointerException();
26        Throwable thr = new Throwable(message, npe);
27        assertEquals("message is incorrect.", message, thr.getMessage());
28        assertEquals("cause is incorrect.", npe, thr.getCause());
29
30        thr = new Throwable(null, npe);
31        assertNull("message is not null.", thr.getMessage());
32        assertEquals("cause is incorrect.", npe, thr.getCause());
33
34        thr = new Throwable(message, null);
35        assertEquals("message is incorrect.", message, thr.getMessage());
36        assertNull("cause is not null.", thr.getCause());
37    }
38
39    public void test_ConstructorLThrowable() {
40
41        NullPointerException npe = new NullPointerException();
42        Throwable thr = new Throwable(npe);
43
44        assertEquals("Returned cause is incorrect.", npe, thr.getCause());
45
46        thr = new Throwable((Throwable) null);
47        assertNull("The cause is not null.", thr.getCause());
48    }
49
50    public void test_getLocalizedMessage() {
51        String testMessage = "Test message";
52        Throwable e = new Throwable(testMessage);
53        assertEquals("Returned incorrect localized message.",
54                testMessage, e.getLocalizedMessage());
55
56        TestThrowable tt = new TestThrowable(testMessage);
57        assertEquals("localized message", tt.getLocalizedMessage());
58    }
59
60    class TestThrowable extends Throwable {
61
62        public TestThrowable(String message) {
63            super(message);
64        }
65
66        public String getLocalizedMessage() {
67            return "localized message";
68        }
69    }
70
71    public void test_getStackTrace() {
72        String message = "Test message";
73        NullPointerException npe = new NullPointerException();
74        Throwable thr = new Throwable(message, npe);
75        StackTraceElement[] ste = thr.getStackTrace();
76        assertNotNull("Returned stack trace is empty", ste.length != 0);
77    }
78
79    public void test_initCause() {
80        String message = "Test message";
81        NullPointerException npe = new NullPointerException();
82        IllegalArgumentException iae = new IllegalArgumentException();
83        Throwable thr = new Throwable();
84        thr.initCause(iae);
85        assertEquals("getCause returns incorrect cause.", iae, thr.getCause());
86
87        thr = new Throwable("message");
88        thr.initCause(npe);
89        assertEquals("getCause returns incorrect cause.", npe, thr.getCause());
90
91        thr = new Throwable(message, npe);
92        try {
93            thr.initCause(iae);
94            fail("IllegalStateException was not thrown.");
95        } catch(IllegalStateException ise) {
96            //expected
97        }
98
99        thr = new Throwable(npe);
100        try {
101            thr.initCause(iae);
102            fail("IllegalStateException was not thrown.");
103        } catch(IllegalStateException ise) {
104            //expected
105        }
106
107        thr = new Throwable();
108        try {
109            thr.initCause(thr);
110            fail("IllegalArgumentException was not thrown.");
111        } catch(IllegalArgumentException ise) {
112            //expected
113        }
114    }
115
116    public void test_setStackTrace() {
117        NullPointerException npe = new NullPointerException();
118        Throwable thr = new Throwable(npe);
119        StackTraceElement[] ste = thr.getStackTrace();
120        Throwable thr1 = new Throwable(npe);
121        thr1.setStackTrace(ste);
122        assertEquals(ste.length, thr1.getStackTrace().length);
123
124        try {
125            thr.setStackTrace(null);
126            fail("NullPointerException is not thrown.");
127        } catch(NullPointerException np) {
128            //expected
129        }
130    }
131}
132