scolebourne 2003/08/01 16:11:55
Modified: lang/src/test/org/apache/commons/lang
StringUtilsSubstringTest.java
lang/src/java/org/apache/commons/lang StringUtils.java
Log:
Relax exceptions in left(), right() and mid()
Revision Changes Path
1.12 +19 -19
jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsSubstringTest.java
Index: StringUtilsSubstringTest.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/lang/src/test/org/apache/commons/lang/StringUtilsSubstringTest.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- StringUtilsSubstringTest.java 1 Aug 2003 22:05:43 -0000 1.11
+++ StringUtilsSubstringTest.java 1 Aug 2003 23:11:54 -0000 1.12
@@ -139,30 +139,30 @@
assertSame(null, StringUtils.left(null, -1));
assertSame(null, StringUtils.left(null, 0));
assertSame(null, StringUtils.left(null, 2));
- assertSame("", StringUtils.left("", 0));
- assertSame("", StringUtils.left("", 2));
+
+ assertEquals("", StringUtils.left("", -1));
+ assertEquals("", StringUtils.left("", 0));
+ assertEquals("", StringUtils.left("", 2));
+
+ assertEquals("", StringUtils.left(FOOBAR, -1));
assertEquals("", StringUtils.left(FOOBAR, 0));
assertEquals(FOO, StringUtils.left(FOOBAR, 3));
assertSame(FOOBAR, StringUtils.left(FOOBAR, 80));
- try {
- StringUtils.left(FOOBAR, -1);
- fail();
- } catch (IllegalArgumentException ex) {}
}
public void testRight_String() {
assertSame(null, StringUtils.right(null, -1));
assertSame(null, StringUtils.right(null, 0));
assertSame(null, StringUtils.right(null, 2));
- assertSame("", StringUtils.right("", 0));
- assertSame("", StringUtils.right("", 2));
+
+ assertEquals("", StringUtils.right("", -1));
+ assertEquals("", StringUtils.right("", 0));
+ assertEquals("", StringUtils.right("", 2));
+
+ assertEquals("", StringUtils.right(FOOBAR, -1));
assertEquals("", StringUtils.right(FOOBAR, 0));
assertEquals(BAR, StringUtils.right(FOOBAR, 3));
assertSame(FOOBAR, StringUtils.right(FOOBAR, 80));
- try {
- StringUtils.right(FOOBAR, -1);
- fail();
- } catch (IllegalArgumentException ex) {}
}
public void testMid_String() {
@@ -170,8 +170,12 @@
assertSame(null, StringUtils.mid(null, 0, -1));
assertSame(null, StringUtils.mid(null, 3, 0));
assertSame(null, StringUtils.mid(null, 3, 2));
- assertSame("", StringUtils.mid("", 0, 0));
- assertSame("", StringUtils.mid("", 0, 2));
+
+ assertEquals("", StringUtils.mid("", 0, -1));
+ assertEquals("", StringUtils.mid("", 0, 0));
+ assertEquals("", StringUtils.mid("", 0, 2));
+
+ assertEquals("", StringUtils.mid(FOOBAR, 3, -1));
assertEquals("", StringUtils.mid(FOOBAR, 3, 0));
assertEquals("b", StringUtils.mid(FOOBAR, 3, 1));
assertEquals(FOO, StringUtils.mid(FOOBAR, 0, 3));
@@ -180,10 +184,6 @@
assertEquals(BAR, StringUtils.mid(FOOBAR, 3, 80));
assertEquals("", StringUtils.mid(FOOBAR, 9, 3));
assertEquals(FOO, StringUtils.mid(FOOBAR, -1, 3));
- try {
- StringUtils.mid(FOOBAR, 0, -1);
- fail();
- } catch (IllegalArgumentException ex) {}
}
//-----------------------------------------------------------------------
1.88 +7 -13
jakarta-commons/lang/src/java/org/apache/commons/lang/StringUtils.java
Index: StringUtils.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/lang/src/java/org/apache/commons/lang/StringUtils.java,v
retrieving revision 1.87
retrieving revision 1.88
diff -u -r1.87 -r1.88
--- StringUtils.java 1 Aug 2003 23:01:52 -0000 1.87
+++ StringUtils.java 1 Aug 2003 23:11:55 -0000 1.88
@@ -1431,7 +1431,7 @@
*
* <pre>
* StringUtils.left(null, *) = null
- * StringUtils.left(*, -ve) = IllegalArgumentException
+ * StringUtils.left(*, -ve) = ""
* StringUtils.left("", *) = ""
* StringUtils.left("abc", 0) = ""
* StringUtils.left("abc", 2) = "ab"
@@ -1441,14 +1441,13 @@
* @param str the String to get the leftmost characters from, may be null
* @param len the length of the required String, must be zero or positive
* @return the leftmost characters, <code>null</code> if null String input
- * @throws IllegalArgumentException if len is less than zero
*/
public static String left(String str, int len) {
if (str == null) {
return null;
}
if (len < 0) {
- throw new IllegalArgumentException("Requested String length " + len + "
is less than zero");
+ return "";
}
if (str.length() <= len) {
return str;
@@ -1466,7 +1465,7 @@
*
* <pre>
* StringUtils.right(null, *) = null
- * StringUtils.right(*, -ve) = IllegalArgumentException
+ * StringUtils.right(*, -ve) = ""
* StringUtils.right("", *) = ""
* StringUtils.right("abc", 0) = ""
* StringUtils.right("abc", 2) = "bc"
@@ -1476,14 +1475,13 @@
* @param str the String to get the rightmost characters from, may be null
* @param len the length of the required String, must be zero or positive
* @return the rightmost characters, <code>null</code> if null String input
- * @throws IllegalArgumentException if len is less than zero
*/
public static String right(String str, int len) {
if (str == null) {
return null;
}
if (len < 0) {
- throw new IllegalArgumentException("Requested String length " + len + "
is less than zero");
+ return "";
}
if (str.length() <= len) {
return str;
@@ -1502,7 +1500,7 @@
*
* <pre>
* StringUtils.mid(null, *, *) = null
- * StringUtils.mid(*, *, -ve) = IllegalArgumentException
+ * StringUtils.mid(*, *, -ve) = ""
* StringUtils.mid("", 0, *) = ""
* StringUtils.mid("abc", 0, 2) = "ab"
* StringUtils.mid("abc", 0, 4) = "abc"
@@ -1515,20 +1513,16 @@
* @param pos the position to start from, negative treated as zero
* @param len the length of the required String, must be zero or positive
* @return the middle characters, <code>null</code> if null String input
- * @throws IllegalArgumentException if len is less than zero
*/
public static String mid(String str, int pos, int len) {
if (str == null) {
return null;
}
- if (pos > str.length()) {
+ if (len < 0 || pos > str.length()) {
return "";
}
if (pos < 0) {
pos = 0;
- }
- if (len < 0) {
- throw new IllegalArgumentException("Requested String length " + len + "
is less than zero");
}
if (str.length() <= (pos + len)) {
return str.substring(pos);
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]