Javadoc fixes and improved tests
bug 21952, from Phil Steitz git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137524 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
0c1c4431d2
commit
3a8907682b
|
@ -63,7 +63,7 @@ package org.apache.commons.lang;
|
|||
* @author <a href="bayard@generationjava.com">Henri Yandell</a>
|
||||
* @author Stephen Colebourne
|
||||
* @since 1.0
|
||||
* @version $Id: CharSetUtils.java,v 1.14 2003/07/19 20:22:36 scolebourne Exp $
|
||||
* @version $Id: CharSetUtils.java,v 1.15 2003/07/30 00:08:38 scolebourne Exp $
|
||||
*/
|
||||
public class CharSetUtils {
|
||||
|
||||
|
@ -334,10 +334,11 @@ public class CharSetUtils {
|
|||
*
|
||||
* @param str String to replace characters in, may be null
|
||||
* @param repl String to find that will be replaced, must not be null
|
||||
* @param with String to put into the target String, must not be null
|
||||
* @param with String to put into the target String, must not be null or empty ("")
|
||||
* @return translated String, <code>null</code> if null string input
|
||||
* @throws NullPointerException if <code>target</code>, <code>with</code>
|
||||
* or <code>repl</code> is <code>null</code>
|
||||
* @throws NullPointerException if <code>with</code> or <code>repl</code>
|
||||
* is <code>null</code>
|
||||
* @throws ArrayIndexOutOfBoundsException if <code>with</code> is empty ("")
|
||||
*/
|
||||
public static String translate(String str, String repl, String with) {
|
||||
if (str == null) {
|
||||
|
|
|
@ -64,7 +64,7 @@ import junit.textui.TestRunner;
|
|||
* @author <a href="mailto:bayard@generationjava.com">Henri Yandell</a>
|
||||
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
|
||||
* @author Stephen Colebourne
|
||||
* @version $Id: CharSetUtilsTest.java,v 1.8 2003/07/19 20:22:36 scolebourne Exp $
|
||||
* @version $Id: CharSetUtilsTest.java,v 1.9 2003/07/30 00:08:38 scolebourne Exp $
|
||||
*/
|
||||
public class CharSetUtilsTest extends TestCase {
|
||||
|
||||
|
@ -94,19 +94,27 @@ public class CharSetUtilsTest extends TestCase {
|
|||
|
||||
public void testSqueeze() {
|
||||
assertEquals(null, CharSetUtils.squeeze(null, (String[]) null));
|
||||
assertEquals(null, CharSetUtils.squeeze(null, (String) null));
|
||||
assertEquals(null, CharSetUtils.squeeze(null, new String[] { "el" }));
|
||||
assertEquals("helo", CharSetUtils.squeeze("hello", new String[] { "el" }));
|
||||
assertEquals("hello", CharSetUtils.squeeze("hello", ""));
|
||||
assertEquals("", CharSetUtils.squeeze("", new String[] { "el" }));
|
||||
assertEquals("hello", CharSetUtils.squeeze("hello", new String[] { "e" }));
|
||||
assertEquals("fofof", CharSetUtils.squeeze("fooffooff", new String[] { "of" }));
|
||||
assertEquals("fof", CharSetUtils.squeeze("fooooff", new String[] { "fo" }));
|
||||
try {
|
||||
CharSetUtils.squeeze("hello", (String[]) null);
|
||||
fail("Expecting NullPointerException");
|
||||
} catch (NullPointerException ex) {}
|
||||
try {
|
||||
CharSetUtils.squeeze("hello", new String[] { "", null });
|
||||
fail("Expecting NullPointerException");
|
||||
} catch (NullPointerException ex) {}
|
||||
}
|
||||
|
||||
public void testCount() {
|
||||
assertEquals(0, CharSetUtils.count(null, (String[]) null));
|
||||
assertEquals(0, CharSetUtils.count(null, (String) null));
|
||||
assertEquals(0, CharSetUtils.count(null, new String[] { "el" }));
|
||||
assertEquals(3, CharSetUtils.count("hello", new String[] { "el" }));
|
||||
assertEquals(0, CharSetUtils.count("", new String[] { "el" }));
|
||||
|
@ -114,37 +122,83 @@ public class CharSetUtilsTest extends TestCase {
|
|||
assertEquals(2, CharSetUtils.count("hello", new String[] { "e-i" }));
|
||||
assertEquals(5, CharSetUtils.count("hello", new String[] { "a-z" }));
|
||||
assertEquals(0, CharSetUtils.count("hello", new String[] { "" }));
|
||||
assertEquals(0, CharSetUtils.count("hello", ""));
|
||||
try {
|
||||
CharSetUtils.count("hello", (String[]) null);
|
||||
fail("Expecting NullPointerException");
|
||||
} catch (NullPointerException ex) {}
|
||||
try {
|
||||
CharSetUtils.count("hello", new String[] { "", null });
|
||||
fail("Expecting NullPointerException");
|
||||
} catch (NullPointerException ex) {}
|
||||
}
|
||||
|
||||
public void testKeep() {
|
||||
assertEquals(null, CharSetUtils.keep(null, (String[]) null));
|
||||
assertEquals(null, CharSetUtils.keep(null, (String) null));
|
||||
assertEquals(null, CharSetUtils.keep(null, new String[] { "el" }));
|
||||
assertEquals("ell", CharSetUtils.keep("hello", new String[] { "el" }));
|
||||
assertEquals("hello", CharSetUtils.keep("hello", new String[] { "elho" }));
|
||||
assertEquals("", CharSetUtils.keep("hello", new String[] { "" }));
|
||||
assertEquals("", CharSetUtils.keep("hello", ""));
|
||||
assertEquals("hello", CharSetUtils.keep("hello", new String[] { "a-z" }));
|
||||
assertEquals("----", CharSetUtils.keep("----", new String[] { "-" }));
|
||||
assertEquals("ll", CharSetUtils.keep("hello", new String[] { "l" }));
|
||||
try {
|
||||
CharSetUtils.keep("hello", (String[]) null);
|
||||
fail("Expecting NullPointerException");
|
||||
} catch (NullPointerException ex) {}
|
||||
try {
|
||||
CharSetUtils.keep("hello", new String[] { "", null});
|
||||
fail("Expecting NullPointerException");
|
||||
} catch (NullPointerException ex) {}
|
||||
}
|
||||
|
||||
public void testDelete() {
|
||||
assertEquals(null, CharSetUtils.delete(null, (String[]) null));
|
||||
assertEquals(null, CharSetUtils.delete(null,(String) null));
|
||||
assertEquals(null, CharSetUtils.delete(null, new String[] { "el" }));
|
||||
assertEquals("ho", CharSetUtils.delete("hello", new String[] { "el" }));
|
||||
assertEquals("", CharSetUtils.delete("hello", new String[] { "elho" }));
|
||||
assertEquals("hello", CharSetUtils.delete("hello", new String[] { "" }));
|
||||
assertEquals("hello", CharSetUtils.delete("hello", ""));
|
||||
assertEquals("", CharSetUtils.delete("hello", new String[] { "a-z" }));
|
||||
assertEquals("", CharSetUtils.delete("----", new String[] { "-" }));
|
||||
assertEquals("heo", CharSetUtils.delete("hello", new String[] { "l" }));
|
||||
try {
|
||||
CharSetUtils.delete("hello", (String[]) null);
|
||||
fail("Expecting NullPointerException");
|
||||
} catch (NullPointerException ex) {}
|
||||
try {
|
||||
CharSetUtils.delete("hello", new String[] { "-", null });
|
||||
fail("Expecting NullPointerException");
|
||||
} catch (NullPointerException ex) {}
|
||||
}
|
||||
|
||||
public void testTranslate() {
|
||||
assertEquals(null, CharSetUtils.translate(null, null, null));
|
||||
assertEquals("", CharSetUtils.translate("","a", "b"));
|
||||
assertEquals("jelly", CharSetUtils.translate("hello", "ho", "jy"));
|
||||
assertEquals("jellj", CharSetUtils.translate("hello", "ho", "j"));
|
||||
assertEquals("jelly", CharSetUtils.translate("hello", "ho", "jyx"));
|
||||
assertEquals("\rhello\r", CharSetUtils.translate("\nhello\n", "\n", "\r"));
|
||||
assertEquals("hello", CharSetUtils.translate("hello", "", "x"));
|
||||
assertEquals("hello", CharSetUtils.translate("hello", "", ""));
|
||||
try {
|
||||
CharSetUtils.translate("hello", null, null);
|
||||
fail("Expecting NullPointerException");
|
||||
} catch (NullPointerException ex) {}
|
||||
try {
|
||||
CharSetUtils.translate("hello", "h", null);
|
||||
fail("Expecting NullPointerException");
|
||||
} catch (NullPointerException ex) {}
|
||||
try {
|
||||
CharSetUtils.translate("hello", null, "a");
|
||||
fail("Expecting NullPointerException");
|
||||
} catch (NullPointerException ex) {}
|
||||
try {
|
||||
CharSetUtils.translate("hello", "h", "");
|
||||
fail("Expecting ArrayIndexOutOfBoundsException");
|
||||
} catch (ArrayIndexOutOfBoundsException ex) {}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -65,7 +65,7 @@ import junit.textui.TestRunner;
|
|||
*
|
||||
* @author of original StringUtilsTest.testEscape = ?
|
||||
* @author <a href="mailto:alex@purpletech.com">Alexander Day Chaffee</a>
|
||||
* @version $Id: StringEscapeUtilsTest.java,v 1.9 2003/07/19 20:22:36 scolebourne Exp $
|
||||
* @version $Id: StringEscapeUtilsTest.java,v 1.10 2003/07/30 00:08:38 scolebourne Exp $
|
||||
*/
|
||||
public class StringEscapeUtilsTest extends TestCase {
|
||||
private final static String FOO = "foo";
|
||||
|
@ -153,11 +153,18 @@ public class StringEscapeUtilsTest extends TestCase {
|
|||
fail();
|
||||
} catch (IllegalArgumentException ex) {
|
||||
}
|
||||
try {
|
||||
String str = StringEscapeUtils.unescapeJava("\\u02-3");
|
||||
fail();
|
||||
} catch (RuntimeException ex) {
|
||||
}
|
||||
|
||||
assertUnescapeJava("", "");
|
||||
assertUnescapeJava("test", "test");
|
||||
assertUnescapeJava("\ntest\b", "\\ntest\\b");
|
||||
assertUnescapeJava("\u123425foo\ntest\b", "\\u123425foo\\ntest\\b");
|
||||
assertUnescapeJava("'\foo\teste\r", "\\'\\foo\\teste\\r");
|
||||
assertUnescapeJava("\\", "\\");
|
||||
//foo
|
||||
assertUnescapeJava("lowercase unicode", "\uABCDx", "\\uabcdx");
|
||||
assertUnescapeJava("uppercase unicode", "\uABCDx", "\\uABCDx");
|
||||
|
@ -174,9 +181,9 @@ public class StringEscapeUtilsTest extends TestCase {
|
|||
|
||||
assertEquals("unescape(String) failed" +
|
||||
(message == null ? "" : (": " + message)) +
|
||||
": expected '" + StringUtils.escape(expected) +
|
||||
": expected '" + StringEscapeUtils.escapeJava(expected) +
|
||||
// we escape this so we can see it in the error message
|
||||
"' actual '" + StringUtils.escape(actual) + "'",
|
||||
"' actual '" + StringEscapeUtils.escapeJava(actual) + "'",
|
||||
expected, actual);
|
||||
|
||||
StringPrintWriter writer = new StringPrintWriter();
|
||||
|
@ -213,6 +220,7 @@ public class StringEscapeUtilsTest extends TestCase {
|
|||
{"no escaping", "plain text", "plain text"},
|
||||
{"no escaping", "plain text", "plain text"},
|
||||
{"empty string", "", ""},
|
||||
{"null", null, null},
|
||||
{"ampersand", "bread & butter", "bread & butter"},
|
||||
{"quotes", ""bread" & butter", "\"bread\" & butter"},
|
||||
{"final character only", "greater than >", "greater than >"},
|
||||
|
@ -241,7 +249,7 @@ public class StringEscapeUtilsTest extends TestCase {
|
|||
assertEquals(htmlEscapes[i][0], htmlEscapes[i][2], StringEscapeUtils.unescapeHtml(htmlEscapes[i][1]));
|
||||
// todo: add test for (and implement) Writer-based version
|
||||
}
|
||||
// \u00E7 is a cedilla (ç)
|
||||
// \u00E7 is a cedilla (c with wiggle under)
|
||||
// note that the test string must be 7-bit-clean (unicode escaped) or else it will compile incorrectly
|
||||
// on some locales
|
||||
assertEquals("funny chars pass through OK", "Fran\u00E7ais", StringEscapeUtils.unescapeHtml("Fran\u00E7ais"));
|
||||
|
@ -272,6 +280,9 @@ public class StringEscapeUtilsTest extends TestCase {
|
|||
|
||||
assertEquals("ain't", StringEscapeUtils.unescapeXml("ain't"));
|
||||
assertEquals("ain't", StringEscapeUtils.escapeXml("ain't"));
|
||||
assertEquals("", StringEscapeUtils.escapeXml(""));
|
||||
assertEquals(null, StringEscapeUtils.escapeXml(null));
|
||||
assertEquals(null, StringEscapeUtils.unescapeXml(null));
|
||||
}
|
||||
|
||||
// SQL
|
||||
|
@ -281,6 +292,8 @@ public class StringEscapeUtilsTest extends TestCase {
|
|||
public void testEscapeSql() throws Exception
|
||||
{
|
||||
assertEquals("don''t stop", StringEscapeUtils.escapeSql("don't stop"));
|
||||
assertEquals("", StringEscapeUtils.escapeSql(""));
|
||||
assertEquals(null, StringEscapeUtils.escapeSql(null));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue