From 31c87c4b23e2f952c9a56ae509dec311e0d79cc0 Mon Sep 17 00:00:00 2001 From: Steven Caswell Date: Wed, 11 Sep 2002 18:16:53 +0000 Subject: [PATCH] refactored to share funcrionality from AbstractNestableTestCase git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137006 13f79535-47bb-0310-9956-ffa450edef68 --- .../exception/NestableExceptionTestCase.java | 516 +++--------------- .../NestableRuntimeExceptionTestCase.java | 511 +++-------------- 2 files changed, 176 insertions(+), 851 deletions(-) diff --git a/src/test/org/apache/commons/lang/exception/NestableExceptionTestCase.java b/src/test/org/apache/commons/lang/exception/NestableExceptionTestCase.java index 2eb19b27f..32ce67545 100644 --- a/src/test/org/apache/commons/lang/exception/NestableExceptionTestCase.java +++ b/src/test/org/apache/commons/lang/exception/NestableExceptionTestCase.java @@ -64,13 +64,16 @@ import junit.textui.TestRunner; * Tests the org.apache.commons.lang.exception.NestableException class. * * @author Steven Caswell - * @version $Id: NestableExceptionTestCase.java,v 1.3 2002/08/25 13:21:24 stevencaswell Exp $ + * @version $Id: NestableExceptionTestCase.java,v 1.4 2002/09/11 18:16:53 stevencaswell Exp $ */ -public class NestableExceptionTestCase extends junit.framework.TestCase +public class NestableExceptionTestCase extends AbstractNestableTestCase { /** - * Construct a new instance of NestableExceptionTestCase with the specified name + * Construct a new instance of + * NestableExceptionTestCase. + * + * @param name test case name */ public NestableExceptionTestCase(String name) { @@ -96,435 +99,91 @@ public class NestableExceptionTestCase extends junit.framework.TestCase { } - /** - * Test the implementation - */ - public void testGetCause() - { - NestableException ne1 = new NestableException(); - assertNull("nestable exception() cause is null", ne1.getCause()); - - NestableException ne2 = new NestableException("ne2"); - assertNull("nestable exception(\"ne2\") cause is null", ne2.getCause()); - - NestableException ne3 = new NestableException(new Exception("ne3 exception")); - assertNotNull("nestable exception(new Exception(\"ne3 exception\") cause is not null", - ne3.getCause()); - assertTrue("nestable exception(new Exception(\"ne3 exception\") cause message == ne3 exception", - ne3.getCause().getMessage().equals("ne3 exception")); - - NestableException ne4 = new NestableException("ne4", new Exception("ne4 exception")); - assertNotNull("nestable exception(\"ne4\", new Exception(\"ne4 exception\") cause is not null", - ne4.getCause()); - - NestableException ne5 = new NestableException("ne5", null); - assertNull("nestable exception(\"ne5\", null) cause is null", - ne5.getCause()); - - NestableException ne6 = new NestableException(null, new Exception("ne6 exception")); - assertNotNull("nestable exception(null, new Exception(\"ne6 exception\") cause is not null", - ne6.getCause()); - } - - public void testGetThrowableCount() - { - NestableException ne1 = new NestableException(); - assertEquals("ne1 throwable count", 1, ne1.getThrowableCount()); - - NestableException ne2 = new NestableException("ne2"); - assertEquals("ne2 throwable count", 1, ne2.getThrowableCount()); - - NestableException ne3 = new NestableException(new Exception("ne3 exception")); - assertEquals("ne3 throwable count", 2, ne3.getThrowableCount()); - - NestableException ne4 = new NestableException("ne4", new Exception("ne4 exception")); - assertEquals("ne4 throwable count", 2, ne4.getThrowableCount()); - - NestableException ne5 = new NestableException("ne5", null); - assertEquals("ne 5 throwable count", 1, ne5.getThrowableCount()); - - NestableException ne6 = new NestableException(null, new Exception("ne6 exception")); - assertEquals("ne 6 throwable count", 2, ne6.getThrowableCount()); - - NestableException ne7 = new NestableException("ne7o", new NestableException("ne7i", new Exception("ne7 exception"))); - assertEquals("ne 7 throwable count", 3, ne7.getThrowableCount()); - - NestableException ne8 = new NestableException("level 1", new NestableException("level 2", new NestableException(new NestableException("level 4", new Exception("level 5"))))); - assertEquals("ne 8 throwable count", 5, ne8.getThrowableCount()); - } - - - public void testGetMessage() - { - NestableException ne1 = new NestableException(); - assertNull("nestable exception() message is null", ne1.getMessage()); - - NestableException ne2 = new NestableException("ne2"); - assertNotNull("nestable exception(\"ne2\") message is not null", ne2.getMessage()); - assertTrue("nestable exception(\"ne2\") message == ne2", ne2.getMessage().equals("ne2")); - - NestableException ne3 = new NestableException(new Exception("ne3 exception")); - assertNotNull("nestable exception(new Exception(\"ne3 exception\") message is not null", - ne3.getMessage()); - assertTrue("nestable exception(new Exception(\"ne3 exception\") message == cause message", - ne3.getMessage().equals(ne3.getCause().getMessage())); - - NestableException ne4 = new NestableException("ne4", new Exception("ne4 exception")); - assertNotNull("nestable exception(\"ne4\", new Exception(\"ne4 exception\") message is not null", - ne4.getMessage()); - assertTrue("nestable exception(\"ne4\", new Exception(\"ne4 exception\") message == ne4: ne4 exception", - ne4.getMessage().equals("ne4: ne4 exception")); - - NestableException ne5 = new NestableException("ne5", null); - assertNotNull("nestable exception(\"ne5\", new Exception(\"ne5 exception\") message is not null", - ne5.getMessage()); - assertTrue("nestable exception(\"ne5\", null) message == ne5", - ne5.getMessage().equals("ne5")); - - NestableException ne6 = new NestableException(null, new Exception("ne6 exception")); - assertTrue("nestable exception(null, new Exception(\"ne6 exception\") cause == ne6 exception", - ne6.getMessage().equals("ne6 exception")); - - NestableException ne7 = new NestableException("ne7o", new NestableException("ne7i", new Exception("ne7 exception"))); - assertTrue("nextable exception(\"ne7o\", new NestableException(\"ne7i\", new Exception(\"ne7 exception\"))) message is ne7o: ne7i: ne7 exception", - ne7.getMessage().equals("ne7o: ne7i: ne7 exception")); - - } - - public void testGetMessageI() - { - String[] msgs = new String[5]; - msgs[0] = "level 1"; - msgs[1] = "level 2"; - msgs[2] = null; - msgs[3] = "level 4"; - msgs[4] = "level 5"; - NestableException ne = new NestableException(msgs[0], new NestableException(msgs[1], new NestableException(new NestableException(msgs[3], new Exception(msgs[4]))))); - for(int i = 0; i < msgs.length; i++) - { - assertEquals("message " + i, msgs[i], ne.getMessage(i)); - } - - // Test for index out of bounds - try - { - String msg = ne.getMessage(-1); - fail("getMessage(-1) should have thrown IndexOutOfBoundsException"); - } - catch(IndexOutOfBoundsException ioode) - { - } - try - { - String msg = ne.getMessage(msgs.length + 100); - fail("getMessage(999) should have thrown IndexOutOfBoundsException"); - } - catch(IndexOutOfBoundsException ioode) - { - } - } - - public void testGetMessages() - { - String[] msgs = new String[5]; - msgs[0] = "level 1"; - msgs[1] = "level 2"; - msgs[2] = null; - msgs[3] = "level 4"; - msgs[4] = "level 5"; - NestableException ne = new NestableException(msgs[0], new NestableException(msgs[1], new NestableException(new NestableException(msgs[3], new Exception(msgs[4]))))); - String[] nMsgs = ne.getMessages(); - assertEquals("messages length", msgs.length, nMsgs.length); - for(int i = 0; i < nMsgs.length; i++) - { - assertEquals("message " + i, msgs[i], nMsgs[i]); - } - } - - public void testGetThrowableI() - { - Nestable n = null; - String msgs[] = null; - Class[] throwables = null; - - msgs = new String[2]; - msgs[0] = null; - msgs[1] = "level 2"; - throwables = new Class[2]; - throwables[0] = NestableExceptionTester1.class; - throwables[1] = Exception.class; - n = new NestableExceptionTester1(new Exception(msgs[1])); - doNestableExceptionGetThrowableI(n, throwables, msgs); - - msgs = new String[5]; - msgs[0] = "level 1"; - msgs[1] = "level 2"; - msgs[2] = null; - msgs[3] = "level 4"; - msgs[4] = "level 5"; - throwables = new Class[5]; - throwables[0] = NestableExceptionTester1.class; - throwables[1] = NestableExceptionTester2.class; - throwables[2] = NestableExceptionTester1.class; - throwables[3] = NestableExceptionTester2.class; - throwables[4] = Exception.class; - n = new NestableExceptionTester1(msgs[0], new NestableExceptionTester2(msgs[1], new NestableExceptionTester1(new NestableExceptionTester2(msgs[3], new Exception(msgs[4]))))); - doNestableExceptionGetThrowableI(n, throwables, msgs); - } - - private void doNestableExceptionGetThrowableI(Nestable n, Class[] classes, String[] msgs) - { - Throwable t = null; - String msg = null; - - for(int i = 0; i < classes.length; i++) - { - t = n.getThrowable(i); - assertEquals("throwable class", classes[i], t.getClass()); - if(Nestable.class.isInstance(t)) - { - msg = ((Nestable) t).getMessage(0); - } - else - { - msg = t.getMessage(); - } - assertEquals("throwable message", msgs[i], msg); - } - - // Test for index out of bounds - try - { - t = n.getThrowable(-1); - fail("getThrowable(-1) should have thrown IndexOutOfBoundsException"); - } - catch(IndexOutOfBoundsException ioobe) - { - } - try - { - t = n.getThrowable(999); - fail("getThrowable(999) should have thrown IndexOutOfBoundsException"); - } - catch(IndexOutOfBoundsException ioobe) - { - } - } - - public void testGetThrowables() - { - Nestable n = null; - String msgs[] = null; - Class[] throwables = null; - - msgs = new String[2]; - msgs[0] = null; - msgs[1] = "level 2"; - throwables = new Class[2]; - throwables[0] = NestableExceptionTester1.class; - throwables[1] = Exception.class; - n = new NestableExceptionTester1(new Exception(msgs[1])); - doNestableExceptionGetThrowables(n, throwables, msgs); - - msgs = new String[5]; - msgs[0] = "level 1"; - msgs[1] = "level 2"; - msgs[2] = null; - msgs[3] = "level 4"; - msgs[4] = "level 5"; - throwables = new Class[5]; - throwables[0] = NestableExceptionTester1.class; - throwables[1] = NestableExceptionTester2.class; - throwables[2] = NestableExceptionTester1.class; - throwables[3] = NestableExceptionTester2.class; - throwables[4] = Exception.class; - n = new NestableExceptionTester1(msgs[0], new NestableExceptionTester2(msgs[1], new NestableExceptionTester1(new NestableExceptionTester2(msgs[3], new Exception(msgs[4]))))); - doNestableExceptionGetThrowables(n, throwables, msgs); - } - - private void doNestableExceptionGetThrowables(Nestable n, Class[] classes, String[] msgs) - { - String msg = null; - - Throwable throwables[] = n.getThrowables(); - assertEquals("throwables length", classes.length, throwables.length); - for(int i = 0; i < classes.length; i++) - { - assertEquals("throwable class", classes[i], throwables[i].getClass()); - Throwable t = throwables[i]; - if(Nestable.class.isInstance(t)) - { - msg = ((Nestable) t).getMessage(0); - } - else - { - msg = t.getMessage(); - } - assertEquals("throwable message", msgs[i], msg); - } - } - - public void testIndexOfThrowable() - { - Nestable n = null; - String msgs[] = null; - Class[] throwables = null; - - msgs = new String[5]; - msgs[0] = "level 1"; - msgs[1] = "level 2"; - msgs[2] = null; - msgs[3] = "level 4"; - msgs[4] = "level 5"; - throwables = new Class[5]; - throwables[0] = NestableExceptionTester1.class; - throwables[1] = NestableExceptionTester2.class; - throwables[2] = NestableExceptionTester1.class; - throwables[3] = NestableExceptionTester2.class; - throwables[4] = Exception.class; - int[] indexes = {0, 1, 0, 1, 4}; - n = new NestableExceptionTester1(msgs[0], new NestableExceptionTester2(msgs[1], new NestableExceptionTester1(new NestableExceptionTester2(msgs[3], new Exception(msgs[4]))))); - for(int i = 0; i < throwables.length; i++) - { - doNestableExceptionIndexOfThrowable(n, throwables[i], indexes[i], msgs[indexes[i]]); - } - doNestableExceptionIndexOfThrowable(n, java.util.Date.class, -1, null); - } - - private void doNestableExceptionIndexOfThrowable(Nestable n, Class type, int expectedIndex, String expectedMsg) - { - Throwable t = null; - - int index = n.indexOfThrowable(type); - assertEquals("index of throwable " + type.getName(), expectedIndex, index); - if(expectedIndex > -1) - { - t = n.getThrowable(index); - if(expectedMsg != null) - { - String msg = null; - if(Nestable.class.isInstance(t)) - { - msg = ((Nestable) t).getMessage(0); - } - else - { - msg = t.getMessage(); - } - assertEquals("message of indexed throwable", expectedMsg, msg); - } - } - } - - public void testIndexOfThrowableI() - { - Nestable n = null; - String msgs[] = null; - Class[] throwables = null; - - msgs = new String[5]; - msgs[0] = "level 1"; - msgs[1] = "level 2"; - msgs[2] = null; - msgs[3] = "level 4"; - msgs[4] = "level 5"; - throwables = new Class[5]; - throwables[0] = NestableExceptionTester1.class; - throwables[1] = NestableExceptionTester2.class; - throwables[2] = NestableExceptionTester1.class; - throwables[3] = NestableExceptionTester2.class; - throwables[4] = Exception.class; - int[] indexes = {0, 1, 0, 1, 4}; - n = new NestableExceptionTester1(msgs[0], new NestableExceptionTester2(msgs[1], new NestableExceptionTester1(new NestableExceptionTester2(msgs[3], new Exception(msgs[4]))))); - for(int i = 0; i < throwables.length; i++) - { - doNestableExceptionIndexOfThrowableI(n, throwables[i], 0, indexes[i], msgs[indexes[i]]); - } - doNestableExceptionIndexOfThrowableI(n, NestableExceptionTester2.class, 2, 3, msgs[3]); - doNestableExceptionIndexOfThrowableI(n, NestableExceptionTester1.class, 1, 2, msgs[2]); - doNestableExceptionIndexOfThrowableI(n, NestableExceptionTester1.class, 3, -1, null); - doNestableExceptionIndexOfThrowableI(n, NestableExceptionTester1.class, 4, -1, null); - doNestableExceptionIndexOfThrowableI(n, Exception.class, 2, 4, msgs[4]); - doNestableExceptionIndexOfThrowableI(n, java.util.Date.class, 0, -1, null); - - // Test for index out of bounds - try - { - int index = n.indexOfThrowable(NestableExceptionTester1.class, -1); - fail("method should have thrown IndexOutOfBoundsException"); - } - catch(IndexOutOfBoundsException iooob) - { - } - try - { - int index = n.indexOfThrowable(NestableExceptionTester1.class, 5); - fail("method should have thrown IndexOutOfBoundsException"); - } - catch(IndexOutOfBoundsException iooob) - { - } - - } - - private void doNestableExceptionIndexOfThrowableI(Nestable n, Class type, int fromIndex, int expectedIndex, String expectedMsg) - { - Throwable t = null; - - int index = n.indexOfThrowable(type, fromIndex); - assertEquals("index of throwable " + type.getName(), expectedIndex, index); - if(expectedIndex > -1) - { - t = n.getThrowable(index); - if(expectedMsg != null) - { - String msg = null; - if(Nestable.class.isInstance(t)) - { - msg = ((Nestable) t).getMessage(0); - } - else - { - msg = t.getMessage(); - } - assertEquals("message of indexed throwable", expectedMsg, msg); - } - } - - } - - public void testPrintPartialStackTrace() - { - NestableException ne9 = new NestableException("ne9", new Exception("ne9 exception")); - ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); - PrintStream ps2 = new PrintStream(baos2); - PrintWriter pw2 = new PrintWriter(ps2, true); - ne9.printPartialStackTrace(pw2); - String stack2 = baos2.toString(); - assertTrue("stack trace startsWith == org.apache.commons.lang.exception.NestableException: ne9: ne9 exception", - stack2.startsWith("org.apache.commons.lang.exception.NestableException: ne9: ne9 exception")); - assertEquals("stack trace indexOf rethrown == -1", - stack2.indexOf("rethrown"), -1); - } - - public void testPrintStackTrace() - { - NestableException ne8 = new NestableException("ne8", new Exception("ne8 exception")); - ByteArrayOutputStream baos1 = new ByteArrayOutputStream(); - PrintStream ps1 = new PrintStream(baos1); - PrintWriter pw1 = new PrintWriter(ps1, true); - ne8.printStackTrace(ps1); - String stack1 = baos1.toString(); - assertTrue("stack trace startsWith == java.lang.Exception: ne8 exception", - stack1.startsWith("java.lang.Exception: ne8 exception")); - assertTrue("stack trace indexOf org.apache.commons.lang.exception.NestableException: ne8: ne8 exception > -1", - stack1.indexOf("org.apache.commons.lang.exception.NestableException: ne8: ne8 exception") > -1); - } - public static void main(String args[]) { TestRunner.run(suite()); } + + public Nestable getNestable() + { + return new NestableException(); + } + + public Nestable getNestable(Nestable n) + { + return new NestableException((Throwable) n); + } + + public Nestable getNestable(String msg) + { + return new NestableException(msg); + } + + public Nestable getNestable(Throwable t) + { + return new NestableException(t); + } + + public Nestable getNestable(String msg, Throwable t) + { + return new NestableException(msg, t); + } + + public Nestable getNestable(String msg, Nestable n) + { + return new NestableException(msg, (Throwable) n); + } + + public Nestable getTester1(Throwable t) + { + return new NestableExceptionTester1(t); + } + + public Nestable getTester1(Nestable n) + { + return new NestableExceptionTester1((Throwable) n); + } + + public Nestable getTester1(String msg, Throwable t) + { + return new NestableExceptionTester1(msg, t); + } + + public Nestable getTester1(String msg, Nestable n) + { + return new NestableExceptionTester1(msg, (Throwable) n); + } + + public Class getTester1Class() + { + return NestableExceptionTester1.class; + } + + public Nestable getTester2(String msg, Throwable t) + { + return new NestableExceptionTester2(msg, t); + } + + public Nestable getTester2(String msg, Nestable n) + { + return new NestableExceptionTester2(msg, (Throwable) n); + } + + public Class getTester2Class() + { + return NestableExceptionTester2.class; + } + + public Throwable getThrowable(String msg) + { + return new Exception(msg); + } + + public Class getThrowableClass() + { + return Exception.class; + } + } class NestableExceptionTester1 extends NestableException @@ -574,4 +233,3 @@ class NestableExceptionTester2 extends NestableException } } - diff --git a/src/test/org/apache/commons/lang/exception/NestableRuntimeExceptionTestCase.java b/src/test/org/apache/commons/lang/exception/NestableRuntimeExceptionTestCase.java index a8de5fd47..718f0f2e4 100644 --- a/src/test/org/apache/commons/lang/exception/NestableRuntimeExceptionTestCase.java +++ b/src/test/org/apache/commons/lang/exception/NestableRuntimeExceptionTestCase.java @@ -64,12 +64,16 @@ import junit.textui.TestRunner; * Tests the org.apache.commons.lang.exception.NestableRuntimeException class. * * @author Steven Caswell - * @version $Id: NestableRuntimeExceptionTestCase.java,v 1.3 2002/08/25 13:21:24 stevencaswell Exp $ + * @version $Id: NestableRuntimeExceptionTestCase.java,v 1.4 2002/09/11 18:16:53 stevencaswell Exp $ */ -public class NestableRuntimeExceptionTestCase extends junit.framework.TestCase +public class NestableRuntimeExceptionTestCase extends AbstractNestableTestCase { + /** - * Construct a new instance of NestableRuntimeExceptionTestCase with the specified name + * Construct a new instance of + * NestableRuntimeExceptionTestCase. + * + * @param name test case name */ public NestableRuntimeExceptionTestCase(String name) { @@ -85,7 +89,7 @@ public class NestableRuntimeExceptionTestCase extends junit.framework.TestCase public static Test suite() { - return new TestSuite(NestableRuntimeExceptionTestCase.class); + return new TestSuite(NestableExceptionTestCase.class); } /** @@ -95,428 +99,91 @@ public class NestableRuntimeExceptionTestCase extends junit.framework.TestCase { } - /** - * Test the implementation - */ - public void testGetCause() - { - NestableRuntimeException ne1 = new NestableRuntimeException(); - assertNull("nestable runtime exception() cause is null", ne1.getCause()); - - NestableRuntimeException ne2 = new NestableRuntimeException("ne2"); - assertNull("nestable runtime exception(\"ne2\") cause is null", ne2.getCause()); - - NestableRuntimeException ne3 = new NestableRuntimeException(new Exception("ne3 exception")); - assertNotNull("nestable runtime exception(new Exception(\"ne3 exception\") cause is not null", - ne3.getCause()); - assertTrue("nestable runtime exception(new Exception(\"ne3 exception\") cause message == ne3 exception", - ne3.getCause().getMessage().equals("ne3 exception")); - - NestableRuntimeException ne4 = new NestableRuntimeException("ne4", new Exception("ne4 exception")); - assertNotNull("nestable runtime exception(\"ne4\", new Exception(\"ne4 exception\") cause is not null", - ne4.getCause()); - - NestableRuntimeException ne5 = new NestableRuntimeException("ne5", null); - assertNull("nestable runtime exception(\"ne5\", null) cause is null", - ne5.getCause()); - - NestableRuntimeException ne6 = new NestableRuntimeException(null, new Exception("ne6 exception")); - assertNotNull("nestable runtime exception(null, new Exception(\"ne6 exception\") cause is not null", - ne6.getCause()); - } - - public void testGetThrowableCount() - { - NestableRuntimeException ne1 = new NestableRuntimeException(); - assertEquals("ne1 throwable count", 1, ne1.getThrowableCount()); - - NestableRuntimeException ne2 = new NestableRuntimeException("ne2"); - assertEquals("ne2 throwable count", 1, ne2.getThrowableCount()); - - NestableRuntimeException ne3 = new NestableRuntimeException(new Exception("ne3 exception")); - assertEquals("ne3 throwable count", 2, ne3.getThrowableCount()); - - NestableRuntimeException ne4 = new NestableRuntimeException("ne4", new Exception("ne4 exception")); - assertEquals("ne4 throwable count", 2, ne4.getThrowableCount()); - - NestableRuntimeException ne5 = new NestableRuntimeException("ne5", null); - assertEquals("ne 5 throwable count", 1, ne5.getThrowableCount()); - - NestableRuntimeException ne6 = new NestableRuntimeException(null, new Exception("ne6 exception")); - assertEquals("ne 6 throwable count", 2, ne6.getThrowableCount()); - - NestableRuntimeException ne7 = new NestableRuntimeException("ne7o", new NestableRuntimeException("ne7i", new Exception("ne7 exception"))); - assertEquals("ne 7 throwable count", 3, ne7.getThrowableCount()); - - NestableRuntimeException ne8 = new NestableRuntimeException("level 1", new NestableRuntimeException("level 2", new NestableRuntimeException(new NestableRuntimeException("level 4", new Exception("level 5"))))); - assertEquals("ne 8 throwable count", 5, ne8.getThrowableCount()); - } - - public void testGetMessage() - { - NestableRuntimeException ne1 = new NestableRuntimeException(); - assertNull("nestable runtime exception() message is null", ne1.getMessage()); - - NestableRuntimeException ne2 = new NestableRuntimeException("ne2"); - assertNotNull("nestable runtime exception(\"ne2\") message is not null", ne2.getMessage()); - assertTrue("nestable runtime exception(\"ne2\") message == ne2", ne2.getMessage().equals("ne2")); - - NestableRuntimeException ne3 = new NestableRuntimeException(new Exception("ne3 exception")); - assertNotNull("nestable runtime exception(new Exception(\"ne3 exception\") message is not null", - ne3.getMessage()); - assertTrue("nestable runtime exception(new Exception(\"ne3 exception\") message == cause message", - ne3.getMessage().equals(ne3.getCause().getMessage())); - - NestableRuntimeException ne4 = new NestableRuntimeException("ne4", new Exception("ne4 exception")); - assertNotNull("nestable runtime exception(\"ne4\", new Exception(\"ne4 exception\") message is not null", - ne4.getMessage()); - assertTrue("nestable runtime exception(\"ne4\", new Exception(\"ne4 exception\") message == ne4: ne4 exception", - ne4.getMessage().equals("ne4: ne4 exception")); - - NestableRuntimeException ne5 = new NestableRuntimeException("ne5", null); - assertNotNull("nestable runtime exception(\"ne5\", new Exception(\"ne5 exception\") message is not null", - ne5.getMessage()); - assertTrue("nestable runtime exception(\"ne5\", null) message == ne5", - ne5.getMessage().equals("ne5")); - - NestableRuntimeException ne6 = new NestableRuntimeException(null, new Exception("ne6 exception")); - assertTrue("nestable runtime exception(null, new Exception(\"ne6 exception\") cause == ne6 exception", - ne6.getMessage().equals("ne6 exception")); - - NestableRuntimeException ne7 = new NestableRuntimeException("ne7o", new NestableRuntimeException("ne7i", new Exception("ne7 exception"))); - assertTrue("nextable exception(\"ne7o\", new NestableRuntimeException(\"ne7i\", new Exception(\"ne7 exception\"))) message is ne7o: ne7i: ne7 exception", - ne7.getMessage().equals("ne7o: ne7i: ne7 exception")); - } - - public void testGetMessageI() - { - String[] msgs = new String[5]; - msgs[0] = "level 1"; - msgs[1] = "level 2"; - msgs[2] = null; - msgs[3] = "level 4"; - msgs[4] = "level 5"; - NestableRuntimeException ne = new NestableRuntimeException(msgs[0], new NestableRuntimeException(msgs[1], new NestableRuntimeException(new NestableRuntimeException(msgs[3], new Exception(msgs[4]))))); - for(int i = 0; i < msgs.length; i++) - { - assertEquals("message " + i, msgs[i], ne.getMessage(i)); - } - - // Test for index out of bounds - try - { - String msg = ne.getMessage(-1); - fail("getMessage(-1) should have thrown IndexOutOfBoundsException"); - } - catch(IndexOutOfBoundsException ioode) - { - } - try - { - String msg = ne.getMessage(msgs.length + 100); - fail("getMessage(999) should have thrown IndexOutOfBoundsException"); - } - catch(IndexOutOfBoundsException ioode) - { - } - } - - public void testGetMessages() - { - String[] msgs = new String[5]; - msgs[0] = "level 1"; - msgs[1] = "level 2"; - msgs[2] = null; - msgs[3] = "level 4"; - msgs[4] = "level 5"; - NestableRuntimeException ne = new NestableRuntimeException(msgs[0], new NestableRuntimeException(msgs[1], new NestableRuntimeException(new NestableRuntimeException(msgs[3], new Exception(msgs[4]))))); - String[] nMsgs = ne.getMessages(); - assertEquals("messages length", msgs.length, nMsgs.length); - for(int i = 0; i < nMsgs.length; i++) - { - assertEquals("message " + i, msgs[i], nMsgs[i]); - } - } - - public void testGetThrowableI() - { - Nestable n = null; - String msgs[] = null; - Class[] throwables = null; - - msgs = new String[2]; - msgs[0] = null; - msgs[1] = "level 2"; - throwables = new Class[2]; - throwables[0] = NestableRuntimeExceptionTester1.class; - throwables[1] = Exception.class; - n = new NestableRuntimeExceptionTester1(new Exception(msgs[1])); - doNestableRuntimeExceptionGetThrowableI(n, throwables, msgs); - - msgs = new String[5]; - msgs[0] = "level 1"; - msgs[1] = "level 2"; - msgs[2] = null; - msgs[3] = "level 4"; - msgs[4] = "level 5"; - throwables = new Class[5]; - throwables[0] = NestableRuntimeExceptionTester1.class; - throwables[1] = NestableRuntimeExceptionTester2.class; - throwables[2] = NestableRuntimeExceptionTester1.class; - throwables[3] = NestableRuntimeExceptionTester2.class; - throwables[4] = Exception.class; - n = new NestableRuntimeExceptionTester1(msgs[0], new NestableRuntimeExceptionTester2(msgs[1], new NestableRuntimeExceptionTester1(new NestableRuntimeExceptionTester2(msgs[3], new Exception(msgs[4]))))); - doNestableRuntimeExceptionGetThrowableI(n, throwables, msgs); - } - - private void doNestableRuntimeExceptionGetThrowableI(Nestable n, Class[] classes, String[] msgs) - { - Throwable t = null; - String msg = null; - - for(int i = 0; i < classes.length; i++) - { - t = n.getThrowable(i); - assertEquals("throwable class", classes[i], t.getClass()); - if(Nestable.class.isInstance(t)) - { - msg = ((Nestable) t).getMessage(0); - } - else - { - msg = t.getMessage(); - } - assertEquals("throwable message", msgs[i], msg); - } - - // Test for index out of bounds - try - { - t = n.getThrowable(-1); - fail("getThrowable(-1) should have thrown IndexOutOfBoundsException"); - } - catch(IndexOutOfBoundsException ioobe) - { - } - try - { - t = n.getThrowable(999); - fail("getThrowable(999) should have thrown IndexOutOfBoundsException"); - } - catch(IndexOutOfBoundsException ioobe) - { - } - } - - public void testGetThrowables() - { - Nestable n = null; - String msgs[] = null; - Class[] throwables = null; - - msgs = new String[2]; - msgs[0] = null; - msgs[1] = "level 2"; - throwables = new Class[2]; - throwables[0] = NestableRuntimeExceptionTester1.class; - throwables[1] = Exception.class; - n = new NestableRuntimeExceptionTester1(new Exception(msgs[1])); - doNestableRuntimeExceptionGetThrowables(n, throwables, msgs); - - msgs = new String[5]; - msgs[0] = "level 1"; - msgs[1] = "level 2"; - msgs[2] = null; - msgs[3] = "level 4"; - msgs[4] = "level 5"; - throwables = new Class[5]; - throwables[0] = NestableRuntimeExceptionTester1.class; - throwables[1] = NestableRuntimeExceptionTester2.class; - throwables[2] = NestableRuntimeExceptionTester1.class; - throwables[3] = NestableRuntimeExceptionTester2.class; - throwables[4] = Exception.class; - n = new NestableRuntimeExceptionTester1(msgs[0], new NestableRuntimeExceptionTester2(msgs[1], new NestableRuntimeExceptionTester1(new NestableRuntimeExceptionTester2(msgs[3], new Exception(msgs[4]))))); - doNestableRuntimeExceptionGetThrowables(n, throwables, msgs); - } - - private void doNestableRuntimeExceptionGetThrowables(Nestable n, Class[] classes, String[] msgs) - { - String msg = null; - - Throwable throwables[] = n.getThrowables(); - assertEquals("throwables length", classes.length, throwables.length); - for(int i = 0; i < classes.length; i++) - { - assertEquals("throwable class", classes[i], throwables[i].getClass()); - Throwable t = throwables[i]; - if(Nestable.class.isInstance(t)) - { - msg = ((Nestable) t).getMessage(0); - } - else - { - msg = t.getMessage(); - } - assertEquals("throwable message", msgs[i], msg); - } - } - - public void testIndexOfThrowable() - { - Nestable n = null; - String msgs[] = null; - Class[] throwables = null; - - msgs = new String[5]; - msgs[0] = "level 1"; - msgs[1] = "level 2"; - msgs[2] = null; - msgs[3] = "level 4"; - msgs[4] = "level 5"; - throwables = new Class[5]; - throwables[0] = NestableRuntimeExceptionTester1.class; - throwables[1] = NestableRuntimeExceptionTester2.class; - throwables[2] = NestableRuntimeExceptionTester1.class; - throwables[3] = NestableRuntimeExceptionTester2.class; - throwables[4] = Exception.class; - int[] indexes = {0, 1, 0, 1, 4}; - n = new NestableRuntimeExceptionTester1(msgs[0], new NestableRuntimeExceptionTester2(msgs[1], new NestableRuntimeExceptionTester1(new NestableRuntimeExceptionTester2(msgs[3], new Exception(msgs[4]))))); - for(int i = 0; i < throwables.length; i++) - { - doNestableRuntimeExceptionIndexOfThrowable(n, throwables[i], indexes[i], msgs[indexes[i]]); - } - doNestableRuntimeExceptionIndexOfThrowable(n, java.util.Date.class, -1, null); - } - - private void doNestableRuntimeExceptionIndexOfThrowable(Nestable n, Class type, int expectedIndex, String expectedMsg) - { - Throwable t = null; - - int index = n.indexOfThrowable(type); - assertEquals("index of throwable " + type.getName(), expectedIndex, index); - if(expectedMsg != null) - { - t = n.getThrowable(index); - String msg = null; - if(Nestable.class.isInstance(t)) - { - msg = ((Nestable) t).getMessage(0); - } - else - { - msg = t.getMessage(); - } - assertEquals("message of indexed throwable", expectedMsg, msg); - } - } - - public void testIndexOfThrowableI() - { - Nestable n = null; - String msgs[] = null; - Class[] throwables = null; - - msgs = new String[5]; - msgs[0] = "level 1"; - msgs[1] = "level 2"; - msgs[2] = null; - msgs[3] = "level 4"; - msgs[4] = "level 5"; - throwables = new Class[5]; - throwables[0] = NestableRuntimeExceptionTester1.class; - throwables[1] = NestableRuntimeExceptionTester2.class; - throwables[2] = NestableRuntimeExceptionTester1.class; - throwables[3] = NestableRuntimeExceptionTester2.class; - throwables[4] = Exception.class; - int[] indexes = {0, 1, 0, 1, 4}; - n = new NestableRuntimeExceptionTester1(msgs[0], new NestableRuntimeExceptionTester2(msgs[1], new NestableRuntimeExceptionTester1(new NestableRuntimeExceptionTester2(msgs[3], new Exception(msgs[4]))))); - for(int i = 0; i < throwables.length; i++) - { - doNestableRuntimeExceptionIndexOfThrowableI(n, throwables[i], 0, indexes[i], msgs[indexes[i]]); - } - doNestableRuntimeExceptionIndexOfThrowableI(n, NestableRuntimeExceptionTester2.class, 2, 3, msgs[3]); - doNestableRuntimeExceptionIndexOfThrowableI(n, NestableRuntimeExceptionTester1.class, 1, 2, msgs[2]); - doNestableRuntimeExceptionIndexOfThrowableI(n, NestableRuntimeExceptionTester1.class, 3, -1, null); - doNestableRuntimeExceptionIndexOfThrowableI(n, NestableRuntimeExceptionTester1.class, 4, -1, null); - doNestableRuntimeExceptionIndexOfThrowableI(n, java.util.Date.class, 0, -1, null); - - try - { - int index = n.indexOfThrowable(NestableRuntimeExceptionTester1.class, -1); - fail("method should have thrown IndexOutOfBoundsException"); - } - catch(IndexOutOfBoundsException iooob) - { - } - try - { - int index = n.indexOfThrowable(NestableRuntimeExceptionTester1.class, 5); - fail("method should have thrown IndexOutOfBoundsException"); - } - catch(IndexOutOfBoundsException iooob) - { - } - - } - - private void doNestableRuntimeExceptionIndexOfThrowableI(Nestable n, Class type, int fromIndex, int expectedIndex, String expectedMsg) - { - Throwable t = null; - - int index = n.indexOfThrowable(type, fromIndex); - assertEquals("index of throwable " + type.getName(), expectedIndex, index); - if(expectedIndex > -1) - { - t = n.getThrowable(index); - if(expectedMsg != null) - { - String msg = null; - if(Nestable.class.isInstance(t)) - { - msg = ((Nestable) t).getMessage(0); - } - else - { - msg = t.getMessage(); - } - assertEquals("message of indexed throwable", expectedMsg, msg); - } - } - - } - - public void testPrintStackTrace() - { - NestableRuntimeException ne8 = new NestableRuntimeException("ne8", new Exception("ne8 exception")); - ByteArrayOutputStream baos1 = new ByteArrayOutputStream(); - PrintStream ps1 = new PrintStream(baos1); - PrintWriter pw1 = new PrintWriter(ps1, true); - ne8.printStackTrace(ps1); - String stack1 = baos1.toString(); - assertTrue("stack trace startsWith == java.lang.Exception: ne8 exception", - stack1.startsWith("java.lang.Exception: ne8 exception")); - assertTrue("stack trace indexOf org.apache.commons.lang.exception.NestableRuntimeException: ne8: ne8 exception > -1", - stack1.indexOf("org.apache.commons.lang.exception.NestableRuntimeException: ne8: ne8 exception") > -1); - } - - public void testPrintPartialStackTrace() - { - NestableRuntimeException ne9 = new NestableRuntimeException("ne9", new Exception("ne9 exception")); - ByteArrayOutputStream baos2 = new ByteArrayOutputStream(); - PrintStream ps2 = new PrintStream(baos2); - PrintWriter pw2 = new PrintWriter(ps2, true); - ne9.printPartialStackTrace(pw2); - String stack2 = baos2.toString(); - assertTrue("stack trace startsWith == org.apache.commons.lang.exception.NestableRuntimeException: ne9: ne9 exception", - stack2.startsWith("org.apache.commons.lang.exception.NestableRuntimeException: ne9: ne9 exception")); - assertEquals("stack trace indexOf rethrown == -1", - stack2.indexOf("rethrown"), -1); - } - public static void main(String args[]) { TestRunner.run(suite()); } + + public Nestable getNestable() + { + return new NestableRuntimeException(); + } + + public Nestable getNestable(Nestable n) + { + return new NestableRuntimeException((Throwable) n); + } + + public Nestable getNestable(String msg) + { + return new NestableRuntimeException(msg); + } + + public Nestable getNestable(Throwable t) + { + return new NestableRuntimeException(t); + } + + public Nestable getNestable(String msg, Throwable t) + { + return new NestableRuntimeException(msg, t); + } + + public Nestable getNestable(String msg, Nestable n) + { + return new NestableRuntimeException(msg, (Throwable) n); + } + + public Nestable getTester1(Throwable t) + { + return new NestableRuntimeExceptionTester1(t); + } + + public Nestable getTester1(Nestable n) + { + return new NestableRuntimeExceptionTester1((Throwable) n); + } + + public Nestable getTester1(String msg, Throwable t) + { + return new NestableRuntimeExceptionTester1(msg, t); + } + + public Nestable getTester1(String msg, Nestable n) + { + return new NestableRuntimeExceptionTester1(msg, (Throwable) n); + } + + public Class getTester1Class() + { + return NestableRuntimeExceptionTester1.class; + } + + public Nestable getTester2(String msg, Throwable t) + { + return new NestableRuntimeExceptionTester2(msg, t); + } + + public Nestable getTester2(String msg, Nestable n) + { + return new NestableRuntimeExceptionTester1(msg, (Throwable) n); + } + + public Class getTester2Class() + { + return NestableRuntimeExceptionTester2.class; + } + + public Throwable getThrowable(String msg) + { + return new RuntimeException(msg); + } + + public Class getThrowableClass() + { + return RuntimeException.class; + } + } class NestableRuntimeExceptionTester1 extends NestableRuntimeException