### Question:
StringUtil { public static String trimOrPadString(String str, int len, char pad) { if (len < 0) { len = 0; } if (str == null) { str = ""; } StringBuffer s = new StringBuffer(); if (str.length() > len - 1) { s.append(str.substring(str.length() - len)); } else { for (int i = 0; i < len - str.length(); i++) { s.append(pad); } s.append(str); } return s.toString(); } private  StringUtil(); static boolean isNullOrEmpty(final String string); static boolean isBlank(final String string); static String convertStreamToString(final InputStream is, final Charset charset); static String convertStreamToUTF8String(final InputStream is); static String clipString(String input, int numChars, boolean appendEllipses); static String convertArrayToString(String[] stringArr); static String convertSetToString(Set<String> stringSet); static String trimOrPadString(String str, int len, char pad); static final String UTF_8; }### Answer:
@Test public void trimOrPadString_trimsString() { assertEquals(StringUtil.trimOrPadString("abcdefg", 7, ' '), "abcdefg"); assertEquals(StringUtil.trimOrPadString("abcdefg", 6, ' '), "bcdefg"); assertEquals(StringUtil.trimOrPadString("abcdefg", 5, ' '), "cdefg"); assertEquals(StringUtil.trimOrPadString("abcdefg", 4, ' '), "defg"); assertEquals(StringUtil.trimOrPadString("abcdefg", 3, ' '), "efg"); assertEquals(StringUtil.trimOrPadString("abcdefg", 2, ' '), "fg"); assertEquals(StringUtil.trimOrPadString("abcdefg", 1, ' '), "g"); assertEquals(StringUtil.trimOrPadString("abcdefg", 0, ' '), ""); }
@Test public void trimOrPadString_padsString() { assertEquals(StringUtil.trimOrPadString("abc", 7, '_'), "____abc"); assertEquals(StringUtil.trimOrPadString("abc", 6, '-'), "---abc"); assertEquals(StringUtil.trimOrPadString("abc", 5, '\\'), "\\\\abc"); assertEquals(StringUtil.trimOrPadString("abc", 4, '$'), "$abc"); assertEquals(StringUtil.trimOrPadString("", 10, '&'), "&&&&&&&&&&"); assertEquals(StringUtil.trimOrPadString("", 5, '"'), "\"\"\"\"\""); assertEquals(StringUtil.trimOrPadString("\b\b", 5, '\n'), "\n\n\n\b\b"); int l = 100; String s = StringUtil.trimOrPadString("", l, '\\'); assertEquals(s.length(), l); for (int i = 0; i < s.length(); i++) { assertEquals(s.charAt(i), '\\'); } }
@Test public void trimOrPadString_worksWith_invalidArgs() { assertEquals(StringUtil.trimOrPadString("abcdefg", -1, ' '), ""); assertEquals(StringUtil.trimOrPadString("abcdefg", -2, ' '), ""); assertEquals(StringUtil.trimOrPadString(null, 5, '+'), "+++++"); }