### Question:
TemplateRewriter implements GadgetRewriter { public void rewrite(Gadget gadget, MutableContent content) throws RewritingException { Map<String, Feature> directFeatures = gadget.getSpec().getModulePrefs() .getFeatures(); Feature feature = directFeatures.get(TEMPLATES_FEATURE_NAME); if (feature == null && directFeatures.containsKey(OSML_FEATURE_NAME)) { feature = directFeatures.get(OSML_FEATURE_NAME); } if (feature != null && isServerTemplatingEnabled(feature)) { try { rewriteImpl(gadget, feature, content); } catch (GadgetException ge) { throw new RewritingException(ge, ge.getHttpStatusCode()); } } } @Inject  TemplateRewriter(Provider<TemplateProcessor> processor,
      MessageBundleFactory messageBundleFactory, Expressions expressions, 
      TagRegistry baseTagRegistry, TemplateLibraryFactory libraryFactory,
      ContainerTagLibraryFactory containerTags); void rewrite(Gadget gadget, MutableContent content); final static Set<String> TAGS; static final String TEMPLATES_FEATURE_NAME; static final String OSML_FEATURE_NAME; }### Answer:
@Test public void autoUpdateTemplate() throws Exception { setupGadget(getGadgetXml(CONTENT_WITH_AUTO_UPDATE)); rewriter.rewrite(gadget, content); assertTrue("Template wasn't transformed", content.getContent().indexOf("Hello, John") > 0); assertTrue("Template tag was removed", content.getContent().contains("text/os-template")); assertTrue("ID span was not created", content.getContent().contains("<span id=\"_T_template_auto0\">")); testFeatureNotRemoved(); }
@Test public void templateWithLibrary() throws Exception { setupGadget(getGadgetXmlWithLibrary(CONTENT_WITH_TAG_FROM_LIBRARY)); rewriter.rewrite(gadget, content); assertTrue("Script not inserted", content.getContent().indexOf( "<script type=\"text/javascript\">script</script>") > 0); assertTrue("Style not inserted", content.getContent().indexOf( "<style type=\"text/css\">style</style>") > 0); assertTrue("Tag not executed", content.getContent().indexOf( "external4") > 0); testFeatureRemoved(); }
@Test public void osmlWithLibrary() throws Exception { setupGadget(getGadgetXmlWithLibrary(CONTENT_WITH_TAG_FROM_LIBRARY, "osml")); rewriter.rewrite(gadget, content); assertTrue("Custom tags were evaluated", content.getContent().equals( "<html><head></head><body><my:Tag4></my:Tag4></body></html>")); testFeatureRemoved(); }
@Test public void tagPrecedenceRules() throws Exception { config.put("${Cur['gadgets.features'].osml.library}", "org/apache/shindig/gadgets/rewrite/OSML_test.xml"); setupGadget(getGadgetXmlWithLibrary(CONTENT_TESTING_PRECEDENCE_RULES)); rewriter.rewrite(gadget, content); assertTrue("Precedence rules violated", content.getContent().indexOf("default1osml2inline3external4") > 0); testFeatureRemoved(); }
@Test public void tagPrecedenceRulesWithOSMLFeature() throws Exception { config.put("${Cur['gadgets.features'].osml.library}", "org/apache/shindig/gadgets/rewrite/OSML_test.xml"); setupGadget(getGadgetXmlWithLibrary(CONTENT_TESTING_PRECEDENCE_RULES, "osml")); rewriter.rewrite(gadget, content); assertTrue("Precedence rules violated", content.getContent().indexOf( "default1osml2<my:Tag3></my:Tag3><my:Tag4></my:Tag4>") > 0); testFeatureRemoved(); }
@Test public void tagPrecedenceRulesWithoutOSML() throws Exception { config.put("${Cur['gadgets.features'].osml.library}", ""); setupGadget(getGadgetXmlWithLibrary(CONTENT_TESTING_PRECEDENCE_RULES)); rewriter.rewrite(gadget, content); assertTrue("Precedence rules violated", content.getContent().indexOf("default1inline2inline3external4") > 0); testFeatureRemoved(); }