diff --git a/src/java/org/apache/commons/lang/exception/NestableDelegate.java b/src/java/org/apache/commons/lang/exception/NestableDelegate.java index 2e43fbb71..650d3b6b8 100644 --- a/src/java/org/apache/commons/lang/exception/NestableDelegate.java +++ b/src/java/org/apache/commons/lang/exception/NestableDelegate.java @@ -106,68 +106,54 @@ public NestableDelegate(Nestable nestable) { } /** - * Returns the error message of the Throwable in the chain - * of Throwables at the specified index, numbered from 0. - * - * @param index the index of the Throwable in the chain of - * Throwables - * @return the error message, or null if the Throwable at the - * specified index in the chain does not contain a message - * @throws IndexOutOfBoundsException if the index argument is - * negative or not less than the count of Throwables in the - * chain + * Returns the error message of the Throwable in the chain of Throwables at the + * specified index, numbered from 0. + * + * @param index + * the index of the Throwable in the chain of Throwables + * @return the error message, or null if the Throwable at the specified index in the chain does not + * contain a message + * @throws IndexOutOfBoundsException + * if the index argument is negative or not less than the count of Throwables + * 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 Nestable - * and any nested Throwables. - * - * @param baseMsg the base message to use when creating the full - * message. Should be generally be called via - * nestableHelper.getMessage(super.getMessage()), - * where super is an instance of {@link - * java.lang.Throwable}. - * @return The concatenated message for this and all nested - * Throwables + * Returns the full message contained by the Nestable and any nested Throwables. + * + * @param baseMsg + * the base message to use when creating the full message. Should be generally be called via + * nestableHelper.getMessage(super.getMessage()), where super is an + * instance of {@link java.lang.Throwable}. + * @return The concatenated message for this and all nested Throwables * @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 Throwables - * in an array of Strings, one element for each message. Any - * Throwable 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 Throwables in an array of Strings, one element + * for each message. Any Throwable 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 */ diff --git a/src/test/org/apache/commons/lang/exception/NestableDelegateTestCase.java b/src/test/org/apache/commons/lang/exception/NestableDelegateTestCase.java index d5225c5db..2ddee3a29 100644 --- a/src/test/org/apache/commons/lang/exception/NestableDelegateTestCase.java +++ b/src/test/org/apache/commons/lang/exception/NestableDelegateTestCase.java @@ -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() {