### Question:
WebResourceTransformation { DownloadableResource transformDownloadableResource(PluginAccessor pluginAccessor, DownloadableResource resource, ResourceLocation resourceLocation, String filePath) { DownloadableResource lastResource = resource; for (Map.Entry<String, Element> entry : transformerElements.entrySet()) { boolean found = false; for (WebResourceTransformerModuleDescriptor descriptor : pluginAccessor.getEnabledModuleDescriptorsByClass(WebResourceTransformerModuleDescriptor.class)) { if (descriptor.getKey().equals(entry.getKey())) { found = true; lastResource = descriptor.getModule().transform(entry.getValue(), resourceLocation, filePath, lastResource); } } if (!found) { log.warn("Web resource transformer " + entry.getKey() + " not found for resource " + resourceLocation.getName() + ", skipping"); } } return lastResource; } WebResourceTransformation(Element element);   }### Answer:
@Test public void testTransformDownloadableResource() throws DocumentException { Element element = DocumentHelper.parseText( "<transformation extension=\"js\">\n" + "<transformer key=\"foo\" />\n" + "</transformation>").getRootElement(); WebResourceTransformation trans = new WebResourceTransformation(element); PluginAccessor pluginAccessor = mock(PluginAccessor.class); WebResourceTransformerModuleDescriptor descriptor = mock(WebResourceTransformerModuleDescriptor.class); when(descriptor.getKey()).thenReturn("foo"); WebResourceTransformer transformer = mock(WebResourceTransformer.class); when(descriptor.getModule()).thenReturn(transformer); when(pluginAccessor.getEnabledModuleDescriptorsByClass(WebResourceTransformerModuleDescriptor.class)).thenReturn( Arrays.asList(descriptor)); ResourceLocation loc = mock(ResourceLocation.class); when(loc.getName()).thenReturn("foo.js"); DownloadableResource originalResource = mock(DownloadableResource.class); DownloadableResource transResource = mock(DownloadableResource.class); when(transformer.transform(element.element("transformer"), loc, "", originalResource)).thenReturn(transResource); DownloadableResource testResource = trans.transformDownloadableResource(pluginAccessor, originalResource, loc, ""); assertEquals(transResource, testResource); }
@Test public void testTransformTwoDownloadableResources() throws DocumentException, DownloadException { Element element = DocumentHelper.parseText( "<transformation extension=\"js\">\n" + "<transformer key=\"foo\" />\n" + "<transformer key=\"bar\" />\n" + "</transformation>").getRootElement(); WebResourceTransformation trans = new WebResourceTransformation(element); PluginAccessor pluginAccessor = mock(PluginAccessor.class); WebResourceTransformerModuleDescriptor fooDescriptor = createTransformer("foo"); WebResourceTransformerModuleDescriptor barDescriptor = createTransformer("bar"); when(pluginAccessor.getEnabledModuleDescriptorsByClass(WebResourceTransformerModuleDescriptor.class)).thenReturn( Arrays.asList( fooDescriptor, barDescriptor)); ResourceLocation loc = mock(ResourceLocation.class); when(loc.getName()).thenReturn("foo.js"); DownloadableResource originalResource = new StringDownloadableResource("resource"); DownloadableResource testResource = trans.transformDownloadableResource(pluginAccessor, originalResource, loc, ""); ByteArrayOutputStream bout = new ByteArrayOutputStream(); testResource.streamResource(bout); assertEquals("bar: foo: resource", new String(bout.toByteArray())); }