/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

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