From 332399a528d6fd460f85cc5d226fe36e64ca88bf Mon Sep 17 00:00:00 2001 From: Steven Caswell Date: Thu, 7 Aug 2003 00:50:30 +0000 Subject: [PATCH] added test coverage git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137578 13f79535-47bb-0310-9956-ffa450edef68 --- .../exception/NestableExceptionTestCase.java | 39 ++++++++++++++++- .../NestableRuntimeExceptionTestCase.java | 43 ++++++++++++++++++- 2 files changed, 80 insertions(+), 2 deletions(-) diff --git a/src/test/org/apache/commons/lang/exception/NestableExceptionTestCase.java b/src/test/org/apache/commons/lang/exception/NestableExceptionTestCase.java index 0c504774f..377d8773e 100644 --- a/src/test/org/apache/commons/lang/exception/NestableExceptionTestCase.java +++ b/src/test/org/apache/commons/lang/exception/NestableExceptionTestCase.java @@ -57,6 +57,7 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; +import java.io.PrintStream; import junit.framework.Test; import junit.framework.TestSuite; @@ -66,7 +67,7 @@ import junit.textui.TestRunner; * Tests the org.apache.commons.lang.exception.NestableException class. * * @author Steven Caswell - * @version $Id: NestableExceptionTestCase.java,v 1.8 2003/05/21 23:49:14 scolebourne Exp $ + * @version $Id: NestableExceptionTestCase.java,v 1.9 2003/08/07 00:50:30 stevencaswell Exp $ */ public class NestableExceptionTestCase extends AbstractNestableTestCase { @@ -242,6 +243,42 @@ public class NestableExceptionTestCase extends AbstractNestableTestCase { { return Exception.class; } + + public void testSpecificPrintStackTrace() + { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + NestableException ne = new NestableException("outer", new NestableException("inner", new Exception("another exception"))); + for(int i = 0; i < 2; i++) + { + if(i == 0) + { + // Test printStackTrac() + // Replace System.err with our own PrintStream so that we can + // obtain and check the printStrackTrace output + PrintStream err = System.err; + System.setErr(ps); + ne.printStackTrace(); + // Restore the System.err + System.setErr(err); + } + else + { + // Test printStackTrace(PrintStream) + ne.printStackTrace(ps); + } + } + String msg = baos.toString(); + assertTrue( "printStackTrace() starts with outer message", msg.startsWith("org.apache.commons.lang.exception.NestableException: outer")); + assertTrue( "printStackTrace() contains 1st nested message", msg.indexOf("Caused by: org.apache.commons.lang.exception.NestableException: inner") >= 0); + assertTrue( "printStackTrace() contains 2nd nested message", msg.indexOf("Caused by: java.lang.Exception: another exception") >= 0); + assertTrue( "printStackTrace() inner message after outer message", + msg.indexOf("org.apache.commons.lang.exception.NestableException: outer") < + msg.indexOf("Caused by: org.apache.commons.lang.exception.NestableException: inner")); + assertTrue( "printStackTrace() cause message after inner message", + msg.indexOf("Caused by: org.apache.commons.lang.exception.NestableException: inner") < + msg.indexOf("Caused by: java.lang.Exception: another exception")); + } public void testSerialization() throws java.io.IOException, ClassNotFoundException diff --git a/src/test/org/apache/commons/lang/exception/NestableRuntimeExceptionTestCase.java b/src/test/org/apache/commons/lang/exception/NestableRuntimeExceptionTestCase.java index c44deadcf..45f8a519d 100644 --- a/src/test/org/apache/commons/lang/exception/NestableRuntimeExceptionTestCase.java +++ b/src/test/org/apache/commons/lang/exception/NestableRuntimeExceptionTestCase.java @@ -53,6 +53,8 @@ */ package org.apache.commons.lang.exception; +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; import junit.framework.Test; import junit.framework.TestSuite; import junit.textui.TestRunner; @@ -61,7 +63,7 @@ import junit.textui.TestRunner; * Tests the org.apache.commons.lang.exception.NestableRuntimeException class. * * @author Steven Caswell - * @version $Id: NestableRuntimeExceptionTestCase.java,v 1.7 2003/05/21 23:49:14 scolebourne Exp $ + * @version $Id: NestableRuntimeExceptionTestCase.java,v 1.8 2003/08/07 00:50:30 stevencaswell Exp $ */ public class NestableRuntimeExceptionTestCase extends AbstractNestableTestCase { @@ -238,6 +240,45 @@ public class NestableRuntimeExceptionTestCase extends AbstractNestableTestCase { return RuntimeException.class; } + public void testSpecificPrintStackTrace() + { + // Test printStackTrac() + // Replace System.err with our own PrintStream so that we can obtain + // and check the printStrackTrace output + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + PrintStream ps = new PrintStream(baos); + NestableRuntimeException ne = new NestableRuntimeException("outer", new NestableRuntimeException("inner", new Exception("another exception"))); + for(int i = 0; i < 2; i++) + { + if(i == 0) + { + // Test printStackTrac() + // Replace System.err with our own PrintStream so that we can + // obtain and check the printStrackTrace output + PrintStream err = System.err; + System.setErr(ps); + ne.printStackTrace(); + // Restore the System.err + System.setErr(err); + } + else + { + // Test printStackTrace(PrintStream) + ne.printStackTrace(ps); + } + } + String msg = baos.toString(); + assertTrue( "printStackTrace() starts with outer message", msg.startsWith("org.apache.commons.lang.exception.NestableRuntimeException: outer")); + assertTrue( "printStackTrace() contains 1st nested message", msg.indexOf("Caused by: org.apache.commons.lang.exception.NestableRuntimeException: inner") >= 0); + assertTrue( "printStackTrace() contains 2nd nested message", msg.indexOf("Caused by: java.lang.Exception: another exception") >= 0); + assertTrue( "printStackTrace() inner message after outer message", + msg.indexOf("org.apache.commons.lang.exception.NestableRuntimeException: outer") < + msg.indexOf("Caused by: org.apache.commons.lang.exception.NestableRuntimeException: inner")); + assertTrue( "printStackTrace() cause message after inner message", + msg.indexOf("Caused by: org.apache.commons.lang.exception.NestableRuntimeException: inner") < + msg.indexOf("Caused by: java.lang.Exception: another exception")); + } + } /**