Skip to content

Commit 2f07d02

Browse files
authored
'abbreviate' contract violations (#1490)
* add unit tests showing 'abbreviate' contract violations * add unit tests showing mishandling of null abbrevMarker * fix handling of null abbrevMarker - treat null marker as empty string - ensure offset and maxWidth are applied as usual (simple 'substring' won't cut it)
1 parent 52b1a3a commit 2f07d02

2 files changed

Lines changed: 19 additions & 6 deletions

File tree

src/main/java/org/apache/commons/lang3/StringUtils.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -352,13 +352,13 @@ public static String abbreviate(final String str, final String abbrevMarker, fin
352352
* @throws IllegalArgumentException if the width is too small.
353353
* @since 3.6
354354
*/
355-
public static String abbreviate(final String str, final String abbrevMarker, int offset, final int maxWidth) {
356-
if (isNotEmpty(str) && EMPTY.equals(abbrevMarker) && maxWidth > 0) {
357-
return substring(str, 0, maxWidth);
358-
}
359-
if (isAnyEmpty(str, abbrevMarker)) {
355+
public static String abbreviate(final String str, String abbrevMarker, int offset, final int maxWidth) {
356+
if (isEmpty(str)) {
360357
return str;
361358
}
359+
if (abbrevMarker == null) {
360+
abbrevMarker = EMPTY;
361+
}
362362
final int abbrevMarkerLength = abbrevMarker.length();
363363
final int minAbbrevWidth = abbrevMarkerLength + 1;
364364
final int minAbbrevWidthOffset = abbrevMarkerLength + abbrevMarkerLength + 1;

src/test/java/org/apache/commons/lang3/StringUtilsAbbreviateTest.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,16 @@ void testAbbreviate_StringIntInt() {
101101
assertAbbreviateWithOffset("...ijklmno", 15, 10);
102102
assertAbbreviateWithOffset("...ijklmno", 16, 10);
103103
assertAbbreviateWithOffset("...ijklmno", Integer.MAX_VALUE, 10);
104+
105+
// abbreviating a shorter string allows maxWidth < 7
106+
assertEquals("...efg", StringUtils.abbreviate("abcdefg", 5, 6));
104107
}
105108

106109
@Test
107110
void testAbbreviate_StringStringInt() {
108111
assertNull(StringUtils.abbreviate(null, null, 10));
109112
assertNull(StringUtils.abbreviate(null, "...", 10));
110-
assertEquals("paranaguacu", StringUtils.abbreviate("paranaguacu", null, 10));
113+
assertEquals("paranaguac", StringUtils.abbreviate("paranaguacu", null, 10));
111114
assertEquals("", StringUtils.abbreviate("", "...", 2));
112115
assertEquals("wai**", StringUtils.abbreviate("waiheke", "**", 5));
113116
assertEquals("And af,,,,", StringUtils.abbreviate("And after a long time, he finally met his son.", ",,,,", 10));
@@ -130,6 +133,12 @@ void testAbbreviate_StringStringIntInt() {
130133
assertNull(StringUtils.abbreviate(null, "...", 10, 12));
131134
assertEquals("", StringUtils.abbreviate("", null, 0, 10));
132135
assertEquals("", StringUtils.abbreviate("", "...", 2, 10));
136+
assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", null, 2, 10));
137+
assertEquals("abcdefg", StringUtils.abbreviate("abcdefg", "", 2, 10));
138+
assertEquals("abc", StringUtils.abbreviate("abcdefg", null, 0, 3));
139+
assertEquals("cde", StringUtils.abbreviate("abcdefg", null, 2, 3));
140+
assertEquals("abc", StringUtils.abbreviate("abcdefg", "", 0, 3));
141+
assertEquals("cde", StringUtils.abbreviate("abcdefg", "", 2, 3));
133142
assertIllegalArgumentException(() -> StringUtils.abbreviate("abcdefghij", "::", 0, 2),
134143
"StringUtils.abbreviate expecting IllegalArgumentException");
135144
assertIllegalArgumentException(() -> StringUtils.abbreviate("abcdefghij", "!!!", 5, 6),
@@ -157,6 +166,10 @@ void testAbbreviate_StringStringIntInt() {
157166
assertAbbreviateWithAbbrevMarkerAndOffset("999ijklmno", "999", 15, 10);
158167
assertAbbreviateWithAbbrevMarkerAndOffset("_ghijklmno", "_", 16, 10);
159168
assertAbbreviateWithAbbrevMarkerAndOffset("+ghijklmno", "+", Integer.MAX_VALUE, 10);
169+
170+
// abbreviating a shorter string allows maxWidth < abbrevMarker.length * 2 + 1
171+
assertEquals("..de", StringUtils.abbreviate("abcde", "..", 4, 4));
172+
assertEquals("....fg", StringUtils.abbreviate("abcdefg", "....", 5, 6));
160173
}
161174

162175
// Fixed LANG-1463

0 commit comments

Comments
 (0)