1/*-
2 * See the file LICENSE for redistribution information.
3 *
4 * Copyright (c) 2002,2008 Oracle.  All rights reserved.
5 *
6 * $Id: ExceptionWrapperTest.java,v 12.8 2008/02/07 17:12:33 mark Exp $
7 */
8
9package com.sleepycat.util.test;
10
11import java.io.IOException;
12import java.io.PrintWriter;
13import java.io.StringWriter;
14
15import junit.framework.Test;
16import junit.framework.TestCase;
17import junit.framework.TestSuite;
18
19import com.sleepycat.util.ExceptionUnwrapper;
20import com.sleepycat.util.IOExceptionWrapper;
21import com.sleepycat.util.RuntimeExceptionWrapper;
22import com.sleepycat.util.test.SharedTestUtils;
23
24/**
25 * @author Mark Hayes
26 */
27public class ExceptionWrapperTest extends TestCase {
28
29    public static void main(String[] args)
30        throws Exception {
31
32        junit.framework.TestResult tr =
33            junit.textui.TestRunner.run(suite());
34        if (tr.errorCount() > 0 ||
35            tr.failureCount() > 0) {
36            System.exit(1);
37        } else {
38            System.exit(0);
39        }
40    }
41
42    public static Test suite()
43        throws Exception {
44
45        TestSuite suite = new TestSuite(ExceptionWrapperTest.class);
46        return suite;
47    }
48
49    public ExceptionWrapperTest(String name) {
50
51        super(name);
52    }
53
54    public void setUp() {
55
56        SharedTestUtils.printTestName("ExceptionWrapperTest." + getName());
57    }
58
59    public void testIOWrapper()
60        throws Exception {
61
62        try {
63            throw new IOExceptionWrapper(new RuntimeException("msg"));
64        } catch (IOException e) {
65            Exception ee = ExceptionUnwrapper.unwrap(e);
66            assertTrue(ee instanceof RuntimeException);
67            assertEquals("msg", ee.getMessage());
68
69            Throwable t = ExceptionUnwrapper.unwrapAny(e);
70            assertTrue(t instanceof RuntimeException);
71            assertEquals("msg", t.getMessage());
72        }
73    }
74
75    public void testRuntimeWrapper()
76        throws Exception {
77
78        try {
79            throw new RuntimeExceptionWrapper(new IOException("msg"));
80        } catch (RuntimeException e) {
81            Exception ee = ExceptionUnwrapper.unwrap(e);
82            assertTrue(ee instanceof IOException);
83            assertEquals("msg", ee.getMessage());
84
85            Throwable t = ExceptionUnwrapper.unwrapAny(e);
86            assertTrue(t instanceof IOException);
87            assertEquals("msg", t.getMessage());
88        }
89    }
90
91    public void testErrorWrapper()
92        throws Exception {
93
94        try {
95            throw new RuntimeExceptionWrapper(new Error("msg"));
96        } catch (RuntimeException e) {
97            try {
98                ExceptionUnwrapper.unwrap(e);
99                fail();
100            } catch (Error ee) {
101                assertTrue(ee instanceof Error);
102                assertEquals("msg", ee.getMessage());
103            }
104
105            Throwable t = ExceptionUnwrapper.unwrapAny(e);
106            assertTrue(t instanceof Error);
107            assertEquals("msg", t.getMessage());
108        }
109    }
110
111    /**
112     * Generates a stack trace for a nested exception and checks the output
113     * for the nested exception.
114     */
115    public void testStackTrace() {
116
117        /* Nested stack traces are not avilable in Java 1.3. */
118        String version = System.getProperty("java.version");
119        if (version.startsWith("1.3.")) {
120            return;
121        }
122
123        Exception ex = new Exception("some exception");
124        String causedBy = "Caused by: java.lang.Exception: some exception";
125
126        try {
127            throw new RuntimeExceptionWrapper(ex);
128        } catch (RuntimeException e) {
129            StringWriter sw = new StringWriter();
130            e.printStackTrace(new PrintWriter(sw));
131            String s = sw.toString();
132            assertTrue(s.indexOf(causedBy) != -1);
133        }
134
135        try {
136            throw new IOExceptionWrapper(ex);
137        } catch (IOException e) {
138            StringWriter sw = new StringWriter();
139            e.printStackTrace(new PrintWriter(sw));
140            String s = sw.toString();
141            assertTrue(s.indexOf(causedBy) != -1);
142        }
143    }
144}
145