001: /*-
002: * See the file LICENSE for redistribution information.
003: *
004: * Copyright (c) 2002,2008 Oracle. All rights reserved.
005: *
006: * $Id: ExceptionWrapperTest.java,v 1.15.2.2 2008/01/07 15:14:36 cwl Exp $
007: */
008:
009: package com.sleepycat.util.test;
010:
011: import java.io.IOException;
012: import java.io.PrintWriter;
013: import java.io.StringWriter;
014:
015: import junit.framework.Test;
016: import junit.framework.TestCase;
017: import junit.framework.TestSuite;
018:
019: import com.sleepycat.collections.test.DbTestUtil;
020: import com.sleepycat.util.ExceptionUnwrapper;
021: import com.sleepycat.util.IOExceptionWrapper;
022: import com.sleepycat.util.RuntimeExceptionWrapper;
023:
024: /**
025: * @author Mark Hayes
026: */
027: public class ExceptionWrapperTest extends TestCase {
028:
029: public static void main(String[] args) throws Exception {
030:
031: junit.framework.TestResult tr = junit.textui.TestRunner
032: .run(suite());
033: if (tr.errorCount() > 0 || tr.failureCount() > 0) {
034: System.exit(1);
035: } else {
036: System.exit(0);
037: }
038: }
039:
040: public static Test suite() throws Exception {
041:
042: TestSuite suite = new TestSuite(ExceptionWrapperTest.class);
043: return suite;
044: }
045:
046: public ExceptionWrapperTest(String name) {
047:
048: super (name);
049: }
050:
051: public void setUp() {
052:
053: DbTestUtil.printTestName("ExceptionWrapperTest." + getName());
054: }
055:
056: public void testIOWrapper() throws Exception {
057:
058: try {
059: throw new IOExceptionWrapper(new RuntimeException("msg"));
060: } catch (IOException e) {
061: Exception ee = ExceptionUnwrapper.unwrap(e);
062: assertTrue(ee instanceof RuntimeException);
063: assertEquals("msg", ee.getMessage());
064:
065: Throwable t = ExceptionUnwrapper.unwrapAny(e);
066: assertTrue(t instanceof RuntimeException);
067: assertEquals("msg", t.getMessage());
068: }
069: }
070:
071: public void testRuntimeWrapper() throws Exception {
072:
073: try {
074: throw new RuntimeExceptionWrapper(new IOException("msg"));
075: } catch (RuntimeException e) {
076: Exception ee = ExceptionUnwrapper.unwrap(e);
077: assertTrue(ee instanceof IOException);
078: assertEquals("msg", ee.getMessage());
079:
080: Throwable t = ExceptionUnwrapper.unwrapAny(e);
081: assertTrue(t instanceof IOException);
082: assertEquals("msg", t.getMessage());
083: }
084: }
085:
086: public void testErrorWrapper() throws Exception {
087:
088: try {
089: throw new RuntimeExceptionWrapper(new Error("msg"));
090: } catch (RuntimeException e) {
091: try {
092: ExceptionUnwrapper.unwrap(e);
093: fail();
094: } catch (Error ee) {
095: assertTrue(ee instanceof Error);
096: assertEquals("msg", ee.getMessage());
097: }
098:
099: Throwable t = ExceptionUnwrapper.unwrapAny(e);
100: assertTrue(t instanceof Error);
101: assertEquals("msg", t.getMessage());
102: }
103: }
104:
105: /**
106: * Generates a stack trace for a nested exception and checks the output
107: * for the nested exception.
108: */
109: public void testStackTrace() {
110:
111: /* Nested stack traces are not avilable in Java 1.3. */
112: String version = System.getProperty("java.version");
113: if (version.startsWith("1.3.")) {
114: return;
115: }
116:
117: Exception ex = new Exception("some exception");
118: String causedBy = "Caused by: java.lang.Exception: some exception";
119:
120: try {
121: throw new RuntimeExceptionWrapper(ex);
122: } catch (RuntimeException e) {
123: StringWriter sw = new StringWriter();
124: e.printStackTrace(new PrintWriter(sw));
125: String s = sw.toString();
126: assertTrue(s.indexOf(causedBy) != -1);
127: }
128:
129: try {
130: throw new IOExceptionWrapper(ex);
131: } catch (IOException e) {
132: StringWriter sw = new StringWriter();
133: e.printStackTrace(new PrintWriter(sw));
134: String s = sw.toString();
135: assertTrue(s.indexOf(causedBy) != -1);
136: }
137: }
138: }
|