BasePlaceholder { public String process(ReplacementData data, String formatPattern) { StringBuffer sb = new StringBuffer(); Matcher matcher = pattern.matcher(formatPattern); while (matcher.find()) { matcher.appendReplacement(sb, Matcher.quoteReplacement(getReplacementFor(matcher.group(), data))); } matcher.appendTail(sb); return sb.toString(); } BasePlaceholder(String placeholderRegex); String process(ReplacementData data, String formatPattern);  }
@Test public void testProcessShouldForwardDataToGetReplacementFor() { TestPlaceholder underTest = new TestPlaceholder("%s", "test"); underTest.process(data, "%s"); assertThat(underTest.data).isSameAs(data); }
@Test public void testProcessShouldReplaceNothingForNotMatchingPlaceholder() { BasePlaceholder underTest = new TestPlaceholder("%s", "##foo##"); String result = underTest.process(null, "%d"); assertThat(result).isEqualTo("%d"); }
@Test public void testProcessShouldReplaceSinglePlaceholder() { BasePlaceholder underTest = new TestPlaceholder("%s", "##foo##"); String result = underTest.process(null, "%s"); assertThat(result).isEqualTo("##foo##"); }
@Test public void testProcessShouldReplaceSinglePlaceholderSurroundedByText() { BasePlaceholder underTest = new TestPlaceholder("%s", "##bar##"); String result = underTest.process(null, "Test %s()"); assertThat(result).isEqualTo("Test ##bar##()"); }
@Test public void testProcessShouldReplaceSinglePlaceholderSurroundedByOtherPlaceholders() { BasePlaceholder underTest = new TestPlaceholder("%s", "##baz##"); String result = underTest.process(null, "%a%s%b"); assertThat(result).isEqualTo("%a##baz##%b"); }
@Test public void testProcessShouldReplaceMultiplePlaceholders() { BasePlaceholder underTest = new TestPlaceholder("%s", "##bla##"); String result = underTest.process(null, "ss%ss%ss%ss%"); assertThat(result).isEqualTo("ss##bla##s##bla##s##bla##s%"); }
@Test public void testProcessShouldReplacePlaceholderNotCauseAnStackOverflowExceptionIfPlaceholderIsReplacedbyItself() { BasePlaceholder underTest = new TestPlaceholder("%s", "%s"); String result = underTest.process(null, "%s"); assertThat(result).isEqualTo("%s"); }
@Test public void testProcessShouldReplacePlaceholderNotRecursivelyForSimpleFormatPattern() { BasePlaceholder underTest = new TestPlaceholder("%s", "1234%s"); String result = underTest.process(null, "%s"); assertThat(result).isEqualTo("1234%s"); }
@Test public void testProcessShouldReplacePlaceholderNotRecursivelyForSimpleFormatPatternContainingPlaceholderTwice() { BasePlaceholder underTest = new TestPlaceholder("%s", "1234%s"); String result = underTest.process(null, "%s%s"); assertThat(result).isEqualTo("1234%s1234%s"); }
@Test public void testProcessShouldReplacePlaceholderNotRecursivelyForComplexFormatPattern() { BasePlaceholder underTest = new TestPlaceholder("%s", "abc%s%"); String result = underTest.process(null, "%ss%s"); assertThat(result).isEqualTo("abc%s%sabc%s%"); }
@Test public void testProcessShouldHandleDollarSignsCorrectly() { BasePlaceholder underTest = new TestPlaceholder("%s", "$"); String result = underTest.process(null, "%s"); assertThat(result).isEqualTo("$"); }
@Test public void testProcessShouldHandleBackslashesCorrectly() { BasePlaceholder underTest = new TestPlaceholder("%s", "\\"); String result = underTest.process(null, "%s"); assertThat(result).isEqualTo("\\"); }