### Question:
PipelineDataGadgetRewriter implements GadgetRewriter { public void rewrite(Gadget gadget, MutableContent content) { if (!gadget.getSpec().getModulePrefs().getFeatures().containsKey("opensocial-data")) { return; } Document doc = content.getDocument(); Map<PipelinedData, Node> pipelineNodes = parsePipelinedData(gadget, doc); if (pipelineNodes.isEmpty()) { return; } PipelineExecutor.Results results = executor.execute(gadget.getContext(), pipelineNodes.keySet()); for (Map.Entry<PipelinedData, Node> nodeEntry : pipelineNodes.entrySet()) { if (!results.remainingPipelines.contains(nodeEntry.getKey())) { Node node = nodeEntry.getValue(); node.getParentNode().removeChild(node); MutableContent.notifyEdit(doc); } } if (!results.keyedResults.isEmpty()) { Element head = (Element) DomUtil.getFirstNamedChildNode(doc.getDocumentElement(), "head"); Element pipelineScript = doc.createElement("script"); pipelineScript.setAttribute("type", "text/javascript"); StringBuilder script = new StringBuilder(); for (Map.Entry<String, ? extends Object> entry : results.keyedResults.entrySet()) { String key = entry.getKey(); content.addPipelinedData(key, entry.getValue()); script.append("opensocial.data.DataContext.putDataSet(\"") .append(key) .append("\",") .append(JsonSerializer.serialize(entry.getValue())) .append(");"); } pipelineScript.appendChild(doc.createTextNode(script.toString())); head.appendChild(pipelineScript); MutableContent.notifyEdit(doc); } if (results.remainingPipelines.isEmpty()) { gadget.addFeature("opensocial-data-context"); gadget.removeFeature("opensocial-data"); } } @Inject  PipelineDataGadgetRewriter(PipelineExecutor executor); void rewrite(Gadget gadget, MutableContent content);  }### Answer:
@Test public void rewrite() throws Exception { setupGadget(getGadgetXml(CONTENT)); Capture<PipelinedData.Batch> batchCapture = new Capture<PipelinedData.Batch>(); Callable<PreloadedData> callable = createPreloadTask( "key", "{result: {foo: 'bar'}}"); expect(preloader.createPreloadTasks(same(gadget.getContext()), and(eqBatch(1, 1), capture(batchCapture)))) .andReturn(ImmutableList.of(callable)); control.replay(); rewriter.rewrite(gadget, content); assertTrue("Script not inserted", content.getContent().contains("DataContext.putDataSet(\"key\",{\"foo\":\"bar\"})")); assertFalse("os-data wasn't deleted", content.getContent().contains("type=\"text/os-data\"")); assertTrue(batchCapture.getValue().getPreloads().containsKey("me")); assertTrue(batchCapture.getValue().getPreloads().containsKey("json")); assertFalse(gadget.getDirectFeatureDeps().contains("opensocial-data")); assertTrue(gadget.getDirectFeatureDeps().contains("opensocial-data-context")); control.verify(); }
@Test public void rewriteWithBlockedBatch() throws Exception { setupGadget(getGadgetXml(BLOCKED_FIRST_BATCH_CONTENT)); expect( preloader.createPreloadTasks(same(gadget.getContext()), eqBatch(0, 0))) .andReturn(ImmutableList.<Callable<PreloadedData>>of()); control.replay(); rewriter.rewrite(gadget, content); control.verify(); assertFalse("DataContext write shouldn't be present", content.getContent().indexOf( "DataContext.putDataSet(") > 0); assertTrue("os-data was deleted", content.getContent().indexOf("type=\"text/os-data\"") > 0); }
@Test public void rewriteWithoutPipeline() throws Exception { setupGadget(XML_WITHOUT_PIPELINE); control.replay(); rewriter.rewrite(gadget, content); control.verify(); }
@Test public void rewriteWithoutFeature() throws Exception { setupGadget(XML_WITHOUT_FEATURE); control.replay(); rewriter.rewrite(gadget, content); control.verify(); }