PageRenderTime 32ms CodeModel.GetById 18ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/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
  1package ${PACKAGE};
  2
  3import com.atlassian.plugin.PluginAccessor;
  4import com.atlassian.plugin.elements.ResourceLocation;
  5import com.atlassian.plugin.servlet.DownloadException;
  6import com.atlassian.plugin.servlet.DownloadableResource;
  7import com.atlassian.plugin.webresource.transformer.WebResourceTransformer;
  8import com.atlassian.plugin.webresource.transformer.WebResourceTransformerModuleDescriptor;
  9import org.apache.commons.io.IOUtils;
 10import org.dom4j.DocumentException;
 11import org.dom4j.DocumentHelper;
 12import org.dom4j.Element;
 13import javax.servlet.http.HttpServletRequest;
 14import javax.servlet.http.HttpServletResponse;
 15import java.io.ByteArrayOutputStream;
 16import java.io.IOException;
 17import java.io.OutputStream;
 18import java.util.Arrays;
 19import org.junit.Test;
 20import org.junit.After;
 21import org.junit.Before;
 22import org.mockito.Mockito;
 23import static org.junit.Assert.*;
 24import static org.mockito.Mockito.*;
 25import ${FQ_CLASS_UNDER_TEST};
 26
 27public class ${CLASSNAME} {
 28
 29    @Before
 30    public void setup() {
 31
 32    }
 33
 34    @After
 35    public void tearDown() {
 36
 37    }
 38
 39    @Test
 40    public void testTransformDownloadableResource() throws DocumentException, DownloadException {
 41        String transformerKey = "my-transformer";
 42
 43        Element element = DocumentHelper.parseText(
 44                "<transformation extension=\"js\">\n" +
 45                        "<transformer key=\"" + transformerKey + "\" />\n" +
 46                        "</transformation>").getRootElement();
 47        WebResourceTransformation trans = new WebResourceTransformation(element);
 48        PluginAccessor pluginAccessor = mock(PluginAccessor.class);
 49
 50        WebResourceTransformerModuleDescriptor descriptor = mock(WebResourceTransformerModuleDescriptor.class);
 51        when(descriptor.getKey()).thenReturn(transformerKey);
 52
 53        ${CLASS_UNDER_TEST} transformer = new ${CLASS_UNDER_TEST}(transformerKey + ":");
 54
 55        when(descriptor.getModule()).thenReturn(transformer);
 56
 57        when(pluginAccessor.getEnabledModuleDescriptorsByClass(WebResourceTransformerModuleDescriptor.class)).thenReturn(Arrays.asList(descriptor));
 58        ResourceLocation loc = mock(ResourceLocation.class);
 59        when(loc.getName()).thenReturn("foo.js");
 60
 61        DownloadableResource originalResource = new StringDownloadableResource("resource");
 62        DownloadableResource testResource = trans.transformDownloadableResource(pluginAccessor, originalResource, loc, "");
 63        ByteArrayOutputStream bout = new ByteArrayOutputStream();
 64        testResource.streamResource(bout);
 65
 66        assertEquals("wrong transformation value","my-transformer:resource", new String(bout.toByteArray()));
 67    }
 68
 69    private static class StringDownloadableResource implements DownloadableResource
 70    {
 71        final String value;
 72
 73        public StringDownloadableResource(String value)
 74        {
 75            this.value = value;
 76        }
 77
 78        public boolean isResourceModified(HttpServletRequest request, HttpServletResponse response)
 79        {
 80            return false;
 81        }
 82
 83        public void serveResource(HttpServletRequest request, HttpServletResponse response) throws DownloadException
 84        {
 85
 86        }
 87
 88        public void streamResource(OutputStream out) throws DownloadException
 89        {
 90            try
 91            {
 92                IOUtils.write(value, out);
 93            }
 94            catch (IOException e)
 95            {
 96                e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
 97            }
 98        }
 99
100        public String getContentType()
101        {
102            return "text/plain";
103        }
104    }
105}