1   package com.atlassian.plugin.webresource;
2   
3   import com.atlassian.plugin.servlet.DownloadException;
4   import com.atlassian.plugin.servlet.DownloadableResource;
5   import com.atlassian.plugin.servlet.util.CapturingHttpServletResponse;
6   import com.atlassian.plugin.webresource.util.DownloadableResourceTestImpl;
7   
8   import java.io.ByteArrayOutputStream;
9   import java.util.Collections;
10  import java.util.Map;
11  import java.util.TreeMap;
12  
13  import junit.framework.TestCase;
14  
15  public class TestBatchPluginResource extends TestCase
16  {
17      public void testIsCacheSupported() throws Exception
18      {
19          final BatchPluginResource resource = BatchPluginResource.parse("/download/batch/test.plugin:webresources/test.plugin:webresources.css",
20              Collections.<String, String>emptyMap());
21          assertTrue(resource.isCacheSupported());
22  
23          final Map<String, String> queryParams = new TreeMap<String, String>();
24          queryParams.put("cache", "false");
25          final BatchPluginResource resource2 = BatchPluginResource.parse("/download/batch/test.plugin:webresources/test.plugin:webresources.css",
26              queryParams);
27          assertFalse(resource2.isCacheSupported());
28      }
29  
30      public void testParse() throws Exception
31      {
32          final BatchPluginResource resource = BatchPluginResource.parse("/download/batch/test.plugin:webresources/test.plugin:webresources.css",
33              Collections.<String, String>emptyMap());
34          assertEquals("css", resource.getType());
35          assertEquals("test.plugin:webresources", resource.getModuleCompleteKey());
36  
37          final Map<String, String> params = resource.getParams();
38  
39          assertEquals(0, params.size());
40      }
41  
42      public void testParseWithParams() throws Exception
43      {
44          final Map<String, String> queryParams = new TreeMap<String, String>();
45          queryParams.put("ieonly", "true");
46          queryParams.put("foo", "bar");
47  
48          final BatchPluginResource resource = BatchPluginResource.parse("/download/batch/test.plugin:webresources/test.plugin:webresources.css",
49              queryParams);
50          assertEquals("css", resource.getType());
51          assertEquals("test.plugin:webresources", resource.getModuleCompleteKey());
52  
53          final Map<String, String> params = new TreeMap<String, String>();
54          params.put("ieonly", "true");
55          params.put("foo", "bar");
56  
57          assertEquals(params, resource.getParams());
58      }
59  
60      public void testParseWithParams2() throws Exception
61      {
62          final Map<String, String> queryParams = new TreeMap<String, String>();
63          queryParams.put("ieonly", "true");
64          queryParams.put("foo", "bar");
65  
66          final BatchPluginResource resource = BatchPluginResource.parse("/download/batch/test.plugin:webresources/test.plugin:webresources.css?ieonly=true&foo=bar",
67              queryParams);
68          assertEquals("css", resource.getType());
69          assertEquals("test.plugin:webresources", resource.getModuleCompleteKey());
70  
71          final Map<String, String> params = new TreeMap<String, String>();
72          params.put("ieonly", "true");
73          params.put("foo", "bar");
74  
75          assertEquals(params, resource.getParams());
76      }
77  
78      public void testParseWithParamsAndRandomPrefix() throws Exception
79      {
80          final Map<String, String> queryParams = new TreeMap<String, String>();
81          queryParams.put("ieonly", "true");
82          queryParams.put("foo", "bar");
83  
84          final BatchPluginResource resource = BatchPluginResource.parse("/random/stuff/download/batch/test.plugin:webresources/test.plugin:webresources.css?ieonly=true&foo=bar",
85              queryParams);
86          assertEquals("css", resource.getType());
87          assertEquals("test.plugin:webresources", resource.getModuleCompleteKey());
88  
89          final Map<String, String> params = new TreeMap<String, String>();
90          params.put("ieonly", "true");
91          params.put("foo", "bar");
92  
93          assertEquals(params, resource.getParams());
94      }
95  
96      public void testGetUrl()
97      {
98          final BatchPluginResource resource = new BatchPluginResource("test.plugin:webresources", "js", Collections.<String, String>emptyMap());
99          assertEquals("/download/batch/test.plugin:webresources/test.plugin:webresources.js", resource.getUrl());
100     }
101 
102     public void testGetUrlWithParams()
103     {
104         final Map<String, String> params = new TreeMap<String, String>();
105         params.put("foo", "bar");
106         params.put("moo", "cow");
107 
108         final BatchPluginResource resource = new BatchPluginResource("test.plugin:webresources", "js", params);
109         assertEquals("/download/batch/test.plugin:webresources/test.plugin:webresources.js?foo=bar&moo=cow", resource.getUrl());
110     }
111 
112     public void testRoundTrip() throws Exception
113     {
114         final Map<String, String> params = new TreeMap<String, String>();
115         params.put("foo", "bar");
116 
117         final String moduleKey = "test.plugin:webresources";
118         final BatchPluginResource resource = new BatchPluginResource(moduleKey, "js", params);
119         final String url = resource.getUrl();
120         final BatchPluginResource parsedResource = BatchPluginResource.parse(url, params);
121 
122         assertEquals(resource.getType(), parsedResource.getType());
123         assertEquals(resource.getModuleCompleteKey(), parsedResource.getModuleCompleteKey());
124         assertEquals(resource.getParams(), parsedResource.getParams());
125         assertEquals(moduleKey + ".js", resource.getResourceName());
126     }
127 
128     public void testEquals()
129     {
130         final String moduleKey = "test.plugin:webresources";
131         final String type = "js";
132 
133         final Map<String, String> params1 = new TreeMap<String, String>();
134         params1.put("key", "value");
135         params1.put("foo", "bar");
136         final BatchPluginResource batch1 = new BatchPluginResource(moduleKey, type, params1);
137 
138         final Map<String, String> params2 = new TreeMap<String, String>();
139         params2.put("key", "value");
140         params2.put("foo", "bar");
141         final BatchPluginResource batch2 = new BatchPluginResource(moduleKey, type, params2);
142 
143         final Map<String, String> params3 = new TreeMap<String, String>();
144         params3.put("key", "value");
145         params3.put("foo", "bart");
146         final BatchPluginResource batch3 = new BatchPluginResource(moduleKey, type, params3);
147 
148         assertEquals(batch1, batch2);
149         assertNotSame(batch1, batch3);
150     }
151 
152     public void testNewLineStreamingHttpResponse() throws DownloadException
153     {
154         final BatchPluginResource batchResource = new BatchPluginResource("test.plugin:webresources", "js", null);
155 
156         final DownloadableResource testResource1 = new DownloadableResourceTestImpl("text/js", "Test1");
157         final DownloadableResource testResource2 = new DownloadableResourceTestImpl("text/js", "Test2");
158         batchResource.add(testResource1);
159         batchResource.add(testResource2);
160 
161         final CapturingHttpServletResponse response = new CapturingHttpServletResponse();
162         batchResource.serveResource(null, response);
163 
164         final String actualResponse = response.toString();
165         assertEquals("Test1\nTest2\n", actualResponse);
166     }
167 
168     public void testNewLineStreamingOutputStream() throws DownloadException
169     {
170         final BatchPluginResource batchResource = new BatchPluginResource("test.plugin:webresources", "js", null);
171 
172         final DownloadableResource testResource1 = new DownloadableResourceTestImpl("text/js", "Test1");
173         final DownloadableResource testResource2 = new DownloadableResourceTestImpl("text/js", "Test2");
174         batchResource.add(testResource1);
175         batchResource.add(testResource2);
176 
177         final ByteArrayOutputStream baos = new ByteArrayOutputStream();
178         batchResource.streamResource(baos);
179 
180         final String actualResponse = baos.toString();
181         assertEquals("Test1\nTest2\n", actualResponse);
182     }
183 
184     public void testParseInvlaidUrlThrowsException()
185     {
186         try
187         {
188             BatchPluginResource.parse("/download/batch/blah.css", Collections.<String, String>emptyMap());
189             fail("Should have thrown exception for invalid url");
190         }
191         catch (UrlParseException e)
192         {
193             //expected
194         }
195     }
196 }