updated tests to reflect the following changes:

deprecated method getLength(), replaced with getThrowableCount();
deprecated indexOfThrowable(int, Class), replaced with indexOfThrowable(Class, int) to make signature consistent with other APIs;
changed functionality of methods that receive an index argument to throw an IndexOutOfBoundsException if the index is specified incorrectly, rather than absorbing invalid indices silently, to bring in line with the String API


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@136951 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Steven Caswell 2002-07-26 20:30:58 +00:00
parent e0340c8969
commit 58503e7eec
3 changed files with 502 additions and 132 deletions

View File

@ -65,7 +65,7 @@ import junit.textui.TestRunner;
*
* @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
* @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
* @version $Id: NestableDelegateTestCase.java,v 1.1 2002/07/19 03:35:55 bayard Exp $
* @version $Id: NestableDelegateTestCase.java,v 1.2 2002/07/26 20:30:58 stevencaswell Exp $
*/
public class NestableDelegateTestCase extends junit.framework.TestCase
{
@ -153,36 +153,36 @@ public class NestableDelegateTestCase extends junit.framework.TestCase
nd2.getMessage("base").equals("base: " + ne2.getCause().getMessage()));
}
public void testNestableDelegateGetLength()
public void testNestableDelegateGetThrowableCount()
{
Nestable n = null;
NestableDelegate d = null;
n = new NestableDelegateTester1();
d = new NestableDelegate(n);
doNestableDelegateGetLength(d, 1);
doNestableDelegateGetThrowableCount(d, 1);
n = new NestableDelegateTester1("level 1");
d = new NestableDelegate(n);
doNestableDelegateGetLength(d, 1);
doNestableDelegateGetThrowableCount(d, 1);
n = new NestableDelegateTester1(new Exception());
d = new NestableDelegate(n);
doNestableDelegateGetLength(d, 2);
doNestableDelegateGetThrowableCount(d, 2);
n = new NestableDelegateTester1(new Exception("level 2"));
d = new NestableDelegate(n);
doNestableDelegateGetLength(d, 2);
doNestableDelegateGetThrowableCount(d, 2);
n = new NestableDelegateTester1("level 1", new NestableDelegateTester2("level 2", new NestableDelegateTester1(new NestableDelegateTester2("level 4", new Exception("level 5")))));
d = new NestableDelegate(n);
doNestableDelegateGetLength(d, 5);
doNestableDelegateGetThrowableCount(d, 5);
}
private void doNestableDelegateGetLength(NestableDelegate d, int len)
private void doNestableDelegateGetThrowableCount(NestableDelegate d, int len)
{
// Compare the lengths
assertEquals("delegate length", len, d.getLength());
assertEquals("delegate length", len, d.getThrowableCount());
}
public void testNestableDelegateGetMessages()
@ -252,11 +252,27 @@ public class NestableDelegateTestCase extends junit.framework.TestCase
{
assertEquals("message " + i, msgs[i], d.getMessage(i));
}
assertEquals("message -1", msgs[0], d.getMessage(-1));
assertEquals("message -1", msgs[msgs.length - 1], d.getMessage(msgs.length + 100));
// Test for index out of bounds
try
{
String msg = d.getMessage(-1);
fail("getMessage(-1) should have thrown IndexOutOfBoundsException");
}
catch(IndexOutOfBoundsException ioode)
{
}
try
{
String msg = d.getMessage(msgs.length + 100);
fail("getMessage(999) should have thrown IndexOutOfBoundsException");
}
catch(IndexOutOfBoundsException ioode)
{
}
}
public void testNestableDelegateGetThrowable()
public void testNestableDelegateGetThrowableN()
{
Nestable n = null;
NestableDelegate d = null;
@ -271,7 +287,7 @@ public class NestableDelegateTestCase extends junit.framework.TestCase
throwables[1] = Exception.class;
n = new NestableDelegateTester1(new Exception(msgs[1]));
d = new NestableDelegate(n);
doNestableDelegateGetThrowable(d, throwables, msgs);
doNestableDelegateGetThrowableN(d, throwables, msgs);
msgs = new String[5];
msgs[0] = "level 1";
@ -287,10 +303,10 @@ public class NestableDelegateTestCase extends junit.framework.TestCase
throwables[4] = Exception.class;
n = new NestableDelegateTester1(msgs[0], new NestableDelegateTester2(msgs[1], new NestableDelegateTester1(new NestableDelegateTester2(msgs[3], new Exception(msgs[4])))));
d = new NestableDelegate(n);
doNestableDelegateGetThrowable(d, throwables, msgs);
doNestableDelegateGetThrowableN(d, throwables, msgs);
}
private void doNestableDelegateGetThrowable(NestableDelegate d, Class[] classes, String[] msgs)
private void doNestableDelegateGetThrowableN(NestableDelegate d, Class[] classes, String[] msgs)
{
Throwable t = null;
String msg = null;
@ -309,28 +325,24 @@ public class NestableDelegateTestCase extends junit.framework.TestCase
}
assertEquals("throwable message", msgs[i], msg);
}
t = d.getThrowable(-1);
assertEquals("throwable(-1)", classes[0], t.getClass());
if(Nestable.class.isInstance(t))
// Test for index out of bounds
try
{
msg = ((Nestable) t).getMessage(0);
t = d.getThrowable(-1);
fail("getThrowable(-1) should have thrown IndexOutOfBoundsException");
}
else
catch(IndexOutOfBoundsException ioobe)
{
msg = t.getMessage();
}
assertEquals("throwable message", msgs[0], msg);
t = d.getThrowable(999);
assertEquals("throwable(999)", classes[classes.length - 1], t.getClass());
if(Nestable.class.isInstance(t))
try
{
msg = ((Nestable) t).getMessage(0);
t = d.getThrowable(999);
fail("getThrowable(999) should have thrown IndexOutOfBoundsException");
}
else
catch(IndexOutOfBoundsException ioobe)
{
msg = t.getMessage();
}
assertEquals("throwable message", msgs[msgs.length - 1], msg);
}
public void testNestableDelegateGetThrowables()
@ -418,28 +430,52 @@ public class NestableDelegateTestCase extends junit.framework.TestCase
}
doNestableDelegateIndexOfThrowable(d, NestableDelegateTester2.class, 2, 3, msgs[3]);
doNestableDelegateIndexOfThrowable(d, NestableDelegateTester1.class, 1, 2, msgs[2]);
doNestableDelegateIndexOfThrowable(d, NestableDelegateTester1.class, 3, -1, null);
doNestableDelegateIndexOfThrowable(d, NestableDelegateTester1.class, 4, -1, null);
doNestableDelegateIndexOfThrowable(d, Exception.class, 2, 4, msgs[4]);
doNestableDelegateIndexOfThrowable(d, java.util.Date.class, 0, -1, null);
// Test for index out of bounds
try
{
int index = d.indexOfThrowable(NestableDelegateTester1.class, -1);
fail("method should have thrown IndexOutOfBoundsException");
}
catch(IndexOutOfBoundsException iooob)
{
}
try
{
int index = d.indexOfThrowable(NestableDelegateTester1.class, 5);
fail("method should have thrown IndexOutOfBoundsException");
}
catch(IndexOutOfBoundsException iooob)
{
}
}
private void doNestableDelegateIndexOfThrowable(NestableDelegate d, Class type, int pos, int expectedIndex, String expectedMsg)
private void doNestableDelegateIndexOfThrowable(NestableDelegate d, Class type, int fromIndex, int expectedIndex, String expectedMsg)
{
Throwable t = null;
int index = d.indexOfThrowable(pos, type);
int index = d.indexOfThrowable(type, fromIndex);
assertEquals("index of throwable " + type.getName(), expectedIndex, index);
t = d.getThrowable(index);
if(expectedMsg != null)
if(expectedIndex > -1)
{
String msg = null;
if(Nestable.class.isInstance(t))
t = d.getThrowable(index);
if(expectedMsg != null)
{
msg = ((Nestable) t).getMessage(0);
String msg = null;
if(Nestable.class.isInstance(t))
{
msg = ((Nestable) t).getMessage(0);
}
else
{
msg = t.getMessage();
}
assertEquals("message of indexed throwable", expectedMsg, msg);
}
else
{
msg = t.getMessage();
}
assertEquals("message of indexed throwable", expectedMsg, msg);
}
}
@ -571,7 +607,7 @@ class NestableDelegateTester1 extends Exception implements Nestable
* Returns the number of nested <code>Throwable</code>s represented by
* this <code>Nestable</code>, including this <code>Nestable</code>.
*/
public int getLength()
public int getThrowableCount()
{
return 1;
}
@ -639,6 +675,36 @@ class NestableDelegateTester1 extends Exception implements Nestable
* @return index of the first occurrence of the type in the chain, or -1 if
* the type is not found
*/
public int indexOfThrowable(Class type, int fromIndex)
{
return -1;
}
/** Returns the number of nested <code>Throwable</code>s represented by
* this <code>Nestable</code>, including this <code>Nestable</code>.
* @deprecated
*/
public int getLength()
{
return 1;
}
/** Returns the index, numbered from 0, of the first <code>Throwable</code>
* that matches the specified type in the chain of <code>Throwable</code>s
* with an index greater than or equal to the specified position, or -1 if
* the type is not found. If <code>pos</code> is negative, the effect is the
* same as if it were 0. If <code>pos</code> is greater than or equal to the
* length of the chain, the effect is the same as if it were the index of
* the last element in the chain.
*
* @param pos index, numbered from 0, of the starting position in the chain
* to be searched
* @param type <code>Class</code> to be found
*
* @return index of the first occurrence of the type in the chain, or -1 if
* the type is not found
* @deprecated replaced by {@link #indexOfThrowable(Class, int)}
*/
public int indexOfThrowable(int pos, Class type)
{
return -1;
@ -735,7 +801,7 @@ class NestableDelegateTester2 extends Throwable implements Nestable
* Returns the number of nested <code>Throwable</code>s represented by
* this <code>Nestable</code>, including this <code>Nestable</code>.
*/
public int getLength()
public int getThrowableCount()
{
return 1;
}
@ -803,6 +869,36 @@ class NestableDelegateTester2 extends Throwable implements Nestable
* @return index of the first occurrence of the type in the chain, or -1 if
* the type is not found
*/
public int indexOfThrowable(Class type, int fromIndex)
{
return -1;
}
/** Returns the number of nested <code>Throwable</code>s represented by
* this <code>Nestable</code>, including this <code>Nestable</code>.
* @deprecated
*/
public int getLength()
{
return 1;
}
/** Returns the index, numbered from 0, of the first <code>Throwable</code>
* that matches the specified type in the chain of <code>Throwable</code>s
* with an index greater than or equal to the specified position, or -1 if
* the type is not found. If <code>pos</code> is negative, the effect is the
* same as if it were 0. If <code>pos</code> is greater than or equal to the
* length of the chain, the effect is the same as if it were the index of
* the last element in the chain.
*
* @param pos index, numbered from 0, of the starting position in the chain
* to be searched
* @param type <code>Class</code> to be found
*
* @return index of the first occurrence of the type in the chain, or -1 if
* the type is not found
* @deprecated replaced by {@link #indexOfThrowable(Class, int)}
*/
public int indexOfThrowable(int pos, Class type)
{
return -1;
@ -818,7 +914,7 @@ class ThrowableNestable extends Throwable implements Nestable
* Returns the number of nested <code>Throwable</code>s represented by
* this <code>Nestable</code>, including this <code>Nestable</code>.
*/
public int getLength()
public int getThrowableCount()
{
return 1;
}
@ -929,6 +1025,36 @@ class ThrowableNestable extends Throwable implements Nestable
* @return index of the first occurrence of the type in the chain, or -1 if
* the type is not found
*/
public int indexOfThrowable(Class type, int fromIndex)
{
return indexOfThrowable(type);
}
/** Returns the number of nested <code>Throwable</code>s represented by
* this <code>Nestable</code>, including this <code>Nestable</code>.
* @deprecated
*/
public int getLength()
{
return 1;
}
/** Returns the index, numbered from 0, of the first <code>Throwable</code>
* that matches the specified type in the chain of <code>Throwable</code>s
* with an index greater than or equal to the specified position, or -1 if
* the type is not found. If <code>pos</code> is negative, the effect is the
* same as if it were 0. If <code>pos</code> is greater than or equal to the
* length of the chain, the effect is the same as if it were the index of
* the last element in the chain.
*
* @param pos index, numbered from 0, of the starting position in the chain
* to be searched
* @param type <code>Class</code> to be found
*
* @return index of the first occurrence of the type in the chain, or -1 if
* the type is not found
* @deprecated replaced by {@link #indexOfThrowable(Class, int)}
*/
public int indexOfThrowable(int pos, Class type)
{
return indexOfThrowable(type);
@ -945,7 +1071,7 @@ class ThrowableNestedNestable extends Throwable implements Nestable
this.cause = cause;
}
public int getLength()
public int getThrowableCount()
{
return 1;
}
@ -1046,6 +1172,36 @@ class ThrowableNestedNestable extends Throwable implements Nestable
* @return index of the first occurrence of the type in the chain, or -1 if
* the type is not found
*/
public int indexOfThrowable(Class type, int fromIndex)
{
return indexOfThrowable(type);
}
/** Returns the number of nested <code>Throwable</code>s represented by
* this <code>Nestable</code>, including this <code>Nestable</code>.
* @deprecated
*/
public int getLength()
{
return 1;
}
/** Returns the index, numbered from 0, of the first <code>Throwable</code>
* that matches the specified type in the chain of <code>Throwable</code>s
* with an index greater than or equal to the specified position, or -1 if
* the type is not found. If <code>pos</code> is negative, the effect is the
* same as if it were 0. If <code>pos</code> is greater than or equal to the
* length of the chain, the effect is the same as if it were the index of
* the last element in the chain.
*
* @param pos index, numbered from 0, of the starting position in the chain
* to be searched
* @param type <code>Class</code> to be found
*
* @return index of the first occurrence of the type in the chain, or -1 if
* the type is not found
* @deprecated replaced by {@link #indexOfThrowable(Class, int)}
*/
public int indexOfThrowable(int pos, Class type)
{
return indexOfThrowable(type);
@ -1055,7 +1211,7 @@ class ThrowableNestedNestable extends Throwable implements Nestable
class NonThrowableNestable implements Nestable
{
public int getLength()
public int getThrowableCount()
{
return 1;
}
@ -1148,6 +1304,36 @@ class NonThrowableNestable implements Nestable
* @return index of the first occurrence of the type in the chain, or -1 if
* the type is not found
*/
public int indexOfThrowable(Class type, int fromIndex)
{
return -1;
}
/** Returns the number of nested <code>Throwable</code>s represented by
* this <code>Nestable</code>, including this <code>Nestable</code>.
* @deprecated
*/
public int getLength()
{
return 1;
}
/** Returns the index, numbered from 0, of the first <code>Throwable</code>
* that matches the specified type in the chain of <code>Throwable</code>s
* with an index greater than or equal to the specified position, or -1 if
* the type is not found. If <code>pos</code> is negative, the effect is the
* same as if it were 0. If <code>pos</code> is greater than or equal to the
* length of the chain, the effect is the same as if it were the index of
* the last element in the chain.
*
* @param pos index, numbered from 0, of the starting position in the chain
* to be searched
* @param type <code>Class</code> to be found
*
* @return index of the first occurrence of the type in the chain, or -1 if
* the type is not found
* @deprecated replaced by {@link #indexOfThrowable(Class, int)}
*/
public int indexOfThrowable(int pos, Class type)
{
return -1;

View File

@ -64,7 +64,7 @@ import junit.textui.TestRunner;
* Tests the org.apache.commons.lang.exception.NestableException class.
*
* @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
* @version $Id: NestableExceptionTestCase.java,v 1.1 2002/07/19 03:35:55 bayard Exp $
* @version $Id: NestableExceptionTestCase.java,v 1.2 2002/07/26 20:30:58 stevencaswell Exp $
*/
public class NestableExceptionTestCase extends junit.framework.TestCase
{
@ -128,6 +128,7 @@ public class NestableExceptionTestCase extends junit.framework.TestCase
public void testGetLength()
{
// test the deprecated method
NestableException ne1 = new NestableException();
assertEquals("ne1 length", 1, ne1.getLength());
@ -153,6 +154,34 @@ public class NestableExceptionTestCase extends junit.framework.TestCase
assertEquals("ne 8 length", 5, ne8.getLength());
}
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();
@ -190,7 +219,7 @@ public class NestableExceptionTestCase extends junit.framework.TestCase
}
public void testGetMessageN()
public void testGetMessageI()
{
String[] msgs = new String[5];
msgs[0] = "level 1";
@ -203,8 +232,24 @@ public class NestableExceptionTestCase extends junit.framework.TestCase
{
assertEquals("message " + i, msgs[i], ne.getMessage(i));
}
assertEquals("message -1", msgs[0], ne.getMessage(-1));
assertEquals("message 999", msgs[4], ne.getMessage(999));
// 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()
@ -224,7 +269,7 @@ public class NestableExceptionTestCase extends junit.framework.TestCase
}
}
public void testGetThrowable()
public void testGetThrowableI()
{
Nestable n = null;
String msgs[] = null;
@ -237,7 +282,7 @@ public class NestableExceptionTestCase extends junit.framework.TestCase
throwables[0] = NestableExceptionTester1.class;
throwables[1] = Exception.class;
n = new NestableExceptionTester1(new Exception(msgs[1]));
doNestableExceptionGetThrowable(n, throwables, msgs);
doNestableExceptionGetThrowableI(n, throwables, msgs);
msgs = new String[5];
msgs[0] = "level 1";
@ -252,10 +297,10 @@ public class NestableExceptionTestCase extends junit.framework.TestCase
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])))));
doNestableExceptionGetThrowable(n, throwables, msgs);
doNestableExceptionGetThrowableI(n, throwables, msgs);
}
private void doNestableExceptionGetThrowable(Nestable n, Class[] classes, String[] msgs)
private void doNestableExceptionGetThrowableI(Nestable n, Class[] classes, String[] msgs)
{
Throwable t = null;
String msg = null;
@ -274,28 +319,24 @@ public class NestableExceptionTestCase extends junit.framework.TestCase
}
assertEquals("throwable message", msgs[i], msg);
}
t = n.getThrowable(-1);
assertEquals("throwable(-1)", classes[0], t.getClass());
if(Nestable.class.isInstance(t))
// Test for index out of bounds
try
{
msg = ((Nestable) t).getMessage(0);
t = n.getThrowable(-1);
fail("getThrowable(-1) should have thrown IndexOutOfBoundsException");
}
else
catch(IndexOutOfBoundsException ioobe)
{
msg = t.getMessage();
}
assertEquals("throwable message", msgs[0], msg);
t = n.getThrowable(999);
assertEquals("throwable(999)", classes[classes.length - 1], t.getClass());
if(Nestable.class.isInstance(t))
try
{
msg = ((Nestable) t).getMessage(0);
t = n.getThrowable(999);
fail("getThrowable(999) should have thrown IndexOutOfBoundsException");
}
else
catch(IndexOutOfBoundsException ioobe)
{
msg = t.getMessage();
}
assertEquals("throwable message", msgs[msgs.length - 1], msg);
}
public void testGetThrowables()
@ -384,23 +425,26 @@ public class NestableExceptionTestCase extends junit.framework.TestCase
int index = n.indexOfThrowable(type);
assertEquals("index of throwable " + type.getName(), expectedIndex, index);
t = n.getThrowable(index);
if(expectedMsg != null)
if(expectedIndex > -1)
{
String msg = null;
if(Nestable.class.isInstance(t))
t = n.getThrowable(index);
if(expectedMsg != null)
{
msg = ((Nestable) t).getMessage(0);
String msg = null;
if(Nestable.class.isInstance(t))
{
msg = ((Nestable) t).getMessage(0);
}
else
{
msg = t.getMessage();
}
assertEquals("message of indexed throwable", expectedMsg, msg);
}
else
{
msg = t.getMessage();
}
assertEquals("message of indexed throwable", expectedMsg, msg);
}
}
public void testIndexOfThrowableN()
public void testIndexOfThrowableI()
{
Nestable n = null;
String msgs[] = null;
@ -422,32 +466,83 @@ public class NestableExceptionTestCase extends junit.framework.TestCase
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++)
{
doNestableExceptionIndexOfThrowableN(n, throwables[i], 0, indexes[i], msgs[indexes[i]]);
doNestableExceptionIndexOfThrowableI(n, throwables[i], 0, indexes[i], msgs[indexes[i]]);
}
doNestableExceptionIndexOfThrowableN(n, NestableExceptionTester2.class, 2, 3, msgs[3]);
doNestableExceptionIndexOfThrowableN(n, NestableExceptionTester1.class, 1, 2, msgs[2]);
doNestableExceptionIndexOfThrowableN(n, java.util.Date.class, 0, -1, null);
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)
{
}
// test the deprecated method
int index = n.indexOfThrowable(-1, NestableExceptionTester1.class);
assertEquals("deprecated method index", 0, index);
index = n.indexOfThrowable(999, Exception.class);
assertEquals("deprecated method index", 4, index);
}
private void doNestableExceptionIndexOfThrowableN(Nestable n, Class type, int pos, int expectedIndex, String expectedMsg)
private void doNestableExceptionIndexOfThrowableI(Nestable n, Class type, int fromIndex, int expectedIndex, String expectedMsg)
{
Throwable t = null;
int index = n.indexOfThrowable(pos, type);
int index = n.indexOfThrowable(type, fromIndex);
assertEquals("index of throwable " + type.getName(), expectedIndex, index);
t = n.getThrowable(index);
if(expectedMsg != null)
if(expectedIndex > -1)
{
String msg = null;
if(Nestable.class.isInstance(t))
t = n.getThrowable(index);
if(expectedMsg != null)
{
msg = ((Nestable) t).getMessage(0);
String msg = null;
if(Nestable.class.isInstance(t))
{
msg = ((Nestable) t).getMessage(0);
}
else
{
msg = t.getMessage();
}
assertEquals("message of indexed throwable", expectedMsg, msg);
}
else
}
// test the deprecated method
int index1 = n.indexOfThrowable(fromIndex, type);
assertEquals("index of throwable " + type.getName(), expectedIndex, index);
if(expectedIndex > -1)
{
t = n.getThrowable(index1);
if(expectedMsg != null)
{
msg = t.getMessage();
String msg = null;
if(Nestable.class.isInstance(t))
{
msg = ((Nestable) t).getMessage(0);
}
else
{
msg = t.getMessage();
}
assertEquals("message of indexed throwable", expectedMsg, msg);
}
assertEquals("message of indexed throwable", expectedMsg, msg);
}
}

View File

@ -64,7 +64,7 @@ import junit.textui.TestRunner;
* Tests the org.apache.commons.lang.exception.NestableRuntimeException class.
*
* @author <a href="mailto:steven@caswell.name">Steven Caswell</a>
* @version $Id: NestableRuntimeExceptionTestCase.java,v 1.1 2002/07/19 03:35:56 bayard Exp $
* @version $Id: NestableRuntimeExceptionTestCase.java,v 1.2 2002/07/26 20:30:58 stevencaswell Exp $
*/
public class NestableRuntimeExceptionTestCase extends junit.framework.TestCase
{
@ -125,8 +125,36 @@ public class NestableRuntimeExceptionTestCase extends junit.framework.TestCase
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 testGetLength()
{
// test the deprecated method
NestableRuntimeException ne1 = new NestableRuntimeException();
assertEquals("ne1 length", 1, ne1.getLength());
@ -151,7 +179,7 @@ public class NestableRuntimeExceptionTestCase extends junit.framework.TestCase
NestableRuntimeException ne8 = new NestableRuntimeException("level 1", new NestableRuntimeException("level 2", new NestableRuntimeException(new NestableRuntimeException("level 4", new Exception("level 5")))));
assertEquals("ne 8 length", 5, ne8.getLength());
}
public void testGetMessage()
{
NestableRuntimeException ne1 = new NestableRuntimeException();
@ -188,7 +216,7 @@ public class NestableRuntimeExceptionTestCase extends junit.framework.TestCase
ne7.getMessage().equals("ne7o: ne7i: ne7 exception"));
}
public void testGetMessageN()
public void testGetMessageI()
{
String[] msgs = new String[5];
msgs[0] = "level 1";
@ -201,8 +229,24 @@ public class NestableRuntimeExceptionTestCase extends junit.framework.TestCase
{
assertEquals("message " + i, msgs[i], ne.getMessage(i));
}
assertEquals("message -1", msgs[0], ne.getMessage(-1));
assertEquals("message 999", msgs[4], ne.getMessage(999));
// 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()
@ -222,7 +266,7 @@ public class NestableRuntimeExceptionTestCase extends junit.framework.TestCase
}
}
public void testGetThrowable()
public void testGetThrowableI()
{
Nestable n = null;
String msgs[] = null;
@ -235,7 +279,7 @@ public class NestableRuntimeExceptionTestCase extends junit.framework.TestCase
throwables[0] = NestableRuntimeExceptionTester1.class;
throwables[1] = Exception.class;
n = new NestableRuntimeExceptionTester1(new Exception(msgs[1]));
doNestableRuntimeExceptionGetThrowable(n, throwables, msgs);
doNestableRuntimeExceptionGetThrowableI(n, throwables, msgs);
msgs = new String[5];
msgs[0] = "level 1";
@ -250,10 +294,10 @@ public class NestableRuntimeExceptionTestCase extends junit.framework.TestCase
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])))));
doNestableRuntimeExceptionGetThrowable(n, throwables, msgs);
doNestableRuntimeExceptionGetThrowableI(n, throwables, msgs);
}
private void doNestableRuntimeExceptionGetThrowable(Nestable n, Class[] classes, String[] msgs)
private void doNestableRuntimeExceptionGetThrowableI(Nestable n, Class[] classes, String[] msgs)
{
Throwable t = null;
String msg = null;
@ -272,28 +316,24 @@ public class NestableRuntimeExceptionTestCase extends junit.framework.TestCase
}
assertEquals("throwable message", msgs[i], msg);
}
t = n.getThrowable(-1);
assertEquals("throwable(-1)", classes[0], t.getClass());
if(Nestable.class.isInstance(t))
// Test for index out of bounds
try
{
msg = ((Nestable) t).getMessage(0);
t = n.getThrowable(-1);
fail("getThrowable(-1) should have thrown IndexOutOfBoundsException");
}
else
catch(IndexOutOfBoundsException ioobe)
{
msg = t.getMessage();
}
assertEquals("throwable message", msgs[0], msg);
t = n.getThrowable(999);
assertEquals("throwable(999)", classes[classes.length - 1], t.getClass());
if(Nestable.class.isInstance(t))
try
{
msg = ((Nestable) t).getMessage(0);
t = n.getThrowable(999);
fail("getThrowable(999) should have thrown IndexOutOfBoundsException");
}
else
catch(IndexOutOfBoundsException ioobe)
{
msg = t.getMessage();
}
assertEquals("throwable message", msgs[msgs.length - 1], msg);
}
public void testGetThrowables()
@ -382,9 +422,9 @@ public class NestableRuntimeExceptionTestCase extends junit.framework.TestCase
int index = n.indexOfThrowable(type);
assertEquals("index of throwable " + type.getName(), expectedIndex, index);
t = n.getThrowable(index);
if(expectedMsg != null)
{
t = n.getThrowable(index);
String msg = null;
if(Nestable.class.isInstance(t))
{
@ -398,7 +438,7 @@ public class NestableRuntimeExceptionTestCase extends junit.framework.TestCase
}
}
public void testIndexOfThrowableN()
public void testIndexOfThrowableI()
{
Nestable n = null;
String msgs[] = null;
@ -420,32 +460,81 @@ public class NestableRuntimeExceptionTestCase extends junit.framework.TestCase
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++)
{
doNestableRuntimeExceptionIndexOfThrowableN(n, throwables[i], 0, indexes[i], msgs[indexes[i]]);
doNestableRuntimeExceptionIndexOfThrowableI(n, throwables[i], 0, indexes[i], msgs[indexes[i]]);
}
doNestableRuntimeExceptionIndexOfThrowableN(n, NestableRuntimeExceptionTester2.class, 2, 3, msgs[3]);
doNestableRuntimeExceptionIndexOfThrowableN(n, NestableRuntimeExceptionTester1.class, 1, 2, msgs[2]);
doNestableRuntimeExceptionIndexOfThrowableN(n, java.util.Date.class, 0, -1, null);
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)
{
}
// test the deprecated method
int index = n.indexOfThrowable(-1, NestableRuntimeExceptionTester1.class);
assertEquals("deprecated method index", 0, index);
index = n.indexOfThrowable(999, Exception.class);
assertEquals("deprecated method index", 4, index);
}
private void doNestableRuntimeExceptionIndexOfThrowableN(Nestable n, Class type, int pos, int expectedIndex, String expectedMsg)
private void doNestableRuntimeExceptionIndexOfThrowableI(Nestable n, Class type, int fromIndex, int expectedIndex, String expectedMsg)
{
Throwable t = null;
int index = n.indexOfThrowable(pos, type);
int index = n.indexOfThrowable(type, fromIndex);
assertEquals("index of throwable " + type.getName(), expectedIndex, index);
t = n.getThrowable(index);
if(expectedMsg != null)
if(expectedIndex > -1)
{
String msg = null;
if(Nestable.class.isInstance(t))
t = n.getThrowable(index);
if(expectedMsg != null)
{
msg = ((Nestable) t).getMessage(0);
String msg = null;
if(Nestable.class.isInstance(t))
{
msg = ((Nestable) t).getMessage(0);
}
else
{
msg = t.getMessage();
}
assertEquals("message of indexed throwable", expectedMsg, msg);
}
else
}
// test the deprecated method
int index1 = n.indexOfThrowable(fromIndex, type);
assertEquals("index of throwable " + type.getName(), expectedIndex, index);
if(expectedIndex > -1)
{
t = n.getThrowable(index);
if(expectedMsg != null)
{
msg = t.getMessage();
String msg = null;
if(Nestable.class.isInstance(t))
{
msg = ((Nestable) t).getMessage(0);
}
else
{
msg = t.getMessage();
}
assertEquals("message of indexed throwable", expectedMsg, msg);
}
assertEquals("message of indexed throwable", expectedMsg, msg);
}
}