StringFormatter { public String shortenName(String name, int len) { boolean needEllipsis = false; if (len <= 0) { len = MAXLEN; } String[] parts = name.split(" "); if (parts.length > 2) { if (parts[0].length() > MAXWORDLEN || parts[parts.length - 1].length() > MAXWORDLEN) { name = parts[0].substring(0, Math.min(MAXWORDLEN, parts[0].length())); needEllipsis = true; } else { name = parts[0] + " ... " + parts[parts.length - 1]; } } if (name.length() > len) { name = name.substring(0, len); needEllipsis = true; } if (needEllipsis) { name += "..."; } return name; }  String escapeChars(String value); String fixCutName(String name, int len); String shortenName(String name, int len);  }
@Test public void testShortenName() { try { if (!stringFormatter.shortenName("Great WhiteVeryLongStringWhiteVeryLongStringWhiteVeryLongStringWhiteVeryLongString Shark", 0).equals("Great ... Shark")) { fail("Shorten name is not correct"); } if (!stringFormatter.shortenName("Great White", 0).equals("Great White")) { fail("Shorten name is not correct"); } if (!stringFormatter.shortenName("GreatAlpineRoadGreatOceanRoadGreatWesternHighway", 30) .equals("GreatAlpineRoadGreatOceanRoadG...")) { fail("Shorten name is not correct"); } System.out.println("Testing StringFormatter - shortenName()"); } catch (Exception e) { fail("Exception occurred: " + e.getMessage()); } }