1 package com.atlassian.plugin.webresource;
2
3 import static org.hamcrest.CoreMatchers.containsString;
4 import static org.hamcrest.CoreMatchers.endsWith;
5 import static org.hamcrest.CoreMatchers.startsWith;
6 import static org.hamcrest.MatcherAssert.assertThat;
7 import static org.junit.Assert.assertEquals;
8 import static org.mockito.Matchers.anyString;
9 import static org.mockito.Mockito.when;
10
11 import java.util.Collections;
12 import java.util.List;
13
14 import org.junit.Before;
15 import org.junit.Test;
16 import org.mockito.Mock;
17 import org.mockito.MockitoAnnotations;
18
19 import com.atlassian.plugin.ModuleDescriptor;
20 import com.atlassian.plugin.Plugin;
21 import com.atlassian.plugin.PluginAccessor;
22 import com.atlassian.plugin.PluginInformation;
23 import com.google.common.collect.ImmutableList;
24
25 public class TestBatchResourceContentsWebFormatter
26 {
27 private @Mock WebResourceIntegration webResourceIntegration;
28 private @Mock PluginAccessor pluginAccessor;
29 @SuppressWarnings("rawtypes")
30 private @Mock ModuleDescriptor moduleDescriptor;
31 private @Mock Plugin plugin;
32 private @Mock PluginInformation pluginInformation;
33
34 @SuppressWarnings("unchecked")
35 @Before
36 public void setUp() throws Exception
37 {
38 MockitoAnnotations.initMocks(this);
39 when(webResourceIntegration.getPluginAccessor()).thenReturn(pluginAccessor);
40 when(pluginAccessor.getEnabledPluginModule(anyString())).thenReturn(moduleDescriptor);
41 when(moduleDescriptor.getPlugin()).thenReturn(plugin);
42 when(plugin.getPluginInformation()).thenReturn(pluginInformation);
43 when(pluginInformation.getVersion()).thenReturn("1.3-m4");
44 }
45
46 @Test
47 public void testNoAttributesForSinglePluginResource()
48 {
49 SinglePluginResource resource = new SinglePluginResource("abc", "a.b:c", false);
50 String formattedResource = "<script></script>";
51 String modified = BatchResourceContentsWebFormatter.insertBatchResourceContents(resource, formattedResource);
52
53 assertEquals(formattedResource, modified);
54 }
55
56 @Test
57 public void testInsertDependencies()
58 {
59 SuperBatchPluginResource resource = new SuperBatchPluginResource("js", Collections.<String,String>emptyMap());
60 resource.addBatchedWebResourceDescriptor(new BatchedWebResourceDescriptor("2.3", "apple.banana:spoon"));
61 resource.addBatchedWebResourceDescriptor(new BatchedWebResourceDescriptor("3.4.6-m2", "spam.spam:spam"));
62
63 String formattedResource = "<SCRIPT src=\"somewhere\"></SCRIPT>";
64 String modified = BatchResourceContentsWebFormatter.insertBatchResourceContents(resource, formattedResource);
65
66
67 assertThat(modified, startsWith("<SCRIPT "));
68 assertThat(modified, endsWith(" src=\"somewhere\"></SCRIPT>"));
69 assertThat(modified, containsString(" data-atlassian-webresource-contents="));
70 assertThat(modified, containsString("apple.banana:spoon[2.3]"));
71 assertThat(modified, containsString("spam.spam:spam[3.4.6-m2]"));
72 }
73
74 @Test
75 public void testInsertDependenciesAndContexts()
76 {
77 List<String> includedContexts = ImmutableList.of("contextA", "contextB", "contextC");
78 List<String> excludedContexts = ImmutableList.of("excludedcontextA");
79
80 ContextBatchPluginResource resource = new ContextBatchPluginResource("batch", includedContexts, excludedContexts, "2345", "css", Collections.<String,String>emptyMap(),
81 Collections.singleton(new BatchedWebResourceDescriptor("1", "food.fruit:apple")));
82
83 String formattedResource = "<LINK href=\"somewhere\"></LINK>";
84 String modified = BatchResourceContentsWebFormatter.insertBatchResourceContents(resource, formattedResource);
85
86
87 assertThat(modified, startsWith("<LINK "));
88 assertThat(modified, endsWith(" href=\"somewhere\"></LINK>"));
89 assertThat(modified, containsString(" data-atlassian-webresource-contents=\"food.fruit:apple[1]\""));
90 assertThat(modified, containsString(" data-atlassian-webresource-contexts=\"context"));
91 assertThat(modified, containsString("contextA"));
92 assertThat(modified, containsString("contextB"));
93 assertThat(modified, containsString("contextC"));
94 assertThat(modified, containsString(" data-atlassian-webresource-excluded-contexts=\"excludedcontextA\""));
95 }
96
97 @Test
98 public void testInsertDependenciesWithUnsafeNames()
99 {
100 SuperBatchPluginResource resource = new SuperBatchPluginResource("js", Collections.<String,String>emptyMap());
101 resource.addBatchedWebResourceDescriptor(new BatchedWebResourceDescriptor("2.3-\"m\"", "apple.banana:\"spork\""));
102
103 String formattedResource = "<SCRIPT src=\"somewhere\"></SCRIPT>";
104 String modified = BatchResourceContentsWebFormatter.insertBatchResourceContents(resource, formattedResource);
105
106
107 assertThat(modified, startsWith("<SCRIPT "));
108 assertThat(modified, endsWith(" src=\"somewhere\"></SCRIPT>"));
109 assertThat(modified, containsString(" data-atlassian-webresource-contents="));
110 assertThat(modified, containsString("apple.banana:"spork"[2.3-"m"]"));
111 }
112
113 @Test
114 public void testOnConditionallyWrapperResources()
115 {
116 SuperBatchPluginResource resource = new SuperBatchPluginResource("js", Collections.<String,String>emptyMap());
117 resource.addBatchedWebResourceDescriptor(new BatchedWebResourceDescriptor("2.3", "apple.banana:spoon"));
118
119 String formattedResource = "<!--[if lte IE 9]>\n<link href=\"something\">\n<!--[endif]-->";
120 String modified = BatchResourceContentsWebFormatter.insertBatchResourceContents(resource, formattedResource);
121
122 assertThat(modified, startsWith("<!--[if lte IE 9]>\n<link "));
123 assertThat(modified, endsWith(" href=\"something\">\n<!--[endif]-->"));
124 assertThat(modified, containsString(" data-atlassian-webresource-contents=\"apple.banana:spoon[2.3]\""));
125 }
126
127 @Test
128 public void testBatchPluginResource()
129 {
130 final String completeKey = "abc.def.ghi:jkl";
131 BatchPluginResource resource = new BatchPluginResource(new ResourceKey.Strict(completeKey, "name", "js"), Collections.<String,String>emptyMap(),
132 new BatchedWebResourceDescriptor("1.3-m4", completeKey));
133
134 String formattedResource = "<script src=\"abc\"></script>";
135 String modified = BatchResourceContentsWebFormatter.insertBatchResourceContents(resource, formattedResource);
136
137 assertThat(modified, startsWith("<script "));
138 assertThat(modified, endsWith(" src=\"abc\"></script>"));
139 assertThat(modified, containsString("data-atlassian-webresource-contents=\"abc.def.ghi:jkl[1.3-m4]\" "));
140 }
141 }