RegexUtils { public static String normalizeFormName(String formName, boolean returnNullIfNothingChanged) { if (formName == null) { return null; } Matcher matcher = CONTROL_CHAR_PATTERN.matcher(formName); return matcher.find() ? matcher.replaceAll(" ") : (returnNullIfNothingChanged ? null : formName); } private  RegexUtils(); static String normalizeFormName(String formName, boolean returnNullIfNothingChanged);  }
@Test public void normalizeFormNameTest() { assertNull(normalizeFormName(null, false)); assertEquals("Lorem", normalizeFormName("Lorem", false)); assertEquals("Lorem ipsum", normalizeFormName("Lorem ipsum", false)); assertEquals("Lorem ipsum", normalizeFormName("Lorem\nipsum", false)); assertEquals("Lorem ipsum", normalizeFormName("Lorem\n\nipsum", false)); assertEquals(" Lorem ipsum ", normalizeFormName("\nLorem\nipsum\n", false)); assertNull(normalizeFormName(null, true)); assertNull(normalizeFormName("Lorem", true)); assertNull(normalizeFormName("Lorem ipsum", true)); assertEquals("Lorem ipsum", normalizeFormName("Lorem\nipsum", true)); assertEquals("Lorem ipsum", normalizeFormName("Lorem\n\nipsum", true)); assertEquals(" Lorem ipsum ", normalizeFormName("\nLorem\nipsum\n", true)); }