### Question:
StringUtil { public static String truncate( Object obj, int maxLength ) { return truncate(obj, maxLength, null); } private  StringUtil(); static String combineLines( String[] lines ); static String combineLines( String[] lines,
                                       char separator ); static List<String> splitLines( final String content ); static String createString( String pattern,
                                       Object... parameters ); static String createString( final char charToRepeat,
                                       int numberOfRepeats ); static String setLength( String original,
                                    int length,
                                    char padChar ); static String justifyRight( String str,
                                       final int width,
                                       char padWithChar ); static String justifyLeft( String str,
                                      final int width,
                                      char padWithChar ); static String justifyCenter( String str,
                                        final int width,
                                        char padWithChar ); static String truncate( Object obj,
                                   int maxLength ); static String truncate( Object obj,
                                   int maxLength,
                                   String suffix ); static String read( Reader reader ); static String read( InputStream stream ); static void write( String content,
                              OutputStream stream ); static void write( String content,
                              Writer writer ); static String getStackTrace( Throwable throwable ); static String normalize( String text ); static String getHexString( byte[] bytes ); static byte[] fromHexString( String hexadecimal ); static final String[] EMPTY_STRING_ARRAY; }### Answer:
@Test public void truncateShouldReturnEmptyStringIfNullReferenceIsSupplied() { assertThat(StringUtil.truncate(null, 0), is("")); assertThat(StringUtil.truncate(null, 1), is("")); assertThat(StringUtil.truncate(null, 100), is("")); }
@Test( expected = IllegalArgumentException.class ) public void truncateShouldNotAllowNegativeLength() { StringUtil.truncate("some string", -1); }
@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 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 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")); }