scolebourne    2003/07/16 16:56:45

  Modified:    lang/src/test/org/apache/commons/lang StringUtilsTest.java
               lang/src/java/org/apache/commons/lang StringUtils.java
  Log:
  Update Null handling in StringUtils to handle nulls quietly
  
  Revision  Changes    Path
  1.25      +2 -6      
jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsTest.java
  
  Index: StringUtilsTest.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsTest.java,v
  retrieving revision 1.24
  retrieving revision 1.25
  diff -u -r1.24 -r1.25
  --- StringUtilsTest.java      16 Jul 2003 23:45:39 -0000      1.24
  +++ StringUtilsTest.java      16 Jul 2003 23:56:44 -0000      1.25
  @@ -279,16 +279,12 @@
       }
   
       public void testRepeat() {
  +        assertEquals(null, StringUtils.repeat(null, 2));
           assertEquals("", StringUtils.repeat("ab", 0));
           assertEquals("", StringUtils.repeat("", 3));
           assertEquals("aaa", StringUtils.repeat("a", 3));
           assertEquals("ababab", StringUtils.repeat("ab", 3));
           assertEquals("abcabcabc", StringUtils.repeat("abc", 3));
  -        try {
  -            StringUtils.repeat(null, 0);
  -            fail();
  -        } catch (NullPointerException ex) {
  -        }
       }
   
       public void testCenter() {
  
  
  
  1.61      +24 -15    
jakarta-commons/lang/src/java/org/apache/commons/lang/StringUtils.java
  
  Index: StringUtils.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/StringUtils.java,v
  retrieving revision 1.60
  retrieving revision 1.61
  diff -u -r1.60 -r1.61
  --- StringUtils.java  16 Jul 2003 23:45:39 -0000      1.60
  +++ StringUtils.java  16 Jul 2003 23:56:45 -0000      1.61
  @@ -71,8 +71,15 @@
    *  <li>whitespace - the characters defined by [EMAIL PROTECTED] 
Character#isWhitespace(char)}
    * </ul>
    * 
  - * <p>The <code>StringUtils</code> class varies in its handling of
  - * <code>null</code>. Each method should be consulted individually.</p>
  + * <p>The <code>StringUtils</code> tries to handle <code>null</code> input
  + * quietly. That is to say that a <code>null</code> will generally return a 
  + * sensible value rather than throw an exception.
  + * Typically, <code>null</code> in gives <code>null</code> out.
  + * Each method should be consulted individually for full details.</p>
  + * 
  + * <p>A side effect of the <code>null</code> handling is that a 
  + * NullPointerException should be considered a bug in <code>StringUtils</code>.
  + * (Except for deprecated methods).</p>
    *
    * @author <a href="http://jakarta.apache.org/turbine/";>Apache Jakarta Turbine</a>
    * @author GenerationJavaCore
  @@ -1626,25 +1633,27 @@
        * new string.</p>
        *
        * <pre>
  +     * StringUtils.repeat(null, 2) = null
        * StringUtils.repeat("", 0)   = ""
        * StringUtils.repeat("", 2)   = ""
        * StringUtils.repeat("a", 3)  = "aaa"
        * StringUtils.repeat("ab", 2) = "abab"
  -     * StringUtils.repeat(null, 2) = NullPointerException
  -     * StringUtils.repeat("a", -2) = NegativeArraySizeException
  +     * StringUtils.repeat("a", -2) = ""
        * </pre>
        *
  -     * @param str  the String to repeat, must not be null
  -     * @param repeat  number of times to repeat str
  -     * @return a new String consisting of the original String repeated
  -     * @throws NegativeArraySizeException if <code>repeat &lt; 0</code>
  -     * @throws NullPointerException if str is <code>null</code>
  +     * @param str  the String to repeat, may be null
  +     * @param repeat  number of times to repeat str, negative treated as zero
  +     * @return a new String consisting of the original String repeated,
  +     *  <code>null</code> if null string input
        */
       public static String repeat(String str, int repeat) {
  -        int inputLength = str.length();
  -        if (repeat == 0) {
  +        if (str == null) {
  +            return null;
  +        }
  +        if (repeat <= 0) {
               return "";
           }
  +        int inputLength = str.length();
           if (inputLength == 1 && repeat <= PAD_LIMIT) {
              return padding(repeat, str.charAt(0));
           }
  @@ -1904,8 +1913,8 @@
        * StringUtils.leftPad("bat", 8, "yz")  = "yzyzybat"
        * StringUtils.leftPad("bat", 1, "yz")  = "bat"
        * StringUtils.leftPad("bat", -1, "yz") = "bat"
  -     * StringUtils.leftPad("bat", 1, null)  = NullPointerException
  -     * StringUtils.leftPad("bat", 1, "")    = ArithmeticException
  +     * StringUtils.leftPad("bat", 1, null)  = IllegalArgumentException
  +     * StringUtils.leftPad("bat", 1, "")    = IllegalArgumentException
        * </pre>
        *
        * @param str  the String to pad out, may be null
  @@ -2841,7 +2850,7 @@
       /**
        * <p>Find the Levenshtein distance between two Strings.</p>
        *
  -     * <P>This is the number of changes needed to change one String into
  +     * <p>This is the number of changes needed to change one String into
        * another. Where each change is a single character modification.</p>
        *
        * <p>This implemmentation of the levenshtein distance algorithm
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to