1 package com.atlassian.plugin.webresource;
2
3 import java.util.Collections;
4
5 import com.atlassian.plugin.ModuleDescriptor;
6 import com.atlassian.plugin.Plugin;
7 import com.atlassian.plugin.PluginAccessor;
8 import com.atlassian.plugin.elements.ResourceLocation;
9 import com.atlassian.plugin.servlet.DownloadableClasspathResource;
10 import com.atlassian.plugin.servlet.DownloadableResource;
11 import com.atlassian.plugin.servlet.ServletContextFactory;
12
13 import com.google.common.collect.ImmutableMap;
14
15 import org.junit.After;
16 import org.junit.Before;
17 import org.junit.Test;
18 import org.junit.runner.RunWith;
19 import org.mockito.Mock;
20 import org.mockito.runners.MockitoJUnitRunner;
21
22 import static org.junit.Assert.assertEquals;
23 import static org.junit.Assert.assertNotNull;
24 import static org.mockito.Mockito.when;
25
26 @RunWith(MockitoJUnitRunner.class)
27 public class TestSingleDownloadableResourceBuilder
28 {
29 public static final String MODULE_KEY = "test.plugin.key:module";
30 public static final String PLUGIN_KEY = "test.plugin.key";
31 @Mock
32 private PluginAccessor mockPluginAccessor;
33 @Mock
34 private ServletContextFactory mockServletContextFactory;
35 @Mock
36 ModuleDescriptor mockModuleDescriptor;
37
38 @Mock
39 WebResourceIntegration mockWebResourceIntegration;
40
41 SingleDownloadableResourceBuilder builder;
42 Plugin plugin;
43
44 @Before
45 public void setUp() throws Exception
46 {
47 builder = new SingleDownloadableResourceBuilder(mockPluginAccessor, mockServletContextFactory, mockWebResourceIntegration);
48
49 plugin = TestUtils.createTestPlugin(PLUGIN_KEY, "1.0");
50
51 when(mockPluginAccessor.getEnabledPluginModule(MODULE_KEY)).thenReturn(mockModuleDescriptor);
52 when(mockPluginAccessor.getPlugin(PLUGIN_KEY)).thenReturn(plugin);
53 }
54
55 @After
56 public void tearDown() throws Exception
57 {
58 mockPluginAccessor = null;
59 mockServletContextFactory = null;
60 }
61
62 @Test
63 public void testParseWithSimpleName() throws Exception
64 {
65 final ResourceLocation location = new ResourceLocation("", "mydownload.jpg", "download", "image/jpeg", "", Collections.<String, String>emptyMap());
66 when(mockModuleDescriptor.getResourceLocation("download", "mydownload.jpg")).thenReturn(location);
67
68 DownloadableResource resource = builder.parse("/download/resources/test.plugin.key:module/mydownload.jpg", Collections.<String, String>emptyMap());
69
70 assertNotNull(resource);
71 assertEquals(location.getContentType(), resource.getContentType());
72 }
73
74 @Test
75 public void testParseWithSlashesInName() throws Exception
76 {
77 final ResourceLocation location = new ResourceLocation("", "mydownload.swf", "download", "application/x-shockwave-flash", "", Collections.<String, String>emptyMap());
78 when(mockModuleDescriptor.getResourceLocation("download", "path/to/mydownload.swf")).thenReturn(location);
79
80 DownloadableResource resource = builder.parse("/download/resources/test.plugin.key:module/path/to/mydownload.swf", Collections.<String, String>emptyMap());
81
82 assertNotNull(resource);
83 assertEquals(location.getContentType(), resource.getContentType());
84 }
85
86 @Test
87 public void testRoundTrip() throws Exception
88 {
89 SinglePluginResource resource = new SinglePluginResource("foo.css", MODULE_KEY, false);
90 String url = resource.getUrl();
91
92 final ResourceLocation location = new ResourceLocation("", "foo.css", "download", "text/css", "", Collections.<String, String>emptyMap());
93 when(mockModuleDescriptor.getResourceLocation("download", "foo.css")).thenReturn(location);
94
95 DownloadableResource parsedResource = builder.parse(url, Collections.<String, String>emptyMap());
96
97 assertNotNull(parsedResource);
98 assertEquals(location.getContentType(), parsedResource.getContentType());
99 }
100
101 @Test(expected = UrlParseException.class)
102 public void testParseInvalidUrlThrowsException() throws UrlParseException
103 {
104 builder.parse("/download/resources/blah.png", Collections.<String, String>emptyMap());
105 }
106
107 @Test
108 public void testVariableSubstitutionWithEmptyReplacement()
109 {
110 final ResourceLocation location = new ResourceLocation("${x}foo.css", "foo.css", "download", "text/css", "", Collections.<String, String>emptyMap());
111 when(mockModuleDescriptor.getResourceLocation("download", "foo.css")).thenReturn(location);
112
113
114 when(mockWebResourceIntegration.getResourceSubstitutionVariables()).thenReturn(ImmutableMap.of("x", ""));
115 DownloadableClasspathResource resource = (DownloadableClasspathResource) builder.find(MODULE_KEY, "foo.css");
116 assertNotNull(resource);
117 assertEquals("foo.css", resource.getResourceLocation().getLocation());
118 }
119
120 @Test
121 public void testVariableSubsitutionWithPrefixReplacement()
122 {
123 final ResourceLocation location = new ResourceLocation("${x}foo.css", "foo.css", "download", "text/css", "", Collections.<String, String>emptyMap());
124 when(mockModuleDescriptor.getResourceLocation("download", "foo.css")).thenReturn(location);
125
126
127 when(mockWebResourceIntegration.getResourceSubstitutionVariables()).thenReturn(ImmutableMap.of("x", "old/"));
128 DownloadableClasspathResource resource = (DownloadableClasspathResource) builder.find(MODULE_KEY, "foo.css");
129 assertNotNull(resource);
130 assertEquals("old/foo.css", resource.getResourceLocation().getLocation());
131 }
132
133 @Test
134 public void variableSubsitutionWithMissingVariablesLeavesTemplateInPlace()
135 {
136 final ResourceLocation location = new ResourceLocation("${x}foo.css", "foo.css", "download", "text/css", "", Collections.<String, String>emptyMap());
137 when(mockModuleDescriptor.getResourceLocation("download", "foo.css")).thenReturn(location);
138
139 when(mockWebResourceIntegration.getResourceSubstitutionVariables()).thenReturn(ImmutableMap.<String, String>of());
140 DownloadableClasspathResource resource = (DownloadableClasspathResource) builder.find(MODULE_KEY, "foo.css");
141 assertNotNull(resource);
142 assertEquals("${x}foo.css", resource.getResourceLocation().getLocation());
143 }
144 }