KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > util > test > ExceptionWrapperTest


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: ExceptionWrapperTest.java,v 1.15 2006/10/30 21:14:55 bostic Exp $
7  */

8
9 package com.sleepycat.util.test;
10
11 import java.io.IOException JavaDoc;
12 import java.io.PrintWriter JavaDoc;
13 import java.io.StringWriter JavaDoc;
14
15 import junit.framework.Test;
16 import junit.framework.TestCase;
17 import junit.framework.TestSuite;
18
19 import com.sleepycat.collections.test.DbTestUtil;
20 import com.sleepycat.util.ExceptionUnwrapper;
21 import com.sleepycat.util.IOExceptionWrapper;
22 import com.sleepycat.util.RuntimeExceptionWrapper;
23
24 /**
25  * @author Mark Hayes
26  */

27 public class ExceptionWrapperTest extends TestCase {
28
29     public static void main(String JavaDoc[] args)
30         throws Exception JavaDoc {
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 JavaDoc {
44
45         TestSuite suite = new TestSuite(ExceptionWrapperTest.class);
46         return suite;
47     }
48
49     public ExceptionWrapperTest(String JavaDoc name) {
50
51         super(name);
52     }
53
54     public void setUp() {
55
56         DbTestUtil.printTestName("ExceptionWrapperTest." + getName());
57     }
58
59     public void testIOWrapper()
60         throws Exception JavaDoc {
61
62         try {
63             throw new IOExceptionWrapper(new RuntimeException JavaDoc("msg"));
64         } catch (IOException JavaDoc e) {
65             Exception JavaDoc ee = ExceptionUnwrapper.unwrap(e);
66             assertTrue(ee instanceof RuntimeException JavaDoc);
67             assertEquals("msg", ee.getMessage());
68
69             Throwable JavaDoc t = ExceptionUnwrapper.unwrapAny(e);
70             assertTrue(t instanceof RuntimeException JavaDoc);
71             assertEquals("msg", t.getMessage());
72         }
73     }
74
75     public void testRuntimeWrapper()
76         throws Exception JavaDoc {
77
78         try {
79             throw new RuntimeExceptionWrapper(new IOException JavaDoc("msg"));
80         } catch (RuntimeException JavaDoc e) {
81             Exception JavaDoc ee = ExceptionUnwrapper.unwrap(e);
82             assertTrue(ee instanceof IOException JavaDoc);
83             assertEquals("msg", ee.getMessage());
84
85             Throwable JavaDoc t = ExceptionUnwrapper.unwrapAny(e);
86             assertTrue(t instanceof IOException JavaDoc);
87             assertEquals("msg", t.getMessage());
88         }
89     }
90
91     public void testErrorWrapper()
92         throws Exception JavaDoc {
93
94         try {
95             throw new RuntimeExceptionWrapper(new Error JavaDoc("msg"));
96         } catch (RuntimeException JavaDoc e) {
97             try {
98                 ExceptionUnwrapper.unwrap(e);
99                 fail();
100             } catch (Error JavaDoc ee) {
101                 assertTrue(ee instanceof Error JavaDoc);
102                 assertEquals("msg", ee.getMessage());
103             }
104
105             Throwable JavaDoc t = ExceptionUnwrapper.unwrapAny(e);
106             assertTrue(t instanceof Error JavaDoc);
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 JavaDoc version = System.getProperty("java.version");
119         if (version.startsWith("1.3.")) {
120             return;
121         }
122
123         Exception JavaDoc ex = new Exception JavaDoc("some exception");
124         String JavaDoc causedBy = "Caused by: java.lang.Exception: some exception";
125
126         try {
127             throw new RuntimeExceptionWrapper(ex);
128         } catch (RuntimeException JavaDoc e) {
129             StringWriter JavaDoc sw = new StringWriter JavaDoc();
130             e.printStackTrace(new PrintWriter JavaDoc(sw));
131             String JavaDoc s = sw.toString();
132             assertTrue(s.indexOf(causedBy) != -1);
133         }
134
135         try {
136             throw new IOExceptionWrapper(ex);
137         } catch (IOException JavaDoc e) {
138             StringWriter JavaDoc sw = new StringWriter JavaDoc();
139             e.printStackTrace(new PrintWriter JavaDoc(sw));
140             String JavaDoc s = sw.toString();
141             assertTrue(s.indexOf(causedBy) != -1);
142         }
143     }
144 }
145
Popular Tags