Main Page | Class Hierarchy | Data Structures | Directories | File List | Data Fields | Related Pages

ExceptionWrapperTest.java

00001 /*-
00002  * See the file LICENSE for redistribution information.
00003  *
00004  * Copyright (c) 2002-2005
00005  *      Sleepycat Software.  All rights reserved.
00006  *
00007  * $Id: ExceptionWrapperTest.java,v 12.2 2005/08/01 20:25:35 mark Exp $
00008  */
00009 
00010 package com.sleepycat.util.test;
00011 
00012 import java.io.IOException;
00013 import java.io.PrintWriter;
00014 import java.io.StringWriter;
00015 
00016 import junit.framework.Test;
00017 import junit.framework.TestCase;
00018 import junit.framework.TestSuite;
00019 
00020 import com.sleepycat.collections.test.DbTestUtil;
00021 import com.sleepycat.util.ExceptionUnwrapper;
00022 import com.sleepycat.util.IOExceptionWrapper;
00023 import com.sleepycat.util.RuntimeExceptionWrapper;
00024 
00028 public class ExceptionWrapperTest extends TestCase {
00029 
00030     public static void main(String[] args)
00031         throws Exception {
00032 
00033         junit.framework.TestResult tr =
00034             junit.textui.TestRunner.run(suite());
00035         if (tr.errorCount() > 0 ||
00036             tr.failureCount() > 0) {
00037             System.exit(1);
00038         } else {
00039             System.exit(0);
00040         }
00041     }
00042 
00043     public static Test suite()
00044         throws Exception {
00045 
00046         TestSuite suite = new TestSuite(ExceptionWrapperTest.class);
00047         return suite;
00048     }
00049 
00050     public ExceptionWrapperTest(String name) {
00051 
00052         super(name);
00053     }
00054 
00055     public void setUp() {
00056 
00057         DbTestUtil.printTestName("ExceptionWrapperTest." + getName());
00058     }
00059 
00060     public void testIOWrapper()
00061         throws Exception {
00062 
00063         try {
00064             throw new IOExceptionWrapper(new RuntimeException("msg"));
00065         } catch (IOException e) {
00066             Exception ee = ExceptionUnwrapper.unwrap(e);
00067             assertTrue(ee instanceof RuntimeException);
00068             assertEquals("msg", ee.getMessage());
00069 
00070             Throwable t = ExceptionUnwrapper.unwrapAny(e);
00071             assertTrue(t instanceof RuntimeException);
00072             assertEquals("msg", t.getMessage());
00073         }
00074     }
00075 
00076     public void testRuntimeWrapper()
00077         throws Exception {
00078 
00079         try {
00080             throw new RuntimeExceptionWrapper(new IOException("msg"));
00081         } catch (RuntimeException e) {
00082             Exception ee = ExceptionUnwrapper.unwrap(e);
00083             assertTrue(ee instanceof IOException);
00084             assertEquals("msg", ee.getMessage());
00085 
00086             Throwable t = ExceptionUnwrapper.unwrapAny(e);
00087             assertTrue(t instanceof IOException);
00088             assertEquals("msg", t.getMessage());
00089         }
00090     }
00091 
00092     public void testErrorWrapper()
00093         throws Exception {
00094 
00095         try {
00096             throw new RuntimeExceptionWrapper(new Error("msg"));
00097         } catch (RuntimeException e) {
00098             try {
00099                 ExceptionUnwrapper.unwrap(e);
00100                 fail();
00101             } catch (Error ee) {
00102                 assertTrue(ee instanceof Error);
00103                 assertEquals("msg", ee.getMessage());
00104             }
00105 
00106             Throwable t = ExceptionUnwrapper.unwrapAny(e);
00107             assertTrue(t instanceof Error);
00108             assertEquals("msg", t.getMessage());
00109         }
00110     }
00111 
00116     public void testStackTrace() {
00117 
00118         /* Nested stack traces are not avilable in Java 1.3. */
00119         String version = System.getProperty("java.version");
00120         if (version.startsWith("1.3.")) {
00121             return;
00122         }
00123 
00124         Exception ex = new Exception("some exception");
00125         String causedBy = "Caused by: java.lang.Exception: some exception";
00126 
00127         try {
00128             throw new RuntimeExceptionWrapper(ex);
00129         } catch (RuntimeException e) {
00130             StringWriter sw = new StringWriter();
00131             e.printStackTrace(new PrintWriter(sw));
00132             String s = sw.toString();
00133             assertTrue(s.indexOf(causedBy) != -1);
00134         }
00135 
00136         try {
00137             throw new IOExceptionWrapper(ex);
00138         } catch (IOException e) {
00139             StringWriter sw = new StringWriter();
00140             e.printStackTrace(new PrintWriter(sw));
00141             String s = sw.toString();
00142             assertTrue(s.indexOf(causedBy) != -1);
00143         }
00144     }
00145 }

Generated on Sun Dec 25 12:14:56 2005 for Berkeley DB 4.4.16 by  doxygen 1.4.2