1 8 9 package com.sleepycat.util.test; 10 11 import java.io.IOException ; 12 import java.io.PrintWriter ; 13 import java.io.StringWriter ; 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 27 public 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 DbTestUtil.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 115 public void testStackTrace() { 116 117 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 | Popular Tags |