1 package com.atlassian.plugin.webresource;
2
3 import junit.framework.TestCase;
4 import org.apache.commons.lang.StringEscapeUtils;
5
6 import java.util.HashMap;
7 import java.util.Map;
8
9 public class TestJavascriptWebResource extends TestCase
10 {
11 private JavascriptWebResource javascriptWebResource;
12
13 protected void setUp() throws Exception
14 {
15 super.setUp();
16 javascriptWebResource = new JavascriptWebResource();
17 }
18
19 protected void tearDown() throws Exception
20 {
21 javascriptWebResource = null;
22 super.tearDown();
23 }
24
25 public void testMatches()
26 {
27 assertTrue(javascriptWebResource.matches("blah.js"));
28 assertFalse(javascriptWebResource.matches("blah.css"));
29 }
30
31 public void testFormatResource()
32 {
33 final String url = "/confluence/download/resources/confluence.web.resources:ajs/atlassian.js";
34 assertEquals("<script type=\"text/javascript\" src=\"" + url + "\" ></script>\n",
35 javascriptWebResource.formatResource(url, new HashMap<String, String>()));
36 }
37
38
39 public void testUrlIsEscaped()
40 {
41 final String url = "/confluence/s/en\"><script>alert(document.cookie)</script>/2153/1/1/_/download/superbatch/js/batch.js";
42
43 assertEquals("<script type=\"text/javascript\" src=\"" + StringEscapeUtils.escapeHtml(url) +
44 "\" ></script>\n", javascriptWebResource.formatResource(url, new HashMap<String, String>()));
45 }
46
47 public void testFormatIEResource()
48 {
49 final String url = "/confluence/download/resources/confluence.web.resources:ajs/atlassian-ie.js";
50
51 Map<String, String> params = new HashMap<String, String>();
52 params.put("ieonly", "true");
53 params.put("media", "screen");
54 assertEquals("<!--[if IE]>\n" +
55 "<script type=\"text/javascript\" src=\"" + url + "\" ></script>\n" +
56 "<![endif]-->\n",
57 javascriptWebResource.formatResource(url, params));
58 }
59
60 public void testFormatConditionalResource()
61 {
62 final String url = "/confluence/download/resources/confluence.web.resources:ajs/atlassian-ie.js";
63
64 Map<String, String> params = new HashMap<String, String>();
65 params.put("conditionalComment", "IE");
66 params.put("media", "screen");
67 assertEquals("<!--[if IE]>\n" +
68 "<script type=\"text/javascript\" src=\"" + url + "\" ></script>\n" +
69 "<![endif]-->\n",
70 javascriptWebResource.formatResource(url, params));
71 }
72
73 public void testFormatConditionOverridesIEResource()
74 {
75 final String url = "/confluence/download/resources/confluence.web.resources:ajs/atlassian-ie.js";
76
77 Map<String, String> params = new HashMap<String, String>();
78 params.put("conditionalComment", "!IE");
79 params.put("ieonly", "true");
80 params.put("media", "screen");
81 assertEquals("<!--[if !IE]>\n" +
82 "<script type=\"text/javascript\" src=\"" + url + "\" ></script>\n" +
83 "<![endif]-->\n",
84 javascriptWebResource.formatResource(url, params));
85 }
86 }