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