View Javadoc

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 TestCssWebResource extends TestCase
10  {
11      private CssWebResource cssWebResource;
12  
13      protected void setUp() throws Exception
14      {
15          super.setUp();
16          cssWebResource = new CssWebResource();
17      }
18  
19      protected void tearDown() throws Exception
20      {
21          cssWebResource = null;
22          super.tearDown();
23      }
24  
25      public void testMatches()
26      {
27          assertTrue(cssWebResource.matches("blah.css"));
28          assertFalse(cssWebResource.matches("blah.js"));
29      }
30  
31      public void testFormatResource()
32      {
33          final String url = "/confluence/download/resources/confluence.web.resources:master-styles/master.css";
34  
35          assertEquals("<link type=\"text/css\" rel=\"stylesheet\" href=\"" + url + "\" media=\"all\">\n",
36                      cssWebResource.formatResource(url, new HashMap<String, String>()));
37      }
38  
39      // PLUG-753
40      public void testUrlIsEscaped()
41      {
42          final String url = "/confluence/s/en\"><script>alert(document.cookie)</script>/2153/1/1/_/download/superbatch/css/batch.css";
43  
44          assertEquals("<link type=\"text/css\" rel=\"stylesheet\" href=\"" + StringEscapeUtils.escapeHtml(url) +
45                  "\" media=\"all\">\n", cssWebResource.formatResource(url, new HashMap<String, String>()));
46      }
47  
48      public void testFormatResourceWithParameters()
49      {
50          final String url = "/confluence/download/resources/confluence.web.resources:master-styles/master.css";
51          Map<String, String> params = new HashMap<String, String>();
52          params.put("title", "Confluence Master CSS");
53          params.put("charset", "utf-8");
54          params.put("foo", "bar"); // test invalid parameter
55  
56          assertEquals("<link type=\"text/css\" rel=\"stylesheet\" href=\"" + url + "\" title=\"Confluence Master CSS\"" +
57                      " charset=\"utf-8\" media=\"all\">\n",
58                      cssWebResource.formatResource(url, params));
59      }
60  
61      public void testFormatIEResource()
62      {
63          final String url = "/confluence/download/resources/confluence.web.resources:master-styles/master-ie.css";
64  
65          Map<String, String> params = new HashMap<String, String>();
66          params.put("ieonly", "true");
67          params.put("media", "screen");
68          assertEquals("<!--[if IE]>\n" +
69                      "<link type=\"text/css\" rel=\"stylesheet\" href=\"" + url + "\" media=\"screen\">\n" +
70                      "<![endif]-->\n",
71                      cssWebResource.formatResource(url, params));
72      }
73  
74      public void testFormatConditionalResource()
75      {
76          final String url = "/confluence/download/resources/confluence.web.resources:master-styles/master-ie.css";
77  
78          Map<String, String> params = new HashMap<String, String>();
79          params.put("conditionalComment", "IE");
80          params.put("media", "screen");
81          assertEquals("<!--[if IE]>\n" +
82                      "<link type=\"text/css\" rel=\"stylesheet\" href=\"" + url + "\" media=\"screen\">\n" +
83                      "<![endif]-->\n",
84                      cssWebResource.formatResource(url, params));
85      }
86  
87      public void testFormatConditionOverridesIEResource()
88      {
89          final String url = "/confluence/download/resources/confluence.web.resources:master-styles/master-ie.css";
90  
91          Map<String, String> params = new HashMap<String, String>();
92          params.put("conditionalComment", "!IE");
93          params.put("ieonly", "true");
94          params.put("media", "screen");
95          assertEquals("<!--[if !IE]>\n" +
96                      "<link type=\"text/css\" rel=\"stylesheet\" href=\"" + url + "\" media=\"screen\">\n" +
97                      "<![endif]-->\n",
98                      cssWebResource.formatResource(url, params));
99      }
100 }