/** * Truncate the supplied string to be no more than the specified length. This method returns an empty string if the supplied * object is null. * * @param obj the object from which the string is to be obtained using {@link Object#toString()}. * @param maxLength the maximum length of the string being returned * @return the supplied string if no longer than the maximum length, or the supplied string truncated to be no longer than the * maximum length (including the suffix) * @throws IllegalArgumentException if the maximum length is negative */ public static String truncate( Object obj, int maxLength ) { return truncate(obj, maxLength, null); }
/** * Truncate the supplied string to be no more than the specified length. This method returns an empty string if the supplied * object is null. * * @param obj the object from which the string is to be obtained using {@link Object#toString()}. * @param maxLength the maximum length of the string being returned * @return the supplied string if no longer than the maximum length, or the supplied string truncated to be no longer than the * maximum length (including the suffix) * @throws IllegalArgumentException if the maximum length is negative */ public static String truncate( Object obj, int maxLength ) { return truncate(obj, maxLength, null); }
/** * Truncate the supplied string to be no more than the specified length. This method returns an empty string if the supplied * object is null. * * @param obj the object from which the string is to be obtained using {@link Object#toString()}. * @param maxLength the maximum length of the string being returned * @return the supplied string if no longer than the maximum length, or the supplied string truncated to be no longer than the * maximum length (including the suffix) * @throws IllegalArgumentException if the maximum length is negative */ public static String truncate( Object obj, int maxLength ) { return truncate(obj, maxLength, null); }
@Test( expected = IllegalArgumentException.class ) public void truncateShouldNotAllowNegativeLength() { StringUtil.truncate("some string", -1); }
@Test public void truncateShouldNotTruncateStringWithLengthEqualToMaximumLength() { String str = "This is the string with some text"; assertThat(StringUtil.truncate(str, str.length()), is(str)); assertThat(StringUtil.truncate(str, str.length(), null), is(str)); assertThat(StringUtil.truncate(str, str.length(), "really long suffix"), is(str)); }
@Test public void truncateShouldProperlyTruncateStringWithLengthGreaterThanMaximumLength() { String str = "This is the string"; assertThat(StringUtil.truncate(str, str.length() - 1), is("This is the st...")); assertThat(StringUtil.truncate(str, str.length() - 1, null), is("This is the st...")); assertThat(StringUtil.truncate(str, str.length() - 1, "X"), is("This is the striX")); }
@Test public void truncateShouldReturnEmptyStringForMaximumLengthOfZero() { String str = "This is the string with some text"; assertThat(StringUtil.truncate(str, 0), is("")); assertThat(StringUtil.truncate("", 0), is("")); assertThat(StringUtil.truncate(str, 0, "123"), is("")); assertThat(StringUtil.truncate("", 0, "123"), is("")); }
@Test public void truncateShouldNotTruncateStringShorterThanMaximumLength() { String str = "This is the string with some text"; assertThat(StringUtil.truncate(str, str.length() + 2), is(str)); assertThat(StringUtil.truncate(str, str.length() + 2, null), is(str)); assertThat(StringUtil.truncate(str, str.length() + 2, "really long suffix"), is(str)); }
@Test public void truncateShouldProperlyTruncateStringWithLengthGreaterThanMaximumLengthAndMaximumLengthLongerThanPrefixLength() { String str = "This is the string"; assertThat(StringUtil.truncate(str, 2), is("..")); assertThat(StringUtil.truncate(str, 2, null), is("..")); assertThat(StringUtil.truncate(str, 1, "XX"), is("X")); }