DuplicateCharEraser { public String removeRepeatedChar(String inputStr) { if (StringUtils.isEmpty(inputStr)) { return inputStr; } Matcher matcher = removeRepeatCharPattern.matcher(inputStr); return matcher.replaceAll("$1"); } DuplicateCharEraser();  DuplicateCharEraser(char repeatChar);  DuplicateCharEraser(String repeatChar); String removeRepeatedChar(String inputStr);  }
@Test public void testRemoveDuplicateCR() { DuplicateCharEraser duplicateCharEraser = new DuplicateCharEraser(); String input = "a\rbccccdeaa\r\r\ry"; assertEquals("a\rbccccdeaa\ry", duplicateCharEraser.removeRepeatedChar(input)); }
@Test public void testRemoveDuplicateLF() { DuplicateCharEraser duplicateCharEraser = new DuplicateCharEraser(); String input = "a\nbccccdeaa\n\n\ny"; assertEquals("a\nbccccdeaa\ny", duplicateCharEraser.removeRepeatedChar(input)); }
@Test public void testRemoveDuplicateCRLF() { DuplicateCharEraser duplicateCharEraser = new DuplicateCharEraser(); String input = "a\r\nbccccdeaa\r\n\r\n\r\ny"; assertEquals("a\r\nbccccdeaa\r\ny", duplicateCharEraser.removeRepeatedChar(input)); }
@Test public void testRemoveDuplicateTAB() { DuplicateCharEraser duplicateCharEraser = new DuplicateCharEraser(); String input = "a\tbccccdeaa\t\t\t\t\t\ty"; assertEquals("a\tbccccdeaa\ty", duplicateCharEraser.removeRepeatedChar(input)); }
@Test public void testRemoveDuplicateLetter() { DuplicateCharEraser duplicateCharEraser = new DuplicateCharEraser('c'); String input = "atbccccdeaaCCtcy"; assertEquals("atbcdeaaCCtcy", duplicateCharEraser.removeRepeatedChar(input)); duplicateCharEraser = new DuplicateCharEraser('a'); input = "aaatbccccdeaaCCtcy"; assertEquals("atbccccdeaCCtcy", duplicateCharEraser.removeRepeatedChar(input)); input = "acacacactbccccdeaCCtaccy"; input = "abcdef"; assertEquals("abcdef", duplicateCharEraser.removeRepeatedChar(input)); }
@Test public void testRemoveDuplicateNumber() { DuplicateCharEraser duplicateCharEraser = new DuplicateCharEraser('1'); String input = "011111123"; assertEquals("0123", duplicateCharEraser.removeRepeatedChar(input)); duplicateCharEraser = new DuplicateCharEraser('3'); input = "apple 12333"; assertEquals("apple 123", duplicateCharEraser.removeRepeatedChar(input)); }
@Test public void testRemoveDuplicateNull1() { DuplicateCharEraser duplicateCharEraser = new DuplicateCharEraser('c'); String input = null; assertEquals(null, duplicateCharEraser.removeRepeatedChar(input)); input = ""; assertEquals("", duplicateCharEraser.removeRepeatedChar(input)); }
@Test public void testRemoveDuplicateNull2() { DuplicateCharEraser duplicateCharEraser = new DuplicateCharEraser(); String input = "aaabc"; assertEquals(input, duplicateCharEraser.removeRepeatedChar(input)); duplicateCharEraser = new DuplicateCharEraser(' '); assertEquals(input, duplicateCharEraser.removeRepeatedChar(input)); duplicateCharEraser = new DuplicateCharEraser(); assertEquals(input, duplicateCharEraser.removeRepeatedChar(input)); }
@Test public void testRemoveWhiteSpace() { DuplicateCharEraser duplicateCharEraser = new DuplicateCharEraser(); String input = "a b\t\t\tc\n\n\nd\r\re\f\ff"; String cleanStr = duplicateCharEraser.removeRepeatedChar(input); assertEquals("a b\tc\nd\re\ff", cleanStr); input = "aaab\r\n\r\n\r\nx"; cleanStr = duplicateCharEraser.removeRepeatedChar(input); assertEquals("aaab\r\nx", cleanStr); input = "a\u0085\u0085\u0085b\u00A0\u00A0c\u1680\u1680d\u180E\u180Ee\u2000\u2000f\u2001\u2001g\u2002\u2002h\u2003\u2003i\u2004\u2004"; cleanStr = duplicateCharEraser.removeRepeatedChar(input); assertEquals("a\u0085b\u00A0c\u1680d\u180Ee\u2000f\u2001g\u2002h\u2003i\u2004", cleanStr); input = "a\u2005\u2005\u2005b\u2006\u2006c\u2007\u2007d\u2008\u2008e\u2009\u2009f\u200A\u200Ag\u2028\u2028h\u2029\u2029i\u202F\u202Fj\u205F\u205Fk\u3000\u3000l"; cleanStr = duplicateCharEraser.removeRepeatedChar(input); assertEquals("a\u2005b\u2006c\u2007d\u2008e\u2009f\u200Ag\u2028h\u2029i\u202Fj\u205Fk\u3000l", cleanStr); }
@Test public void testRemoveWhiteSpaceNull() { DuplicateCharEraser duplicateCharEraser = new DuplicateCharEraser(); String input = ""; String cleanStr = duplicateCharEraser.removeRepeatedChar(input); assertEquals("", cleanStr); input = null; cleanStr = duplicateCharEraser.removeRepeatedChar(input); assertNull(cleanStr); }
@Test public void testRemoveWhiteSpacWithoutSpace() { DuplicateCharEraser duplicateCharEraser = new DuplicateCharEraser(); String input = "abccdef"; String cleanStr = duplicateCharEraser.removeRepeatedChar(input); assertEquals("abccdef", cleanStr); }
@Test public void testRemoveSpecialCharacter() { DuplicateCharEraser duplicateCharEraser = new DuplicateCharEraser(')'); String input = "Gooooalllll))))]]]]]]++++++[[[^^^\\\\(((|||"; assertEquals("Gooooalllll)]]]]]]++++++[[[^^^\\\\(((|||", duplicateCharEraser.removeRepeatedChar(input)); duplicateCharEraser = new DuplicateCharEraser(']'); assertEquals("Gooooalllll))))]++++++[[[^^^\\\\(((|||", duplicateCharEraser.removeRepeatedChar(input)); duplicateCharEraser = new DuplicateCharEraser('+'); assertEquals("Gooooalllll))))]]]]]]+[[[^^^\\\\(((|||", duplicateCharEraser.removeRepeatedChar(input)); duplicateCharEraser = new DuplicateCharEraser('\\'); assertEquals("Gooooalllll))))]]]]]]++++++[[[^^^\\(((|||", duplicateCharEraser.removeRepeatedChar(input)); duplicateCharEraser = new DuplicateCharEraser('^'); assertEquals("Gooooalllll))))]]]]]]++++++[[[^\\\\(((|||", duplicateCharEraser.removeRepeatedChar(input)); duplicateCharEraser = new DuplicateCharEraser('['); assertEquals("Gooooalllll))))]]]]]]++++++[^^^\\\\(((|||", duplicateCharEraser.removeRepeatedChar(input)); duplicateCharEraser = new DuplicateCharEraser('('); assertEquals("Gooooalllll))))]]]]]]++++++[[[^^^\\\\(|||", duplicateCharEraser.removeRepeatedChar(input)); duplicateCharEraser = new DuplicateCharEraser('|'); assertEquals("Gooooalllll))))]]]]]]++++++[[[^^^\\\\(((|", duplicateCharEraser.removeRepeatedChar(input)); }
@Test public void testSeveralCharsShouldBeDeduplicated() { DuplicateCharEraser duplicateCharEraser = new DuplicateCharEraser("abc"); String input = "abcabcabc"; String cleanStr = duplicateCharEraser.removeRepeatedChar(input); assertEquals("abc", cleanStr); }
@Test public void testMustAcceptRegexSpecialChars() { DuplicateCharEraser duplicateCharEraser = new DuplicateCharEraser("a*({]."); String input = "a*({].a*({].a*({].a*({]."; String cleanStr = duplicateCharEraser.removeRepeatedChar(input); assertEquals("a*({].", cleanStr); }