TextValueCleaner { public String sanitizeValue(@Nullable String value) { String cleanValue = value; if (cleanValue != null) { cleanValue = cleanValue.trim(); cleanValue = cleanValue.replace(NEWLINE, SPACE); cleanValue = cleanValue.replace(TAB, SPACE); } return cleanValue; } @Inject  TextValueCleaner(); String cleanVal(@Nullable String value); String sanitizeValue(@Nullable String value);  }
@Test public void sanitizeValueShouldReturnSameIfNothingToReplace() { TextValueCleaner textValueCleaner = new TextValueCleaner(); String cleanedValue = textValueCleaner.sanitizeValue("abc"); assertEquals("abc", cleanedValue); }
@Test public void sanitizeValueShouldReturnNullIfNullString() { TextValueCleaner textValueCleaner = new TextValueCleaner(); String cleanedValue = textValueCleaner.sanitizeValue(null); assertNull(cleanedValue); }
@Test public void sanitizeValueShouldTrimSpaces() { TextValueCleaner textValueCleaner = new TextValueCleaner(); String cleanedValue = textValueCleaner.sanitizeValue(" abc "); assertEquals("abc", cleanedValue); }
@Test public void sanitizeValueShouldReplaceNewLine() { TextValueCleaner textValueCleaner = new TextValueCleaner(); String cleanedValue = textValueCleaner.sanitizeValue( TextValueCleaner.NEWLINE + "a" + TextValueCleaner.NEWLINE + "bc" + TextValueCleaner.NEWLINE); assertEquals("a bc", cleanedValue); }
@Test public void sanitizeValueShouldReplaceTab() { TextValueCleaner textValueCleaner = new TextValueCleaner(); String cleanedValue = textValueCleaner.sanitizeValue( TextValueCleaner.TAB + "a" + TextValueCleaner.TAB + "bc" + TextValueCleaner.TAB); assertEquals("a bc", cleanedValue); }