PageRenderTime 51ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

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

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