raise IllegalArgumentException if upper < -1 or upper < lower and respective testcases
Project: http://git-wip-us.apache.org/repos/asf/commons-text/repo Commit: http://git-wip-us.apache.org/repos/asf/commons-text/commit/44847c52 Tree: http://git-wip-us.apache.org/repos/asf/commons-text/tree/44847c52 Diff: http://git-wip-us.apache.org/repos/asf/commons-text/diff/44847c52 Branch: refs/heads/master Commit: 44847c52959d830c3fc941f9e3765d5e0facf786 Parents: e3ea9dd Author: Amey Jadiye <ameyjad...@gmail.com> Authored: Thu May 4 21:53:48 2017 +0530 Committer: Amey Jadiye <ameyjad...@gmail.com> Committed: Thu May 4 21:53:48 2017 +0530 ---------------------------------------------------------------------- .../java/org/apache/commons/text/WordUtils.java | 15 ++++++++++----- .../java/org/apache/commons/text/WordUtilsTest.java | 16 +++++++++++----- 2 files changed, 21 insertions(+), 10 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/commons-text/blob/44847c52/src/main/java/org/apache/commons/text/WordUtils.java ---------------------------------------------------------------------- diff --git a/src/main/java/org/apache/commons/text/WordUtils.java b/src/main/java/org/apache/commons/text/WordUtils.java index 34c0728..1023354 100644 --- a/src/main/java/org/apache/commons/text/WordUtils.java +++ b/src/main/java/org/apache/commons/text/WordUtils.java @@ -765,6 +765,16 @@ public class WordUtils { 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()) { @@ -777,11 +787,6 @@ public class WordUtils { 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) { http://git-wip-us.apache.org/repos/asf/commons-text/blob/44847c52/src/test/java/org/apache/commons/text/WordUtilsTest.java ---------------------------------------------------------------------- diff --git a/src/test/java/org/apache/commons/text/WordUtilsTest.java b/src/test/java/org/apache/commons/text/WordUtilsTest.java index 7d3fd9e..dbb7e80 100644 --- a/src/test/java/org/apache/commons/text/WordUtilsTest.java +++ b/src/test/java/org/apache/commons/text/WordUtilsTest.java @@ -423,7 +423,6 @@ public class WordUtilsTest { 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,"")); } @@ -432,9 +431,7 @@ public class WordUtilsTest { @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,"")); } @@ -442,9 +439,7 @@ public class WordUtilsTest { @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,"")); } @@ -467,6 +462,17 @@ public class WordUtilsTest { 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