1   package com.atlassian.plugin.webresource;
2   
3   import junit.framework.TestCase;
4   
5   import java.util.Collections;
6   import java.util.Map;
7   import java.util.TreeMap;
8   
9   public class TestSuperBatchPluginResource extends TestCase
10  {
11      public void testParseCss()
12      {
13          String path = "/download/superbatch/css/batch.css";
14          assertTrue(SuperBatchPluginResource.matches(path));
15          SuperBatchPluginResource resource = SuperBatchPluginResource.parse(path, Collections.<String, String>emptyMap());
16          assertEquals("css", resource.getType());
17          assertEquals(resource.getUrl(), path);
18          assertEquals("batch.css", resource.getResourceName());
19      }
20  
21      // For some reason the download manager doesn't strip context paths before sending it in to be matched.
22      public void testParseWithContextPath()
23      {
24          assertTrue(SuperBatchPluginResource.matches("/confluence/download/superbatch/css/batch.css"));
25      }
26  
27      public void testParseJavascript()
28      {
29          String path = "/download/superbatch/js/batch.js";
30          assertTrue(SuperBatchPluginResource.matches(path));
31          SuperBatchPluginResource resource = SuperBatchPluginResource.parse(path, Collections.<String, String>emptyMap());
32          assertEquals("js", resource.getType());
33          assertEquals(resource.getUrl(), path);
34          assertEquals("batch.js", resource.getResourceName());
35      }
36      
37      public void testParseWithParam()
38      {
39          String path="/download/superbatch/js/batch.js";
40          Map<String, String> params = Collections.singletonMap("ieOnly", "true");
41          SuperBatchPluginResource resource = SuperBatchPluginResource.parse(path, params);
42          assertEquals(params, resource.getParams());
43          assertEquals(path + "?ieOnly=true", resource.getUrl());
44          assertEquals("batch.js", resource.getResourceName());
45      }
46  
47      public void testParseWithParams()
48      {
49          String path="/download/superbatch/js/batch.js";
50          Map<String, String> params = new TreeMap<String, String>();
51          params.put("ieOnly", "true");
52          params.put("zomg", "false");
53          SuperBatchPluginResource resource = SuperBatchPluginResource.parse(path, params);
54          assertEquals(params, resource.getParams());
55          assertEquals(path + "?ieOnly=true&zomg=false", resource.getUrl());
56          assertEquals("batch.js", resource.getResourceName());
57      }
58  
59      public void testNotSuperbatches()
60      {
61          assertFalse("wrong path", SuperBatchPluginResource.matches("/download/superbitch/css/batch.css"));
62          assertFalse("wrong path", SuperBatchPluginResource.matches("/download/superbatch/css/images/foo.png"));
63      }
64  
65      public void testGetType()
66      {
67          assertEquals("css", SuperBatchPluginResource.getType("/foo.css"));
68          assertEquals("js", SuperBatchPluginResource.getType("/superbatch/js/foo.js"));
69          assertEquals("", SuperBatchPluginResource.getType("/superbatch/js/foo."));
70          assertEquals("", SuperBatchPluginResource.getType("/superbatch/js/foo"));
71      }
72  }