Update null behaviour for consistency and clarity

Doument null behaviour


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137458 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Stephen Colebourne 2003-07-19 20:22:36 +00:00
parent 8c6294dd70
commit 9001aa2613
8 changed files with 296 additions and 155 deletions

View File

@ -56,10 +56,14 @@ package org.apache.commons.lang;
/**
* <p>Numerous routines to manipulate a <code>CharSet</code>.</p>
*
* <p>This class tries to handle <code>null</code> input gracefully.
* An exception will generally not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.</p>
*
* @author <a href="bayard@generationjava.com">Henri Yandell</a>
* @author Stephen Colebourne
* @since 1.0
* @version $Id: CharSetUtils.java,v 1.13 2003/07/16 00:39:05 scolebourne Exp $
* @version $Id: CharSetUtils.java,v 1.14 2003/07/19 20:22:36 scolebourne Exp $
*/
public class CharSetUtils {
@ -84,8 +88,8 @@ public class CharSetUtils {
* <li>&quot;ej-m&quot; implies e,j->m. e,j,k,l,m.</li>
* </ul>
*
* @param set
* @return CharSet
* @param set the set, must not be null
* @return a CharSet instance
* @throws NullPointerException if any of set[i] is null or if set is null
*/
public static CharSet evaluateSet(String[] set) {
@ -102,12 +106,15 @@ public class CharSetUtils {
* </ul>
* @see #evaluateSet(java.lang.String[]) for set-syntax.
*
* @param str the string to work from
* @param set the character set to use for manipulation
* @throws NullPointerException if <code>str</code> is <code>null</code>
* @param str the string to squeeze, may be null
* @param set the character set to use for manipulation, must not be null
* @return modified String, <code>null</code> if null string input
* @throws NullPointerException if <code>set</code> is <code>null</code>
*/
public static String squeeze(String str, String set) {
if (str == null) {
return null;
}
String[] strs = new String[1];
strs[0] = set;
return squeeze(str, strs);
@ -123,13 +130,16 @@ public class CharSetUtils {
* </ul>
* @see #evaluateSet(java.lang.String[]) for set-syntax.
*
* @param str the string to work from
* @param set the character set to use for manipulation
* @throws NullPointerException if <code>str</code> is <code>null</code>
* @param str the string to squeeze, may be null
* @param set the character set to use for manipulation, must not be null
* @return modified String, <code>null</code> if null string input
* @throws NullPointerException if <code>set</code> is <code>null</code>
* or any element is <code>null</code>
*/
public static String squeeze(String str, String[] set) {
if (str == null) {
return null;
}
CharSet chars = evaluateSet(set);
StringBuffer buffer = new StringBuffer(str.length());
char[] chrs = str.toCharArray();
@ -158,12 +168,15 @@ public class CharSetUtils {
* <li>count(&quot;hello&quot;, {&quot;c-f&quot;, &quot;o&quot;}) returns 2.</li>
* </ul>
*
* @param str String target to count characters in
* @param set String set of characters to count
* @throws NullPointerException if <code>str</code> is <code>null</code>
* @param str String to count characters in, may be null
* @param set String set of characters to count, must not be null
* @return character count, zero if null string input
* @throws NullPointerException if <code>set</code> is <code>null</code>
*/
public static int count(String str, String set) {
if (str == null) {
return 0;
}
String[] strs = new String[1];
strs[0] = set;
return count(str, strs);
@ -178,13 +191,16 @@ public class CharSetUtils {
* <li>count(&quot;hello&quot;, {&quot;c-f&quot;, &quot;o&quot;}) returns 2.</li>
* </ul>
*
* @param str String target to count characters in
* @param set String[] set of characters to count
* @throws NullPointerException if <code>str</code> is <code>null</code>
* @param str String to count characters in, may be null
* @param set String[] set of characters to count, must not be null
* @return character count, zero if null string input
* @throws NullPointerException if <code>set</code> is <code>null</code>
* or any element is <code>null</code>
*/
public static int count(String str, String[] set) {
if (str == null) {
return 0;
}
CharSet chars = evaluateSet(set);
int count = 0;
char[] chrs = str.toCharArray();
@ -206,12 +222,15 @@ public class CharSetUtils {
* <li>keep(&quot;hello&quot;, {&quot;c-fo&quot;}) returns &quot;hll&quot;</li>
* </ul>
*
* @param str String target to keep characters from
* @param set String set of characters to keep
* @throws NullPointerException if <code>str</code> is <code>null</code>
* @param str String to keep characters from, may be null
* @param set String set of characters to keep, must not be null
* @return modified String, <code>null</code> if null string input
* @throws NullPointerException if <code>set</code> is <code>null</code>
*/
public static String keep(String str, String set) {
if (str == null) {
return null;
}
String[] strs = new String[1];
strs[0] = set;
return keep(str, strs);
@ -227,13 +246,16 @@ public class CharSetUtils {
* returns &quot;hll&quot;</li>
* </ul>
*
* @param str String target to keep characters from
* @param set String[] set of characters to keep
* @throws NullPointerException if <code>str</code> is <code>null</code>
* @param str String to keep characters from, may be null
* @param set String[] set of characters to keep, must not be null
* @return modified String, <code>null</code> if null string input
* @throws NullPointerException if <code>set</code> is <code>null</code>
* or any element is <code>null</code>
*/
public static String keep(String str, String[] set) {
if (str == null) {
return null;
}
return modify(str, set, true);
}
@ -246,12 +268,15 @@ public class CharSetUtils {
* <li>delete(&quot;hello&quot;, {&quot;c-fo&quot;}) returns &quot;hll&quot;</li>
* </ul>
*
* @param str String target to delete characters from
* @param set String set of characters to delete
* @throws NullPointerException if <code>str</code> is <code>null</code>
* @param str String to delete characters from, may be null
* @param set String set of characters to delete, must not be null
* @return modified String, <code>null</code> if null string input
* @throws NullPointerException if <code>set</code> is <code>null</code>
*/
public static String delete(String str, String set) {
if (str == null) {
return null;
}
String[] strs = new String[1];
strs[0] = set;
return delete(str, strs);
@ -267,13 +292,16 @@ public class CharSetUtils {
* &quot;hll&quot;</li>
* </ul>
*
* @param str String target to delete characters from
* @param set String[] set of characters to delete
* @throws NullPointerException if <code>str</code> is <code>null</code>
* @param str String to delete characters from, may be null
* @param set String[] set of characters to delete, must not be null
* @return modified String, <code>null</code> if null string input
* @throws NullPointerException if <code>set</code> is <code>null</code>
* or any element is <code>null</code>
*/
public static String delete(String str, String[] set) {
if (str == null) {
return null;
}
return modify(str, set, false);
}
@ -304,15 +332,19 @@ public class CharSetUtils {
* length of characters to replace, then the last character is
* used.</p>
*
* @param target String to replace characters in
* @param repl String to find that will be replaced
* @param with String to put into the target String
* @throws NullPointerException if <code>target</code>, with
* @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
* @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>
*/
public static String translate(String target, String repl, String with) {
StringBuffer buffer = new StringBuffer(target.length());
char[] chrs = target.toCharArray();
public static String translate(String str, String repl, String with) {
if (str == null) {
return null;
}
StringBuffer buffer = new StringBuffer(str.length());
char[] chrs = str.toCharArray();
char[] withChrs = with.toCharArray();
int sz = chrs.length;
int withMax = with.length() - 1;

View File

@ -72,13 +72,16 @@ import java.io.Serializable;
* <li>Deserialize managing finally and IOException
* </ul>
*
* <p>This class throws exceptions for invalid <code>null</code> inputs.
* Each method documents its behaviour in more detail.</p>
*
* @author <a href="mailto:nissim@nksystems.com">Nissim Karpenstein</a>
* @author <a href="mailto:janekdb@yahoo.co.uk">Janek Bogucki</a>
* @author <a href="mailto:dlr@finemaltcoding.com">Daniel Rall</a>
* @author Stephen Colebourne
* @author Jeff Varszegi
* @since 1.0
* @version $Id: SerializationUtils.java,v 1.6 2003/06/23 22:36:50 scolebourne Exp $
* @version $Id: SerializationUtils.java,v 1.7 2003/07/19 20:22:36 scolebourne Exp $
*/
public class SerializationUtils {
@ -120,11 +123,15 @@ public class SerializationUtils {
* <p>The stream passed in is not buffered internally within this method.
* This is the responsibility of your application if desired.</p>
*
* @param obj the object to serialize to bytes
* @param outputStream the stream to write to
* @param obj the object to serialize to bytes, may be null
* @param outputStream the stream to write to, must not be null
* @throws IllegalArgumentException if <code>outputStream</code> is <code>null</code>
* @throws SerializationException (runtime) if the serialization fails
*/
public static void serialize(Serializable obj, OutputStream outputStream) {
if (outputStream == null) {
throw new NullArgumentException("OutputStream");
}
ObjectOutputStream out = null;
try {
// stream closed in the finally
@ -168,11 +175,15 @@ public class SerializationUtils {
* <p>The stream passed in is not buffered internally within this method.
* This is the responsibility of your application if desired.</p>
*
* @param inputStream the serialized object input stream
* @param inputStream the serialized object input stream, must not be null
* @return the deserialized object
* @throws IllegalArgumentException if <code>inputStream</code> is <code>null</code>
* @throws SerializationException (runtime) if the serialization fails
*/
public static Object deserialize(InputStream inputStream) {
if (inputStream == null) {
throw new NullArgumentException("InputStream");
}
ObjectInputStream in = null;
try {
// stream closed in the finally
@ -197,11 +208,15 @@ public class SerializationUtils {
/**
* <p>Deserializes a single <code>Object</code> from an array of bytes.</p>
*
* @param objectData the serialized object
* @param objectData the serialized object, must not be null
* @return the deserialized object
* @throws IllegalArgumentException if <code>objectData</code> is <code>null</code>
* @throws SerializationException (runtime) if the serialization fails
*/
public static Object deserialize(byte[] objectData) {
if (objectData == null) {
throw new NullArgumentException("byte[]");
}
ByteArrayInputStream bais = new ByteArrayInputStream(objectData);
return deserialize(bais);
}

View File

@ -60,12 +60,9 @@ import org.apache.commons.lang.exception.NestableRuntimeException;
/**
* <p>Escapes and unescapes <code>String</code>s for Java, Java Script, HTML, XML, and SQL.
*
* <p>Originally from
* <a href="http://jakarta.apache.org/turbine/">Turbine</a> and the
* GenerationJavaCore library and from
* <a href="http://www.purpletech.com/code/">Purple Technology</a>
* </p>
*
* @author Apache Jakarta Turbine
* @author GenerationJavaCore library
* @author Purple Technology
* @author <a href="mailto:bayard@generationjava.com">Henri Yandell</a>
* @author <a href="mailto:alex@purpletech.com">Alexander Day Chaffee</a>
* @author <a href="mailto:cybertiger@cyberiantiger.org">Antony Riley</a>
@ -73,7 +70,7 @@ import org.apache.commons.lang.exception.NestableRuntimeException;
* @author <a href="sean@boohai.com">Sean Brown</a>
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @since 2.0
* @version $Id: StringEscapeUtils.java,v 1.16 2003/07/08 05:59:58 bayard Exp $
* @version $Id: StringEscapeUtils.java,v 1.17 2003/07/19 20:22:36 scolebourne Exp $
*/
public class StringEscapeUtils {
@ -115,9 +112,8 @@ public class StringEscapeUtils {
* </pre>
* </p>
*
* @param str String to escape values in
* @return String with escaped values
* @throws NullPointerException if str is <code>null</code>
* @param str String to escape values in, may be null
* @return String with escaped values, <code>null</code> if null string input
*/
public static String escapeJava(String str) {
return escapeJavaStyleString(str, false);
@ -127,10 +123,12 @@ public class StringEscapeUtils {
* <p>Escapes the characters in a <code>String</code> using Java String rules to
* a <code>Writer</code>.</p>
*
* <p>A <code>null</code> string input has no effect.</p>
*
* @see #escapeJava(java.lang.String)
* @param out Writer to write escaped string into
* @param str String to escape values in
* @throws NullPointerException if str is <code>null</code>
* @param str String to escape values in, may be null
* @throws IllegalArgumentException if the Writer is <code>null</code>
* @throws IOException if error occurs on undelying Writer
*/
public static void escapeJava(Writer out, String str) throws IOException {
@ -155,9 +153,8 @@ public class StringEscapeUtils {
* </pre>
* </p>
*
* @param str String to escape values in
* @return String with escaped values
* @throws NullPointerException if str is <code>null</code>
* @param str String to escape values in, may be null
* @return String with escaped values, <code>null</code> if null string input
*/
public static String escapeJavaScript(String str) {
return escapeJavaStyleString(str, true);
@ -167,10 +164,12 @@ public class StringEscapeUtils {
* <p>Escapes the characters in a <code>String</code> using JavaScript String rules
* to a <code>Writer</code>.</p>
*
* <p>A <code>null</code> string input has no effect.</p>
*
* @see #escapeJavaScript(java.lang.String)
* @param out Writer to write escaped string into
* @param str String to escape values in
* @throws NullPointerException if str is <code>null</code>
* @param str String to escape values in, may be null
* @throws IllegalArgumentException if the Writer is <code>null</code>
* @throws IOException if error occurs on undelying Writer
**/
public static void escapeJavaScript(Writer out, String str) throws IOException {
@ -178,6 +177,9 @@ public class StringEscapeUtils {
}
private static String escapeJavaStyleString(String str, boolean escapeSingleQuotes) {
if (str == null) {
return null;
}
try {
StringPrintWriter writer = new StringPrintWriter(str.length() * 2);
escapeJavaStyleString(writer, str, escapeSingleQuotes);
@ -190,6 +192,12 @@ public class StringEscapeUtils {
}
private static void escapeJavaStyleString(Writer out, String str, boolean escapeSingleQuote) throws IOException {
if (out == null) {
throw new NullArgumentException("Writer");
}
if (str == null) {
return;
}
int sz;
sz = str.length();
for (int i = 0; i < sz; i++) {
@ -271,10 +279,13 @@ public class StringEscapeUtils {
* <code>'n'</code> into a newline character, unless the <code>'\'</code>
* is preceded by another <code>'\'</code>.</p>
*
* @param str The <code>String</code> to unescape.
* @return A new unescaped <code>String</code>.
* @param str the <code>String</code> to unescape, may be null
* @return a new unescaped <code>String</code>, <code>null</code> if null string input
*/
public static String unescapeJava(String str) {
if (str == null) {
return null;
}
try {
StringPrintWriter writer = new StringPrintWriter(str.length());
unescapeJava(writer, str);
@ -294,10 +305,19 @@ public class StringEscapeUtils {
* <code>'n'</code> into a newline character, unless the <code>'\'</code>
* is preceded by another <code>'\'</code>.</p>
*
* @param out The <code>Writer</code> used to output unescaped characters.
* @param str The <code>String</code> to unescape.
* <p>A <code>null</code> string input has no effect.</p>
*
* @param out the <code>Writer</code> used to output unescaped characters
* @param str the <code>String</code> to unescape, may be null
* @throws IllegalArgumentException if the Writer is <code>null</code>
*/
public static void unescapeJava(Writer out, String str) throws IOException {
if (out == null) {
throw new NullArgumentException("Writer");
}
if (str == null) {
return;
}
int sz = str.length();
StringBuffer unicode = new StringBuffer(4);
boolean hadSlash = false;
@ -382,9 +402,9 @@ public class StringEscapeUtils {
* into a newline character, unless the <code>'\'</code> is preceded by another
* <code>'\'</code>.</p>
*
* @param str The <code>String</code> to unescape.
* @return A new unescaped <code>String</code>.
* @see #unescapeJava(String)
* @param str the <code>String</code> to unescape, may be null
* @return A new unescaped <code>String</code>, <code>null</code> if null string input
*/
public static String unescapeJavaScript(String str) {
return unescapeJava(str);
@ -398,10 +418,12 @@ public class StringEscapeUtils {
* into a newline character, unless the <code>'\'</code> is preceded by another
* <code>'\'</code>.</p>
*
* @param out The <code>Writer</code> used to output unescaped characters.
* @param str The <code>String</code> to unescape.
* <p>A <code>null</code> string input has no effect.</p>
*
* @see #unescapeJava(Writer,String)
* @param out the <code>Writer</code> used to output unescaped characters
* @param str the <code>String</code> to unescape, may be null
* @throws IllegalArgumentException if the Writer is <code>null</code>
*/
public static void unescapeJavaScript(Writer out, String str) throws IOException {
unescapeJava(out, str);
@ -419,8 +441,8 @@ public class StringEscapeUtils {
*
* <p>Supports all known HTML 4.0 entities, including funky accents.</p>
*
* @param str The <code>String</code> to escape
* @return A new escaped <code>String</code>.
* @param str the <code>String</code> to escape, may be null
* @return a new escaped <code>String</code>, <code>null</code> if null string input
*
* @see Entities
* @see #unescapeHtml(String)
@ -431,6 +453,9 @@ public class StringEscapeUtils {
* @see </br><a href="http://www.w3.org/TR/html401/charset.html#code-position">HTML 4.01 Code positions</a>
**/
public static String escapeHtml(String str) {
if (str == null) {
return null;
}
//todo: add a version that takes a Writer
//todo: rewrite underlying method to use a Writer instead of a StringBuffer
return Entities.HTML40.escape(str);
@ -448,11 +473,14 @@ public class StringEscapeUtils {
* verbatim into the result string. e.g. "&amp;gt;&amp;zzzz;x" will
* become "&gt;&amp;zzzz;x".</p>
*
* @param str The <code>String</code> to unescape
* @return A new unescaped <code>String</code>.
* @param str the <code>String</code> to unescape, may be null
* @return a new unescaped <code>String</code>, <code>null</code> if null string input
* @see #escapeHtml(String)
**/
public static String unescapeHtml(String str) {
if (str == null) {
return null;
}
return Entities.HTML40.unescape(str);
}
@ -466,11 +494,14 @@ public class StringEscapeUtils {
* <p>Supports only the four basic XML entities (gt, lt, quot, amp).
* Does not support DTDs or external entities.</p>
*
* @param str The <code>String</code> to escape
* @return A new escaped <code>String</code>.
* @param str the <code>String</code> to escape, may be null
* @return a new escaped <code>String</code>, <code>null</code> if null string input
* @see #unescapeXml(java.lang.String)
**/
public static String escapeXml(String str) {
if (str == null) {
return null;
}
return Entities.XML.escape(str);
}
@ -482,11 +513,14 @@ public class StringEscapeUtils {
* <p>Supports only the four basic XML entities (gt, lt, quot, amp).
* Does not support DTDs or external entities.</p>
*
* @param str The <code>String</code> to unescape
* @return A new unescaped <code>String</code>.
* @param str the <code>String</code> to unescape, may be null
* @return a new unescaped <code>String</code>, <code>null</code> if null string input
* @see #escapeXml(String)
**/
public static String unescapeXml(String str) {
if (str == null) {
return null;
}
return Entities.XML.unescape(str);
}
@ -505,12 +539,14 @@ public class StringEscapeUtils {
* handle the cases of percent (%) or underscore (_) for use in LIKE clauses.</p>
*
* see http://www.jguru.com/faq/view.jsp?EID=8881
* @param s the string to escape
* @return A new String, escaped for SQL
* @param str the string to escape, may be null
* @return a new String, escaped for SQL, <code>null</code> if null string input
*/
public static String escapeSql(String s)
{
return StringUtils.replace(s, "'", "''");
public static String escapeSql(String str) {
if (str == null) {
return null;
}
return StringUtils.replace(str, "'", "''");
}
}

View File

@ -57,13 +57,17 @@ import java.util.NoSuchElementException;
import java.util.StringTokenizer;
/**
* <code>WordWrapUtils</code> is a utility class to assist with word wrapping.
* <p><code>WordWrapUtils</code> is a utility class to assist with word wrapping.</p>
*
* <p>This class tries to handle <code>null</code> input gracefully.
* An exception will not be thrown for a <code>null</code> input.
* Each method documents its behaviour in more detail.</p>
*
* @author Henri Yandell
* @author Stephen Colebourne
* @author <a href="mailto:hps@intermeta.de">Henning P. Schmiedehausen</a>
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
* @version $Id: WordWrapUtils.java,v 1.6 2003/07/12 03:06:23 bayard Exp $
* @version $Id: WordWrapUtils.java,v 1.7 2003/07/19 20:22:36 scolebourne Exp $
*/
public class WordWrapUtils {
@ -91,13 +95,20 @@ public class WordWrapUtils {
* since tabs are a single character but are displayed as 4 or 8
* spaces. Remove the tabs.</p>
*
* @param str text which is in need of word-wrapping
* @param newline the characters that define a newline
* @param str text which is in need of word-wrapping, may be null
* @param newLineChars the characters that define a newline, null treated as \n
* @param wrapColumn the column to wrap the words at
* @return the text with all the long lines word-wrapped
* @return the text with all the long lines word-wrapped,
* <code>null</code> if null string input
*/
public static String wrapText(String str, String newline, int wrapColumn) {
StringTokenizer lineTokenizer = new StringTokenizer(str, newline, true);
public static String wrapText(String str, String newLineChars, int wrapColumn) {
if (str == null) {
return null;
}
if (newLineChars == null) {
newLineChars = "\n";
}
StringTokenizer lineTokenizer = new StringTokenizer(str, newLineChars, true);
StringBuffer stringBuffer = new StringBuffer();
while (lineTokenizer.hasMoreTokens()) {
@ -106,7 +117,7 @@ public class WordWrapUtils {
if (nextLine.length() > wrapColumn) {
// This line is long enough to be wrapped.
nextLine = wrapLine(nextLine, newline, wrapColumn);
nextLine = wrapLine(nextLine, newLineChars, wrapColumn);
}
stringBuffer.append(nextLine);

View File

@ -63,10 +63,11 @@ 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>
* @version $Id: CharSetUtilsTest.java,v 1.7 2003/03/23 21:47:30 scolebourne Exp $
* @author Stephen Colebourne
* @version $Id: CharSetUtilsTest.java,v 1.8 2003/07/19 20:22:36 scolebourne Exp $
*/
public class CharSetUtilsTest extends TestCase
{
public class CharSetUtilsTest extends TestCase {
public CharSetUtilsTest(String name) {
super(name);
}
@ -91,66 +92,59 @@ public class CharSetUtilsTest extends TestCase
//-----------------------------------------------------------------------
public void testSqueeze()
{
assertEquals("squeeze(String,String[]) failed",
"helo", CharSetUtils.squeeze("hello", new String[] {"el"}));
assertEquals("squeeze(String,String[]) failed",
"", CharSetUtils.squeeze("", new String[] {"el"}));
assertEquals("squeeze(String,String[]) failed",
"hello", CharSetUtils.squeeze("hello", new String[] {"e"}));
assertEquals("squeeze(String,String[]) failed",
"fofof", CharSetUtils.squeeze("fooffooff", new String[] {"of"}));
assertEquals("squeeze(String,String[]) failed",
"fof", CharSetUtils.squeeze("fooooff", new String[] {"fo"}));
public void testSqueeze() {
assertEquals(null, CharSetUtils.squeeze(null, (String[]) null));
assertEquals(null, CharSetUtils.squeeze(null, new String[] { "el" }));
assertEquals("helo", CharSetUtils.squeeze("hello", new String[] { "el" }));
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);
} catch (NullPointerException ex) {}
}
public void testCount()
{
assertEquals("count(String,String[]) failed",
3, CharSetUtils.count("hello", new String[] {"el"}));
assertEquals("count(String,String[]) failed",
0, CharSetUtils.count("", new String[] {"el"}));
assertEquals("count(String,String[]) failed",
0, CharSetUtils.count("hello", new String[] {"x"}));
assertEquals("count(String,String[]) failed",
2, CharSetUtils.count("hello", new String[] {"e-i"}));
assertEquals("count(String,String[]) failed",
5, CharSetUtils.count("hello", new String[] {"a-z"}));
assertEquals("count(String,String[]) failed",
0, CharSetUtils.count("hello", new String[] {""}));
public void testCount() {
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" }));
assertEquals(0, CharSetUtils.count("hello", new String[] { "x" }));
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[] { "" }));
try {
CharSetUtils.count("hello", (String[]) null);
} catch (NullPointerException ex) {}
}
public void testKeep()
{
assertEquals("keep(String,String[]) failed",
"ell", CharSetUtils.keep("hello", new String[] {"el"}));
assertEquals("keep(String,String[]) failed",
"hello", CharSetUtils.keep("hello", new String[] {"elho"}));
assertEquals("keep(String,String[]) failed",
"", CharSetUtils.keep("hello", new String[] {""}));
assertEquals("keep(String,String[]) failed",
"hello", CharSetUtils.keep("hello", new String[] {"a-z"}));
assertEquals("keep(String,String[]) failed",
"----", CharSetUtils.keep("----", new String[] {"-"}));
assertEquals("keep(String,String[]) failed",
"ll", CharSetUtils.keep("hello", new String[] {"l"}));
public void testKeep() {
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("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);
} catch (NullPointerException ex) {}
}
public void testDelete()
{
assertEquals("delete(String,String[]) failed",
"ho", CharSetUtils.delete("hello", new String[] {"el"}));
assertEquals("delete(String,String[]) failed",
"", CharSetUtils.delete("hello", new String[] {"elho"}));
assertEquals("delete(String,String[]) failed",
"hello", CharSetUtils.delete("hello", new String[] {""}));
assertEquals("delete(String,String[]) failed",
"", CharSetUtils.delete("hello", new String[] {"a-z"}));
assertEquals("delete(String,String[]) failed",
"", CharSetUtils.delete("----", new String[] {"-"}));
assertEquals("delete(String,String[]) failed",
"heo", CharSetUtils.delete("hello", new String[] {"l"}));
public void testDelete() {
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("", 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);
} catch (NullPointerException ex) {}
}
}

View File

@ -69,7 +69,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: SerializationUtilsTest.java,v 1.2 2003/03/23 21:50:58 scolebourne Exp $
* @version $Id: SerializationUtilsTest.java,v 1.3 2003/07/19 20:22:36 scolebourne Exp $
*/
public class SerializationUtilsTest extends TestCase {
private String iString;
@ -157,7 +157,7 @@ public class SerializationUtilsTest extends TestCase {
ByteArrayOutputStream streamTest = new ByteArrayOutputStream();
try {
SerializationUtils.serialize(iMap, null);
} catch (NullPointerException ex) {
} catch (IllegalArgumentException ex) {
return;
}
fail();
@ -167,7 +167,7 @@ public class SerializationUtilsTest extends TestCase {
ByteArrayOutputStream streamTest = new ByteArrayOutputStream();
try {
SerializationUtils.serialize(null, null);
} catch (NullPointerException ex) {
} catch (IllegalArgumentException ex) {
return;
}
fail();
@ -210,7 +210,7 @@ public class SerializationUtilsTest extends TestCase {
public void testDeserializeStreamNull() throws Exception {
try {
SerializationUtils.deserialize((InputStream) null);
} catch (NullPointerException ex) {
} catch (IllegalArgumentException ex) {
return;
}
fail();
@ -304,7 +304,7 @@ public class SerializationUtilsTest extends TestCase {
public void testDeserializeBytesNull() throws Exception {
try {
SerializationUtils.deserialize((byte[]) null);
} catch (NullPointerException ex) {
} catch (IllegalArgumentException ex) {
return;
}
fail();

View File

@ -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.8 2003/06/29 03:05:45 alex Exp $
* @version $Id: StringEscapeUtilsTest.java,v 1.9 2003/07/19 20:22:36 scolebourne Exp $
*/
public class StringEscapeUtilsTest extends TestCase {
private final static String FOO = "foo";
@ -87,6 +87,22 @@ public class StringEscapeUtilsTest extends TestCase {
//-----------------------------------------------------------------------
public void testEscapeJava() throws IOException {
assertEquals(null, StringEscapeUtils.escapeJava(null));
try {
StringEscapeUtils.escapeJava(null, null);
fail();
} catch (IOException ex) {
fail();
} catch (IllegalArgumentException ex) {
}
try {
StringEscapeUtils.escapeJava(null, "");
fail();
} catch (IOException ex) {
fail();
} catch (IllegalArgumentException ex) {
}
assertEscapeJava("empty string", "", "");
assertEscapeJava(FOO, FOO);
assertEscapeJava("tab", "\\t", "\t");
@ -122,6 +138,22 @@ public class StringEscapeUtilsTest extends TestCase {
}
public void testUnescapeJava() throws IOException {
assertEquals(null, StringEscapeUtils.unescapeJava(null));
try {
StringEscapeUtils.unescapeJava(null, null);
fail();
} catch (IOException ex) {
fail();
} catch (IllegalArgumentException ex) {
}
try {
StringEscapeUtils.unescapeJava(null, "");
fail();
} catch (IOException ex) {
fail();
} catch (IllegalArgumentException ex) {
}
assertUnescapeJava("", "");
assertUnescapeJava("test", "test");
assertUnescapeJava("\ntest\b", "\\ntest\\b");
@ -154,6 +186,22 @@ public class StringEscapeUtilsTest extends TestCase {
}
public void testEscapeJavaScript() {
assertEquals(null, StringEscapeUtils.escapeJavaScript(null));
try {
StringEscapeUtils.escapeJavaScript(null, null);
fail();
} catch (IOException ex) {
fail();
} catch (IllegalArgumentException ex) {
}
try {
StringEscapeUtils.escapeJavaScript(null, "");
fail();
} catch (IOException ex) {
fail();
} catch (IllegalArgumentException ex) {
}
assertEquals("He didn\\'t say, \\\"stop!\\\"", StringEscapeUtils.escapeJavaScript("He didn't say, \"stop!\""));
}

View File

@ -61,7 +61,7 @@ import junit.framework.TestSuite;
* Unit tests for the wrap methods of WordWrapUtils.
*
* @author <a href="mailto:ridesmet@users.sourceforge.net">Ringo De Smet</a>
* @version $Id: WordWrapUtilsTest.java,v 1.1 2003/04/15 14:28:41 bayard Exp $
* @version $Id: WordWrapUtilsTest.java,v 1.2 2003/07/19 20:22:36 scolebourne Exp $
*/
public class WordWrapUtilsTest extends TestCase {
@ -107,4 +107,9 @@ public class WordWrapUtilsTest extends TestCase {
"Here is one line\nof\ttext that is\ngoing to be wrapped\nafter 20 columns.";
assertEquals("Text with tab at wrapping index didn't wrap correctly, ", expected, WordWrapUtils.wrapText(input, "\n", 20));
}
public void testWrapText4() {
assertEquals(null, WordWrapUtils.wrapText(null, "\n", 20));
assertEquals("", WordWrapUtils.wrapText("", "\n", 20));
}
}