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
This commit is contained in:
Phil Steitz 2003-11-23 20:44:39 +00:00
parent 6b2919f243
commit 9ebdce7348
2 changed files with 29 additions and 9 deletions

View File

@ -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 {
}
/**
* <p>Splits the provided text into an array, separators specified.
* This is an alternative to using StringTokenizer.</p>
* <p>Splits the provided text into an array with a maximum length,
* separators specified.</p>
*
* <p>The separator is not included in the returned String array.
* Adjacent separators are treated as one separator.</p>
*
* <p>A <code>null</code> input String returns <code>null</code>.
* A <code>null</code> separatorChars splits on whitespace.</p>
*
*
* <p>If more than <code>max</code> delimited substrings are found, the last
* returned string includes all characters after the first <code>max - 1</code>
* returned strings (including separator characters).</p>
*
* <pre>
* 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"]
* </pre>
*
* @param str the String to parse, may be null

View File

@ -75,7 +75,8 @@ import junit.textui.TestRunner;
* @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
* @author Phil Steitz
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @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) {