PageRenderTime 44ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

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

https://bitbucket.org/atlassian/atlassian-plugins-webresource
Java | 303 lines | 236 code | 44 blank | 23 comment | 0 complexity | 060595f385d15e0b603f5348faa0d995 MD5 | raw file
  1. package com.atlassian.plugin.webresource.integration.special;
  2. import com.atlassian.plugin.webresource.integration.TestCase;
  3. import com.atlassian.plugin.webresource.integration.stub.WebResource;
  4. import com.atlassian.plugin.webresource.util.DuplicateContentMatcher;
  5. import com.atlassian.webresource.api.assembler.resource.CompleteWebResourceKey;
  6. import com.atlassian.webresource.api.assembler.resource.ResourcePhase;
  7. import org.junit.Before;
  8. import org.junit.Rule;
  9. import org.junit.Test;
  10. import org.junit.contrib.java.lang.system.RestoreSystemProperties;
  11. import org.junit.rules.TestRule;
  12. import java.util.List;
  13. import static com.atlassian.plugin.webresource.integration.stub.WebResource.PathHelpers.superBatchUrl;
  14. import static com.atlassian.plugin.webresource.util.DuplicateContentMatcher.doesNotDuplicateAnyContents;
  15. import static com.atlassian.plugin.webresource.util.ObjectMatcher.matches;
  16. import static com.atlassian.webresource.api.assembler.resource.ResourcePhase.INLINE;
  17. import static com.atlassian.webresource.api.assembler.resource.ResourcePhase.INTERACTION;
  18. import static com.atlassian.webresource.api.assembler.resource.ResourcePhase.REQUIRE;
  19. import static java.util.Arrays.asList;
  20. import static java.util.Collections.emptyList;
  21. import static java.util.Collections.singletonList;
  22. import static org.hamcrest.CoreMatchers.allOf;
  23. import static org.hamcrest.CoreMatchers.not;
  24. import static org.hamcrest.CoreMatchers.startsWith;
  25. import static org.hamcrest.MatcherAssert.assertThat;
  26. import static org.hamcrest.Matchers.contains;
  27. import static org.hamcrest.Matchers.containsString;
  28. public class TestSyncbatchAcrossPhases extends TestCase {
  29. @Rule
  30. public final TestRule restoreSystemPropertiesRule = new RestoreSystemProperties();
  31. private WebResource.ConfigurationDsl cfg;
  32. @Before
  33. public void setUp() {
  34. System.setProperty("atlassian.darkfeature.atlassian.webresource.performance.tracking.disable", "true");
  35. cfg = wr.configure();
  36. cfg
  37. .plugin("test.atlassian")
  38. .webResource("first-feature")
  39. .context("first")
  40. .context("all")
  41. .resource("first-1.js")
  42. .resource("first-2.js")
  43. .resource("first-1.css")
  44. .resource("first-2.css")
  45. .webResource("second-feature")
  46. .context("second")
  47. .context("all")
  48. .resource("second-1.css")
  49. .resource("second-1.js")
  50. .resource("second-2.css")
  51. .resource("second-2.js")
  52. .webResource("third-feature")
  53. .context("third")
  54. .context("all")
  55. .dependency("test.atlassian:first-feature")
  56. .dependency("test.atlassian:second-feature")
  57. .resource("third-1.css")
  58. .resource("third-1.js")
  59. .end();
  60. }
  61. @Test
  62. public void when_EmptyMarkedSyncResources_then_NoInlineContentWritten() {
  63. cfg.addToSyncbatch(emptyList());
  64. cfg.end();
  65. wr.requireResource("test.atlassian:first-feature");
  66. final WebResource.DrainResult result = wr.drain();
  67. assertThat(result.html(), allOf(
  68. matches("<link[^\\>]+?first-feature.css"),
  69. not(matches("content of first-1.css", "content of first-2.css")),
  70. matches("<script[^\\>]+?first-feature.js"),
  71. not(matches("content of first-1.js", "content of first-2.js"))
  72. ));
  73. }
  74. @Test
  75. public void when_NonEmptyMarkedSyncResources_then_InlineContentWritten() {
  76. cfg.addToSyncbatch("test.atlassian:first-feature");
  77. cfg.end();
  78. wr.requireResource("test.atlassian:first-feature");
  79. final WebResource.DrainResult result = wr.drain();
  80. assertThat(result.html(), allOf(
  81. // todo PLUGWEB-545 add CSS support for inline phase
  82. // not(matches("<link[^\\>]+?first-feature.css")),
  83. // matches("content of first-1.css", "content of first-2.css"),
  84. not(matches("<script[^\\>]+?first-feature.js")),
  85. matches("content of first-1.js", "content of first-2.js")
  86. ));
  87. }
  88. @Test
  89. public void when_NonEmptyMarkedSyncResourcesButNotExplicitlyRequired_then_InlineContentStillWritten() {
  90. cfg.addToSyncbatch("test.atlassian:first-feature");
  91. cfg.end();
  92. wr.requireResource("test.atlassian:second-feature");
  93. final WebResource.DrainResult result = wr.drain();
  94. assertThat(result.html(), allOf(
  95. // todo PLUGWEB-545 add CSS support for inline phase
  96. // not(matches("<link[^\\>]+?first-feature.css")),
  97. // matches("content of first-1.css", "content of first-2.css"),
  98. not(matches("<script[^\\>]+?first-feature.js")),
  99. matches("content of first-1.js", "content of first-2.js"),
  100. matches("<link[^\\>]+?second-feature.css"),
  101. matches("<script[^\\>]+?second-feature.js")
  102. ));
  103. }
  104. @Test
  105. public void when_MarkedSyncResourcesHaveTransitiveDependencies_then_AllTransitiveResourcesWrittenAsInlineContent() {
  106. cfg.addToSyncbatch("test.atlassian:third-feature");
  107. cfg.end();
  108. wr.requireResource("test.atlassian:third-feature");
  109. final WebResource.DrainResult result = wr.drain();
  110. assertThat(result.html(), allOf(
  111. // todo PLUGWEB-545 add CSS support for inline phase
  112. // matches("content of first-1.css", "content of first-2.css", ...),
  113. 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")
  114. ));
  115. }
  116. @Test
  117. public void when_NonexistentResourcesMarkedAsSync_then_NoInlineContentWritten() {
  118. cfg.addToSyncbatch(
  119. "plugin-which-does-not-exist:web-resource-key",
  120. "test.atlassian:incorrect-key",
  121. "test.atlassian:also-nope"
  122. );
  123. cfg.end();
  124. wr.requireResource("test.atlassian:second-feature");
  125. final WebResource.DrainResult result = wr.drain();
  126. assertThat(result.html(), allOf(
  127. // todo PLUGWEB-545 add CSS support for inline phase
  128. // not(matches("<style>.+?</style>")),
  129. not(matches("<script[^>]*?>.+?</script>"))
  130. ));
  131. }
  132. @Test
  133. public void when_MarkedSyncResourcesAreEquivalentOfSuperbatch_then_NoSuperbatchWritten() {
  134. cfg.addToSyncbatch("test.atlassian:first-feature");
  135. cfg.addToSuperbatch("test.atlassian:first-feature");
  136. cfg.end();
  137. wr.requireResource("test.atlassian:first-feature");
  138. final WebResource.DrainResult result = wr.drain();
  139. assertThat(result.html(), allOf(
  140. // todo PLUGWEB-545 add CSS support for inline phase
  141. // matches("content of first-1.css", "content of first-2.css", ...),
  142. matches("content of first-1.js", "content of first-2.js"),
  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.addToSyncbatch("test.atlassian:first-feature");
  150. cfg.addToSuperbatch("test.atlassian:third-feature");
  151. cfg.end();
  152. final String superbatchJsContent = wr.getContent(superBatchUrl("js"));
  153. assertThat(superbatchJsContent, not(matches("content of first-1.js")));
  154. final String superbatchCssContent = wr.getContent(superBatchUrl("css"));
  155. assertThat(superbatchCssContent, not(matches("content of first-1.css")));
  156. }
  157. @Test
  158. public void when_MarkedSyncResourcesAndSuperbatchDoNotOverlap_then_undefinedBehaviour() {
  159. cfg.addToSyncbatch("test.atlassian:first-feature");
  160. cfg.addToSuperbatch("test.atlassian:second-feature");
  161. cfg.end();
  162. wr.requireResource("test.atlassian:second-feature");
  163. final List<String> tags = wr.drain().tags();
  164. // The first tag is for the sync resource(s)
  165. assertThat(tags, contains(
  166. startsWith("<script"),
  167. // note: The URLs contain -_sync, but don't have to. Could be optimised.
  168. allOf(
  169. startsWith("<link"),
  170. matches("-_sync"),
  171. matches("_super")
  172. ),
  173. allOf(
  174. startsWith("<script"),
  175. matches("-_sync"),
  176. matches("_super")
  177. )
  178. ));
  179. }
  180. @Test
  181. public void given_emptySyncResources_when_otherContentRequested_then_syncNotSubtracted() {
  182. cfg.addToSyncbatch(emptyList());
  183. cfg.addToSuperbatch("test.atlassian:second-feature");
  184. cfg.end();
  185. wr.requireResource("test.atlassian:second-feature");
  186. final List<String> tags = wr.drain().tags();
  187. assertThat(tags, contains(
  188. allOf(
  189. startsWith("<link"),
  190. not(matches("-_sync")),
  191. matches("_super")
  192. ),
  193. allOf(
  194. startsWith("<script"),
  195. not(matches("-_sync")),
  196. matches("_super")
  197. )
  198. ));
  199. }
  200. @Test
  201. public void given_SyncResourcesContainsFirstFeature_when_secondFeatureRequestedInline_then_ContentNotDuplicated() {
  202. // given
  203. cfg.addToSyncbatch("test.atlassian:first-feature");
  204. cfg.end();
  205. // when
  206. wr.getPageBuilderService().assembler().resources().requireWebResource(INLINE, "test.atlassian:second-feature");
  207. wr.getPageBuilderService().assembler().resources().requireWebResource(REQUIRE, "test.atlassian:second-feature");
  208. wr.getPageBuilderService().assembler().resources().requireWebResource(INTERACTION, "test.atlassian:second-feature");
  209. final String html = wr.drain().html();
  210. // then
  211. assertThat(html, allOf(
  212. doesNotDuplicateAnyContents(),
  213. containsString("content of first-1.js"),
  214. containsString("content of first-2.js"),
  215. containsString("content of second-1.js"),
  216. containsString("content of second-2.js"),
  217. containsString("test.atlassian:second-feature"),
  218. not(containsString("WRM.requireLazily"))
  219. ));
  220. }
  221. @Test
  222. public void given_SyncResourcesContainsSecondFeature_when_secondFeatureRequestedInline_then_ContentNotDuplicated() {
  223. // given
  224. cfg.addToSyncbatch("test.atlassian:second-feature");
  225. cfg.end();
  226. // when
  227. wr.getPageBuilderService().assembler().resources().requireWebResource(INLINE, "test.atlassian:second-feature");
  228. wr.getPageBuilderService().assembler().resources().requireWebResource(REQUIRE, "test.atlassian:second-feature");
  229. wr.getPageBuilderService().assembler().resources().requireWebResource(INTERACTION, "test.atlassian:second-feature");
  230. final String html = wr.drain().html();
  231. // then
  232. assertThat(html, allOf(
  233. doesNotDuplicateAnyContents(),
  234. containsString("content of second-1.js"),
  235. containsString("content of second-2.js"),
  236. not(containsString("test.atlassian:second-feature")),
  237. not(containsString("WRM.requireLazily"))
  238. ));
  239. }
  240. @Test
  241. public void given_SyncResourcesContainsThirdFeature_when_secondFeatureRequestedInline_then_ContentNotDuplicated() {
  242. // given
  243. cfg.addToSyncbatch("test.atlassian:third-feature");
  244. cfg.end();
  245. // when
  246. wr.getPageBuilderService().assembler().resources().requireWebResource(INLINE, "test.atlassian:second-feature");
  247. wr.getPageBuilderService().assembler().resources().requireWebResource(REQUIRE, "test.atlassian:second-feature");
  248. wr.getPageBuilderService().assembler().resources().requireWebResource(INTERACTION, "test.atlassian:second-feature");
  249. final String html = wr.drain().html();
  250. // then
  251. assertThat(html, allOf(
  252. doesNotDuplicateAnyContents(),
  253. containsString("content of second-1.js"),
  254. containsString("content of second-2.js"),
  255. not(containsString("test.atlassian:second-feature")),
  256. not(containsString("WRM.requireLazily"))
  257. ));
  258. }
  259. }