GeneralUtils { public static String cleanString(String src) { if (src == null) { return null; } boolean foundBad = false; final CharacterIterator it = new StringCharacterIterator(src); for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) { if (c < ASCII_PRINTABLE_LOW || c >= ASCII_PRINTABLE_HI) { foundBad = true; break; } } if (!foundBad) { return src; } final StringBuilder res = new StringBuilder(); for (char c = it.first(); c != CharacterIterator.DONE; c = it.next()) { if (c < ASCII_PRINTABLE_LOW || c >= ASCII_PRINTABLE_HI) { res.append(ASCII_SPACE); } else { res.append(c); } } return res.toString(); } private  GeneralUtils(); static synchronized void logMemStatus(String title); static void logMaxMem(); static long getCpuTime(); static long getUserTime(); static long getSystemTime(); static void logElapsedTime(String title, long timeInNanos); static String cleanString(String src);  }
@Test public void testCleanStringDoesNotRemoveValidASCII() throws Exception { String t1 = " !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~"; assertEquals("All ASCII string", t1, GeneralUtils.cleanString(t1)); }
@Test public void testCleanStringRemovesOutOfRangeASCII() throws Exception { char vals[] = { 1, 2, 3, 4, 5, 6, 30, 31, 128, 129 }; assertEquals("Clean all values", " ", GeneralUtils.cleanString(new String(vals))); }
@Test public void testCleanStringRemovesOutOfRangeASCII2() throws Exception { char vals[] = { 129, 2, 3, 4, 5, 6, 30, 31, 128, 129 }; assertEquals("Clean all values", " ", GeneralUtils.cleanString(new String(vals))); }
@Test public void testCleanComboOfValidAndOutOfRangeASCII() throws Exception { char vals[] = { 1, 2, 3, 'a', 'b', 'c', 128, 129, 'D', 'E', 'F' }; assertEquals("Clean some values", " abc DEF", GeneralUtils.cleanString(new String(vals))); }
@Test public void testCleanStringReturnsNullForInputNull() throws Exception { assertNull("Verify that if the input is null then the output should be null.", GeneralUtils.cleanString(null)); }