Skip to content

Commit

Permalink
raise IllegalArgumentException if upper < -1 or upper < lower and res…
Browse files Browse the repository at this point in the history
…pective testcases
  • Loading branch information
ameyjadiye committed May 4, 2017
1 parent e3ea9dd commit 44847c5
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
15 changes: 10 additions & 5 deletions src/main/java/org/apache/commons/text/WordUtils.java
Expand Up @@ -765,6 +765,16 @@ public static String abbreviate(String str, int lower, int upper, String appendT
return str;
}

// throw IllegalArgumentException if upper limit is less than -1 which voids contact.
if (upper < -1) {
throw new IllegalArgumentException("upper value cannot be less than -1");
}

// throw IllegalArgumentException if upper value is less than lower value.
if (upper < lower && upper != -1) {
throw new IllegalArgumentException("upper value is less than lower value");
}

// if the lower value is greater than the length of the string,
// set to the length of the string
if (lower > str.length()) {
Expand All @@ -777,11 +787,6 @@ public static String abbreviate(String str, int lower, int upper, String appendT
upper = str.length();
}

// if upper is less than lower, raise it to lower
if (upper < lower) {
upper = lower;
}

final StringBuilder result = new StringBuilder();
final int index = StringUtils.indexOf(str, " ", lower);
if (index == -1) {
Expand Down
16 changes: 11 additions & 5 deletions src/test/java/org/apache/commons/text/WordUtilsTest.java
Expand Up @@ -423,7 +423,6 @@ public void testSwapCase_String() {
public void testAbbreviateForNullAndEmptyString() {
assertEquals(null, (WordUtils.abbreviate(null, 1,-1,"")));
assertEquals(StringUtils.EMPTY, WordUtils.abbreviate("", 1,-1,""));

assertEquals("", WordUtils.abbreviate("0123456790", 0,0,""));
assertEquals("", WordUtils.abbreviate(" 0123456790", 0,-1,""));
}
Expand All @@ -432,19 +431,15 @@ public void testAbbreviateForNullAndEmptyString() {
@Test
public void testAbbreviateForUpperLimit() {
assertEquals("01234", WordUtils.abbreviate("0123456789", 0,5,""));
assertEquals("01234", WordUtils.abbreviate("0123456789", 5, 2,""));
assertEquals("012", WordUtils.abbreviate("012 3456789", 2, 5,""));
assertEquals("012 3", WordUtils.abbreviate("012 3456789", 5, 2,""));
assertEquals("0123456789", WordUtils.abbreviate("0123456789", 0,-1,""));
}

// -----------------------------------------------------------------------
@Test
public void testAbbreviateForUpperLimitAndAppendedString() {
assertEquals("01234-", WordUtils.abbreviate("0123456789", 0,5,"-"));
assertEquals("01234-", WordUtils.abbreviate("0123456789", 5, 2,"-"));
assertEquals("012", WordUtils.abbreviate("012 3456789", 2, 5, null));
assertEquals("012 3", WordUtils.abbreviate("012 3456789", 5, 2,""));
assertEquals("0123456789", WordUtils.abbreviate("0123456789", 0,-1,""));
}

Expand All @@ -467,6 +462,17 @@ public void testAbbreviateForLowerValueAndAppendedString() {
assertEquals("01 23 45 6", WordUtils.abbreviate("01 23 45 67 89", 9, 10, ""));
}

@Test(expected = IllegalArgumentException.class)
public void testAbbreviateForLowerThanMinusOneValues() {
assertEquals("01 23 45 67", WordUtils.abbreviate("01 23 45 67 89", 9, -10, null));
}

@Test(expected = IllegalArgumentException.class)
public void testAbbreviateUpperLessThanLowerValues() {
assertEquals("01234", WordUtils.abbreviate("0123456789", 5, 2,""));
}


@Test
public void testLANG1292() throws Exception {
// Prior to fix, this was throwing StringIndexOutOfBoundsException
Expand Down

0 comments on commit 44847c5

Please sign in to comment.