Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/org/apache/commons/text/StrBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ public StrBuilder append(final char[] chars, final int startIndex, final int len
return appendNull();
}
if (startIndex < 0 || startIndex > chars.length) {
throw new StringIndexOutOfBoundsException("Invalid startIndex: " + length);
throw new StringIndexOutOfBoundsException("Invalid startIndex: " + startIndex);
}
if (length < 0 || startIndex + length > chars.length) {
throw new StringIndexOutOfBoundsException("Invalid length: " + length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,7 @@ public TextStringBuilder append(final char[] chars, final int startIndex, final
return appendNull();
}
if (startIndex < 0 || startIndex > chars.length) {
throw new StringIndexOutOfBoundsException("Invalid startIndex: " + length);
throw new StringIndexOutOfBoundsException("Invalid startIndex: " + startIndex);
}
if (length < 0 || startIndex + length > chars.length) {
throw new StringIndexOutOfBoundsException("Invalid length: " + length);
Expand Down
17 changes: 17 additions & 0 deletions src/test/java/org/apache/commons/text/StrBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2017,4 +2017,21 @@ void testTrim() {
assertEquals("a b c", sb.trim().toString());
}

@Test
void testErrorMessageShowsCorrectVariable() {
final StrBuilder sb = new StrBuilder("Hello");
final char[] chars = {'a', 'b', 'c'};

StringIndexOutOfBoundsException ex = assertThrows(
StringIndexOutOfBoundsException.class,
() -> sb.append(chars, 1, 4)
);
assertTrue(ex.getMessage().contains("length: 4"));

ex = assertThrows(
StringIndexOutOfBoundsException.class,
() -> sb.append(chars, 7, 3)
);
assertTrue(ex.getMessage().contains("startIndex: 7"));
}
}
17 changes: 17 additions & 0 deletions src/test/java/org/apache/commons/text/TextStringBuilderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2393,4 +2393,21 @@ void testWrap_CharArray_Int_Exceptions() {
assertThrows(IllegalArgumentException.class, () -> TextStringBuilder.wrap(ArrayUtils.EMPTY_CHAR_ARRAY, 1));
}

@Test
void testErrorMessageShowsCorrectVariable() {
final TextStringBuilder sb = new TextStringBuilder("Hello");
final char[] chars = {'a', 'b', 'c'};

StringIndexOutOfBoundsException ex = assertThrows(
StringIndexOutOfBoundsException.class,
() -> sb.append(chars, 1, 4)
);
assertTrue(ex.getMessage().contains("length: 4"));

ex = assertThrows(
StringIndexOutOfBoundsException.class,
() -> sb.append(chars, 7, 3)
);
assertTrue(ex.getMessage().contains("startIndex: 7"));
}
}
Loading