### Question:
DownloadSubstituters { public static String substitute(DownloadRequirement req, String basevalue) { return substitute(basevalue, getBasicSubstitutions(req)); } private  DownloadSubstituters(); static String substitute(DownloadRequirement req, String basevalue); static Map<String,Object> getBasicSubstitutions(DownloadRequirement req); static Map<String,Object> getBasicEntitySubstitutions(EntityDriver driver); static Map<String,Object> getBasicAddonSubstitutions(EntityDriver driver, String addon); static String substitute(String basevalue, Map<String,?> substitutions); static Function<DownloadRequirement, DownloadTargets> substituter(Function<? super DownloadRequirement, String> basevalueProducer, Function<? super DownloadRequirement, ? extends Map<String,?>> subsProducer);  }### Answer:
@Test public void testSimpleSubstitution() throws Exception { entity.config().set(BrooklynConfigKeys.SUGGESTED_VERSION, "myversion"); String pattern = "mykey1=${mykey1},mykey2=${mykey2}"; String result = DownloadSubstituters.substitute(pattern, ImmutableMap.of("mykey1", "myval1", "mykey2", "myval2")); assertEquals(result, "mykey1=myval1,mykey2=myval2"); }
@Test public void testSubstitutionIncludesDefaultSubs() throws Exception { entity.config().set(BrooklynConfigKeys.SUGGESTED_VERSION, "myversion"); String pattern = "version=${version},type=${type},simpletype=${simpletype}"; BasicDownloadRequirement req = new BasicDownloadRequirement(driver); String result = DownloadSubstituters.substitute(req, pattern); assertEquals(result, String.format("version=%s,type=%s,simpletype=%s", "myversion", TestEntity.class.getName(), TestEntity.class.getSimpleName())); }
@Test public void testSubstitutionDoesMultipleMatches() throws Exception { String simpleType = TestEntity.class.getSimpleName(); String pattern = "simpletype=${simpletype},simpletype=${simpletype}"; BasicDownloadRequirement req = new BasicDownloadRequirement(driver); String result = DownloadSubstituters.substitute(req, pattern); assertEquals(result, String.format("simpletype=%s,simpletype=%s", simpleType, simpleType)); }
@Test public void testSubstitutionUsesEntityBean() throws Exception { String entityid = entity.getId(); String pattern = "id=${entity.id}"; BasicDownloadRequirement req = new BasicDownloadRequirement(driver); String result = DownloadSubstituters.substitute(req, pattern); assertEquals(result, String.format("id=%s", entityid)); }
@Test public void testSubstitutionUsesDriverBean() throws Exception { String entityid = entity.getId(); String pattern = "id=${driver.entity.id}"; BasicDownloadRequirement req = new BasicDownloadRequirement(driver); String result = DownloadSubstituters.substitute(req, pattern); assertEquals(result, String.format("id=%s", entityid)); }
@Test public void testSubstitutionUsesOverrides() throws Exception { entity.config().set(BrooklynConfigKeys.SUGGESTED_VERSION, "myversion"); String pattern = "version=${version},mykey1=${mykey1}"; BasicDownloadRequirement req = new BasicDownloadRequirement(driver, ImmutableMap.of("version", "overriddenversion", "mykey1", "myval1")); String result = DownloadSubstituters.substitute(req, pattern); assertEquals(result, "version=overriddenversion,mykey1=myval1"); }
@Test public void testThrowsIfUnmatchedSubstitutions() throws Exception { String pattern = "nothere=${nothere}"; BasicDownloadRequirement req = new BasicDownloadRequirement(driver); try { String result = DownloadSubstituters.substitute(req, pattern); fail("Should have failed, but got "+result); } catch (IllegalArgumentException e) { if (!e.toString().contains("${nothere}")) throw e; } }