### Question:
VariableSubstituter { public GadgetSpec substitute(GadgetContext context, GadgetSpec spec) throws GadgetException { Substitutions substitutions = new Substitutions(); for (Substituter substituter : substituters) { substituter.addSubstitutions(substitutions, context, spec); } return spec.substitute(substitutions); } @Inject  VariableSubstituter(@Named("shindig.substituters.gadget") List<Substituter> substituters); GadgetSpec substitute(GadgetContext context, GadgetSpec spec);  }### Answer:
@Test public void messageBundlesSubstituted() throws Exception { String xml = "<Module><ModulePrefs title=''>" + " <Locale>" + " <msg name='foo'>bar</msg>" + " <msg name='bar'>baz</msg>" + " </Locale>" + "</ModulePrefs>" + "<Content>__MSG_foo__ - __MSG_bar__</Content>" + "</Module>"; GadgetSpec spec = substitute(xml); assertEquals("bar - baz", spec.getView("default").getContent()); }
@Test public void bidiSubstituted() throws Exception { String xml = "<Module><ModulePrefs title='__BIDI_END_EDGE__ way'/><Content/></Module>"; GadgetSpec spec = substitute(xml); assertEquals("right way", spec.getModulePrefs().getTitle()); }
@Test public void moduleIdSubstituted() throws Exception { String xml = "<Module><ModulePrefs title='Module is: __MODULE_ID__'/><Content/></Module>"; GadgetSpec spec = substitute(xml); assertEquals("Module is: 0", spec.getModulePrefs().getTitle()); }
@Test public void userPrefsSubstituted() throws Exception { String xml = "<Module>" + "<ModulePrefs title='I heart __UP_foo__'/>" + "<UserPref name='foo'/>" + "<Content/>" + "</Module>"; GadgetSpec spec = new GadgetSpec(Uri.parse("#"), xml); GadgetContext context = new GadgetContext() { @Override public UserPrefs getUserPrefs() { return new UserPrefs(ImmutableMap.of("foo", "shindig")); } }; spec = substituter.substitute(context, spec); assertEquals("I heart shindig", spec.getModulePrefs().getTitle()); }
@Test public void nestedMessageBundleInUserPrefSubstituted() throws Exception { String xml = "<Module>" + " <ModulePrefs title='__UP_title__ for __MODULE_ID__'>" + " <Locale>" + " <msg name='title'>Gadget title</msg>" + " </Locale>" + " </ModulePrefs>" + " <UserPref name='title' default_value='__MSG_title__' />" + " <Content />" + "</Module>"; GadgetSpec spec = substitute(xml); assertEquals("Gadget title for 0", spec.getModulePrefs().getTitle()); }