Additional testing, especially in the area of whitespace

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137453 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-07-19 18:10:30 +00:00
parent 6c009fd782
commit 7e0990cccc
3 changed files with 243 additions and 100 deletions

View File

@ -62,7 +62,7 @@ import junit.textui.TestRunner;
* Unit tests {@link org.apache.commons.lang.StringUtils} - Substring methods
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @version $Id: StringUtilsIsTest.java,v 1.5 2003/03/23 21:51:51 scolebourne Exp $
* @version $Id: StringUtilsIsTest.java,v 1.6 2003/07/19 18:10:30 scolebourne Exp $
*/
public class StringUtilsIsTest extends TestCase {
@ -129,6 +129,8 @@ public class StringUtilsIsTest extends TestCase {
assertEquals(false, StringUtils.isWhitespace("a "));
assertEquals(false, StringUtils.isWhitespace(" a"));
assertEquals(false, StringUtils.isWhitespace("aba"));
assertEquals(true, StringUtils.isWhitespace(StringUtilsTest.WHITESPACE));
assertEquals(false, StringUtils.isWhitespace(StringUtilsTest.NON_WHITESPACE));
}
public void testIsAlphaspace() {

View File

@ -56,6 +56,7 @@ package org.apache.commons.lang;
import java.util.Arrays;
import java.util.Iterator;
import junit.framework.AssertionFailedError;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
@ -71,9 +72,37 @@ import junit.textui.TestRunner;
* @author <a href="mailto:fredrik@westermarck.com>Fredrik Westermarck</a>
* @author Holger Krauth
* @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
* @version $Id: StringUtilsTest.java,v 1.28 2003/07/19 00:22:50 scolebourne Exp $
* @version $Id: StringUtilsTest.java,v 1.29 2003/07/19 18:10:30 scolebourne Exp $
*/
public class StringUtilsTest extends TestCase {
static final String WHITESPACE;
static final String NON_WHITESPACE;
static final String TRIMMABLE;
static final String NON_TRIMMABLE;
static {
String ws = "";
String nws = "";
String tr = "";
String ntr = "";
for (int i = 0; i < Character.MAX_VALUE; i++) {
if (Character.isWhitespace((char) i)) {
ws += String.valueOf((char) i);
if (i > 32) {
ntr += String.valueOf((char) i);
}
} else if (i < 40) {
nws += String.valueOf((char) i);
}
}
for (int i = 0; i <= 32; i++) {
tr += String.valueOf((char) i);
}
WHITESPACE = ws;
NON_WHITESPACE = nws;
TRIMMABLE = tr;
NON_TRIMMABLE = ntr;
}
private static final String[] ARRAY_LIST = { "foo", "bar", "baz" };
private static final String[] EMPTY_ARRAY_LIST = {};
@ -210,53 +239,106 @@ public class StringUtilsTest extends TestCase {
null));
}
public void testSplit() {
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);
assertEquals("a", res[0]);
str = "a" + WHITESPACE + "b" + NON_WHITESPACE + "c";
res = StringUtils.split(str);
assertEquals(2, res.length);
assertEquals("a", res[0]);
assertEquals("b" + NON_WHITESPACE + "c", res[1]);
}
public void testSplit_StringChar() {
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);
assertEquals("a", res[0]);
}
public void testSplit() {
assertEquals(null, StringUtils.split(null, "."));
assertEquals(null, StringUtils.split(null, ".", 3));
String[] res = StringUtils.split("a..b.c", '.');
assertEquals(4, res.length);
assertEquals("a", res[0]);
assertEquals("", res[1]);
assertEquals("b", res[2]);
assertEquals("c", res[3]);
String[] result = StringUtils.split(TEXT_LIST, SEPARATOR, 2);
String[] expected = { "foo", "bar,baz" };
assertEquals("split(Object[], String, int) yielded unexpected length",
expected.length, result.length);
for (int i = 0; i < result.length; i++)
{
assertEquals("split(Object[], String, int) failed", expected[i],
result[i]);
assertEquals(0, StringUtils.split("", ".").length);
assertEquals(0, StringUtils.split("", ".", 3).length);
innerTestSplit('.', ".", ' ');
innerTestSplit('.', ".", ',');
innerTestSplit('.', ".,", 'x');
for (int i = 0; i < WHITESPACE.length(); i++) {
for (int j = 0; j < NON_WHITESPACE.length(); j++) {
innerTestSplit(WHITESPACE.charAt(i), null, NON_WHITESPACE.charAt(j));
innerTestSplit(WHITESPACE.charAt(i), String.valueOf(WHITESPACE.charAt(i)), NON_WHITESPACE.charAt(j));
}
}
}
private void innerTestSplit(char separator, String sepStr, char noMatch) {
try {
final String str = "a" + separator + "b" + separator + separator + noMatch + "c";
String[] res;
// (str, sepStr)
res = StringUtils.split(str, sepStr);
assertEquals(3, res.length);
assertEquals("a", res[0]);
assertEquals("b", res[1]);
assertEquals(noMatch + "c", res[2]);
final String str2 = separator + "a" + separator;
res = StringUtils.split(str2, sepStr);
assertEquals(1, res.length);
assertEquals("a", res[0]);
result = StringUtils.split(TEXT_LIST, SEPARATOR, 0);
expected = ARRAY_LIST;
assertEquals("split(Object[], String, int) yielded unexpected length",
expected.length, result.length);
for (int i = 0; i < result.length; i++)
{
assertEquals("split(Object[], String, int) failed", expected[i],
result[i]);
res = StringUtils.split(str, sepStr, -1);
assertEquals(3, res.length);
assertEquals("a", res[0]);
assertEquals("b", res[1]);
assertEquals(noMatch + "c", res[2]);
res = StringUtils.split(str, sepStr, 0);
assertEquals(3, res.length);
assertEquals("a", res[0]);
assertEquals("b", res[1]);
assertEquals(noMatch + "c", res[2]);
res = StringUtils.split(str, sepStr, 1);
assertEquals(1, res.length);
assertEquals(str, res[0]);
res = StringUtils.split(str, sepStr, 2);
assertEquals(2, res.length);
assertEquals("a", res[0]);
assertEquals(str.substring(2), res[1]);
} catch (AssertionFailedError ex) {
System.out.println("Failed on separator hex(" + Integer.toHexString(separator) +
"), noMatch hex(" + Integer.toHexString(noMatch) + "), sepStr(" + sepStr + ")");
throw ex;
}
result = StringUtils.split(TEXT_LIST, SEPARATOR, -1);
expected = ARRAY_LIST;
assertEquals("split(Object[], String, int) yielded unexpected length",
expected.length, result.length);
for (int i = 0; i < result.length; i++)
{
assertEquals("split(Object[], String, int) failed", expected[i],
result[i]);
}
result = StringUtils.split("one two three four five six", null, 3);
assertEquals("split(Object[], null, int)[0] failed", "one", result[0]);
assertEquals("split(Object[], null, int)[1] failed", "two", result[1]);
assertEquals("split(Object[], null, int)[2] failed", "three four five six", result[2]);
}
public void testReplaceFunctions() {

View File

@ -63,7 +63,7 @@ import junit.textui.TestRunner;
*
* @author <a href="mailto:scolebourne@joda.org">Stephen Colebourne</a>
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
* @version $Id: StringUtilsTrimEmptyTest.java,v 1.10 2003/07/18 02:06:23 scolebourne Exp $
* @version $Id: StringUtilsTrimEmptyTest.java,v 1.11 2003/07/19 18:10:30 scolebourne Exp $
*/
public class StringUtilsTrimEmptyTest extends TestCase {
private static final String FOO = "foo";
@ -108,6 +108,8 @@ public class StringUtilsTrimEmptyTest extends TestCase {
assertEquals(FOO, StringUtils.trim(" " + FOO));
assertEquals(FOO, StringUtils.trim(FOO + ""));
assertEquals("", StringUtils.trim(" \t\r\n\b "));
assertEquals("", StringUtils.trim(StringUtilsTest.TRIMMABLE));
assertEquals(StringUtilsTest.NON_TRIMMABLE, StringUtils.trim(StringUtilsTest.NON_TRIMMABLE));
assertEquals("", StringUtils.trim(""));
assertEquals(null, StringUtils.trim(null));
}
@ -118,6 +120,8 @@ public class StringUtilsTrimEmptyTest extends TestCase {
assertEquals(FOO, StringUtils.trimToNull(" " + FOO));
assertEquals(FOO, StringUtils.trimToNull(FOO + ""));
assertEquals(null, StringUtils.trimToNull(" \t\r\n\b "));
assertEquals(null, StringUtils.trimToNull(StringUtilsTest.TRIMMABLE));
assertEquals(StringUtilsTest.NON_TRIMMABLE, StringUtils.trimToNull(StringUtilsTest.NON_TRIMMABLE));
assertEquals(null, StringUtils.trimToNull(""));
assertEquals(null, StringUtils.trimToNull(null));
}
@ -128,6 +132,8 @@ public class StringUtilsTrimEmptyTest extends TestCase {
assertEquals(FOO, StringUtils.trimToEmpty(" " + FOO));
assertEquals(FOO, StringUtils.trimToEmpty(FOO + ""));
assertEquals("", StringUtils.trimToEmpty(" \t\r\n\b "));
assertEquals("", StringUtils.trimToEmpty(StringUtilsTest.TRIMMABLE));
assertEquals(StringUtilsTest.NON_TRIMMABLE, StringUtils.trimToEmpty(StringUtilsTest.NON_TRIMMABLE));
assertEquals("", StringUtils.trimToEmpty(""));
assertEquals("", StringUtils.trimToEmpty(null));
}
@ -197,79 +203,132 @@ public class StringUtilsTrimEmptyTest extends TestCase {
}
public void testDeleteSpace() {
assertEquals("deleteWhitespace(String) failed",
"", StringUtils.deleteWhitespace(""));
assertEquals("deleteWhitespace(String) failed",
"", StringUtils.deleteWhitespace(" \u000C \t\t\u001F\n\n \u000B "));
assertEquals(null, StringUtils.deleteSpaces(null));
assertEquals("", StringUtils.deleteSpaces(""));
assertEquals("", StringUtils.deleteSpaces(" \t\t\n\n "));
assertEquals("test", StringUtils.deleteSpaces("t \t\ne\rs\n\n \tt"));
}
public void testDeleteWhitespace() {
assertEquals(null, StringUtils.deleteWhitespace(null));
assertEquals("", StringUtils.deleteWhitespace(""));
assertEquals("", StringUtils.deleteWhitespace(" \u000C \t\t\u001F\n\n \u000B "));
assertEquals("", StringUtils.deleteWhitespace(StringUtilsTest.WHITESPACE));
assertEquals(StringUtilsTest.NON_WHITESPACE, StringUtils.deleteWhitespace(StringUtilsTest.NON_WHITESPACE));
// Note: u-2007 and u-000A both cause problems in the source code
// it should ignore 2007 but delete 000A
assertEquals("deleteWhitespace(String) failed",
"\u00A0\u202F", StringUtils.deleteWhitespace(" \u00A0 \t\t\n\n \u202F "));
assertEquals("deleteWhitespace(String) failed",
"\u00A0\u202F", StringUtils.deleteWhitespace("\u00A0\u202F"));
assertEquals("deleteWhitespace(String) failed",
"test", StringUtils.deleteWhitespace("\u000Bt \t\n\u0009e\rs\n\n \tt"));
assertEquals("deleteSpaces(String) failed",
"", StringUtils.deleteSpaces(""));
assertEquals("deleteSpaces(String) failed",
"", StringUtils.deleteSpaces(" \t\t\n\n "));
assertEquals("deleteSpaces(String) failed",
"test", StringUtils.deleteSpaces("t \t\ne\rs\n\n \tt"));
assertEquals("\u00A0\u202F", StringUtils.deleteWhitespace(" \u00A0 \t\t\n\n \u202F "));
assertEquals("\u00A0\u202F", StringUtils.deleteWhitespace("\u00A0\u202F"));
assertEquals("test", StringUtils.deleteWhitespace("\u000Bt \t\n\u0009e\rs\n\n \tt"));
}
public void testStrip() {
// it's important that foo2Space is fooLeftSpace and fooRightSpace
// merged together. So same number of spaces to left as fLS and same
// to right as fLS. Same applies for foo2Dots.
String foo2Space = " "+FOO+" ";
String foo2Dots = "......"+FOO+".........";
String fooLeftSpace = " "+FOO;
String fooLeftDots = "......"+FOO;
String fooRightSpace = FOO+" ";
String fooRightDots = FOO+".........";
public void testStrip_String() {
assertEquals(null, StringUtils.strip(null));
assertEquals("", StringUtils.strip(""));
assertEquals("", StringUtils.strip(" "));
assertEquals(FOO, StringUtils.strip(foo2Space));
assertEquals("abc", StringUtils.strip(" abc "));
assertEquals(StringUtilsTest.NON_WHITESPACE,
StringUtils.strip(StringUtilsTest.WHITESPACE + StringUtilsTest.NON_WHITESPACE + StringUtilsTest.WHITESPACE));
}
public void testStrip_StringString() {
// null strip
assertEquals(null, StringUtils.strip(null, null));
assertEquals("", StringUtils.strip("", null));
assertEquals("", StringUtils.strip(" ", null));
assertEquals("abc", StringUtils.strip(" abc ", null));
assertEquals(StringUtilsTest.NON_WHITESPACE,
StringUtils.strip(StringUtilsTest.WHITESPACE + StringUtilsTest.NON_WHITESPACE + StringUtilsTest.WHITESPACE, null));
// "" strip
assertEquals(null, StringUtils.strip(null, ""));
assertEquals("", StringUtils.strip("", ""));
assertEquals(" ", StringUtils.strip(" ", ""));
assertEquals(" abc ", StringUtils.strip(" abc ", ""));
assertEquals(StringUtilsTest.WHITESPACE, StringUtils.strip(StringUtilsTest.WHITESPACE, ""));
assertEquals(FOO, StringUtils.strip(foo2Space, null));
assertEquals(FOO, StringUtils.strip(foo2Dots, "."));
assertEquals(FOO, StringUtils.strip(fooRightSpace));
assertEquals(FOO, StringUtils.strip(fooRightDots, "."));
assertEquals(FOO, StringUtils.strip(fooLeftSpace));
assertEquals(FOO, StringUtils.strip(fooLeftDots, "."));
// " " strip
assertEquals(null, StringUtils.strip(null, " "));
assertEquals("", StringUtils.strip("", " "));
assertEquals("", StringUtils.strip(" ", " "));
assertEquals("abc", StringUtils.strip(" abc ", " "));
// "ab" strip
assertEquals(null, StringUtils.strip(null, "ab"));
assertEquals("", StringUtils.strip("", "ab"));
assertEquals(" ", StringUtils.strip(" ", "ab"));
assertEquals(" abc ", StringUtils.strip(" abc ", "ab"));
assertEquals("c", StringUtils.strip("abcabab", "ab"));
assertEquals(StringUtilsTest.WHITESPACE, StringUtils.strip(StringUtilsTest.WHITESPACE, ""));
}
public void testStripStart_StringString() {
// null stripStart
assertEquals(null, StringUtils.stripStart(null, null));
assertEquals("", StringUtils.stripStart("", null));
assertEquals("", StringUtils.stripStart(" ", null));
assertEquals("abc ", StringUtils.stripStart(" abc ", null));
assertEquals(StringUtilsTest.NON_WHITESPACE + StringUtilsTest.WHITESPACE,
StringUtils.stripStart(StringUtilsTest.WHITESPACE + StringUtilsTest.NON_WHITESPACE + StringUtilsTest.WHITESPACE, null));
// "" stripStart
assertEquals(null, StringUtils.stripStart(null, ""));
assertEquals("", StringUtils.stripStart("", ""));
assertEquals(" ", StringUtils.stripStart(" ", ""));
assertEquals(" abc ", StringUtils.stripStart(" abc ", ""));
assertEquals(StringUtilsTest.WHITESPACE, StringUtils.stripStart(StringUtilsTest.WHITESPACE, ""));
// " " stripStart
assertEquals(null, StringUtils.stripStart(null, " "));
assertEquals("", StringUtils.stripStart("", " "));
assertEquals(fooRightSpace, StringUtils.stripStart(foo2Space, null));
assertEquals(fooRightSpace, StringUtils.stripStart(foo2Space, " "));
assertEquals(fooRightDots, StringUtils.stripStart(foo2Dots, "."));
assertEquals(fooRightSpace, StringUtils.stripStart(fooRightSpace, " "));
assertEquals(fooRightDots, StringUtils.stripStart(fooRightDots, "."));
assertEquals(FOO, StringUtils.stripStart(fooLeftSpace, " "));
assertEquals(FOO, StringUtils.stripStart(fooLeftDots, "."));
assertEquals("", StringUtils.stripStart(" ", " "));
assertEquals("abc ", StringUtils.stripStart(" abc ", " "));
// "ab" stripStart
assertEquals(null, StringUtils.stripStart(null, "ab"));
assertEquals("", StringUtils.stripStart("", "ab"));
assertEquals(" ", StringUtils.stripStart(" ", "ab"));
assertEquals(" abc ", StringUtils.stripStart(" abc ", "ab"));
assertEquals("cabab", StringUtils.stripStart("abcabab", "ab"));
assertEquals(StringUtilsTest.WHITESPACE, StringUtils.stripStart(StringUtilsTest.WHITESPACE, ""));
}
public void testStripEnd_StringString() {
// null stripEnd
assertEquals(null, StringUtils.stripEnd(null, null));
assertEquals("", StringUtils.stripEnd("", null));
assertEquals("", StringUtils.stripEnd(" ", null));
assertEquals(" abc", StringUtils.stripEnd(" abc ", null));
assertEquals(StringUtilsTest.WHITESPACE + StringUtilsTest.NON_WHITESPACE,
StringUtils.stripEnd(StringUtilsTest.WHITESPACE + StringUtilsTest.NON_WHITESPACE + StringUtilsTest.WHITESPACE, null));
// "" stripEnd
assertEquals(null, StringUtils.stripEnd(null, ""));
assertEquals("", StringUtils.stripEnd("", ""));
assertEquals(" ", StringUtils.stripEnd(" ", ""));
assertEquals(" abc ", StringUtils.stripEnd(" abc ", ""));
assertEquals(StringUtilsTest.WHITESPACE, StringUtils.stripEnd(StringUtilsTest.WHITESPACE, ""));
// " " stripEnd
assertEquals(null, StringUtils.stripEnd(null, " "));
assertEquals("", StringUtils.stripEnd("", " "));
assertEquals(fooLeftSpace, StringUtils.stripEnd(foo2Space, null));
assertEquals(fooLeftSpace, StringUtils.stripEnd(foo2Space, " "));
assertEquals(fooLeftDots, StringUtils.stripEnd(foo2Dots, "."));
assertEquals(FOO, StringUtils.stripEnd(fooRightSpace, " "));
assertEquals(FOO, StringUtils.stripEnd(fooRightDots, "."));
assertEquals(fooLeftSpace, StringUtils.stripEnd(fooLeftSpace, " "));
assertEquals(fooLeftDots, StringUtils.stripEnd(fooLeftDots, "."));
assertEquals(FOO, StringUtils.strip(". . . . ."+FOO+". . ", " ."));
assertEquals("-."+FOO, StringUtils.strip(". . . . -."+FOO+". . ", " ."));
assertEquals(FOO, StringUtils.strip(".. .."+FOO+".. ", " ."));
assertEquals(FOO, StringUtils.strip(".. .."+FOO+".. ", "+= ."));
assertEquals("", StringUtils.stripEnd(" ", " "));
assertEquals(" abc", StringUtils.stripEnd(" abc ", " "));
// "ab" stripEnd
assertEquals(null, StringUtils.stripEnd(null, "ab"));
assertEquals("", StringUtils.stripEnd("", "ab"));
assertEquals(" ", StringUtils.stripEnd(" ", "ab"));
assertEquals(" abc ", StringUtils.stripEnd(" abc ", "ab"));
assertEquals("abc", StringUtils.stripEnd("abcabab", "ab"));
assertEquals(StringUtilsTest.WHITESPACE, StringUtils.stripEnd(StringUtilsTest.WHITESPACE, ""));
}
public void testStripAll() {
// test stripAll method, merely an array version of the above strip
String[] empty = new String[0];
String[] fooSpace = new String[] { foo2Space, fooLeftSpace, fooRightSpace };
String[] fooDots = new String[] { foo2Dots, fooLeftDots, fooRightDots };
String[] fooSpace = new String[] { " "+FOO+" ", " "+FOO, FOO+" " };
String[] fooDots = new String[] { ".."+FOO+"..", ".."+FOO, FOO+".." };
String[] foo = new String[] { FOO, FOO, FOO };
assertEquals(null, StringUtils.stripAll(null));