You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by gg...@apache.org on 2005/08/05 08:16:22 UTC

svn commit: r230403 - in /jakarta/commons/proper/lang/trunk/src: java/org/apache/commons/lang/exception/NestableDelegate.java test/org/apache/commons/lang/exception/NestableDelegateTestCase.java

Author: ggregory
Date: Thu Aug  4 23:16:19 2005
New Revision: 230403

URL: http://svn.apache.org/viewcvs?rev=230403&view=rev
Log:
Slightly increments the test coverage for NestableDelegate. Includes a rework of the getMessage(String) method. Courtesy of Nathan Beyer [nbeyer@kc.rr.com].

Modified:
    jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/exception/NestableDelegate.java
    jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/exception/NestableDelegateTestCase.java

Modified: jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/exception/NestableDelegate.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/exception/NestableDelegate.java?rev=230403&r1=230402&r2=230403&view=diff
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/exception/NestableDelegate.java (original)
+++ jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/exception/NestableDelegate.java Thu Aug  4 23:16:19 2005
@@ -106,68 +106,54 @@
     }
 
     /**
-     * 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
+        }
+        if (baseMsg == null) {
+            return causeMsg;
         }
-        return msg.length() > 0 ? msg.toString() : null;
+        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
      */

Modified: jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/exception/NestableDelegateTestCase.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/exception/NestableDelegateTestCase.java?rev=230403&r1=230402&r2=230403&view=diff
==============================================================================
--- jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/exception/NestableDelegateTestCase.java (original)
+++ jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/exception/NestableDelegateTestCase.java Thu Aug  4 23:16:19 2005
@@ -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()
     {



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org