From 9ebdce7348a1ba1b53bd54f030b1c29f284fb71c Mon Sep 17 00:00:00 2001 From: Phil Steitz Date: Sun, 23 Nov 2003 20:44:39 +0000 Subject: [PATCH] Fixed error in javadoc for StringUtils.split and improved tests. Pr: 24911 Submitted by Al Chou. git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137711 13f79535-47bb-0310-9956-ffa450edef68 --- .../org/apache/commons/lang/StringUtils.java | 14 +++++++---- .../apache/commons/lang/StringUtilsTest.java | 24 +++++++++++++++---- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/src/java/org/apache/commons/lang/StringUtils.java b/src/java/org/apache/commons/lang/StringUtils.java index 20def9ce5..e9b100e19 100644 --- a/src/java/org/apache/commons/lang/StringUtils.java +++ b/src/java/org/apache/commons/lang/StringUtils.java @@ -147,7 +147,7 @@ import java.util.List; * @author Gary Gregory * @author Phil Steitz * @since 1.0 - * @version $Id: StringUtils.java,v 1.117 2003/11/04 21:00:22 fredrik Exp $ + * @version $Id: StringUtils.java,v 1.118 2003/11/23 20:44:39 psteitz Exp $ */ public class StringUtils { // Performance testing notes (JDK 1.4, Jul03, scolebourne) @@ -2087,22 +2087,26 @@ public class StringUtils { } /** - *

Splits the provided text into an array, separators specified. - * This is an alternative to using StringTokenizer.

+ *

Splits the provided text into an array with a maximum length, + * separators specified.

* *

The separator is not included in the returned String array. * Adjacent separators are treated as one separator.

* *

A null input String returns null. * A null separatorChars splits on whitespace.

- * + * + *

If more than max delimited substrings are found, the last + * returned string includes all characters after the first max - 1 + * returned strings (including separator characters).

+ * *
      * StringUtils.split(null, *, *)            = null
      * StringUtils.split("", *, *)              = []
      * StringUtils.split("ab de fg", null, 0)   = ["ab", "cd", "ef"]
      * StringUtils.split("ab   de fg", null, 0) = ["ab", "cd", "ef"]
      * StringUtils.split("ab:cd:ef", ":", 0)    = ["ab", "cd", "ef"]
-     * StringUtils.split("ab:cd:ef", ":", 2)    = ["ab", "cdef"]
+     * StringUtils.split("ab:cd:ef", ":", 2)    = ["ab", "cd:ef"]
      * 
* * @param str the String to parse, may be null diff --git a/src/test/org/apache/commons/lang/StringUtilsTest.java b/src/test/org/apache/commons/lang/StringUtilsTest.java index ca71c1145..66dd6b671 100644 --- a/src/test/org/apache/commons/lang/StringUtilsTest.java +++ b/src/test/org/apache/commons/lang/StringUtilsTest.java @@ -75,7 +75,8 @@ import junit.textui.TestRunner; * @author Henning P. Schmiedehausen * @author Phil Steitz * @author Gary Gregory - * @version $Id: StringUtilsTest.java,v 1.55 2003/11/01 19:20:35 scolebourne Exp $ + * @author Al Chou + * @version $Id: StringUtilsTest.java,v 1.56 2003/11/23 20:44:39 psteitz Exp $ */ public class StringUtilsTest extends TestCase { @@ -280,14 +281,14 @@ public class StringUtilsTest extends TestCase { public void testSplit_String() { assertEquals(null, StringUtils.split(null)); assertEquals(0, StringUtils.split("").length); - + String str = "a b .c"; String[] res = StringUtils.split(str); assertEquals(3, res.length); assertEquals("a", res[0]); assertEquals("b", res[1]); assertEquals(".c", res[2]); - + str = " a "; res = StringUtils.split(str); assertEquals(1, res.length); @@ -297,7 +298,7 @@ public class StringUtilsTest extends TestCase { res = StringUtils.split(str); assertEquals(2, res.length); assertEquals("a", res[0]); - assertEquals("b" + NON_WHITESPACE + "c", res[1]); + assertEquals("b" + NON_WHITESPACE + "c", res[1]); } public void testSplit_StringChar() { @@ -340,6 +341,21 @@ public class StringUtilsTest extends TestCase { innerTestSplit(WHITESPACE.charAt(i), String.valueOf(WHITESPACE.charAt(i)), NON_WHITESPACE.charAt(j)); } } + + String[] results = null; + String[] expectedResults = {"ab", "de fg"}; + results = StringUtils.split("ab de fg", null, 2); + assertEquals(expectedResults.length, results.length); + for (int i = 0; i < expectedResults.length; i++) { + assertEquals(expectedResults[i], results[i]); + } + + String[] expectedResults2 = {"ab", "cd:ef"}; + results = StringUtils.split("ab:cd:ef",":", 2); + assertEquals(expectedResults2.length, results.length); + for (int i = 0; i < expectedResults2.length; i++) { + assertEquals(expectedResults2[i], results[i]); + } } private void innerTestSplit(char separator, String sepStr, char noMatch) {