Introduce StringUtils.truncate()
StringUtils.truncate() serves as central, consistent way for truncating strings used in log messages and exception failure messages, for immediate use in LogFormatUtils and ObjectUtils. See gh-30286 Closes gh-30290
This commit is contained in:
@@ -22,6 +22,8 @@ import java.util.Locale;
|
||||
import java.util.Properties;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.CsvSource;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||
@@ -757,4 +759,26 @@ class StringUtilsTests {
|
||||
assertThat(StringUtils.collectionToCommaDelimitedString(Collections.singletonList(null))).isEqualTo("null");
|
||||
}
|
||||
|
||||
@Test
|
||||
void truncatePreconditions() {
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> StringUtils.truncate("foo", 0))
|
||||
.withMessage("Truncation threshold must be a positive number: 0");
|
||||
assertThatIllegalArgumentException()
|
||||
.isThrownBy(() -> StringUtils.truncate("foo", -99))
|
||||
.withMessage("Truncation threshold must be a positive number: -99");
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@CsvSource(delimiterString = "-->", textBlock = """
|
||||
aardvark --> aardvark
|
||||
aardvark12 --> aardvark12
|
||||
aardvark123 --> aardvark12 (truncated)...
|
||||
aardvark, bird, cat --> aardvark, (truncated)...
|
||||
"""
|
||||
)
|
||||
void truncate(String text, String truncated) {
|
||||
assertThat(StringUtils.truncate(text, 10)).isEqualTo(truncated);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user