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.atlassian.plugin.servlet.DownloadableResource;
24 import com.google.common.collect.ImmutableList;
25
26 public class TestBatchResourceContentsWebFormatter
27 {
28 private BatchResourceContentsWebFormatter formatter;
29
30 private @Mock WebResourceIntegration webResourceIntegration;
31 private @Mock PluginAccessor pluginAccessor;
32 @SuppressWarnings("rawtypes")
33 private @Mock ModuleDescriptor moduleDescriptor;
34 private @Mock Plugin plugin;
35 private @Mock PluginInformation pluginInformation;
36
37 @SuppressWarnings("unchecked")
38 @Before
39 public void setUp() throws Exception
40 {
41 MockitoAnnotations.initMocks(this);
42 formatter = new BatchResourceContentsWebFormatter(webResourceIntegration);
43 when(webResourceIntegration.getPluginAccessor()).thenReturn(pluginAccessor);
44 when(pluginAccessor.getEnabledPluginModule(anyString())).thenReturn(moduleDescriptor);
45 when(moduleDescriptor.getPlugin()).thenReturn(plugin);
46 when(plugin.getPluginInformation()).thenReturn(pluginInformation);
47 when(pluginInformation.getVersion()).thenReturn("1.3-m4");
48 }
49
50 @Test
51 public void testWrongPluginResourceType()
52 {
53 SinglePluginResource resource = new SinglePluginResource("abc", "a.b:c", false);
54 String formattedResource = "<script></script>";
55 String modified = formatter.insertBatchResourceContents(resource, formattedResource);
56
57 assertEquals(formattedResource, modified);
58 }
59
60 @Test
61 public void testInsertDependencies()
62 {
63 SuperBatchPluginResource resource = new SuperBatchPluginResource("js", Collections.<String,String>emptyMap());
64 resource.addBatchedWebResourceDescriptor(new BatchedWebResourceDescriptor("2.3", "apple.banana:spoon"));
65 resource.addBatchedWebResourceDescriptor(new BatchedWebResourceDescriptor("3.4.6-m2", "spam.spam:spam"));
66
67 String formattedResource = "<SCRIPT src=\"somewhere\"></SCRIPT>";
68 String modified = formatter.insertBatchResourceContents(resource, formattedResource);
69
70
71 assertThat(modified, startsWith("<SCRIPT "));
72 assertThat(modified, endsWith(" src=\"somewhere\"></SCRIPT>"));
73 assertThat(modified, containsString(" data-atlassian-webresource-contents="));
74 assertThat(modified, containsString("apple.banana:spoon[2.3]"));
75 assertThat(modified, containsString("spam.spam:spam[3.4.6-m2]"));
76 }
77
78 @Test
79 public void testInsertDependenciesAndContexts()
80 {
81 List<String> includedContexts = ImmutableList.of("contextA", "contextB", "contextC");
82 List<String> excludedContexts = ImmutableList.of("excludedcontextA");
83
84 ContextBatchPluginResource resource = new ContextBatchPluginResource("batch", includedContexts, excludedContexts, "2345", "css", Collections.<String,String>emptyMap(), Collections.<DownloadableResource>emptyList());
85 resource.addBatchedWebResourceDescriptors(Collections.singleton(new BatchedWebResourceDescriptor("1", "food.fruit:apple")));
86
87 String formattedResource = "<LINK href=\"somewhere\"></LINK>";
88 String modified = formatter.insertBatchResourceContents(resource, formattedResource);
89
90
91 assertThat(modified, startsWith("<LINK "));
92 assertThat(modified, endsWith(" href=\"somewhere\"></LINK>"));
93 assertThat(modified, containsString(" data-atlassian-webresource-contents=\"food.fruit:apple[1]\""));
94 assertThat(modified, containsString(" data-atlassian-webresource-contexts=\"context"));
95 assertThat(modified, containsString("contextA"));
96 assertThat(modified, containsString("contextB"));
97 assertThat(modified, containsString("contextC"));
98 assertThat(modified, containsString(" data-atlassian-webresource-excluded-contexts=\"excludedcontextA\""));
99 }
100
101 @Test
102 public void testOnConditionallyWrapperResources()
103 {
104 SuperBatchPluginResource resource = new SuperBatchPluginResource("js", Collections.<String,String>emptyMap());
105 resource.addBatchedWebResourceDescriptor(new BatchedWebResourceDescriptor("2.3", "apple.banana:spoon"));
106
107 String formattedResource = "<!--[if lte IE 9]>\n<link href=\"something\">\n<![endif]-->";
108 String modified = formatter.insertBatchResourceContents(resource, formattedResource);
109
110 assertThat(modified, startsWith("<!--[if lte IE 9]>\n<link "));
111 assertThat(modified, endsWith(" href=\"something\">\n<![endif]-->"));
112 assertThat(modified, containsString(" data-atlassian-webresource-contents=\"apple.banana:spoon[2.3]\""));
113 }
114
115 @Test
116 public void testBatchPluginResource()
117 {
118 BatchPluginResource resource = new BatchPluginResource(new ResourceKey.Strict("abc.def.ghi:jkl", "name", "js"), Collections.<String,String>emptyMap(), Collections.<DownloadableResource>emptySet());
119
120 String formattedResource = "<script src=\"abc\"></script>";
121 String modified = formatter.insertBatchResourceContents(resource, formattedResource);
122
123 assertThat(modified, startsWith("<script "));
124 assertThat(modified, endsWith(" src=\"abc\"></script>"));
125 assertThat(modified, containsString("data-atlassian-webresource-contents=\"abc.def.ghi:jkl[1.3-m4]\" "));
126 }
127 }