View Javadoc

1   package com.atlassian.plugin.webresource;
2   
3   import com.atlassian.plugin.PluginAccessor;
4   import com.atlassian.plugin.cache.filecache.impl.PassThroughFileCache;
5   import com.atlassian.plugin.servlet.DownloadableResource;
6   import com.atlassian.plugin.webresource.cache.FileCacheKey;
7   import junit.framework.TestCase;
8   import org.mockito.Mock;
9   import org.mockito.MockitoAnnotations;
10  
11  import java.util.Collections;
12  import java.util.Map;
13  import java.util.TreeMap;
14  
15  import static org.mockito.Mockito.when;
16  
17  public class TestSuperBatchDownloadableResourceBuilder extends TestCase
18  {
19      @Mock
20      private DefaultResourceDependencyResolver mockDependencyResolver;
21      @Mock
22      private PluginAccessor mockPluginAccessor;   
23      @Mock
24      private WebResourceUrlProvider mockWebResourceUrlProvider;
25      @Mock
26      private DownloadableResourceFinder mockResourceFinder;
27      
28      private SuperBatchDownloadableResourceBuilder builder;
29  
30      @Override
31      public void setUp() throws Exception
32      {
33          super.setUp();
34          
35          MockitoAnnotations.initMocks(this);
36          builder = new SuperBatchDownloadableResourceBuilder(mockDependencyResolver, mockPluginAccessor, mockWebResourceUrlProvider, mockResourceFinder, PassThroughFileCache.<FileCacheKey>instance());
37  
38          when(mockDependencyResolver.getSuperBatchDependencies()).thenReturn(Collections.<WebResourceModuleDescriptor>emptyList());
39      }
40  
41      @Override
42      public void tearDown() throws Exception
43      {
44          mockDependencyResolver = null;
45          mockPluginAccessor = null;
46          mockWebResourceUrlProvider = null;
47          mockResourceFinder = null;
48          
49          super.tearDown();
50      }
51      
52      public void testParseCss() throws UrlParseException
53      {
54          String path = "/download/superbatch/css/batch.css";
55          assertTrue(builder.matches(path));
56          DownloadableResource resource = builder.parse(path, Collections.<String, String>emptyMap());
57          SuperBatchDownloadableResource batchResource = (SuperBatchDownloadableResource) resource;
58          assertEquals("css", batchResource.getType());
59      }
60  
61      // For some reason the download manager doesn't strip context paths before sending it in to be matched.
62      public void testParseWithContextPath()
63      {
64          assertTrue(builder.matches("/confluence/download/superbatch/css/batch.css"));
65      }
66  
67      public void testParseJavascript() throws UrlParseException
68      {
69          String path = "/download/superbatch/js/batch.js";
70          assertTrue(builder.matches(path));
71          DownloadableResource resource = builder.parse(path, Collections.<String, String>emptyMap());
72          SuperBatchDownloadableResource batchResource = (SuperBatchDownloadableResource) resource;
73          assertEquals("js", batchResource.getType());
74      }
75  
76      public void testParseWithParam() throws UrlParseException
77      {
78          String path="/download/superbatch/js/batch.js";
79          Map<String, String> params = Collections.singletonMap("ieOnly", "true");
80          DownloadableResource resource = builder.parse(path, params);
81          SuperBatchDownloadableResource batchResource = (SuperBatchDownloadableResource) resource;
82          assertEquals(params, batchResource.getParams());
83      }
84  
85      public void testParseWithParams() throws UrlParseException
86      {
87          String path="/download/superbatch/js/batch.js";
88          Map<String, String> params = new TreeMap<String, String>();
89          params.put("ieOnly", "true");
90          params.put("zomg", "false");
91          DownloadableResource resource = builder.parse(path, params);
92          SuperBatchDownloadableResource batchResource = (SuperBatchDownloadableResource) resource;
93          assertEquals(params, batchResource.getParams());
94      }
95  
96      public void testNotSuperbatches()
97      {
98          assertFalse("wrong path", builder.matches("/download/superbitch/css/batch.css"));
99          assertFalse("wrong path", builder.matches("/download/superbatch/css/images/foo.png"));
100     }
101 }