/atlassian-plugins-webresource-common/src/test/java/com/atlassian/plugin/servlet/TestAbstractDownloadableResource.java

https://bitbucket.org/purewind/atlassian-plugins · Java · 282 lines · 207 code · 59 blank · 16 comment · 3 complexity · 3d37fbd8621bc86022ef37ae9493d9ea MD5 · raw file

  1. package com.atlassian.plugin.servlet;
  2. import com.atlassian.plugin.elements.ResourceLocation;
  3. import com.atlassian.plugin.servlet.util.CapturingHttpServletResponse;
  4. import com.atlassian.plugin.util.PluginUtils;
  5. import com.google.common.collect.ImmutableMap;
  6. import junit.framework.AssertionFailedError;
  7. import junit.framework.TestCase;
  8. import java.io.ByteArrayInputStream;
  9. import java.io.ByteArrayOutputStream;
  10. import java.io.InputStream;
  11. import java.util.Map;
  12. /**
  13. * A test for AbstractDownloadableResource
  14. */
  15. public class TestAbstractDownloadableResource extends TestCase {
  16. private static final String MINIFIED_CONTENT = "minified content";
  17. private static final String PLAIN_CONTENT = "plain content";
  18. private static final String NEVER_MINIFIED_CONTENT = "never minified content";
  19. private static final Map<String, String> EMPTY_PARAMS = ImmutableMap.of();
  20. private class MinifiedFileServingDownloadableResource extends AbstractDownloadableResource {
  21. public MinifiedFileServingDownloadableResource(final ResourceLocation resourceLocation) {
  22. super(null, resourceLocation, null);
  23. }
  24. @Override
  25. protected String getLocation() {
  26. return "somecode.js";
  27. }
  28. @Override
  29. public String getContentType() {
  30. return "minified/content";
  31. }
  32. @Override
  33. protected InputStream getResourceAsStream(final String resourceLocation) {
  34. if (resourceLocation.contains("-min.")) {
  35. assertEquals("somecode-min.js", resourceLocation);
  36. return newStream(MINIFIED_CONTENT);
  37. }
  38. assertEquals("somecode.js", resourceLocation);
  39. return newStream(PLAIN_CONTENT);
  40. }
  41. private InputStream newStream(final String s) {
  42. return new ByteArrayInputStream(s.getBytes());
  43. }
  44. }
  45. private class MyDownloadableResource extends AbstractDownloadableResource {
  46. private String passedResourceLocation;
  47. public MyDownloadableResource(final ResourceLocation resourceLocation, final boolean disableMinification) {
  48. super(null, resourceLocation, "", disableMinification);
  49. }
  50. @Override
  51. protected InputStream getResourceAsStream(final String resourceLocation) {
  52. passedResourceLocation = resourceLocation;
  53. return newStream(resourceLocation);
  54. }
  55. private InputStream newStream(final String s) {
  56. return new ByteArrayInputStream(s.getBytes());
  57. }
  58. }
  59. private class NotMinifiedFileServingDownloadableResouce extends AbstractDownloadableResource {
  60. public NotMinifiedFileServingDownloadableResouce() {
  61. super(null, new ResourceLocation("/flintstone/fred.jpg", "fred.jpg", "stuff", "stuff", "stuff", EMPTY_PARAMS), null);
  62. }
  63. @Override
  64. protected String getLocation() {
  65. return "somemorecode.js";
  66. }
  67. @Override
  68. public String getContentType() {
  69. return "plain/content";
  70. }
  71. @Override
  72. protected InputStream getResourceAsStream(final String resourceLocation) {
  73. if (resourceLocation.contains("-min.")) {
  74. assertEquals("somemorecode-min.js", resourceLocation);
  75. return null;
  76. }
  77. assertEquals("somemorecode.js", resourceLocation);
  78. return newStream(PLAIN_CONTENT);
  79. }
  80. private InputStream newStream(final String s) {
  81. return new ByteArrayInputStream(s.getBytes());
  82. }
  83. }
  84. private class NeverMinifiedFileServingDownloadableResource extends AbstractDownloadableResource {
  85. public NeverMinifiedFileServingDownloadableResource() {
  86. super(null, new ResourceLocation("/flintstone/fred.jpg", "fred.jpg", "stuff", "stuff", "stuff", EMPTY_PARAMS), null);
  87. }
  88. @Override
  89. protected String getLocation() {
  90. return "neverminified.js";
  91. }
  92. @Override
  93. public String getContentType() {
  94. return "neverminified/content";
  95. }
  96. @Override
  97. protected InputStream getResourceAsStream(final String resourceLocation) {
  98. if (resourceLocation.contains("-min.")) {
  99. fail("it should never ask for this");
  100. }
  101. assertEquals("neverminified.js", resourceLocation);
  102. return newStream(NEVER_MINIFIED_CONTENT);
  103. }
  104. private InputStream newStream(final String s) {
  105. return new ByteArrayInputStream(s.getBytes());
  106. }
  107. }
  108. @Override
  109. public void setUp() throws Exception {
  110. super.setUp();
  111. System.setProperty("atlassian.webresource.disable.minification", "false");
  112. System.setProperty("atlassian.dev.mode", "false");
  113. }
  114. public void testMinificationStrategyWrongFileType() throws Exception {
  115. final ResourceLocation resourceLocation = new ResourceLocation("/flintstone/fred.jpg", "fred.jpg", "stuff", "stuff", "stuff", EMPTY_PARAMS);
  116. final MyDownloadableResource myDownloadableResource = new MyDownloadableResource(resourceLocation, false);
  117. myDownloadableResource.streamResource(new ByteArrayOutputStream());
  118. assertEquals("/flintstone/fred.jpg", myDownloadableResource.passedResourceLocation);
  119. }
  120. public void testMinificationStrategyCss() throws Exception {
  121. final ResourceLocation resourceLocation = new ResourceLocation("/flintstone/fred.css", "fred.css", "stuff", "stuff", "stuff", EMPTY_PARAMS);
  122. final MyDownloadableResource myDownloadableResource = new MyDownloadableResource(resourceLocation, false);
  123. myDownloadableResource.streamResource(new ByteArrayOutputStream());
  124. assertEquals("/flintstone/fred-min.css", myDownloadableResource.passedResourceLocation);
  125. }
  126. public void testMinificationStrategyWithTwoMinsInName() throws Exception {
  127. final ResourceLocation resourceLocation = new ResourceLocation("/flintstone-min./fred-min.js", "fred-min.js", "stuff", "stuff", "stuff", EMPTY_PARAMS);
  128. final MyDownloadableResource myDownloadableResource = new MyDownloadableResource(resourceLocation, false);
  129. myDownloadableResource.streamResource(new ByteArrayOutputStream());
  130. assertEquals("/flintstone-min./fred-min.js", myDownloadableResource.passedResourceLocation);
  131. }
  132. public void testMinificationStrategyAlreadyMinimised() throws Exception {
  133. final ResourceLocation resourceLocation = new ResourceLocation("/flintstone/fred-min.js", "fred-min.js", "stuff", "stuff", "stuff", EMPTY_PARAMS);
  134. final MyDownloadableResource myDownloadableResource = new MyDownloadableResource(resourceLocation, false);
  135. myDownloadableResource.streamResource(new ByteArrayOutputStream());
  136. assertEquals("/flintstone/fred-min.js", myDownloadableResource.passedResourceLocation);
  137. }
  138. public void testMinificationStrategyNotMinimisedAndEnabled() throws Exception {
  139. final ResourceLocation resourceLocation = new ResourceLocation("/flintstone/fred.js", "fred.js", "stuff", "stuff", "stuff", EMPTY_PARAMS);
  140. final MyDownloadableResource myDownloadableResource = new MyDownloadableResource(resourceLocation, false);
  141. myDownloadableResource.streamResource(new ByteArrayOutputStream());
  142. assertEquals("/flintstone/fred-min.js", myDownloadableResource.passedResourceLocation);
  143. }
  144. public void testMinificationStrategyNotMinimisedAndDisabled() throws Exception {
  145. final ResourceLocation resourceLocation = new ResourceLocation("/flintstone/fred.js", "fred.js", "stuff", "stuff", "stuff", EMPTY_PARAMS);
  146. final MyDownloadableResource myDownloadableResource = new MyDownloadableResource(resourceLocation, true);
  147. myDownloadableResource.streamResource(new ByteArrayOutputStream());
  148. assertEquals("/flintstone/fred.js", myDownloadableResource.passedResourceLocation);
  149. }
  150. public void testMinificationStrategyNotMinimisedAndSystemDisabled() throws Exception {
  151. System.setProperty("atlassian.webresource.disable.minification", "true");
  152. final ResourceLocation resourceLocation = new ResourceLocation("/flintstone/fred.js", "fred.js", "stuff", "stuff", "stuff", EMPTY_PARAMS);
  153. final MyDownloadableResource myDownloadableResource = new MyDownloadableResource(resourceLocation, false);
  154. myDownloadableResource.streamResource(new ByteArrayOutputStream());
  155. assertEquals("/flintstone/fred.js", myDownloadableResource.passedResourceLocation);
  156. }
  157. public void testMinificationStrategyNotMinimisedAndSystemEnabled() throws Exception {
  158. System.setProperty("atlassian.webresource.disable.minification", "false");
  159. final ResourceLocation resourceLocation = new ResourceLocation("/flintstone/fred.js", "fred.js", "stuff", "stuff", "stuff", EMPTY_PARAMS);
  160. final MyDownloadableResource myDownloadableResource = new MyDownloadableResource(resourceLocation, false);
  161. myDownloadableResource.streamResource(new ByteArrayOutputStream());
  162. assertEquals("/flintstone/fred-min.js", myDownloadableResource.passedResourceLocation);
  163. }
  164. public void testWithMinifiedStrategyInPlay() throws DownloadException {
  165. // it should ask for -min files first and in this case get content back
  166. final MinifiedFileServingDownloadableResource minifiedFileServingDownloadableResource = new MinifiedFileServingDownloadableResource(new ResourceLocation("/flintstone/fred.jpg", "fred.jpg", "stuff", "stuff", "stuff", EMPTY_PARAMS));
  167. assertContent(minifiedFileServingDownloadableResource, MINIFIED_CONTENT);
  168. // it should ask for -min files first but get null and hence move on to the plain old content case.
  169. final NotMinifiedFileServingDownloadableResouce notMinifiedFileServingDownloadableResource = new NotMinifiedFileServingDownloadableResouce();
  170. assertContent(notMinifiedFileServingDownloadableResource, PLAIN_CONTENT);
  171. }
  172. public void testWhenSystemPropertyIsSet() throws DownloadException {
  173. verifySystemPropertyRespected("atlassian.webresource.disable.minification");
  174. }
  175. public void testWhenDevModeSystemPropertyIsSet() throws DownloadException {
  176. verifySystemPropertyRespected(PluginUtils.ATLASSIAN_DEV_MODE);
  177. }
  178. private void verifySystemPropertyRespected(String sysprop)
  179. throws DownloadException {
  180. try {
  181. System.setProperty(sysprop, "true");
  182. // now in this case it must never ask for minified files. This class used will assert that.
  183. final NeverMinifiedFileServingDownloadableResource neverMinifiedFileServingDownloadableResource = new NeverMinifiedFileServingDownloadableResource();
  184. assertContent(neverMinifiedFileServingDownloadableResource, NEVER_MINIFIED_CONTENT);
  185. final MinifiedFileServingDownloadableResource minifiedFileServingDownloadableResource = new MinifiedFileServingDownloadableResource(new ResourceLocation("/flintstone/fred.jpg", "fred.jpg", "stuff", "stuff", "stuff", EMPTY_PARAMS));
  186. assertContent(minifiedFileServingDownloadableResource, PLAIN_CONTENT);
  187. // it should ask for -min files first but get null and hence move on to the plain old content case.
  188. final NotMinifiedFileServingDownloadableResouce notMinifiedFileServingDownloadableResource = new NotMinifiedFileServingDownloadableResouce();
  189. assertContent(notMinifiedFileServingDownloadableResource, PLAIN_CONTENT);
  190. System.setProperty(sysprop, "false");
  191. // it should ask for -min files first and in this case get content back
  192. assertContent(minifiedFileServingDownloadableResource, MINIFIED_CONTENT);
  193. // it should ask for -min files first but get null and hence move on to the plain old content case.
  194. assertContent(notMinifiedFileServingDownloadableResource, PLAIN_CONTENT);
  195. //
  196. // now this test is wierd but hey. If I call back on a never minified resource class object it should
  197. // throw an assertion exception that it doesnt expect it. This proves that it odes indeed get called
  198. // with a -min version of itself
  199. try {
  200. assertContent(neverMinifiedFileServingDownloadableResource, "doesnt matter");
  201. fail("This should have barfed in NeverMinifiedFileServingDownloadableResource");
  202. } catch (final AssertionFailedError expected) {
  203. // this is expected since the test class asserts tgat a -min file should never be called on it.
  204. // and hence by inference the atlassian.webresource.disable.minification property is not taking effect
  205. }
  206. } finally {
  207. // reset for this test
  208. System.setProperty(sysprop, "false");
  209. }
  210. }
  211. private void assertContent(final AbstractDownloadableResource downloadableResource, final String content) throws DownloadException {
  212. final CapturingHttpServletResponse httpServletResponse = new CapturingHttpServletResponse();
  213. final ByteArrayOutputStream baos = new ByteArrayOutputStream();
  214. try {
  215. downloadableResource.serveResource(null, httpServletResponse);
  216. } catch (final DownloadException e) {
  217. throw new RuntimeException(e);
  218. }
  219. downloadableResource.streamResource(baos);
  220. assertEquals(content, httpServletResponse.toString());
  221. assertEquals(content, baos.toString());
  222. }
  223. }