PageRenderTime 35ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/atlassian-plugins-webresource/src/test/java/com/atlassian/plugin/webresource/integration/TestMarkSyncResources.java

https://bitbucket.org/atlassian/atlassian-plugins-webresource
Java | 228 lines | 175 code | 38 blank | 15 comment | 0 complexity | a80e7d41d67e304fba13ccbc2cbaf7df MD5 | raw file
  1. package com.atlassian.plugin.webresource.integration;
  2. import com.atlassian.plugin.webresource.integration.stub.WebResource;
  3. import com.atlassian.webresource.api.assembler.resource.CompleteWebResourceKey;
  4. import org.junit.Before;
  5. import org.junit.Rule;
  6. import org.junit.Test;
  7. import org.junit.contrib.java.lang.system.RestoreSystemProperties;
  8. import org.junit.rules.TestRule;
  9. import java.util.List;
  10. import static com.atlassian.plugin.webresource.integration.stub.WebResource.PathHelpers.superBatchUrl;
  11. import static com.atlassian.plugin.webresource.util.ObjectMatcher.matches;
  12. import static java.util.Arrays.asList;
  13. import static java.util.Collections.emptyList;
  14. import static java.util.Collections.singletonList;
  15. import static org.hamcrest.CoreMatchers.allOf;
  16. import static org.hamcrest.CoreMatchers.not;
  17. import static org.hamcrest.MatcherAssert.assertThat;
  18. import static org.hamcrest.Matchers.emptyString;
  19. import static org.hamcrest.Matchers.is;
  20. public class TestMarkSyncResources extends TestCase {
  21. private WebResource.ConfigurationDsl cfg;
  22. private WebResource.PluginDsl p2;
  23. @Rule
  24. public final TestRule restoreSystemPropertiesRule = new RestoreSystemProperties();
  25. @Before
  26. public void setUp() {
  27. System.setProperty("atlassian.darkfeature.atlassian.webresource.performance.tracking.disable", "true");
  28. cfg = wr.configure();
  29. p2 = cfg.plugin("test.atlassian");
  30. p2.webResource("first-feature")
  31. .context("first")
  32. .context("all")
  33. .resource("first-1.js")
  34. .resource("first-2.js")
  35. .resource("first-1.css")
  36. .resource("first-2.css")
  37. .webResource("second-feature")
  38. .context("second")
  39. .context("all")
  40. .resource("second-1.css")
  41. .resource("second-1.js")
  42. .resource("second-2.css")
  43. .resource("second-2.js")
  44. .webResource("third-feature")
  45. .context("third")
  46. .context("all")
  47. .dependency("test.atlassian:first-feature")
  48. .dependency("test.atlassian:second-feature")
  49. .resource("third-1.css")
  50. .resource("third-1.js")
  51. .end();
  52. }
  53. @Test
  54. public void when_EmptyMarkedSyncResources_then_NoInlineContentWritten() {
  55. cfg.markSyncResources(emptyList());
  56. cfg.end();
  57. wr.requireResource("test.atlassian:first-feature");
  58. final String result = wr.pathsAsHtml();
  59. assertThat(result, allOf(
  60. matches("<link[^\\>]+?first-feature.css"),
  61. not(matches("content of first-1.css", "content of first-2.css")),
  62. matches("<script[^\\>]+?first-feature.js"),
  63. not(matches("content of first-1.js", "content of first-2.js"))
  64. ));
  65. }
  66. @Test
  67. public void when_NonEmptyMarkedSyncResources_then_InlineContentWritten() {
  68. cfg.markSyncResources(singletonList(
  69. new CompleteWebResourceKey("test.atlassian", "first-feature")
  70. ));
  71. cfg.end();
  72. wr.requireResource("test.atlassian:first-feature");
  73. final String result = wr.pathsAsHtml();
  74. assertThat(result, allOf(
  75. // @todo SPFE-418 inline CSS
  76. // not(matches("<link[^\\>]+?first-feature.css")),
  77. // matches("content of first-1.css", "content of first-2.css"),
  78. not(matches("<script[^\\>]+?first-feature.js")),
  79. matches("content of first-1.js", "content of first-2.js")
  80. ));
  81. }
  82. @Test
  83. public void when_NonEmptyMarkedSyncResourcesButNotExplicitlyRequired_then_InlineContentStillWritten() {
  84. cfg.markSyncResources(singletonList(
  85. new CompleteWebResourceKey("test.atlassian", "first-feature")
  86. ));
  87. cfg.end();
  88. wr.requireResource("test.atlassian:second-feature");
  89. final String result = wr.pathsAsHtml();
  90. assertThat(result, allOf(
  91. // @todo SPFE-418 inline CSS
  92. // not(matches("<link[^\\>]+?first-feature.css")),
  93. // matches("content of first-1.css", "content of first-2.css"),
  94. not(matches("<script[^\\>]+?first-feature.js")),
  95. matches("content of first-1.js", "content of first-2.js"),
  96. matches("<link[^\\>]+?second-feature.css"),
  97. matches("<script[^\\>]+?second-feature.js")
  98. ));
  99. }
  100. @Test
  101. public void when_MarkedSyncResourcesHaveTransitiveDependencies_then_AllTransitiveResourcesWrittenAsInlineContent() {
  102. cfg.markSyncResources(singletonList(
  103. new CompleteWebResourceKey("test.atlassian", "third-feature")
  104. ));
  105. cfg.end();
  106. wr.requireResource("test.atlassian:third-feature");
  107. final String result = wr.pathsAsHtml();
  108. assertThat(result, allOf(
  109. // @todo SPFE-418 inline CSS
  110. // matches("content of first-1.css", "content of first-2.css", ...),
  111. matches("content of first-1.js", "content of first-2.js", "content of second-1.js", "content of second-2.js", "content of third-1.js")
  112. ));
  113. }
  114. @Test
  115. public void when_NonexistentResourcesMarkedAsSync_then_NoInlineContentWritten() {
  116. cfg.markSyncResources(asList(
  117. new CompleteWebResourceKey("plugin-which-does-not-exist", "web-resource-key"),
  118. new CompleteWebResourceKey("test.atlassian", "incorrect-key"),
  119. new CompleteWebResourceKey("test.atlassian", "also-nope")
  120. ));
  121. cfg.end();
  122. wr.requireResource("test.atlassian:second-feature");
  123. final String result = wr.pathsAsHtml();
  124. assertThat(result, allOf(
  125. // @todo SPFE-418 inline CSS
  126. // not(matches("<style>.+?</style>")),
  127. not(matches("<script>.+?</script>"))
  128. ));
  129. }
  130. @Test
  131. public void when_MarkedSyncResourcesAreEquivalentOfSuperbatch_then_NoSuperbatchWritten() {
  132. cfg.markSyncResources(singletonList(
  133. new CompleteWebResourceKey("test.atlassian", "first-feature")
  134. ));
  135. cfg.addToSuperbatch("test.atlassian:first-feature");
  136. cfg.end();
  137. wr.requireResource("test.atlassian:first-feature");
  138. final String result = wr.pathsAsHtml();
  139. assertThat(result, allOf(
  140. // @todo SPFE-418 inline CSS
  141. // matches("content of first-1.css", "content of first-2.css", ...),
  142. matches("<script>", "content of first-1.js", "content of first-2.js", "</script>"),
  143. not(matches("<script[^\\>]+?first-feature.js")),
  144. not(matches("<script[^\\>]+?super/batch.js"))
  145. ));
  146. }
  147. @Test
  148. public void when_MarkedSyncResourcesAreSubsetOfSuperbatch_then_overlappingContentNotPresentInSuperbatch() {
  149. cfg.markSyncResources(singletonList(
  150. new CompleteWebResourceKey("test.atlassian", "first-feature")
  151. ));
  152. cfg.addToSuperbatch("test.atlassian:third-feature");
  153. cfg.end();
  154. wr.requireResource("test.atlassian:first-feature");
  155. final String superbatchJsContent = wr.getContent(superBatchUrl("js"));
  156. assertThat(superbatchJsContent, not(matches("content of first-1.js")));
  157. final String superbatchCssContent = wr.getContent(superBatchUrl("css"));
  158. assertThat(superbatchCssContent, not(matches("content of first-1.css")));
  159. }
  160. @Test
  161. public void when_MarkedSyncResourcesAndSuperbatchDoNotOverlap_then_undefinedBehaviour() {
  162. cfg.markSyncResources(singletonList(
  163. new CompleteWebResourceKey("test.atlassian", "first-feature")
  164. ));
  165. cfg.addToSuperbatch("test.atlassian:second-feature");
  166. cfg.end();
  167. wr.requireResource("test.atlassian:second-feature");
  168. final List<String> urls = wr.paths();
  169. // The first "URL" is for the sync resource(s)
  170. assertThat(urls.get(0), is(emptyString()));
  171. // note: The URL contains -_sync, but doesn't have to. Could be optimised.
  172. assertThat(urls.get(1), allOf(
  173. matches("-_sync"),
  174. matches("_super")
  175. ));
  176. }
  177. @Test
  178. public void given_emptySyncResources_when_otherContentRequested_then_syncNotSubtracted() {
  179. cfg.markSyncResources(emptyList());
  180. cfg.addToSuperbatch("test.atlassian:second-feature");
  181. cfg.end();
  182. wr.requireResource("test.atlassian:second-feature");
  183. final List<String> urls = wr.paths();
  184. // The first "URL" is for the sync resource(s)
  185. assertThat(urls.get(0), allOf(
  186. not(matches("-_sync")),
  187. matches("_super")
  188. ));
  189. }
  190. }