/atlassian-plugins-webresource/src/test/java/com/atlassian/plugin/webresource/integration/TestMarkSyncResources.java
Java | 228 lines | 175 code | 38 blank | 15 comment | 0 complexity | a80e7d41d67e304fba13ccbc2cbaf7df MD5 | raw file
- package com.atlassian.plugin.webresource.integration;
- import com.atlassian.plugin.webresource.integration.stub.WebResource;
- import com.atlassian.webresource.api.assembler.resource.CompleteWebResourceKey;
- import org.junit.Before;
- import org.junit.Rule;
- import org.junit.Test;
- import org.junit.contrib.java.lang.system.RestoreSystemProperties;
- import org.junit.rules.TestRule;
- import java.util.List;
- import static com.atlassian.plugin.webresource.integration.stub.WebResource.PathHelpers.superBatchUrl;
- import static com.atlassian.plugin.webresource.util.ObjectMatcher.matches;
- import static java.util.Arrays.asList;
- import static java.util.Collections.emptyList;
- import static java.util.Collections.singletonList;
- import static org.hamcrest.CoreMatchers.allOf;
- import static org.hamcrest.CoreMatchers.not;
- import static org.hamcrest.MatcherAssert.assertThat;
- import static org.hamcrest.Matchers.emptyString;
- import static org.hamcrest.Matchers.is;
- public class TestMarkSyncResources extends TestCase {
- private WebResource.ConfigurationDsl cfg;
- private WebResource.PluginDsl p2;
- @Rule
- public final TestRule restoreSystemPropertiesRule = new RestoreSystemProperties();
- @Before
- public void setUp() {
- System.setProperty("atlassian.darkfeature.atlassian.webresource.performance.tracking.disable", "true");
- cfg = wr.configure();
- p2 = cfg.plugin("test.atlassian");
- p2.webResource("first-feature")
- .context("first")
- .context("all")
- .resource("first-1.js")
- .resource("first-2.js")
- .resource("first-1.css")
- .resource("first-2.css")
- .webResource("second-feature")
- .context("second")
- .context("all")
- .resource("second-1.css")
- .resource("second-1.js")
- .resource("second-2.css")
- .resource("second-2.js")
- .webResource("third-feature")
- .context("third")
- .context("all")
- .dependency("test.atlassian:first-feature")
- .dependency("test.atlassian:second-feature")
- .resource("third-1.css")
- .resource("third-1.js")
- .end();
- }
- @Test
- public void when_EmptyMarkedSyncResources_then_NoInlineContentWritten() {
- cfg.markSyncResources(emptyList());
- cfg.end();
- wr.requireResource("test.atlassian:first-feature");
- final String result = wr.pathsAsHtml();
- assertThat(result, allOf(
- matches("<link[^\\>]+?first-feature.css"),
- not(matches("content of first-1.css", "content of first-2.css")),
- matches("<script[^\\>]+?first-feature.js"),
- not(matches("content of first-1.js", "content of first-2.js"))
- ));
- }
- @Test
- public void when_NonEmptyMarkedSyncResources_then_InlineContentWritten() {
- cfg.markSyncResources(singletonList(
- new CompleteWebResourceKey("test.atlassian", "first-feature")
- ));
- cfg.end();
- wr.requireResource("test.atlassian:first-feature");
- final String result = wr.pathsAsHtml();
- assertThat(result, allOf(
- // @todo SPFE-418 inline CSS
- // not(matches("<link[^\\>]+?first-feature.css")),
- // matches("content of first-1.css", "content of first-2.css"),
- not(matches("<script[^\\>]+?first-feature.js")),
- matches("content of first-1.js", "content of first-2.js")
- ));
- }
- @Test
- public void when_NonEmptyMarkedSyncResourcesButNotExplicitlyRequired_then_InlineContentStillWritten() {
- cfg.markSyncResources(singletonList(
- new CompleteWebResourceKey("test.atlassian", "first-feature")
- ));
- cfg.end();
- wr.requireResource("test.atlassian:second-feature");
- final String result = wr.pathsAsHtml();
- assertThat(result, allOf(
- // @todo SPFE-418 inline CSS
- // not(matches("<link[^\\>]+?first-feature.css")),
- // matches("content of first-1.css", "content of first-2.css"),
- not(matches("<script[^\\>]+?first-feature.js")),
- matches("content of first-1.js", "content of first-2.js"),
- matches("<link[^\\>]+?second-feature.css"),
- matches("<script[^\\>]+?second-feature.js")
- ));
- }
- @Test
- public void when_MarkedSyncResourcesHaveTransitiveDependencies_then_AllTransitiveResourcesWrittenAsInlineContent() {
- cfg.markSyncResources(singletonList(
- new CompleteWebResourceKey("test.atlassian", "third-feature")
- ));
- cfg.end();
- wr.requireResource("test.atlassian:third-feature");
- final String result = wr.pathsAsHtml();
- assertThat(result, allOf(
- // @todo SPFE-418 inline CSS
- // matches("content of first-1.css", "content of first-2.css", ...),
- 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")
- ));
- }
- @Test
- public void when_NonexistentResourcesMarkedAsSync_then_NoInlineContentWritten() {
- cfg.markSyncResources(asList(
- new CompleteWebResourceKey("plugin-which-does-not-exist", "web-resource-key"),
- new CompleteWebResourceKey("test.atlassian", "incorrect-key"),
- new CompleteWebResourceKey("test.atlassian", "also-nope")
- ));
- cfg.end();
- wr.requireResource("test.atlassian:second-feature");
- final String result = wr.pathsAsHtml();
- assertThat(result, allOf(
- // @todo SPFE-418 inline CSS
- // not(matches("<style>.+?</style>")),
- not(matches("<script>.+?</script>"))
- ));
- }
- @Test
- public void when_MarkedSyncResourcesAreEquivalentOfSuperbatch_then_NoSuperbatchWritten() {
- cfg.markSyncResources(singletonList(
- new CompleteWebResourceKey("test.atlassian", "first-feature")
- ));
- cfg.addToSuperbatch("test.atlassian:first-feature");
- cfg.end();
- wr.requireResource("test.atlassian:first-feature");
- final String result = wr.pathsAsHtml();
- assertThat(result, allOf(
- // @todo SPFE-418 inline CSS
- // matches("content of first-1.css", "content of first-2.css", ...),
- matches("<script>", "content of first-1.js", "content of first-2.js", "</script>"),
- not(matches("<script[^\\>]+?first-feature.js")),
- not(matches("<script[^\\>]+?super/batch.js"))
- ));
- }
- @Test
- public void when_MarkedSyncResourcesAreSubsetOfSuperbatch_then_overlappingContentNotPresentInSuperbatch() {
- cfg.markSyncResources(singletonList(
- new CompleteWebResourceKey("test.atlassian", "first-feature")
- ));
- cfg.addToSuperbatch("test.atlassian:third-feature");
- cfg.end();
- wr.requireResource("test.atlassian:first-feature");
- final String superbatchJsContent = wr.getContent(superBatchUrl("js"));
- assertThat(superbatchJsContent, not(matches("content of first-1.js")));
- final String superbatchCssContent = wr.getContent(superBatchUrl("css"));
- assertThat(superbatchCssContent, not(matches("content of first-1.css")));
- }
- @Test
- public void when_MarkedSyncResourcesAndSuperbatchDoNotOverlap_then_undefinedBehaviour() {
- cfg.markSyncResources(singletonList(
- new CompleteWebResourceKey("test.atlassian", "first-feature")
- ));
- cfg.addToSuperbatch("test.atlassian:second-feature");
- cfg.end();
- wr.requireResource("test.atlassian:second-feature");
- final List<String> urls = wr.paths();
- // The first "URL" is for the sync resource(s)
- assertThat(urls.get(0), is(emptyString()));
- // note: The URL contains -_sync, but doesn't have to. Could be optimised.
- assertThat(urls.get(1), allOf(
- matches("-_sync"),
- matches("_super")
- ));
- }
- @Test
- public void given_emptySyncResources_when_otherContentRequested_then_syncNotSubtracted() {
- cfg.markSyncResources(emptyList());
- cfg.addToSuperbatch("test.atlassian:second-feature");
- cfg.end();
- wr.requireResource("test.atlassian:second-feature");
- final List<String> urls = wr.paths();
- // The first "URL" is for the sync resource(s)
- assertThat(urls.get(0), allOf(
- not(matches("-_sync")),
- matches("_super")
- ));
- }
- }