1 package com.atlassian.plugin.webresource;
2
3 import static com.google.common.base.Suppliers.ofInstance;
4 import static java.util.Collections.emptyMap;
5 import static org.mockito.Mockito.mock;
6 import static org.mockito.Mockito.when;
7
8 import com.atlassian.plugin.servlet.DownloadException;
9 import com.atlassian.plugin.servlet.DownloadableResource;
10 import com.atlassian.plugin.servlet.util.CapturingHttpServletResponse;
11 import com.atlassian.plugin.webresource.cache.CacheHandle;
12 import com.atlassian.plugin.webresource.util.DownloadableResourceTestImpl;
13
14 import java.io.ByteArrayOutputStream;
15 import java.io.IOException;
16 import java.util.Arrays;
17 import java.util.Collections;
18 import java.util.List;
19 import java.util.Map;
20 import java.util.TreeMap;
21
22 import javax.script.ScriptEngine;
23 import javax.script.ScriptEngineManager;
24 import javax.script.ScriptException;
25 import javax.servlet.ServletOutputStream;
26 import javax.servlet.http.HttpServletRequest;
27 import javax.servlet.http.HttpServletResponse;
28
29 import junit.framework.TestCase;
30
31 public class TestBatchPluginResource extends TestCase
32 {
33 private final CacheHandle cacher = CacheHandle.Builder.passthrough();
34
35 public void testGetUrl()
36 {
37 final BatchPluginResource resource = new BatchPluginResource(ResourceKey.Builder.lazy("test.plugin:webresources", ofInstance("js")), Collections.<String, String> emptyMap(),
38 new BatchedWebResourceDescriptor("2.0", "a.b.c.d:e"));
39 assertEquals("/download/batch/test.plugin:webresources/test.plugin:webresources.js", resource.getUrl());
40 }
41
42 public void testGetUrlWithParams()
43 {
44 final Map<String, String> params = new TreeMap<String, String>();
45 params.put("conditionalComment", "lt IE 9");
46 params.put("foo", "bar");
47
48 final BatchPluginResource resource = new BatchPluginResource(ResourceKey.Builder.lazy("test.plugin:webresources", ofInstance("js")), params,
49 new BatchedWebResourceDescriptor("2.0", "a.b.c.d:e"));
50 assertEquals("/download/batch/test.plugin:webresources/test.plugin:webresources.js?conditionalComment=lt+IE+9&foo=bar", resource.getUrl());
51 }
52
53 public void testIsCacheSupported() throws Exception
54 {
55 final Map<String, String> params = new TreeMap<String, String>();
56 params.put("cache", "false");
57
58 final BatchPluginResource batchResource = new BatchPluginResource(ResourceKey.Builder.lazy("test.plugin:webresources",
59 ofInstance("js")), params, new BatchedWebResourceDescriptor("2.0", "a.b.c.d:e"));
60 assertFalse(batchResource.isCacheSupported());
61
62 final BatchPluginResource batchResource2 = new BatchPluginResource(ResourceKey.Builder.lazy("test.plugin:webresources", ofInstance("js")),
63 Collections.<String, String>emptyMap(), new BatchedWebResourceDescriptor("2.0", "a.b.c.d:e"));
64 assertTrue(batchResource2.isCacheSupported());
65 }
66
67
68 public void testEquals()
69 {
70 final String moduleKey = "test.plugin:webresources";
71 final String type = "js";
72
73 final Map<String, String> params1 = new TreeMap<String, String>();
74 params1.put("key", "value");
75 params1.put("foo", "bar");
76 new ResourceKey.Strict("a","test.plugin:webresources", type);
77 final BatchPluginResource batch1 = new BatchPluginResource(ResourceKey.Builder.lazy(moduleKey, ofInstance(type)), params1,
78 new BatchedWebResourceDescriptor("2.0", "a.b.c.d:e"));
79 final Map<String, String> params2 = new TreeMap<String, String>();
80 params2.put("key", "value");
81 params2.put("foo", "bar");
82 final BatchPluginResource batch2 = new BatchPluginResource(ResourceKey.Builder.lazy(moduleKey, ofInstance(type)), params2,
83 new BatchedWebResourceDescriptor("2.0", "a.b.c.d:e"));
84
85 final Map<String, String> params3 = new TreeMap<String, String>();
86 params3.put("key", "value");
87 params3.put("foo", "bart");
88 final BatchPluginResource batch3 = new BatchPluginResource(ResourceKey.Builder.lazy(moduleKey, ofInstance(type)), params3,
89 new BatchedWebResourceDescriptor("2.0", "a.b.c.d:e"));
90
91 assertEquals(batch1, batch2);
92 assertNotSame(batch1, batch3);
93 }
94
95 public void testNewLineStreamingHttpResponse() throws DownloadException
96 {
97
98 final DownloadableResource testResource1 = new DownloadableResourceTestImpl("text/js", "Test1");
99 final DownloadableResource testResource2 = new DownloadableResourceTestImpl("text/js", "Test2");
100 final List<DownloadableResource> resources = Arrays.asList(testResource1, testResource2);
101
102 final Map<String, String> empty = emptyMap();
103 final BatchDownloadableResource batchResource = new BatchDownloadableResource("test.plugin:webresources", "js", empty, resources, cacher);
104
105 final CapturingHttpServletResponse response = new CapturingHttpServletResponse();
106 batchResource.serveResource(null, response);
107
108 final String actualResponse = response.toString();
109 assertEquals("Test1\nTest2\n", actualResponse);
110 }
111
112 public void testNewLineStreamingOutputStream() throws DownloadException
113 {
114 final DownloadableResource testResource1 = new DownloadableResourceTestImpl("text/js", "Test1");
115 final DownloadableResource testResource2 = new DownloadableResourceTestImpl("text/js", "Test2");
116 final List<DownloadableResource> resources = Arrays.asList(testResource1, testResource2);
117
118 final Map<String, String> empty = emptyMap();
119 final BatchDownloadableResource batchResource = new BatchDownloadableResource("test.plugin:webresources", "js", empty, resources, cacher);
120
121 final ByteArrayOutputStream baos = new ByteArrayOutputStream();
122 batchResource.streamResource(baos);
123
124 final String actualResponse = baos.toString();
125 assertEquals("Test1\nTest2\n", actualResponse);
126 }
127
128 public void testTryCatchJavascriptWrappingIfSet() throws Exception
129 {
130 DownloadableResource mockResource = mock(DownloadableResource.class);
131 HttpServletRequest mockRequest = mock(HttpServletRequest.class);
132
133 final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
134 HttpServletResponse mockResponse = mock(HttpServletResponse.class);
135 when(mockResponse.getOutputStream()).thenReturn(new ServletOutputStream()
136 {
137 @Override
138 public void write(int b) throws IOException
139 {
140 outStream.write(b);
141 }
142 });
143
144 System.setProperty(DefaultResourceBatchingConfiguration.PLUGIN_WEBRESOURCE_TRY_CATCH_WRAPPING, "true");
145 try
146 {
147 final BatchDownloadableResource resource = new BatchDownloadableResource("test.plugin:webresources", "js", Collections.<String,String>emptyMap(),
148 Collections.singletonList(mockResource), cacher);
149
150 resource.serveResource(mockRequest, mockResponse);
151
152 String tryCatchBlock = new String(outStream.toByteArray());
153
154 assertTrue(tryCatchBlock.startsWith("try"));
155
156 ScriptEngineManager mgr = new ScriptEngineManager();
157 ScriptEngine jsEngine = mgr.getEngineByName("JavaScript");
158 try
159 {
160 jsEngine.eval(tryCatchBlock);
161 }
162 catch (ScriptException ex) {
163 fail("Exception running the created javascript: " + ex);
164 }
165 }
166 finally
167 {
168 System.setProperty(DefaultResourceBatchingConfiguration.PLUGIN_WEBRESOURCE_TRY_CATCH_WRAPPING, "false");
169 }
170 }
171
172 public void testTryCatchJavascriptWrappingNotSet() throws Exception
173 {
174 DownloadableResource mockResource = mock(DownloadableResource.class);
175
176 final BatchDownloadableResource resource = new BatchDownloadableResource("test.plugin:webresources", "js", Collections.<String,String>emptyMap(),
177 Collections.singletonList(mockResource), cacher);
178
179 HttpServletRequest mockRequest = mock(HttpServletRequest.class);
180 when(mockRequest.getParameter("trycatchwrap")).thenReturn("false");
181
182 final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
183 HttpServletResponse mockResponse = mock(HttpServletResponse.class);
184 when(mockResponse.getOutputStream()).thenReturn(new ServletOutputStream()
185 {
186 @Override
187 public void write(int b) throws IOException
188 {
189 outStream.write(b);
190 }
191 });
192
193 resource.serveResource(mockRequest, mockResponse);
194
195 String tryCatchBlock = new String(outStream.toByteArray());
196
197 assertFalse(tryCatchBlock.startsWith("try"));
198 }
199
200 public void testTryCatchJavascriptWrappingIgnoredForWrongType() throws Exception
201 {
202 DownloadableResource mockResource = mock(DownloadableResource.class);
203
204 final BatchDownloadableResource resource = new BatchDownloadableResource("test.plugin:webresources", "css", Collections.<String,String>emptyMap(),
205 Collections.singletonList(mockResource), cacher);
206
207 HttpServletRequest mockRequest = mock(HttpServletRequest.class);
208 when(mockRequest.getParameter("trycatchwrap")).thenReturn("true");
209
210 final ByteArrayOutputStream outStream = new ByteArrayOutputStream();
211 HttpServletResponse mockResponse = mock(HttpServletResponse.class);
212 when(mockResponse.getOutputStream()).thenReturn(new ServletOutputStream()
213 {
214 @Override
215 public void write(int b) throws IOException
216 {
217 outStream.write(b);
218 }
219 });
220
221 resource.serveResource(mockRequest, mockResponse);
222
223 String tryCatchBlock = new String(outStream.toByteArray());
224
225 assertFalse(tryCatchBlock.startsWith("try"));
226 }
227
228 }