Slightly increments the test coverage for NestableDelegate. Includes a rework of the getMessage(String) method. Courtesy of Nathan Beyer [nbeyer@kc.rr.com].
git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@230403 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
93a49f6cb0
commit
1225f4309c
|
@ -106,68 +106,54 @@ public NestableDelegate(Nestable nestable) {
|
|||
}
|
||||
|
||||
/**
|
||||
* Returns the error message of the <code>Throwable</code> in the chain
|
||||
* of <code>Throwable</code>s at the specified index, numbered from 0.
|
||||
*
|
||||
* @param index the index of the <code>Throwable</code> in the chain of
|
||||
* <code>Throwable</code>s
|
||||
* @return the error message, or null if the <code>Throwable</code> at the
|
||||
* specified index in the chain does not contain a message
|
||||
* @throws IndexOutOfBoundsException if the <code>index</code> argument is
|
||||
* negative or not less than the count of <code>Throwable</code>s in the
|
||||
* chain
|
||||
* Returns the error message of the <code>Throwable</code> in the chain of <code>Throwable</code>s at the
|
||||
* specified index, numbered from 0.
|
||||
*
|
||||
* @param index
|
||||
* the index of the <code>Throwable</code> in the chain of <code>Throwable</code>s
|
||||
* @return the error message, or null if the <code>Throwable</code> at the specified index in the chain does not
|
||||
* contain a message
|
||||
* @throws IndexOutOfBoundsException
|
||||
* if the <code>index</code> argument is negative or not less than the count of <code>Throwable</code>s
|
||||
* in the chain
|
||||
* @since 2.0
|
||||
*/
|
||||
public String getMessage(int index) {
|
||||
Throwable t = this.getThrowable(index);
|
||||
if (Nestable.class.isInstance(t)) {
|
||||
return ((Nestable) t).getMessage(0);
|
||||
} else {
|
||||
return t.getMessage();
|
||||
}
|
||||
return t.getMessage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the full message contained by the <code>Nestable</code>
|
||||
* and any nested <code>Throwable</code>s.
|
||||
*
|
||||
* @param baseMsg the base message to use when creating the full
|
||||
* message. Should be generally be called via
|
||||
* <code>nestableHelper.getMessage(super.getMessage())</code>,
|
||||
* where <code>super</code> is an instance of {@link
|
||||
* java.lang.Throwable}.
|
||||
* @return The concatenated message for this and all nested
|
||||
* <code>Throwable</code>s
|
||||
* Returns the full message contained by the <code>Nestable</code> and any nested <code>Throwable</code>s.
|
||||
*
|
||||
* @param baseMsg
|
||||
* the base message to use when creating the full message. Should be generally be called via
|
||||
* <code>nestableHelper.getMessage(super.getMessage())</code>, where <code>super</code> is an
|
||||
* instance of {@link java.lang.Throwable}.
|
||||
* @return The concatenated message for this and all nested <code>Throwable</code>s
|
||||
* @since 2.0
|
||||
*/
|
||||
public String getMessage(String baseMsg) {
|
||||
StringBuffer msg = new StringBuffer();
|
||||
if (baseMsg != null) {
|
||||
msg.append(baseMsg);
|
||||
}
|
||||
|
||||
Throwable nestedCause = ExceptionUtils.getCause(this.nestable);
|
||||
if (nestedCause != null) {
|
||||
String causeMsg = nestedCause.getMessage();
|
||||
if (causeMsg != null) {
|
||||
if (baseMsg != null) {
|
||||
msg.append(": ");
|
||||
}
|
||||
msg.append(causeMsg);
|
||||
}
|
||||
|
||||
String causeMsg = nestedCause == null ? null : nestedCause.getMessage();
|
||||
if (nestedCause == null || causeMsg == null) {
|
||||
return baseMsg; // may be null, which is a valid result
|
||||
}
|
||||
return msg.length() > 0 ? msg.toString() : null;
|
||||
if (baseMsg == null) {
|
||||
return causeMsg;
|
||||
}
|
||||
return baseMsg + ": " + causeMsg;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the error message of this and any nested <code>Throwable</code>s
|
||||
* in an array of Strings, one element for each message. Any
|
||||
* <code>Throwable</code> not containing a message is represented in the
|
||||
* array by a null. This has the effect of cause the length of the returned
|
||||
* array to be equal to the result of the {@link #getThrowableCount()}
|
||||
* operation.
|
||||
*
|
||||
* Returns the error message of this and any nested <code>Throwable</code>s in an array of Strings, one element
|
||||
* for each message. Any <code>Throwable</code> not containing a message is represented in the array by a null.
|
||||
* This has the effect of cause the length of the returned array to be equal to the result of the
|
||||
* {@link #getThrowableCount()} operation.
|
||||
*
|
||||
* @return the error messages
|
||||
* @since 2.0
|
||||
*/
|
||||
|
|
|
@ -215,6 +215,25 @@ private void doNestableDelegateGetMessages(NestableDelegate d, String[] nMsgs)
|
|||
assertEquals("message " + i, nMsgs[i], dMsgs[i]);
|
||||
}
|
||||
}
|
||||
|
||||
public void testGetMessageString()
|
||||
{
|
||||
NestableDelegateTester1 ndt1 = new NestableDelegateTester1 (new NullPointerException ());
|
||||
NestableDelegate nd = new NestableDelegate (ndt1);
|
||||
assertNull (nd.getMessage((String)null));
|
||||
|
||||
ndt1 = new NestableDelegateTester1 (new NullPointerException ("null pointer"));
|
||||
nd = new NestableDelegate (ndt1);
|
||||
assertNotNull(nd.getMessage((String)null));
|
||||
|
||||
ndt1 = new NestableDelegateTester1 ();
|
||||
nd = new NestableDelegate (ndt1);
|
||||
assertNull(nd.getMessage((String)null));
|
||||
|
||||
ndt1 = new NestableDelegateTester1 ("root");
|
||||
nd = new NestableDelegate (ndt1);
|
||||
assertNull(nd.getMessage((String)null));
|
||||
}
|
||||
|
||||
public void testNestableDelegateGetMessageN()
|
||||
{
|
||||
|
|
Loading…
Reference in New Issue