Substitution { public final String substitute(String input) { if (input == null) { return null; } StringBuilder buf = new StringBuilder(); substitute(buf, input); return buf.toString(); } Substitution();  Substitution(String replacementPrefixes); final String substitute(String input); final void substitute(StringBuilder buf, String input);  }
@Test public void subst_null() { Substitution subs = new MatchResultSubstitution(r1); assertEquals(null, subs.substitute(null)); }
@Test public void setMatchResult() { MatchResultSubstitution subs = new MatchResultSubstitution(); subs.setMatchResult(r1); assertEquals("xxaayy$2zz", subs.substitute("xx$1yy$2zz")); try { subs.setMatchResults(r1, r2); fail(); } catch (IllegalArgumentException e) { assertThat(e, TestUtil.exception("expected 1 MatchResults")); } }
@Test public void matchNormal() { Substitution subs = new MatchResultSubstitution(r1); assertEquals("xxaayy$2zz", subs.substitute("xx$1yy$2zz")); }
@Test public void matchOverflowNumber() { Substitution subs = new MatchResultSubstitution(r1); assertEquals("xx$12345678901234567890yy$2zz", subs.substitute("xx$12345678901234567890yy$2zz")); }
@Test public void matchMulti() { Substitution subs = new MatchResultSubstitution("$%", r1, r2); assertEquals("xxaayycczz$2%2", subs.substitute("xx$1yy%1zz$2%2")); r1 = getMatchResult("\\.(\\w+)\\.com/(.*)", "www.saga67.com/test.htm"); r2 = getMatchResult("a=(\\d+)&b=(\\d+)", "a=1&b=2&c=3"); assertEquals("\\saga67, $1, $x, .saga67.com/test.htm, saga67, test.htm, $3, %1, %x, a=1&b=2, 1, 2, %3, \\", new MatchResultSubstitution("$%", r1, r2) .substitute("\\\\$1, \\$1, $x, $0, $1, $2, $3, \\%1, %x, %0, %1, %2, %3, \\")); }