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
|
* Returns the error message of the <code>Throwable</code> in the chain of <code>Throwable</code>s at the
|
||||||
* of <code>Throwable</code>s at the specified index, numbered from 0.
|
* specified index, numbered from 0.
|
||||||
*
|
*
|
||||||
* @param index the index of the <code>Throwable</code> in the chain of
|
* @param index
|
||||||
* <code>Throwable</code>s
|
* 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
|
* @return the error message, or null if the <code>Throwable</code> at the specified index in the chain does not
|
||||||
* specified index in the chain does not contain a message
|
* contain a message
|
||||||
* @throws IndexOutOfBoundsException if the <code>index</code> argument is
|
* @throws IndexOutOfBoundsException
|
||||||
* negative or not less than the count of <code>Throwable</code>s in the
|
* if the <code>index</code> argument is negative or not less than the count of <code>Throwable</code>s
|
||||||
* chain
|
* in the chain
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
*/
|
*/
|
||||||
public String getMessage(int index) {
|
public String getMessage(int index) {
|
||||||
Throwable t = this.getThrowable(index);
|
Throwable t = this.getThrowable(index);
|
||||||
if (Nestable.class.isInstance(t)) {
|
if (Nestable.class.isInstance(t)) {
|
||||||
return ((Nestable) t).getMessage(0);
|
return ((Nestable) t).getMessage(0);
|
||||||
} else {
|
|
||||||
return t.getMessage();
|
|
||||||
}
|
}
|
||||||
|
return t.getMessage();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the full message contained by the <code>Nestable</code>
|
* Returns the full message contained by the <code>Nestable</code> and any nested <code>Throwable</code>s.
|
||||||
* and any nested <code>Throwable</code>s.
|
*
|
||||||
*
|
* @param baseMsg
|
||||||
* @param baseMsg the base message to use when creating the full
|
* the base message to use when creating the full message. Should be generally be called via
|
||||||
* message. Should be generally be called via
|
* <code>nestableHelper.getMessage(super.getMessage())</code>, where <code>super</code> is an
|
||||||
* <code>nestableHelper.getMessage(super.getMessage())</code>,
|
* instance of {@link java.lang.Throwable}.
|
||||||
* where <code>super</code> is an instance of {@link
|
* @return The concatenated message for this and all nested <code>Throwable</code>s
|
||||||
* java.lang.Throwable}.
|
|
||||||
* @return The concatenated message for this and all nested
|
|
||||||
* <code>Throwable</code>s
|
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
*/
|
*/
|
||||||
public String getMessage(String baseMsg) {
|
public String getMessage(String baseMsg) {
|
||||||
StringBuffer msg = new StringBuffer();
|
|
||||||
if (baseMsg != null) {
|
|
||||||
msg.append(baseMsg);
|
|
||||||
}
|
|
||||||
|
|
||||||
Throwable nestedCause = ExceptionUtils.getCause(this.nestable);
|
Throwable nestedCause = ExceptionUtils.getCause(this.nestable);
|
||||||
if (nestedCause != null) {
|
String causeMsg = nestedCause == null ? null : nestedCause.getMessage();
|
||||||
String causeMsg = nestedCause.getMessage();
|
if (nestedCause == null || causeMsg == null) {
|
||||||
if (causeMsg != null) {
|
return baseMsg; // may be null, which is a valid result
|
||||||
if (baseMsg != null) {
|
|
||||||
msg.append(": ");
|
|
||||||
}
|
|
||||||
msg.append(causeMsg);
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
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
|
* Returns the error message of this and any nested <code>Throwable</code>s in an array of Strings, one element
|
||||||
* in an array of Strings, one element for each message. Any
|
* for each message. Any <code>Throwable</code> not containing a message is represented in the array by a null.
|
||||||
* <code>Throwable</code> not containing a message is represented in the
|
* This has the effect of cause the length of the returned array to be equal to the result of the
|
||||||
* array by a null. This has the effect of cause the length of the returned
|
* {@link #getThrowableCount()} operation.
|
||||||
* array to be equal to the result of the {@link #getThrowableCount()}
|
*
|
||||||
* operation.
|
|
||||||
*
|
|
||||||
* @return the error messages
|
* @return the error messages
|
||||||
* @since 2.0
|
* @since 2.0
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -215,6 +215,25 @@ private void doNestableDelegateGetMessages(NestableDelegate d, String[] nMsgs)
|
||||||
assertEquals("message " + i, nMsgs[i], dMsgs[i]);
|
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()
|
public void testNestableDelegateGetMessageN()
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue