mirror of
https://github.com/apache/commons-lang.git
synced 2025-02-10 12:05:06 +00:00
Add new method center(String, int, char) to complete API
Rework padding internals Example javadoc difference()/differenceAt() Remove dependency on NumberUtils git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@137471 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
5d5af47995
commit
4c1e760dd8
@ -57,8 +57,6 @@
|
|||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.apache.commons.lang.math.NumberUtils;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Common <code>String</code> manipulation routines that are
|
* <p>Common <code>String</code> manipulation routines that are
|
||||||
* <code>null</code> safe.</p>
|
* <code>null</code> safe.</p>
|
||||||
@ -98,7 +96,7 @@
|
|||||||
* @author Arun Mammen Thomas
|
* @author Arun Mammen Thomas
|
||||||
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
|
* @author <a href="mailto:ggregory@seagullsw.com">Gary Gregory</a>
|
||||||
* @since 1.0
|
* @since 1.0
|
||||||
* @version $Id: StringUtils.java,v 1.70 2003/07/20 00:37:09 scolebourne Exp $
|
* @version $Id: StringUtils.java,v 1.71 2003/07/20 10:29:22 scolebourne Exp $
|
||||||
*/
|
*/
|
||||||
public class StringUtils {
|
public class StringUtils {
|
||||||
// Performance testing notes (JDK 1.4, Jul03, scolebourne)
|
// Performance testing notes (JDK 1.4, Jul03, scolebourne)
|
||||||
@ -122,19 +120,16 @@ public class StringUtils {
|
|||||||
*/
|
*/
|
||||||
private static int PAD_LIMIT = 8192;
|
private static int PAD_LIMIT = 8192;
|
||||||
|
|
||||||
/**
|
|
||||||
* <p>A <code>String</code> containing all space characters (' ').</p>
|
|
||||||
*
|
|
||||||
* <p>Used for efficient space padding. The length of the String expands as needed.</p>
|
|
||||||
*/
|
|
||||||
private static String spaces = new String(" ");
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>An array of <code>String</code>s used for padding.</p>
|
* <p>An array of <code>String</code>s used for padding.</p>
|
||||||
*
|
*
|
||||||
* <p>Used for efficient space padding. The length of each String expands as needed.</p>
|
* <p>Used for efficient space padding. The length of each String expands as needed.</p>
|
||||||
*/
|
*/
|
||||||
private final static String[] padding = new String[Character.MAX_VALUE];
|
private final static String[] padding = new String[Character.MAX_VALUE];
|
||||||
|
|
||||||
|
static {
|
||||||
|
padding[32] = " ";
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p><code>StringUtils<code> instances should NOT be constructed in
|
* <p><code>StringUtils<code> instances should NOT be constructed in
|
||||||
@ -2392,27 +2387,6 @@ public static String repeat(String str, int repeat) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* <p>Returns a String containing the requested number of
|
|
||||||
* space characters (' ').</p>
|
|
||||||
*
|
|
||||||
* <pre>
|
|
||||||
* StringUtils.padding(0) = ""
|
|
||||||
* StringUtils.padding(3) = " "
|
|
||||||
* StringUtils.padding(-2) = IndexOutOfBoundsException
|
|
||||||
* </pre>
|
|
||||||
*
|
|
||||||
* @param repeat number of times to repeat space
|
|
||||||
* @return a String with <code>repeat</code> spaces
|
|
||||||
* @throws IndexOutOfBoundsException if <code>repeat < 0</code>
|
|
||||||
*/
|
|
||||||
private static String padding(int repeat) {
|
|
||||||
while (spaces.length() < repeat) {
|
|
||||||
spaces = spaces.concat(spaces);
|
|
||||||
}
|
|
||||||
return spaces.substring(0, repeat);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Returns padding using the specified delimiter repeated
|
* <p>Returns padding using the specified delimiter repeated
|
||||||
* to a given length.</p>
|
* to a given length.</p>
|
||||||
@ -2457,17 +2431,7 @@ private static String padding(int repeat, char padChar) {
|
|||||||
* <code>null</code> if null String input
|
* <code>null</code> if null String input
|
||||||
*/
|
*/
|
||||||
public static String rightPad(String str, int size) {
|
public static String rightPad(String str, int size) {
|
||||||
if (str == null) {
|
return rightPad(str, size, ' ');
|
||||||
return null;
|
|
||||||
}
|
|
||||||
int pads = size - str.length();
|
|
||||||
if (pads <= 0) {
|
|
||||||
return str; // returns original String when possible
|
|
||||||
}
|
|
||||||
if (pads > PAD_LIMIT) {
|
|
||||||
return rightPad(str, size, ' ');
|
|
||||||
}
|
|
||||||
return str.concat(padding(pads));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -2517,7 +2481,7 @@ public static String rightPad(String str, int size, char padChar) {
|
|||||||
* StringUtils.rightPad("bat", -1, "yz") = "bat"
|
* StringUtils.rightPad("bat", -1, "yz") = "bat"
|
||||||
* StringUtils.rightPad("bat", 1, null) = IllegalArgumentException
|
* StringUtils.rightPad("bat", 1, null) = IllegalArgumentException
|
||||||
* StringUtils.rightPad("bat", 1, "") = IllegalArgumentException
|
* StringUtils.rightPad("bat", 1, "") = IllegalArgumentException
|
||||||
* StringUtils.rightPad(null, 1, "") = IllegalArgumentException
|
* StringUtils.rightPad(null, 1, "") = null
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @param str the String to pad out, may be null
|
* @param str the String to pad out, may be null
|
||||||
@ -2528,13 +2492,13 @@ public static String rightPad(String str, int size, char padChar) {
|
|||||||
* @throws IllegalArgumentException if padStr is the empty String or null
|
* @throws IllegalArgumentException if padStr is the empty String or null
|
||||||
*/
|
*/
|
||||||
public static String rightPad(String str, int size, String padStr) {
|
public static String rightPad(String str, int size, String padStr) {
|
||||||
|
if (str == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
int padLen;
|
int padLen;
|
||||||
if (padStr == null || (padLen = padStr.length()) == 0) {
|
if (padStr == null || (padLen = padStr.length()) == 0) {
|
||||||
throw new IllegalArgumentException("Pad String must not be null or empty");
|
throw new IllegalArgumentException("Pad String must not be null or empty");
|
||||||
}
|
}
|
||||||
if (str == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
int strLen = str.length();
|
int strLen = str.length();
|
||||||
int pads = size - strLen;
|
int pads = size - strLen;
|
||||||
if (padLen == 1 && pads <= PAD_LIMIT) {
|
if (padLen == 1 && pads <= PAD_LIMIT) {
|
||||||
@ -2577,17 +2541,7 @@ public static String rightPad(String str, int size, String padStr) {
|
|||||||
* <code>null</code> if null String input
|
* <code>null</code> if null String input
|
||||||
*/
|
*/
|
||||||
public static String leftPad(String str, int size) {
|
public static String leftPad(String str, int size) {
|
||||||
if (str == null) {
|
return leftPad(str, size, ' ');
|
||||||
return null;
|
|
||||||
}
|
|
||||||
int pads = size - str.length();
|
|
||||||
if (pads <= 0) {
|
|
||||||
return str; // returns original String when possible
|
|
||||||
}
|
|
||||||
if (pads > PAD_LIMIT) {
|
|
||||||
return leftPad(str, size, ' ');
|
|
||||||
}
|
|
||||||
return padding(pads).concat(str);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -2637,7 +2591,7 @@ public static String leftPad(String str, int size, char padChar) {
|
|||||||
* StringUtils.leftPad("bat", -1, "yz") = "bat"
|
* StringUtils.leftPad("bat", -1, "yz") = "bat"
|
||||||
* StringUtils.leftPad("bat", 1, null) = IllegalArgumentException
|
* StringUtils.leftPad("bat", 1, null) = IllegalArgumentException
|
||||||
* StringUtils.leftPad("bat", 1, "") = IllegalArgumentException
|
* StringUtils.leftPad("bat", 1, "") = IllegalArgumentException
|
||||||
* StringUtils.leftPad(null, 1, "") = IllegalArgumentException
|
* StringUtils.leftPad(null, 1, "") = null
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @param str the String to pad out, may be null
|
* @param str the String to pad out, may be null
|
||||||
@ -2648,13 +2602,13 @@ public static String leftPad(String str, int size, char padChar) {
|
|||||||
* @throws IllegalArgumentException if padStr is the empty String or null
|
* @throws IllegalArgumentException if padStr is the empty String or null
|
||||||
*/
|
*/
|
||||||
public static String leftPad(String str, int size, String padStr) {
|
public static String leftPad(String str, int size, String padStr) {
|
||||||
|
if (str == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
int padLen;
|
int padLen;
|
||||||
if (padStr == null || (padLen = padStr.length()) == 0) {
|
if (padStr == null || (padLen = padStr.length()) == 0) {
|
||||||
throw new IllegalArgumentException("Pad String must not be null or empty");
|
throw new IllegalArgumentException("Pad String must not be null or empty");
|
||||||
}
|
}
|
||||||
if (str == null) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
int strLen = str.length();
|
int strLen = str.length();
|
||||||
int pads = size - strLen;
|
int pads = size - strLen;
|
||||||
if (padLen == 1 && pads <= PAD_LIMIT) {
|
if (padLen == 1 && pads <= PAD_LIMIT) {
|
||||||
@ -2706,6 +2660,34 @@ public static String leftPad(String str, int size, String padStr) {
|
|||||||
* @return centered String, <code>null</code> if null String input
|
* @return centered String, <code>null</code> if null String input
|
||||||
*/
|
*/
|
||||||
public static String center(String str, int size) {
|
public static String center(String str, int size) {
|
||||||
|
return center(str, size, ' ');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Centers a String in a larger String of size <code>size</code>.
|
||||||
|
* Uses a supplied character as the value to pad the String with.</p>
|
||||||
|
*
|
||||||
|
* <p>If the size is less than the String length, the String is returned.
|
||||||
|
* A <code>null</code> String returns <code>null</code>.
|
||||||
|
* A negative size is treated as zero.</p>
|
||||||
|
*
|
||||||
|
* <pre>
|
||||||
|
* StringUtils.center(null, -1, ' ') = null
|
||||||
|
* StringUtils.center("ab", -1, ' ') = "ab"
|
||||||
|
* StringUtils.center(null, 4, ' ') = null
|
||||||
|
* StringUtils.center("", 4, ' ') = " "
|
||||||
|
* StringUtils.center("ab", 4, ' ') = " ab"
|
||||||
|
* StringUtils.center("abcd", 2, ' ') = "abcd"
|
||||||
|
* StringUtils.center("a", 4, ' ') = " a "
|
||||||
|
* StringUtils.center("a", 4, 'y') = "yayy"
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
|
* @param str the String to center, may be null
|
||||||
|
* @param size the int size of new String, negative treated as zero
|
||||||
|
* @param padChar the character to pad the new String with
|
||||||
|
* @return centered String, <code>null</code> if null String input
|
||||||
|
*/
|
||||||
|
public static String center(String str, int size, char padChar) {
|
||||||
if (str == null || size <= 0) {
|
if (str == null || size <= 0) {
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
@ -2714,15 +2696,14 @@ public static String center(String str, int size) {
|
|||||||
if (pads <= 0) {
|
if (pads <= 0) {
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
str = leftPad(str, strLen + pads / 2, ' ');
|
str = leftPad(str, strLen + pads / 2, padChar);
|
||||||
str = rightPad(str, size, ' ');
|
str = rightPad(str, size, padChar);
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* <p>Centers a String in a larger String of size <code>size</code>.</p>
|
* <p>Centers a String in a larger String of size <code>size</code>.
|
||||||
*
|
* Uses a supplied String as the value to pad the String with.</p>
|
||||||
* <p>Uses a supplied String as the value to pad the String with.</p>
|
|
||||||
*
|
*
|
||||||
* <p>If the size is less than the String length, the String is returned.
|
* <p>If the size is less than the String length, the String is returned.
|
||||||
* A <code>null</code> String returns <code>null</code>.
|
* A <code>null</code> String returns <code>null</code>.
|
||||||
@ -2734,12 +2715,12 @@ public static String center(String str, int size) {
|
|||||||
* StringUtils.center(null, 4, " ") = null
|
* StringUtils.center(null, 4, " ") = null
|
||||||
* StringUtils.center("", 4, " ") = " "
|
* StringUtils.center("", 4, " ") = " "
|
||||||
* StringUtils.center("ab", 4, " ") = " ab"
|
* StringUtils.center("ab", 4, " ") = " ab"
|
||||||
* StringUtils.center("abcd", 2, " ") = " abcd"
|
* StringUtils.center("abcd", 2, " ") = "abcd"
|
||||||
* StringUtils.center("a", 4, " ") = " a "
|
* StringUtils.center("a", 4, " ") = " a "
|
||||||
* StringUtils.center("a", 4, "yz") = "yayz"
|
* StringUtils.center("a", 4, "yz") = "yayz"
|
||||||
* StringUtils.center("abc", 4, null) = IllegalArgumentException
|
* StringUtils.center("abc", 4, null) = IllegalArgumentException
|
||||||
* StringUtils.center("abc", 4, "") = IllegalArgumentException
|
* StringUtils.center("abc", 4, "") = IllegalArgumentException
|
||||||
* StringUtils.center(null, 4, "") = IllegalArgumentException
|
* StringUtils.center(null, 4, "") = null
|
||||||
* </pre>
|
* </pre>
|
||||||
*
|
*
|
||||||
* @param str the String to center, may be null
|
* @param str the String to center, may be null
|
||||||
@ -2749,12 +2730,12 @@ public static String center(String str, int size) {
|
|||||||
* @throws IllegalArgumentException if padStr is <code>null</code> or empty
|
* @throws IllegalArgumentException if padStr is <code>null</code> or empty
|
||||||
*/
|
*/
|
||||||
public static String center(String str, int size, String padStr) {
|
public static String center(String str, int size, String padStr) {
|
||||||
if (padStr == null || padStr.length() == 0) {
|
|
||||||
throw new IllegalArgumentException("Pad String must not be null or empty");
|
|
||||||
}
|
|
||||||
if (str == null || size <= 0) {
|
if (str == null || size <= 0) {
|
||||||
return str;
|
return str;
|
||||||
}
|
}
|
||||||
|
if (padStr == null || padStr.length() == 0) {
|
||||||
|
throw new IllegalArgumentException("Pad String must not be null or empty");
|
||||||
|
}
|
||||||
int strLen = str.length();
|
int strLen = str.length();
|
||||||
int pads = size - strLen;
|
int pads = size - strLen;
|
||||||
if (pads <= 0) {
|
if (pads <= 0) {
|
||||||
@ -3494,6 +3475,17 @@ public static String abbreviate(String str, int offset, int maxWidth) {
|
|||||||
* <p>For example,
|
* <p>For example,
|
||||||
* <code>difference("i am a machine", "i am a robot") -> "robot"</code>.</p>
|
* <code>difference("i am a machine", "i am a robot") -> "robot"</code>.</p>
|
||||||
*
|
*
|
||||||
|
* <pre>
|
||||||
|
* StringUtils.difference(null, null) = null
|
||||||
|
* StringUtils.difference("", "") = ""
|
||||||
|
* StringUtils.difference("", "abc") = "abc"
|
||||||
|
* StringUtils.difference("abc", "") = ""
|
||||||
|
* StringUtils.difference("abc", "abc") = ""
|
||||||
|
* StringUtils.difference("ab", "abxyz") = "xyz"
|
||||||
|
* StringUtils.difference("abcde", "abxyz") = "xyz"
|
||||||
|
* StringUtils.difference("abcde", "xyz") = "xyz"
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
* @param str1 the first String, may be null
|
* @param str1 the first String, may be null
|
||||||
* @param str2 the second String, may be null
|
* @param str2 the second String, may be null
|
||||||
* @return the portion of str2 where it differs from str1; returns the
|
* @return the portion of str2 where it differs from str1; returns the
|
||||||
@ -3520,6 +3512,17 @@ public static String difference(String str1, String str2) {
|
|||||||
* <p>For example,
|
* <p>For example,
|
||||||
* <code>differenceAt("i am a machine", "i am a robot") -> 7</code></p>
|
* <code>differenceAt("i am a machine", "i am a robot") -> 7</code></p>
|
||||||
*
|
*
|
||||||
|
* <pre>
|
||||||
|
* StringUtils.differenceAt(null, null) = -1
|
||||||
|
* StringUtils.differenceAt("", "") = -1
|
||||||
|
* StringUtils.differenceAt("", "abc") = 0
|
||||||
|
* StringUtils.differenceAt("abc", "") = 0
|
||||||
|
* StringUtils.differenceAt("abc", "abc") = -1
|
||||||
|
* StringUtils.differenceAt("ab", "abxyz") = 2
|
||||||
|
* StringUtils.differenceAt("abcde", "abxyz") = 2
|
||||||
|
* StringUtils.differenceAt("abcde", "xyz") = 0
|
||||||
|
* </pre>
|
||||||
|
*
|
||||||
* @param str1 the first String, may be null
|
* @param str1 the first String, may be null
|
||||||
* @param str2 the second String, may be null
|
* @param str2 the second String, may be null
|
||||||
* @return the index where str2 and str1 begin to differ; -1 if they are equal
|
* @return the index where str2 and str1 begin to differ; -1 if they are equal
|
||||||
@ -3610,7 +3613,7 @@ public static int getLevenshteinDistance(String s, String t) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Step 6
|
// Step 6
|
||||||
d[i][j] = NumberUtils.min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
|
d[i][j] = min(d[i - 1][j] + 1, d[i][j - 1] + 1, d[i - 1][j - 1] + cost);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -3618,5 +3621,24 @@ public static int getLevenshteinDistance(String s, String t) {
|
|||||||
return d[n][m];
|
return d[n][m];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* <p>Gets the minimum of three <code>int</code> values.</p>
|
||||||
|
*
|
||||||
|
* @param a value 1
|
||||||
|
* @param b value 2
|
||||||
|
* @param c value 3
|
||||||
|
* @return the smallest of the values
|
||||||
|
*/
|
||||||
|
private static int min(int a, int b, int c) {
|
||||||
|
// Method copied from NumberUtils to avoid dependency on subpackage
|
||||||
|
if (b < a) {
|
||||||
|
a = b;
|
||||||
|
}
|
||||||
|
if (c < a) {
|
||||||
|
a = c;
|
||||||
|
}
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -72,7 +72,7 @@
|
|||||||
* @author <a href="mailto:fredrik@westermarck.com>Fredrik Westermarck</a>
|
* @author <a href="mailto:fredrik@westermarck.com>Fredrik Westermarck</a>
|
||||||
* @author Holger Krauth
|
* @author Holger Krauth
|
||||||
* @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
|
* @author <a href="hps@intermeta.de">Henning P. Schmiedehausen</a>
|
||||||
* @version $Id: StringUtilsTest.java,v 1.31 2003/07/20 00:17:29 scolebourne Exp $
|
* @version $Id: StringUtilsTest.java,v 1.32 2003/07/20 10:29:21 scolebourne Exp $
|
||||||
*/
|
*/
|
||||||
public class StringUtilsTest extends TestCase {
|
public class StringUtilsTest extends TestCase {
|
||||||
|
|
||||||
@ -279,7 +279,7 @@ public void testSplit_StringChar() {
|
|||||||
assertEquals("a", res[0]);
|
assertEquals("a", res[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testSplit() {
|
public void testSplit_StringString_StringStringInt() {
|
||||||
assertEquals(null, StringUtils.split(null, "."));
|
assertEquals(null, StringUtils.split(null, "."));
|
||||||
assertEquals(null, StringUtils.split(null, ".", 3));
|
assertEquals(null, StringUtils.split(null, ".", 3));
|
||||||
|
|
||||||
@ -298,47 +298,43 @@ public void testSplit() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void innerTestSplit(char separator, String sepStr, char noMatch) {
|
private void innerTestSplit(char separator, String sepStr, char noMatch) {
|
||||||
try {
|
String msg = "Failed on separator hex(" + Integer.toHexString(separator) +
|
||||||
final String str = "a" + separator + "b" + separator + separator + noMatch + "c";
|
"), noMatch hex(" + Integer.toHexString(noMatch) + "), sepStr(" + sepStr + ")";
|
||||||
String[] res;
|
|
||||||
// (str, sepStr)
|
final String str = "a" + separator + "b" + separator + separator + noMatch + "c";
|
||||||
res = StringUtils.split(str, sepStr);
|
String[] res;
|
||||||
assertEquals(3, res.length);
|
// (str, sepStr)
|
||||||
assertEquals("a", res[0]);
|
res = StringUtils.split(str, sepStr);
|
||||||
assertEquals("b", res[1]);
|
assertEquals(msg, 3, res.length);
|
||||||
assertEquals(noMatch + "c", res[2]);
|
assertEquals(msg, "a", res[0]);
|
||||||
|
assertEquals(msg, "b", res[1]);
|
||||||
final String str2 = separator + "a" + separator;
|
assertEquals(msg, noMatch + "c", res[2]);
|
||||||
res = StringUtils.split(str2, sepStr);
|
|
||||||
assertEquals(1, res.length);
|
final String str2 = separator + "a" + separator;
|
||||||
assertEquals("a", res[0]);
|
res = StringUtils.split(str2, sepStr);
|
||||||
|
assertEquals(msg, 1, res.length);
|
||||||
|
assertEquals(msg, "a", res[0]);
|
||||||
|
|
||||||
res = StringUtils.split(str, sepStr, -1);
|
res = StringUtils.split(str, sepStr, -1);
|
||||||
assertEquals(3, res.length);
|
assertEquals(msg, 3, res.length);
|
||||||
assertEquals("a", res[0]);
|
assertEquals(msg, "a", res[0]);
|
||||||
assertEquals("b", res[1]);
|
assertEquals(msg, "b", res[1]);
|
||||||
assertEquals(noMatch + "c", res[2]);
|
assertEquals(msg, noMatch + "c", res[2]);
|
||||||
|
|
||||||
res = StringUtils.split(str, sepStr, 0);
|
res = StringUtils.split(str, sepStr, 0);
|
||||||
assertEquals(3, res.length);
|
assertEquals(msg, 3, res.length);
|
||||||
assertEquals("a", res[0]);
|
assertEquals(msg, "a", res[0]);
|
||||||
assertEquals("b", res[1]);
|
assertEquals(msg, "b", res[1]);
|
||||||
assertEquals(noMatch + "c", res[2]);
|
assertEquals(msg, noMatch + "c", res[2]);
|
||||||
|
|
||||||
res = StringUtils.split(str, sepStr, 1);
|
res = StringUtils.split(str, sepStr, 1);
|
||||||
assertEquals(1, res.length);
|
assertEquals(msg, 1, res.length);
|
||||||
assertEquals(str, res[0]);
|
assertEquals(msg, str, res[0]);
|
||||||
|
|
||||||
res = StringUtils.split(str, sepStr, 2);
|
res = StringUtils.split(str, sepStr, 2);
|
||||||
assertEquals(2, res.length);
|
assertEquals(msg, 2, res.length);
|
||||||
assertEquals("a", res[0]);
|
assertEquals(msg, "a", res[0]);
|
||||||
assertEquals(str.substring(2), res[1]);
|
assertEquals(msg, 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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testDeleteSpace_String() {
|
public void testDeleteSpace_String() {
|
||||||
@ -433,7 +429,7 @@ public void testOverlayString() {
|
|||||||
} catch (IndexOutOfBoundsException ex) {}
|
} catch (IndexOutOfBoundsException ex) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testRepeat() {
|
public void testRepeat_StringInt() {
|
||||||
assertEquals(null, StringUtils.repeat(null, 2));
|
assertEquals(null, StringUtils.repeat(null, 2));
|
||||||
assertEquals("", StringUtils.repeat("ab", 0));
|
assertEquals("", StringUtils.repeat("ab", 0));
|
||||||
assertEquals("", StringUtils.repeat("", 3));
|
assertEquals("", StringUtils.repeat("", 3));
|
||||||
@ -442,46 +438,6 @@ public void testRepeat() {
|
|||||||
assertEquals("abcabcabc", StringUtils.repeat("abc", 3));
|
assertEquals("abcabcabc", StringUtils.repeat("abc", 3));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testCenter() {
|
|
||||||
assertEquals(null, StringUtils.center(null, -1));
|
|
||||||
assertEquals(null, StringUtils.center(null, 4));
|
|
||||||
assertEquals("ab", StringUtils.center("ab", 0));
|
|
||||||
assertEquals("ab", StringUtils.center("ab", -1));
|
|
||||||
assertEquals("ab", StringUtils.center("ab", 1));
|
|
||||||
assertEquals(" ", StringUtils.center("", 4));
|
|
||||||
assertEquals(" ab ", StringUtils.center("ab", 4));
|
|
||||||
assertEquals("abcd", StringUtils.center("abcd", 2));
|
|
||||||
assertEquals(" a ", StringUtils.center("a", 4));
|
|
||||||
assertEquals(" a ", StringUtils.center("a", 5));
|
|
||||||
|
|
||||||
assertEquals(null, StringUtils.center(null, -1, " "));
|
|
||||||
assertEquals(null, StringUtils.center(null, 4, " "));
|
|
||||||
assertEquals("ab", StringUtils.center("ab", 0, " "));
|
|
||||||
assertEquals("ab", StringUtils.center("ab", -1, " "));
|
|
||||||
assertEquals("ab", StringUtils.center("ab", 1, " "));
|
|
||||||
assertEquals(" ", StringUtils.center("", 4, " "));
|
|
||||||
assertEquals(" ab ", StringUtils.center("ab", 4, " "));
|
|
||||||
assertEquals("abcd", StringUtils.center("abcd", 2, " "));
|
|
||||||
assertEquals(" a ", StringUtils.center("a", 4, " "));
|
|
||||||
assertEquals("yayz", StringUtils.center("a", 4, "yz"));
|
|
||||||
assertEquals("yzyayzy", StringUtils.center("a", 7, "yz"));
|
|
||||||
try {
|
|
||||||
StringUtils.center(null, 4, null);
|
|
||||||
fail();
|
|
||||||
} catch (IllegalArgumentException ex) {
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
StringUtils.center("abc", 4, null);
|
|
||||||
fail();
|
|
||||||
} catch (IllegalArgumentException ex) {
|
|
||||||
}
|
|
||||||
try {
|
|
||||||
StringUtils.center("abc", 4, "");
|
|
||||||
fail();
|
|
||||||
} catch (IllegalArgumentException ex) {
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testDeprecatedChompFunctions() {
|
public void testDeprecatedChompFunctions() {
|
||||||
assertEquals("chompLast(String) failed",
|
assertEquals("chompLast(String) failed",
|
||||||
FOO, StringUtils.chompLast(FOO + "\n") );
|
FOO, StringUtils.chompLast(FOO + "\n") );
|
||||||
@ -613,147 +569,213 @@ public void testSliceFunctions() {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testPadFunctions() {
|
//-----------------------------------------------------------------------
|
||||||
assertEquals(null, StringUtils.rightPad (null, 8) );
|
public void testRightPad_StringInt() {
|
||||||
assertEquals("1234 ", StringUtils.rightPad ("1234", 8) );
|
assertEquals(null, StringUtils.rightPad(null, 5));
|
||||||
|
assertEquals(" ", StringUtils.rightPad("", 5));
|
||||||
|
assertEquals("abc ", StringUtils.rightPad("abc", 5));
|
||||||
|
assertEquals("abc", StringUtils.rightPad("abc", 2));
|
||||||
|
assertEquals("abc", StringUtils.rightPad("abc", -1));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testRightPad_StringIntChar() {
|
||||||
|
assertEquals(null, StringUtils.rightPad(null, 5, ' '));
|
||||||
|
assertEquals(" ", StringUtils.rightPad("", 5, ' '));
|
||||||
|
assertEquals("abc ", StringUtils.rightPad("abc", 5, ' '));
|
||||||
|
assertEquals("abc", StringUtils.rightPad("abc", 2, ' '));
|
||||||
|
assertEquals("abc", StringUtils.rightPad("abc", -1, ' '));
|
||||||
|
assertEquals("abcxx", StringUtils.rightPad("abc", 5, 'x'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testRightPad_StringIntString() {
|
||||||
|
assertEquals(null, StringUtils.rightPad(null, 5, "-+"));
|
||||||
|
assertEquals(" ", StringUtils.rightPad("", 5, " "));
|
||||||
|
assertEquals(null, StringUtils.rightPad(null, 8, null));
|
||||||
|
assertEquals("abc-+-+", StringUtils.rightPad("abc", 7, "-+"));
|
||||||
|
assertEquals("abc-+~", StringUtils.rightPad("abc", 6, "-+~"));
|
||||||
|
assertEquals("abc-+", StringUtils.rightPad("abc", 5, "-+~"));
|
||||||
|
assertEquals("abc", StringUtils.rightPad("abc", 2, " "));
|
||||||
|
assertEquals("abc", StringUtils.rightPad("abc", -1, " "));
|
||||||
|
try {
|
||||||
|
StringUtils.rightPad("abc56", 6, null);
|
||||||
|
fail();
|
||||||
|
} catch (IllegalArgumentException ex) {}
|
||||||
|
try {
|
||||||
|
StringUtils.rightPad("abc56", 6, "");
|
||||||
|
fail();
|
||||||
|
} catch (IllegalArgumentException ex) {}
|
||||||
|
}
|
||||||
|
|
||||||
assertEquals(null, StringUtils.rightPad (null, 8, "-+") );
|
//-----------------------------------------------------------------------
|
||||||
assertEquals("1234-+-+", StringUtils.rightPad ("1234", 8, "-+") );
|
public void testLeftPad_StringInt() {
|
||||||
assertEquals("123456-+~", StringUtils.rightPad ("123456", 9, "-+~") );
|
assertEquals(null, StringUtils.leftPad(null, 5));
|
||||||
assertEquals("123456-+", StringUtils.rightPad ("123456", 8, "-+~") );
|
assertEquals(" ", StringUtils.leftPad("", 5));
|
||||||
try {
|
assertEquals(" abc", StringUtils.leftPad("abc", 5));
|
||||||
StringUtils.rightPad(null, 6, null);
|
assertEquals("abc", StringUtils.leftPad("abc", 2));
|
||||||
fail();
|
}
|
||||||
} catch (IllegalArgumentException ex) {}
|
|
||||||
try {
|
|
||||||
StringUtils.rightPad("123456", 6, null);
|
|
||||||
fail();
|
|
||||||
} catch (IllegalArgumentException ex) {}
|
|
||||||
try {
|
|
||||||
StringUtils.rightPad("123456", 6, "");
|
|
||||||
fail();
|
|
||||||
} catch (IllegalArgumentException ex) {}
|
|
||||||
|
|
||||||
assertEquals(null, StringUtils.leftPad (null, 8) );
|
public void testLeftPad_StringIntChar() {
|
||||||
assertEquals(" 1234", StringUtils.leftPad("1234", 8) );
|
assertEquals(null, StringUtils.leftPad(null, 5, ' '));
|
||||||
|
assertEquals(" ", StringUtils.leftPad("", 5, ' '));
|
||||||
|
assertEquals(" abc", StringUtils.leftPad("abc", 5, ' '));
|
||||||
|
assertEquals("xxabc", StringUtils.leftPad("abc", 5, 'x'));
|
||||||
|
assertEquals("abc", StringUtils.leftPad("abc", 2, ' '));
|
||||||
|
}
|
||||||
|
|
||||||
assertEquals(null, StringUtils.leftPad (null, 8, "-+") );
|
public void testLeftPad_StringIntString() {
|
||||||
assertEquals("-+-+1234", StringUtils.leftPad("1234", 8, "-+") );
|
assertEquals(null, StringUtils.leftPad(null, 5, "-+"));
|
||||||
assertEquals("-+~123456", StringUtils.leftPad("123456", 9, "-+~") );
|
assertEquals(null, StringUtils.leftPad(null, 5, null));
|
||||||
assertEquals("-+123456", StringUtils.leftPad("123456", 8, "-+~") );
|
assertEquals(" ", StringUtils.leftPad("", 5, " "));
|
||||||
|
assertEquals("-+-+abc", StringUtils.leftPad("abc", 7, "-+"));
|
||||||
|
assertEquals("-+~abc", StringUtils.leftPad("abc", 6, "-+~"));
|
||||||
|
assertEquals("-+abc", StringUtils.leftPad("abc", 5, "-+~"));
|
||||||
|
assertEquals("abc", StringUtils.leftPad("abc", 2, " "));
|
||||||
|
assertEquals("abc", StringUtils.leftPad("abc", -1, " "));
|
||||||
try {
|
try {
|
||||||
StringUtils.leftPad(null, 6, null);
|
StringUtils.leftPad("abc56", 6, null);
|
||||||
fail();
|
fail();
|
||||||
} catch (IllegalArgumentException ex) {}
|
} catch (IllegalArgumentException ex) {}
|
||||||
try {
|
try {
|
||||||
StringUtils.leftPad("123456", 6, null);
|
StringUtils.leftPad("abc56", 6, "");
|
||||||
fail();
|
|
||||||
} catch (IllegalArgumentException ex) {}
|
|
||||||
try {
|
|
||||||
StringUtils.leftPad("123456", 6, "");
|
|
||||||
fail();
|
fail();
|
||||||
} catch (IllegalArgumentException ex) {}
|
} catch (IllegalArgumentException ex) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testReverse() {
|
//-----------------------------------------------------------------------
|
||||||
|
public void testCenter_StringInt() {
|
||||||
|
assertEquals(null, StringUtils.center(null, -1));
|
||||||
|
assertEquals(null, StringUtils.center(null, 4));
|
||||||
|
assertEquals(" ", StringUtils.center("", 4));
|
||||||
|
assertEquals("ab", StringUtils.center("ab", 0));
|
||||||
|
assertEquals("ab", StringUtils.center("ab", -1));
|
||||||
|
assertEquals("ab", StringUtils.center("ab", 1));
|
||||||
|
assertEquals(" ", StringUtils.center("", 4));
|
||||||
|
assertEquals(" ab ", StringUtils.center("ab", 4));
|
||||||
|
assertEquals("abcd", StringUtils.center("abcd", 2));
|
||||||
|
assertEquals(" a ", StringUtils.center("a", 4));
|
||||||
|
assertEquals(" a ", StringUtils.center("a", 5));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testCenter_StringIntChar() {
|
||||||
|
assertEquals(null, StringUtils.center(null, -1, ' '));
|
||||||
|
assertEquals(null, StringUtils.center(null, 4, ' '));
|
||||||
|
assertEquals(" ", StringUtils.center("", 4, ' '));
|
||||||
|
assertEquals("ab", StringUtils.center("ab", 0, ' '));
|
||||||
|
assertEquals("ab", StringUtils.center("ab", -1, ' '));
|
||||||
|
assertEquals("ab", StringUtils.center("ab", 1, ' '));
|
||||||
|
assertEquals(" ", StringUtils.center("", 4, ' '));
|
||||||
|
assertEquals(" ab ", StringUtils.center("ab", 4, ' '));
|
||||||
|
assertEquals("abcd", StringUtils.center("abcd", 2, ' '));
|
||||||
|
assertEquals(" a ", StringUtils.center("a", 4, ' '));
|
||||||
|
assertEquals(" a ", StringUtils.center("a", 5, ' '));
|
||||||
|
assertEquals("xxaxx", StringUtils.center("a", 5, 'x'));
|
||||||
|
}
|
||||||
|
|
||||||
|
public void testCenter_StringIntString() {
|
||||||
|
assertEquals(null, StringUtils.center(null, 4, null));
|
||||||
|
assertEquals(null, StringUtils.center(null, -1, " "));
|
||||||
|
assertEquals(null, StringUtils.center(null, 4, " "));
|
||||||
|
assertEquals(" ", StringUtils.center("", 4, " "));
|
||||||
|
assertEquals("ab", StringUtils.center("ab", 0, " "));
|
||||||
|
assertEquals("ab", StringUtils.center("ab", -1, " "));
|
||||||
|
assertEquals("ab", StringUtils.center("ab", 1, " "));
|
||||||
|
assertEquals(" ", StringUtils.center("", 4, " "));
|
||||||
|
assertEquals(" ab ", StringUtils.center("ab", 4, " "));
|
||||||
|
assertEquals("abcd", StringUtils.center("abcd", 2, " "));
|
||||||
|
assertEquals(" a ", StringUtils.center("a", 4, " "));
|
||||||
|
assertEquals("yayz", StringUtils.center("a", 4, "yz"));
|
||||||
|
assertEquals("yzyayzy", StringUtils.center("a", 7, "yz"));
|
||||||
|
try {
|
||||||
|
StringUtils.center("abc", 4, null);
|
||||||
|
fail();
|
||||||
|
} catch (IllegalArgumentException ex) {
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
StringUtils.center("abc", 4, "");
|
||||||
|
fail();
|
||||||
|
} catch (IllegalArgumentException ex) {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
|
public void testReverse_String() {
|
||||||
assertEquals(null, StringUtils.reverse(null) );
|
assertEquals(null, StringUtils.reverse(null) );
|
||||||
assertEquals("sdrawkcab", StringUtils.reverse("backwards") );
|
|
||||||
assertEquals("", StringUtils.reverse("") );
|
assertEquals("", StringUtils.reverse("") );
|
||||||
|
assertEquals("sdrawkcab", StringUtils.reverse("backwards") );
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testReverseDelimitedString() {
|
public void testReverseDelimitedString_StringChar() {
|
||||||
assertEquals(null, StringUtils.reverseDelimitedString(null, '.') );
|
assertEquals(null, StringUtils.reverseDelimitedString(null, '.') );
|
||||||
|
assertEquals("", StringUtils.reverseDelimitedString("", '.') );
|
||||||
assertEquals("c.b.a", StringUtils.reverseDelimitedString("a.b.c", '.') );
|
assertEquals("c.b.a", StringUtils.reverseDelimitedString("a.b.c", '.') );
|
||||||
assertEquals("a b c", StringUtils.reverseDelimitedString("a b c", '.') );
|
assertEquals("a b c", StringUtils.reverseDelimitedString("a b c", '.') );
|
||||||
assertEquals("", StringUtils.reverseDelimitedString("", '.') );
|
assertEquals("", StringUtils.reverseDelimitedString("", '.') );
|
||||||
|
|
||||||
assertEquals(null, StringUtils.reverseDelimitedString(null, null) );
|
|
||||||
assertEquals("a.b.c", StringUtils.reverseDelimitedString("a.b.c", null) );
|
|
||||||
assertEquals("c b a", StringUtils.reverseDelimitedString("a b c", null) );
|
|
||||||
assertEquals("org.apache.test",
|
|
||||||
StringUtils.reverseDelimitedString("test.apache.org", ".") );
|
|
||||||
assertEquals("reverseDelimitedString(empty-string,'.') failed",
|
|
||||||
"",
|
|
||||||
StringUtils.reverseDelimitedString("", ".") );
|
|
||||||
assertEquals("reverseDelimitedString(String,' ') failed",
|
|
||||||
"once upon a time",
|
|
||||||
StringUtils.reverseDelimitedString("time a upon once"," ") );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testDefaultFunctions() {
|
public void testReverseDelimitedString_StringString() {
|
||||||
|
assertEquals(null, StringUtils.reverseDelimitedString(null, null) );
|
||||||
|
assertEquals("", StringUtils.reverseDelimitedString("", null) );
|
||||||
|
assertEquals("", StringUtils.reverseDelimitedString("", ".") );
|
||||||
|
assertEquals("a.b.c", StringUtils.reverseDelimitedString("a.b.c", null) );
|
||||||
|
assertEquals("c b a", StringUtils.reverseDelimitedString("a b c", null) );
|
||||||
|
assertEquals("c.b.a", StringUtils.reverseDelimitedString("a.b.c", ".") );
|
||||||
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
|
public void testDefault_String() {
|
||||||
assertEquals("", StringUtils.defaultString(null) );
|
assertEquals("", StringUtils.defaultString(null) );
|
||||||
assertEquals("", StringUtils.defaultString("") );
|
assertEquals("", StringUtils.defaultString("") );
|
||||||
assertEquals(FOO, StringUtils.defaultString(FOO) );
|
assertEquals("abc", StringUtils.defaultString("abc") );
|
||||||
|
}
|
||||||
assertEquals(BAR, StringUtils.defaultString(null, BAR) );
|
|
||||||
assertEquals("", StringUtils.defaultString("", BAR) );
|
public void testDefault_StringString() {
|
||||||
assertEquals(FOO, StringUtils.defaultString(FOO, BAR) );
|
assertEquals("xyz", StringUtils.defaultString(null, "xyz") );
|
||||||
|
assertEquals("", StringUtils.defaultString("", "xyz") );
|
||||||
assertEquals("", StringUtils.defaultString((Object) "") );
|
assertEquals("abc", StringUtils.defaultString("abc", "xyz") );
|
||||||
assertEquals(FOO, StringUtils.defaultString((Object) FOO) );
|
}
|
||||||
|
|
||||||
|
public void testDefault_Object() {
|
||||||
assertEquals("", StringUtils.defaultString((Object) null) );
|
assertEquals("", StringUtils.defaultString((Object) null) );
|
||||||
|
assertEquals(Boolean.TRUE.toString(), StringUtils.defaultString(Boolean.TRUE) );
|
||||||
assertEquals("", StringUtils.defaultString((Object) "", BAR) );
|
}
|
||||||
assertEquals(FOO, StringUtils.defaultString((Object) FOO, BAR) );
|
|
||||||
|
public void testDefault_ObjectString() {
|
||||||
assertEquals(BAR, StringUtils.defaultString((Object) null, BAR) );
|
assertEquals(BAR, StringUtils.defaultString((Object) null, BAR) );
|
||||||
assertEquals(Boolean.TRUE.toString(), StringUtils.defaultString(Boolean.TRUE, BAR) );
|
assertEquals(Boolean.TRUE.toString(), StringUtils.defaultString(Boolean.TRUE, BAR) );
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testEscapeFunctions() {
|
//-----------------------------------------------------------------------
|
||||||
assertEquals("escape(empty-string) failed",
|
public void testEscapeFunctions_String() {
|
||||||
"", StringUtils.escape("") );
|
assertEquals("", StringUtils.escape("") );
|
||||||
assertEquals("escape(String) failed",
|
assertEquals("abc", StringUtils.escape("abc") );
|
||||||
FOO, StringUtils.escape(FOO) );
|
assertEquals("\\t", StringUtils.escape("\t") );
|
||||||
assertEquals("escape(String) failed",
|
assertEquals("\\\\", StringUtils.escape("\\") );
|
||||||
"\\t", StringUtils.escape("\t") );
|
assertEquals("\\\\\\b\\t\\r", StringUtils.escape("\\\b\t\r") );
|
||||||
assertEquals("escape(String) failed",
|
assertEquals("\\u1234", StringUtils.escape("\u1234") );
|
||||||
"\\\\", StringUtils.escape("\\") );
|
assertEquals("\\u0234", StringUtils.escape("\u0234") );
|
||||||
assertEquals("escape(String) failed",
|
assertEquals("\\u00FD", StringUtils.escape("\u00fd") );
|
||||||
"\\\\\\b\\t\\r", StringUtils.escape("\\\b\t\r") );
|
|
||||||
assertEquals("escape(String) failed",
|
|
||||||
"\\u1234", StringUtils.escape("\u1234") );
|
|
||||||
assertEquals("escape(String) failed",
|
|
||||||
"\\u0234", StringUtils.escape("\u0234") );
|
|
||||||
assertEquals("escape(String) failed",
|
|
||||||
"\\u00FD", StringUtils.escape("\u00fd") );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testGetLevenshteinDistance() {
|
//-----------------------------------------------------------------------
|
||||||
assertEquals("getLevenshteinDistance(empty-string, empty-string) failed",
|
public void testAbbreviate_StringInt() {
|
||||||
0, StringUtils.getLevenshteinDistance("", "") );
|
|
||||||
assertEquals("getLevenshteinDistance(empty-string, String) failed",
|
|
||||||
1, StringUtils.getLevenshteinDistance("", "a") );
|
|
||||||
assertEquals("getLevenshteinDistance(String, empty-string) failed",
|
|
||||||
7, StringUtils.getLevenshteinDistance("aaapppp", "") );
|
|
||||||
assertEquals("getLevenshteinDistance(String, String) failed",
|
|
||||||
1, StringUtils.getLevenshteinDistance("frog", "fog") );
|
|
||||||
assertEquals("getLevenshteinDistance(String, String) failed",
|
|
||||||
3, StringUtils.getLevenshteinDistance("fly", "ant") );
|
|
||||||
assertEquals("getLevenshteinDistance(String, String) failed",
|
|
||||||
7, StringUtils.getLevenshteinDistance("elephant", "hippo") );
|
|
||||||
assertEquals("getLevenshteinDistance(String, String) failed",
|
|
||||||
7, StringUtils.getLevenshteinDistance("hippo", "elephant") );
|
|
||||||
assertEquals("getLevenshteinDistance(String, String) failed",
|
|
||||||
1, StringUtils.getLevenshteinDistance("hello", "hallo") );
|
|
||||||
}
|
|
||||||
|
|
||||||
public void testAbbreviate() {
|
|
||||||
assertEquals(null, StringUtils.abbreviate(null, 10));
|
assertEquals(null, StringUtils.abbreviate(null, 10));
|
||||||
assertEquals("abbreviate(String,int) failed",
|
assertEquals("", StringUtils.abbreviate("", 10));
|
||||||
"short", StringUtils.abbreviate("short", 10));
|
assertEquals("short", StringUtils.abbreviate("short", 10));
|
||||||
assertEquals("abbreviate(String,int) failed",
|
assertEquals("Now is ...", StringUtils.abbreviate("Now is the time for all good men to come to the aid of their party.", 10));
|
||||||
"Now is ...", StringUtils.abbreviate("Now is the time for all good men to come to the aid of their party.", 10));
|
|
||||||
|
|
||||||
String raspberry = "raspberry peach";
|
String raspberry = "raspberry peach";
|
||||||
assertEquals("abbreviate(String,int) failed (one past limit)",
|
assertEquals("raspberry p...", StringUtils.abbreviate(raspberry, 14));
|
||||||
"raspberry p...", StringUtils.abbreviate(raspberry, 14));
|
assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", 15));
|
||||||
assertEquals("abbreviate(String,int) (at limit)",
|
assertEquals("raspberry peach", StringUtils.abbreviate("raspberry peach", 16));
|
||||||
"raspberry peach", StringUtils.abbreviate("raspberry peach", 15));
|
}
|
||||||
assertEquals("abbreviate(String,int) (one below limit)",
|
|
||||||
"raspberry peach", StringUtils.abbreviate("raspberry peach", 16));
|
public void testAbbreviate_StringIntInt() {
|
||||||
|
assertEquals(null, StringUtils.abbreviate(null, 10, 12));
|
||||||
|
assertEquals("", StringUtils.abbreviate("", 0, 10));
|
||||||
|
assertEquals("", StringUtils.abbreviate("", 2, 10));
|
||||||
|
|
||||||
assertEquals("abbreviate(String,int,int) failed",
|
String raspberry = "raspberry peach";
|
||||||
"raspberry peach", StringUtils.abbreviate(raspberry, 11, 15));
|
assertEquals("raspberry peach", StringUtils.abbreviate(raspberry, 11, 15));
|
||||||
|
|
||||||
assertEquals(null, StringUtils.abbreviate(null, 7, 14));
|
assertEquals(null, StringUtils.abbreviate(null, 7, 14));
|
||||||
assertAbbreviateWithOffset("abcdefg...", -1, 10);
|
assertAbbreviateWithOffset("abcdefg...", -1, 10);
|
||||||
@ -776,11 +798,9 @@ public void testAbbreviate() {
|
|||||||
assertAbbreviateWithOffset("...ijklmno", 15, 10);
|
assertAbbreviateWithOffset("...ijklmno", 15, 10);
|
||||||
assertAbbreviateWithOffset("...ijklmno", 16, 10);
|
assertAbbreviateWithOffset("...ijklmno", 16, 10);
|
||||||
assertAbbreviateWithOffset("...ijklmno", Integer.MAX_VALUE, 10);
|
assertAbbreviateWithOffset("...ijklmno", Integer.MAX_VALUE, 10);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void assertAbbreviateWithOffset(String expected, int offset, int maxWidth)
|
private void assertAbbreviateWithOffset(String expected, int offset, int maxWidth) {
|
||||||
{
|
|
||||||
String abcdefghijklmno = "abcdefghijklmno";
|
String abcdefghijklmno = "abcdefghijklmno";
|
||||||
String message = "abbreviate(String,int,int) failed";
|
String message = "abbreviate(String,int,int) failed";
|
||||||
String actual = StringUtils.abbreviate(abcdefghijklmno, offset, maxWidth);
|
String actual = StringUtils.abbreviate(abcdefghijklmno, offset, maxWidth);
|
||||||
@ -793,23 +813,42 @@ private void assertAbbreviateWithOffset(String expected, int offset, int maxWidt
|
|||||||
assertEquals(message, expected, actual);
|
assertEquals(message, expected, actual);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testDifference() {
|
//-----------------------------------------------------------------------
|
||||||
|
public void testDifference_StringString() {
|
||||||
assertEquals(null, StringUtils.difference(null, null));
|
assertEquals(null, StringUtils.difference(null, null));
|
||||||
|
assertEquals("", StringUtils.difference("", ""));
|
||||||
|
assertEquals("abc", StringUtils.difference("", "abc"));
|
||||||
|
assertEquals("", StringUtils.difference("abc", ""));
|
||||||
assertEquals("i am a robot", StringUtils.difference(null, "i am a robot"));
|
assertEquals("i am a robot", StringUtils.difference(null, "i am a robot"));
|
||||||
assertEquals("i am a machine", StringUtils.difference("i am a machine", null));
|
assertEquals("i am a machine", StringUtils.difference("i am a machine", null));
|
||||||
assertEquals("robot", StringUtils.difference("i am a machine", "i am a robot"));
|
assertEquals("robot", StringUtils.difference("i am a machine", "i am a robot"));
|
||||||
assertEquals("", StringUtils.difference("foo", "foo"));
|
assertEquals("", StringUtils.difference("abc", "abc"));
|
||||||
assertEquals("you are a robot", StringUtils.difference("i am a robot", "you are a robot"));
|
assertEquals("you are a robot", StringUtils.difference("i am a robot", "you are a robot"));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void testDifferenceAt() {
|
public void testDifferenceAt_StringString() {
|
||||||
assertEquals(-1, StringUtils.differenceAt(null, null));
|
assertEquals(-1, StringUtils.differenceAt(null, null));
|
||||||
assertEquals(0, StringUtils.differenceAt(null, "i am a robot"));
|
assertEquals(0, StringUtils.differenceAt(null, "i am a robot"));
|
||||||
|
assertEquals(-1, StringUtils.differenceAt("", ""));
|
||||||
|
assertEquals(0, StringUtils.differenceAt("", "abc"));
|
||||||
|
assertEquals(0, StringUtils.differenceAt("abc", ""));
|
||||||
assertEquals(0, StringUtils.differenceAt("i am a machine", null));
|
assertEquals(0, StringUtils.differenceAt("i am a machine", null));
|
||||||
assertEquals(7, StringUtils.differenceAt("i am a machine", "i am a robot"));
|
assertEquals(7, StringUtils.differenceAt("i am a machine", "i am a robot"));
|
||||||
assertEquals(-1, StringUtils.differenceAt("foo", "foo"));
|
assertEquals(-1, StringUtils.differenceAt("foo", "foo"));
|
||||||
assertEquals(0, StringUtils.differenceAt("i am a robot", "you are a robot"));
|
assertEquals(0, StringUtils.differenceAt("i am a robot", "you are a robot"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//-----------------------------------------------------------------------
|
||||||
|
public void testGetLevenshteinDistance_StringString() {
|
||||||
|
assertEquals(0, StringUtils.getLevenshteinDistance("", "") );
|
||||||
|
assertEquals(1, StringUtils.getLevenshteinDistance("", "a") );
|
||||||
|
assertEquals(7, StringUtils.getLevenshteinDistance("aaapppp", "") );
|
||||||
|
assertEquals(1, StringUtils.getLevenshteinDistance("frog", "fog") );
|
||||||
|
assertEquals(3, StringUtils.getLevenshteinDistance("fly", "ant") );
|
||||||
|
assertEquals(7, StringUtils.getLevenshteinDistance("elephant", "hippo") );
|
||||||
|
assertEquals(7, StringUtils.getLevenshteinDistance("hippo", "elephant") );
|
||||||
|
assertEquals(1, StringUtils.getLevenshteinDistance("hello", "hallo") );
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user