Skip to content

Commit 8b605a3

Browse files
committed
fix text-239
1 parent 71f75f2 commit 8b605a3

2 files changed

Lines changed: 18 additions & 1 deletion

File tree

src/main/java/org/apache/commons/text/TextStringBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -483,7 +483,7 @@ public TextStringBuilder append(final char[] chars, final int startIndex, final
483483
return appendNull();
484484
}
485485
if (startIndex < 0 || startIndex > chars.length) {
486-
throw new StringIndexOutOfBoundsException("Invalid startIndex: " + length);
486+
throw new StringIndexOutOfBoundsException("Invalid startIndex: " + startIndex);
487487
}
488488
if (length < 0 || startIndex + length > chars.length) {
489489
throw new StringIndexOutOfBoundsException("Invalid length: " + length);

src/test/java/org/apache/commons/text/TextStringBuilderTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2393,4 +2393,21 @@ void testWrap_CharArray_Int_Exceptions() {
23932393
assertThrows(IllegalArgumentException.class, () -> TextStringBuilder.wrap(ArrayUtils.EMPTY_CHAR_ARRAY, 1));
23942394
}
23952395

2396+
@Test
2397+
void testErrorMessageShowsCorrectVariable() {
2398+
TextStringBuilder sb = new TextStringBuilder("Hello");
2399+
char[] chars = {'a', 'b', 'c'};
2400+
2401+
StringIndexOutOfBoundsException ex = assertThrows(
2402+
StringIndexOutOfBoundsException.class,
2403+
() -> sb.append(chars, 1, 4)
2404+
);
2405+
assertTrue(ex.getMessage().contains("length: 4"));
2406+
2407+
ex = assertThrows(
2408+
StringIndexOutOfBoundsException.class,
2409+
() -> sb.append(chars, 7, 3)
2410+
);
2411+
assertTrue(ex.getMessage().contains("startIndex: 7"));
2412+
}
23962413
}

0 commit comments

Comments
 (0)