CompressString { public String compress(String src) { validateInput(src); if (src.isEmpty() || src.length() == 1) { return src; } StringBuilder stringBuilder = new StringBuilder(); int repeatedCharCounter = 1; char previousChar = src.charAt(0); for (int i = 1; i < src.length(); i++) { char currentChar = src.charAt(i); if (isCharRepeated(previousChar, currentChar)) { repeatedCharCounter++; } else { addChar(stringBuilder, previousChar); if (repeatedCharCounter > 1) { stringBuilder.append(repeatedCharCounter); repeatedCharCounter = 1; } previousChar = currentChar; } } appendLastCharIfNeeded(stringBuilder, repeatedCharCounter, previousChar); return stringBuilder.toString(); }  String compress(String src); String compressRecursive(String src);  }
@Test(expected = IllegalArgumentException.class) public void shouldNotAcceptNullStringsAsInput() { compressString.compress(null); }
@Test public void shouldNotCompressEmptyWords() { String wordToCompress = ""; String result = compressString.compress(wordToCompress); assertEquals("", result); }
@Test public void shouldNotCompressWordsWithJustOneChar() { String wordToCompress = "a"; String result = compressString.compress(wordToCompress); assertEquals("a", result); }
@Test public void shouldCompressWordsFullOfTheSameChars() { String wordToCompress = "aaa"; String result = compressString.compress(wordToCompress); assertEquals("a3", result); }
@Test public void shouldCompressWords() { String wordToCompress = "aabcccccaaa"; String result = compressString.compress(wordToCompress); assertEquals("a2bc5a3", result); }