1 package com.atlassian.plugin.webresource;
2
3 import static java.util.Collections.emptyMap;
4
5 import com.atlassian.plugin.servlet.DownloadException;
6 import com.atlassian.plugin.servlet.DownloadableResource;
7 import com.atlassian.plugin.servlet.util.CapturingHttpServletResponse;
8 import com.atlassian.plugin.webresource.util.DownloadableResourceTestImpl;
9
10 import java.io.ByteArrayOutputStream;
11 import java.util.Arrays;
12 import java.util.Collections;
13 import java.util.List;
14 import java.util.Map;
15 import java.util.TreeMap;
16
17 import junit.framework.TestCase;
18
19 public class TestBatchPluginResource extends TestCase
20 {
21 public void testGetUrl()
22 {
23 final BatchPluginResource resource = new BatchPluginResource("test.plugin:webresources", "js", Collections.<String, String> emptyMap(),
24 Collections.<DownloadableResource> emptyList());
25 assertEquals("/download/batch/test.plugin:webresources/test.plugin:webresources.js", resource.getUrl());
26 }
27
28 public void testGetUrlWithParams()
29 {
30 final Map<String, String> params = new TreeMap<String, String>();
31 params.put("conditionalComment", "lt IE 9");
32 params.put("foo", "bar");
33
34 final BatchPluginResource resource = new BatchPluginResource("test.plugin:webresources", "js", params,
35 Collections.<DownloadableResource> emptyList());
36 assertEquals("/download/batch/test.plugin:webresources/test.plugin:webresources.js?conditionalComment=lt+IE+9&foo=bar", resource.getUrl());
37 }
38
39 public void testEquals()
40 {
41 final String moduleKey = "test.plugin:webresources";
42 final String type = "js";
43
44 final Map<String, String> params1 = new TreeMap<String, String>();
45 params1.put("key", "value");
46 params1.put("foo", "bar");
47 final BatchPluginResource batch1 = new BatchPluginResource(moduleKey, type, params1, Collections.<DownloadableResource> emptyList());
48
49 final Map<String, String> params2 = new TreeMap<String, String>();
50 params2.put("key", "value");
51 params2.put("foo", "bar");
52 final BatchPluginResource batch2 = new BatchPluginResource(moduleKey, type, params2, Collections.<DownloadableResource> emptyList());
53
54 final Map<String, String> params3 = new TreeMap<String, String>();
55 params3.put("key", "value");
56 params3.put("foo", "bart");
57 final BatchPluginResource batch3 = new BatchPluginResource(moduleKey, type, params3, Collections.<DownloadableResource> emptyList());
58
59 assertEquals(batch1, batch2);
60 assertNotSame(batch1, batch3);
61 }
62
63 public void testNewLineStreamingHttpResponse() throws DownloadException
64 {
65
66 final DownloadableResource testResource1 = new DownloadableResourceTestImpl("text/js", "Test1");
67 final DownloadableResource testResource2 = new DownloadableResourceTestImpl("text/js", "Test2");
68 final List<DownloadableResource> resources = Arrays.asList(testResource1, testResource2);
69
70 final Map<String, String> empty = emptyMap();
71 final BatchPluginResource batchResource = new BatchPluginResource("test.plugin:webresources", "js", empty, resources);
72
73 final CapturingHttpServletResponse response = new CapturingHttpServletResponse();
74 batchResource.serveResource(null, response);
75
76 final String actualResponse = response.toString();
77 assertEquals("Test1\nTest2\n", actualResponse);
78 }
79
80 public void testNewLineStreamingOutputStream() throws DownloadException
81 {
82 final DownloadableResource testResource1 = new DownloadableResourceTestImpl("text/js", "Test1");
83 final DownloadableResource testResource2 = new DownloadableResourceTestImpl("text/js", "Test2");
84 final List<DownloadableResource> resources = Arrays.asList(testResource1, testResource2);
85
86 final Map<String, String> empty = emptyMap();
87 final BatchPluginResource batchResource = new BatchPluginResource("test.plugin:webresources", "js", empty, resources);
88
89 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
90 batchResource.streamResource(baos);
91
92 final String actualResponse = baos.toString();
93 assertEquals("Test1\nTest2\n", actualResponse);
94 }
95 }