View Javadoc

1   package com.atlassian.plugin.webresource;
2   
3   import com.atlassian.plugin.ModuleDescriptor;
4   import com.atlassian.plugin.Plugin;
5   import com.atlassian.plugin.PluginAccessor;
6   import com.atlassian.plugin.elements.ResourceLocation;
7   import com.atlassian.plugin.servlet.DownloadableResource;
8   import com.atlassian.plugin.servlet.ServletContextFactory;
9   import junit.framework.TestCase;
10  import org.mockito.Mock;
11  import org.mockito.MockitoAnnotations;
12  
13  import java.util.Collections;
14  
15  import static org.mockito.Mockito.when;
16  
17  public class TestSingleDownloadableResourceBuilder extends TestCase
18  {
19      public static final String MODULE_KEY = "test.plugin.key:module";
20      public static final String PLUGIN_KEY = "test.plugin.key";
21      @Mock
22      private PluginAccessor mockPluginAccessor;
23      @Mock
24      private ServletContextFactory mockServletContextFactory;
25      @Mock
26      ModuleDescriptor mockModuleDescriptor;
27  
28      SingleDownloadableResourceBuilder builder;
29      Plugin plugin;
30  
31      @Override
32      public void setUp() throws Exception
33      {
34          super.setUp();
35          
36          MockitoAnnotations.initMocks(this);
37          builder = new SingleDownloadableResourceBuilder(mockPluginAccessor, mockServletContextFactory);
38  
39          plugin = TestUtils.createTestPlugin(PLUGIN_KEY, "1.0");
40  
41          when(mockPluginAccessor.getEnabledPluginModule(MODULE_KEY)).thenReturn(mockModuleDescriptor);
42          when(mockPluginAccessor.getPlugin(PLUGIN_KEY)).thenReturn(plugin);
43      }
44  
45      @Override
46      public void tearDown() throws Exception
47      {
48          mockPluginAccessor = null;
49          mockServletContextFactory = null;
50          
51          super.tearDown();
52      }
53      
54      public void testParseWithSimpleName() throws Exception
55      {
56          final ResourceLocation location = new ResourceLocation("", "mydownload.jpg", "download", "image/jpeg", "", Collections.<String, String>emptyMap());
57          when(mockModuleDescriptor.getResourceLocation("download", "mydownload.jpg")).thenReturn(location);
58  
59          DownloadableResource resource = builder.parse("/download/resources/test.plugin.key:module/mydownload.jpg", Collections.<String, String>emptyMap());
60  
61          assertNotNull(resource);
62          assertEquals(location.getContentType(), resource.getContentType());
63      }
64  
65      public void testParseWithSlashesInName() throws Exception
66      {
67          final ResourceLocation location = new ResourceLocation("", "mydownload.swf", "download", "application/x-shockwave-flash", "", Collections.<String, String>emptyMap());
68          when(mockModuleDescriptor.getResourceLocation("download", "path/to/mydownload.swf")).thenReturn(location);
69  
70          DownloadableResource resource = builder.parse("/download/resources/test.plugin.key:module/path/to/mydownload.swf", Collections.<String, String>emptyMap());
71  
72          assertNotNull(resource);
73          assertEquals(location.getContentType(), resource.getContentType());
74      }
75  
76      public void testRoundTrip() throws Exception
77      {
78          SinglePluginResource resource = new SinglePluginResource("foo.css", MODULE_KEY, false);
79          String url = resource.getUrl();
80  
81          final ResourceLocation location = new ResourceLocation("", "foo.css", "download", "text/css", "", Collections.<String, String>emptyMap());
82          when(mockModuleDescriptor.getResourceLocation("download", "foo.css")).thenReturn(location);
83  
84          DownloadableResource parsedResource = builder.parse(url, Collections.<String, String>emptyMap());
85  
86          assertNotNull(parsedResource);
87          assertEquals(location.getContentType(), parsedResource.getContentType());
88      }
89  
90      public void testParseInvlaidUrlThrowsException()
91      {
92          try
93          {
94              builder.parse("/download/resources/blah.png", Collections.<String, String>emptyMap());
95              fail("Should have thrown exception for invalid url");
96          }
97          catch (UrlParseException e)
98          {
99              //expected
100         }
101     }
102     
103 }