1 16 17 package org.springframework.core; 18 19 import java.io.ByteArrayOutputStream ; 20 import java.io.PrintWriter ; 21 22 import javax.servlet.ServletException ; 23 24 import junit.framework.TestCase; 25 26 30 public class NestedExceptionTests extends TestCase { 31 32 public void testNestedRuntimeExceptionWithNoRootCause() { 33 String mesg = "mesg of mine"; 34 NestedRuntimeException nex = new NestedRuntimeException(mesg) {}; 36 assertNull(nex.getCause()); 37 assertEquals(nex.getMessage(), mesg); 38 39 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 41 PrintWriter pw = new PrintWriter (baos); 42 nex.printStackTrace(pw); 43 pw.flush(); 44 String stackTrace = new String (baos.toByteArray()); 45 assertFalse(stackTrace.indexOf(mesg) == -1); 46 } 47 48 public void testNestedRuntimeExceptionWithRootCause() { 49 String myMessage = "mesg for this exception"; 50 String rootCauseMesg = "this is the obscure message of the root cause"; 51 ServletException rootCause = new ServletException (rootCauseMesg); 52 NestedRuntimeException nex = new NestedRuntimeException(myMessage, rootCause) {}; 54 assertEquals(nex.getCause(), rootCause); 55 assertTrue(nex.getMessage().indexOf(myMessage) != -1); 56 assertTrue(nex.getMessage().indexOf(rootCauseMesg) != -1); 57 58 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 60 PrintWriter pw = new PrintWriter (baos); 61 nex.printStackTrace(pw); 62 pw.flush(); 63 String stackTrace = new String (baos.toByteArray()); 64 assertFalse(stackTrace.indexOf(rootCause.getClass().getName()) == -1); 65 assertFalse(stackTrace.indexOf(rootCauseMesg) == -1); 66 } 67 68 public void testNestedCheckedExceptionWithNoRootCause() { 69 String mesg = "mesg of mine"; 70 NestedCheckedException nex = new NestedCheckedException(mesg) {}; 72 assertNull(nex.getCause()); 73 assertEquals(nex.getMessage(), mesg); 74 75 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 77 PrintWriter pw = new PrintWriter (baos); 78 nex.printStackTrace(pw); 79 pw.flush(); 80 String stackTrace = new String (baos.toByteArray()); 81 assertFalse(stackTrace.indexOf(mesg) == -1); 82 } 83 84 public void testNestedCheckedExceptionWithRootCause() { 85 String myMessage = "mesg for this exception"; 86 String rootCauseMesg = "this is the obscure message of the root cause"; 87 ServletException rootCause = new ServletException (rootCauseMesg); 88 NestedCheckedException nex = new NestedCheckedException(myMessage, rootCause) {}; 90 assertEquals(nex.getCause(), rootCause); 91 assertTrue(nex.getMessage().indexOf(myMessage) != -1); 92 assertTrue(nex.getMessage().indexOf(rootCauseMesg) != -1); 93 94 ByteArrayOutputStream baos = new ByteArrayOutputStream (); 96 PrintWriter pw = new PrintWriter (baos); 97 nex.printStackTrace(pw); 98 pw.flush(); 99 String stackTrace = new String (baos.toByteArray()); 100 assertFalse(stackTrace.indexOf(rootCause.getClass().getName()) == -1); 101 assertFalse(stackTrace.indexOf(rootCauseMesg) == -1); 102 } 103 104 } 105 | Popular Tags |