scolebourne    2003/01/19 10:15:38

  Modified:    lang/src/test/org/apache/commons/lang StringUtilsTest.java
               lang/src/java/org/apache/commons/lang StringUtils.java
  Log:
  Fix infinite recursion in replace() when blank string used
  from Holger Krauth
  
  Revision  Changes    Path
  1.12      +26 -25    
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.11
  retrieving revision 1.12
  diff -u -r1.11 -r1.12
  --- StringUtilsTest.java      7 Dec 2002 21:50:30 -0000       1.11
  +++ StringUtilsTest.java      19 Jan 2003 18:15:38 -0000      1.12
  @@ -1,5 +1,3 @@
  -package org.apache.commons.lang;
  -
   /* ====================================================================
    * The Apache Software License, Version 1.1
    *
  @@ -53,6 +51,7 @@
    * information on the Apache Software Foundation, please see
    * <http://www.apache.org/>.
    */
  +package org.apache.commons.lang;
   
   import java.util.Arrays;
   
  @@ -65,13 +64,14 @@
    *
    * @author <a href="mailto:[EMAIL PROTECTED]";>Daniel Rall</a>
    * @author <a href="mailto:[EMAIL PROTECTED]";>Henri Yandell</a>
  - * @author <a href="mailto:[EMAIL PROTECTED]";>Stephen Colebourne</a>
  + * @author Stephen Colebourne
    * @author <a href="mailto:[EMAIL PROTECTED]";>Ringo De Smet</a>
    * @author <a href="mailto:[EMAIL PROTECTED]>Fredrik Westermarck</a>
  + * @author Holger Krauth
    * @version $Id$
    */
  -public class StringUtilsTest extends TestCase
  -{
  +public class StringUtilsTest extends TestCase {
  +
       private static final String[] ARRAY_LIST = { "foo", "bar", "baz" };
   
       private static final String SEPARATOR = ",";
  @@ -108,8 +108,7 @@
   
       //-----------------------------------------------------------------------
   
  -    public void testCaseFunctions()
  -    {
  +    public void testCaseFunctions() {
           assertEquals("capitalise(String) failed",
                        CAP_FOO, StringUtils.capitalise(FOO) );
           assertEquals("capitalise(empty-string) failed",
  @@ -144,8 +143,7 @@
                        "Hello aPACHE", StringUtils.swapCase("hELLO Apache") );
       }
   
  -    public void testJoin()
  -    {
  +    public void testJoin() {
           assertEquals("concatenate(Object[]) failed", 
                        "foobarbaz", StringUtils.concatenate(ARRAY_LIST));
           assertEquals("join(Object[], String) failed", TEXT_LIST,
  @@ -155,8 +153,7 @@
                                         SEPARATOR));
       }
   
  -    public void testSplit()
  -    {
  +    public void testSplit() {
           String[] result = StringUtils.split(TEXT_LIST, SEPARATOR, 2);
           String[] expected = { "foo", "bar,baz" };
           assertEquals("split(Object[], String, int) yielded unexpected length",
  @@ -193,8 +190,7 @@
           assertEquals("split(Object[], null, int)[2] failed", "three four five six", 
result[2]);
       }
   
  -    public void testReplaceFunctions()
  -    {
  +    public void testReplaceFunctions() {
           assertEquals("replace(String, String, String, int) failed",
                        FOO, StringUtils.replace("oo" + FOO, "o", "", 2));
           assertEquals("replace(String, String, String) failed",
  @@ -203,29 +199,35 @@
                        FOO, StringUtils.replaceOnce(FOO + FOO, FOO, ""));
           assertEquals("carriage-return replace(String,String,String) failed",
                        "test123", StringUtils.replace("test\r1\r2\r3", "\r", ""));
  +
  +        assertEquals("replace(String, String, String) failed",
  +            "FOO", StringUtils.replace("FOO", "", "any"));
  +        assertEquals("replace(String, String, String) failed",
  +            "FOO", StringUtils.replace("FOO", null, "any"));
  +        assertEquals("replace(String, String, String) failed",
  +            "FOO", StringUtils.replace("FOO", "F", null));
  +        assertEquals("replace(String, String, String) failed",
  +            "FOO", StringUtils.replace("FOO", null, null));
  +        assertEquals("replace(String, String, String) failed",
  +            null, StringUtils.replace(null, "", "any"));
       }
   
  -    public void testOverlayString()
  -    {
  +    public void testOverlayString() {
           assertEquals("overlayString(String, String, int, int) failed",
                        "foo foor baz", StringUtils.overlayString(SENTENCE, FOO, 4, 6) 
);
       }
   
  -    public void testRepeat()
  -    {
  +    public void testRepeat() {
           assertEquals("repeat(String, int) failed",
                        FOO + FOO + FOO, StringUtils.repeat(FOO, 3) );
       }
   
  -    public void testCenter()
  -    {
  +    public void testCenter() {
           assertEquals("center(String, int) failed",
                        "   "+FOO+"   ", StringUtils.center(FOO, 9) );
       }
   
  -    public void testChompFunctions()
  -    {
  -
  +    public void testChompFunctions() {
           assertEquals("chomp(String) failed",
                        FOO, StringUtils.chomp(FOO + "\n" + FOO) );
   
  @@ -248,8 +250,7 @@
                        FOO, StringUtils.chopNewline(FOO + "\r\n") );
       }
   
  -    public void testPadFunctions()
  -    {
  +    public void testPadFunctions() {
           assertEquals("rightPad(String, int) failed",
                        "1234    ", StringUtils.rightPad ("1234", 8) );
   
  
  
  
  1.31      +12 -11    
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.30
  retrieving revision 1.31
  diff -u -r1.30 -r1.31
  --- StringUtils.java  23 Dec 2002 00:32:24 -0000      1.30
  +++ StringUtils.java  19 Jan 2003 18:15:38 -0000      1.31
  @@ -73,6 +73,7 @@
    * @author <a href="mailto:[EMAIL PROTECTED]";>Rand McNeely</a>
    * @author Stephen Colebourne
    * @author <a href="mailto:[EMAIL PROTECTED]";>Fredrik Westermarck</a>
  + * @author Holger Krauth
    * @since 1.0
    * @version $Id$
    */
  @@ -584,7 +585,9 @@
       
       /**
        * <p>Replace a String with another String inside a larger String, once.</p>
  -     *
  +     * 
  +     * <p>A <code>null</code> reference passed to this method is a no-op.</p>
  +     * 
        * @see #replace(String text, String repl, String with, int max)
        * @param text text to search and replace in
        * @param repl String to search for
  @@ -598,6 +601,8 @@
       /**
        * <p>Replace all occurances of a String within another String.</p>
        *
  +     * <p>A <code>null</code> reference passed to this method is a no-op.</p>
  +     * 
        * @see #replace(String text, String repl, String with, int max)
        * @param text text to search and replace in
        * @param repl String to search for
  @@ -612,21 +617,17 @@
        * <p>Replace a String with another String inside a larger String,
        * for the first <code>max</code> values of the search String.</p>
        *
  -     * <p>A <code>null</code> reference is passed to this method is a
  -     * no-op.</p>
  +     * <p>A <code>null</code> reference passed to this method is a no-op.</p>
        *
        * @param text text to search and replace in
        * @param repl String to search for
        * @param with String to replace with
  -     * @param max maximum number of values to replace, or
  -     *  <code>-1</code> if no maximum
  +     * @param max maximum number of values to replace, or <code>-1</code> if no 
maximum
        * @return the text with any replacements processed
  -     * @throws NullPointerException if repl is <code>null</code>
        */
  -    public static String replace(String text, String repl, String with,
  -                                 int max) {
  -        if (text == null) {
  -            return null;
  +    public static String replace(String text, String repl, String with, int max) {
  +        if (text == null || repl == null || with == null || repl.length() == 0) {
  +            return text;
           }
   
           StringBuffer buf = new StringBuffer(text.length());
  
  
  

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

Reply via email to