The attached patch slightly increments the test coverage for
NestableDelegate. It also includes a small rework of the getMessage() method to
minimize StringBuffer creation. The application of the latter piece is up to
the discretion of the committers, obviously. -Nathan Beyer |
Index: src/test/org/apache/commons/lang/exception/NestableDelegateTestCase.java =================================================================== --- src/test/org/apache/commons/lang/exception/NestableDelegateTestCase.java (revision 230383) +++ src/test/org/apache/commons/lang/exception/NestableDelegateTestCase.java (working copy) @@ -215,6 +215,25 @@ 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() { Index: src/java/org/apache/commons/lang/exception/NestableDelegate.java =================================================================== --- src/java/org/apache/commons/lang/exception/NestableDelegate.java (revision 230383) +++ src/java/org/apache/commons/lang/exception/NestableDelegate.java (working copy) @@ -141,23 +141,18 @@ * @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 (new StringBuffer (baseMsg).append(": ").append(causeMsg).toString()); } /**
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]