/plugin-module-codegen-engine/src/main/resources/templates/common/web/webrersourcetransformer/WebResourceTransformerTest.java.vtl
https://bitbucket.org/mmeinhold/amps · Unknown · 105 lines · 84 code · 21 blank · 0 comment · 0 complexity · bcb1502fe86d9688fb2b9da76f53d172 MD5 · raw file
- package ${PACKAGE};
- import com.atlassian.plugin.PluginAccessor;
- import com.atlassian.plugin.elements.ResourceLocation;
- import com.atlassian.plugin.servlet.DownloadException;
- import com.atlassian.plugin.servlet.DownloadableResource;
- import com.atlassian.plugin.webresource.transformer.WebResourceTransformer;
- import com.atlassian.plugin.webresource.transformer.WebResourceTransformerModuleDescriptor;
- import org.apache.commons.io.IOUtils;
- import org.dom4j.DocumentException;
- import org.dom4j.DocumentHelper;
- import org.dom4j.Element;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.ByteArrayOutputStream;
- import java.io.IOException;
- import java.io.OutputStream;
- import java.util.Arrays;
- import org.junit.Test;
- import org.junit.After;
- import org.junit.Before;
- import org.mockito.Mockito;
- import static org.junit.Assert.*;
- import static org.mockito.Mockito.*;
- import ${FQ_CLASS_UNDER_TEST};
- public class ${CLASSNAME} {
- @Before
- public void setup() {
- }
- @After
- public void tearDown() {
- }
- @Test
- public void testTransformDownloadableResource() throws DocumentException, DownloadException {
- String transformerKey = "my-transformer";
- Element element = DocumentHelper.parseText(
- "<transformation extension=\"js\">\n" +
- "<transformer key=\"" + transformerKey + "\" />\n" +
- "</transformation>").getRootElement();
- WebResourceTransformation trans = new WebResourceTransformation(element);
- PluginAccessor pluginAccessor = mock(PluginAccessor.class);
- WebResourceTransformerModuleDescriptor descriptor = mock(WebResourceTransformerModuleDescriptor.class);
- when(descriptor.getKey()).thenReturn(transformerKey);
- ${CLASS_UNDER_TEST} transformer = new ${CLASS_UNDER_TEST}(transformerKey + ":");
- 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 = new StringDownloadableResource("resource");
- DownloadableResource testResource = trans.transformDownloadableResource(pluginAccessor, originalResource, loc, "");
- ByteArrayOutputStream bout = new ByteArrayOutputStream();
- testResource.streamResource(bout);
- assertEquals("wrong transformation value","my-transformer:resource", new String(bout.toByteArray()));
- }
- private static class StringDownloadableResource implements DownloadableResource
- {
- final String value;
- public StringDownloadableResource(String value)
- {
- this.value = value;
- }
- public boolean isResourceModified(HttpServletRequest request, HttpServletResponse response)
- {
- return false;
- }
- public void serveResource(HttpServletRequest request, HttpServletResponse response) throws DownloadException
- {
- }
- public void streamResource(OutputStream out) throws DownloadException
- {
- try
- {
- IOUtils.write(value, out);
- }
- catch (IOException e)
- {
- e.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
- }
- }
- public String getContentType()
- {
- return "text/plain";
- }
- }
- }