1   package com.atlassian.plugins.codegen.modules.common;
2   
3   import java.util.List;
4   
5   import com.atlassian.plugins.codegen.AbstractCodegenTestCase;
6   import com.atlassian.plugins.codegen.modules.PluginModuleLocation;
7   
8   import org.dom4j.Document;
9   import org.dom4j.Node;
10  import org.junit.Before;
11  import org.junit.Test;
12  
13  import static org.junit.Assert.assertEquals;
14  import static org.junit.Assert.assertNotNull;
15  
16  /**
17   * @since 3.6
18   */
19  public class DownloadablePluginResourceTest extends AbstractCodegenTestCase<DownloadablePluginResourceProperties>
20  {
21  
22      public static final String XPATH_RESOURCE = "/atlassian-plugin/resource";
23      public static final String XPATH_PARAM_RELATIVE = "param";
24  
25      /*
26          The base module creator auto-adds an i18n resource, and so all of our tests need to add 1 to their counts
27       */
28  
29      @Before
30      public void setupCreator() throws Exception
31      {
32          setCreator(new DownloadablePluginResourceModuleCreator());
33          setModuleLocation(new PluginModuleLocation.Builder(srcDir)
34                  .resourcesDirectory(resourcesDir)
35                  .testDirectory(testDir)
36                  .templateDirectory(templateDir)
37                  .build());
38      }
39  
40      @Test
41      public void singleResourceAdded() throws Exception
42      {
43          Resource resource = new Resource();
44          resource.setName("style.css");
45          resource.setLocation("com/example/plugin/style.css");
46          resource.setType("download");
47  
48          setProps(new DownloadablePluginResourceProperties(resource));
49          creator.createModule(moduleLocation, props);
50  
51          Document pluginDoc = getXmlDocument(pluginXml);
52          List<Node> resourceList = pluginDoc.selectNodes(XPATH_RESOURCE);
53  
54          assertEquals("expected single resource", 2, resourceList.size());
55  
56          String nodeXpath = "//resource[@name='style.css' and @location='com/example/plugin/style.css' and @type='download']";
57          assertNotNull("single resource not found", pluginDoc.selectSingleNode(nodeXpath));
58  
59      }
60  
61      @Test
62      public void singleResourceNamePatternAdded() throws Exception
63      {
64          Resource resource = new Resource();
65          resource.setNamePattern("*.css");
66          resource.setLocation("com/example/plugin/style.css");
67          resource.setType("download");
68  
69          setProps(new DownloadablePluginResourceProperties(resource));
70          creator.createModule(moduleLocation, props);
71  
72          Document pluginDoc = getXmlDocument(pluginXml);
73          List<Node> resourceList = pluginDoc.selectNodes(XPATH_RESOURCE);
74  
75          assertEquals("expected single resource", 2, resourceList.size());
76  
77          String nodeXpath = "//resource[@namePattern='*.css' and @location='com/example/plugin/style.css' and @type='download']";
78          assertNotNull("single resource not found", pluginDoc.selectSingleNode(nodeXpath));
79  
80      }
81  
82      @Test
83      public void nameChosenOverPattern() throws Exception
84      {
85          Resource resource = new Resource();
86          resource.setName("style.css");
87          resource.setNamePattern("*.css");
88          resource.setLocation("com/example/plugin/style.css");
89          resource.setType("download");
90  
91          setProps(new DownloadablePluginResourceProperties(resource));
92          creator.createModule(moduleLocation, props);
93  
94          Document pluginDoc = getXmlDocument(pluginXml);
95          List<Node> resourceList = pluginDoc.selectNodes(XPATH_RESOURCE);
96  
97          assertEquals("expected single resource", 2, resourceList.size());
98  
99          String nodeXpath = "//resource[not(@namePattern) and @name='style.css' and @location='com/example/plugin/style.css' and @type='download']";
100         assertNotNull("single resource not found", pluginDoc.selectSingleNode(nodeXpath));
101 
102     }
103 
104     @Test
105     public void resourceParamsAdded() throws Exception
106     {
107         Resource resource = new Resource();
108         resource.setName("style.css");
109         resource.setLocation("com/example/plugin/style.css");
110         resource.setType("download");
111         resource.getParams()
112                 .put("content-type", "text/css");
113         resource.getParams()
114                 .put("awesome", "me");
115 
116         setProps(new DownloadablePluginResourceProperties(resource));
117         creator.createModule(moduleLocation, props);
118 
119         Document pluginDoc = getXmlDocument(pluginXml);
120         List<Node> resourceList = pluginDoc.selectNodes(XPATH_RESOURCE);
121 
122         assertEquals("expected single resource", 2, resourceList.size());
123 
124         String nodeXpath = "//resource[not(@namePattern) and @name='style.css' and @location='com/example/plugin/style.css' and @type='download']";
125         Node resourceNode = pluginDoc.selectSingleNode(nodeXpath);
126 
127         List<Node> paramList = resourceNode.selectNodes(XPATH_PARAM_RELATIVE);
128         assertEquals("expected resource params", 2, paramList.size());
129 
130         assertNotNull("missing content param", resourceNode.selectSingleNode("param[@name='content-type' and @value='text/css']"));
131         assertNotNull("missing awesome param", resourceNode.selectSingleNode("param[@name='awesome' and @value='me']"));
132 
133     }
134 
135 
136 }