PageRenderTime 25ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/atlassian-plugins-webresource/src/test/java/com/atlassian/plugin/webresource/impl/helpers/TestResourceCaching.java

https://bitbucket.org/atlassian/atlassian-plugins-webresource
Java | 153 lines | 126 code | 23 blank | 4 comment | 0 complexity | ed6f6b163a8b90b7180ba74a921e4bdd MD5 | raw file
  1. package com.atlassian.plugin.webresource.impl.helpers;
  2. import com.atlassian.plugin.webresource.impl.RequestCache;
  3. import com.atlassian.plugin.webresource.impl.snapshot.resource.Resource;
  4. import com.atlassian.plugin.webresource.integration.TestCase;
  5. import com.atlassian.plugin.webresource.integration.transformers.AddLocationDimensionAware;
  6. import com.atlassian.plugin.webresource.transformer.instance.RelativeUrlTransformerFactory;
  7. import org.junit.Ignore;
  8. import org.junit.Test;
  9. import java.util.Map;
  10. import static com.atlassian.plugin.webresource.TestUtils.buildMap;
  11. import static com.atlassian.plugin.webresource.impl.helpers.ResourceServingHelpers.buildKey;
  12. import static com.atlassian.plugin.webresource.impl.helpers.ResourceServingHelpers.getResource;
  13. import static com.atlassian.plugin.webresource.impl.support.http.BaseRouter.buildUrl;
  14. import static org.hamcrest.Matchers.equalTo;
  15. import static org.junit.Assert.assertThat;
  16. public class TestResourceCaching extends TestCase {
  17. @Test
  18. public void shouldBuildDistinctCacheKeyForResources() {
  19. wr.configure()
  20. .plugin("plugin")
  21. .webResource("a")
  22. .resource("a1.js", "first", "first/aaa.js")
  23. .resource("a2.js", "second", "second/aaa.js")
  24. .resource("b.js")
  25. .resource("like.png", "like!", "images/like.png")
  26. .end();
  27. RequestCache requestCache = new RequestCache(wr.getGlobals());
  28. Map<String, String> params = buildMap();
  29. Resource a1 = getResource(requestCache, "plugin:a", "a1.js");
  30. Resource a2 = getResource(requestCache, "plugin:a", "a2.js");
  31. Resource b = getResource(requestCache, "plugin:a", "b.js");
  32. Resource like = getResource(requestCache, "plugin:a", "like.png");
  33. assertThat(buildKey(wr.getGlobals(), a1, params), equalTo("plugin:a:first/aaa.js"));
  34. assertThat(buildKey(wr.getGlobals(), a2, params), equalTo("plugin:a:second/aaa.js"));
  35. assertThat(buildKey(wr.getGlobals(), b, params), equalTo("plugin:a:b.js"));
  36. assertThat(buildKey(wr.getGlobals(), like, params), equalTo("plugin:a:images/like.png"));
  37. }
  38. @Test
  39. public void shouldBuildDistinctCacheKeyForIndirectlyReferencedResources() {
  40. wr.configure()
  41. .plugin("plugin")
  42. .webResource("a")
  43. .resource("style.css", "content of style")
  44. .resource("assets/icons/", null, "assets/icons")
  45. .withRelativeFile("like.png")
  46. .withRelativeFile("star.png")
  47. .end();
  48. RequestCache requestCache = new RequestCache(wr.getGlobals());
  49. Map<String, String> params = buildMap();
  50. Resource like = getResource(requestCache, "plugin:a", "assets/icons/like.png");
  51. Resource star = getResource(requestCache, "plugin:a", "assets/icons/star.png");
  52. Resource nonexistent = getResource(requestCache, "plugin:a", "assets/icons/nonexistent.png");
  53. assertThat(
  54. buildKey(wr.getGlobals(), like, params),
  55. equalTo("plugin:a:assets/icons/like.png")
  56. );
  57. assertThat(
  58. buildKey(wr.getGlobals(), star, params),
  59. equalTo("plugin:a:assets/icons/star.png")
  60. );
  61. assertThat(
  62. buildKey(wr.getGlobals(), nonexistent, params),
  63. equalTo("plugin:a:assets/icons/nonexistent.png")
  64. );
  65. }
  66. @Test
  67. public void shouldBuildCacheKeyInSuchWayItIncludesOnlyParametersThatAreActuallyUsed() {
  68. wr.configure()
  69. .plugin("plugin")
  70. .transformer("AddLocation", AddLocationDimensionAware.class)
  71. .webResource("a")
  72. .transformation("js", "AddLocation")
  73. .resource("a.js")
  74. .resource("a.css")
  75. .end();
  76. RequestCache requestCache = new RequestCache(wr.getGlobals());
  77. Resource js = getResource(requestCache, "plugin:a", "a.js");
  78. Resource css = getResource(requestCache, "plugin:a", "a.css");
  79. String fullResourceName = js.getKey() + ":" + js.getName();
  80. // It should return only used parameters if parameters for transformer is known,
  81. Map<String, String> params = buildMap("location", "true", "some", "thing", RelativeUrlTransformerFactory.RELATIVE_URL_QUERY_KEY, "false");
  82. assertThat(
  83. buildKey(wr.getGlobals(), js, params),
  84. equalTo(buildUrl(fullResourceName, buildMap("location", "true", RelativeUrlTransformerFactory.RELATIVE_URL_QUERY_KEY, "false")))
  85. );
  86. // It should only add known parameters if they are present.
  87. params = buildMap("some", "thing");
  88. assertThat(
  89. buildKey(wr.getGlobals(), js, params),
  90. equalTo(buildUrl(fullResourceName, buildMap()))
  91. );
  92. }
  93. @Test
  94. @Ignore("TODO: We need to refactor TransformerCache and how its dimensions are computed to do it by transformer type")
  95. public void shouldOnlyUseParametersInTransformsForThisResourceFileType() {
  96. wr.configure()
  97. .plugin("plugin")
  98. .transformer("AddLocation", AddLocationDimensionAware.class)
  99. .webResource("a")
  100. .transformation("js", "AddLocation")
  101. .resource("a.js")
  102. .resource("a.css")
  103. .end();
  104. RequestCache requestCache = new RequestCache(wr.getGlobals());
  105. Resource js = getResource(requestCache, "plugin:a", "a.js");
  106. Resource css = getResource(requestCache, "plugin:a", "a.css");
  107. String fullResourceName = js.getKey() + ":" + js.getName();
  108. // It should return only used parameters if parameters for transformer is known,
  109. Map<String, String> params = buildMap("location", "true", "some", "thing", RelativeUrlTransformerFactory.RELATIVE_URL_QUERY_KEY, "false");
  110. assertThat(
  111. buildKey(wr.getGlobals(), js, params),
  112. equalTo(buildUrl(fullResourceName, buildMap("location", "true", RelativeUrlTransformerFactory.RELATIVE_URL_QUERY_KEY, "false")))
  113. );
  114. assertThat(
  115. "nothing transforming CSS files, so no params should affect it",
  116. buildKey(wr.getGlobals(), css, params),
  117. equalTo("plugin:a:a.css")
  118. );
  119. // It should only add known parameters if they are present.
  120. params = buildMap("some", "thing");
  121. assertThat(
  122. buildKey(wr.getGlobals(), js, params),
  123. equalTo(buildUrl(fullResourceName, buildMap()))
  124. );
  125. assertThat(
  126. buildKey(wr.getGlobals(), css, params),
  127. equalTo("plugin:a:a.css")
  128. );
  129. }
  130. }