1   package com.atlassian.plugins.codegen.modules.jira;
2   
3   import java.io.File;
4   import java.util.List;
5   import java.util.regex.Matcher;
6   
7   import com.atlassian.plugins.codegen.AbstractCodegenTestCase;
8   import com.atlassian.plugins.codegen.modules.PluginModuleLocation;
9   import com.atlassian.plugins.codegen.modules.common.Resource;
10  
11  import org.dom4j.Document;
12  import org.dom4j.Node;
13  import org.junit.Before;
14  import org.junit.Test;
15  
16  import static org.junit.Assert.*;
17  
18  /**
19   * @since 3.6
20   */
21  public class CustomFieldTest extends AbstractCodegenTestCase<CustomFieldProperties>
22  {
23      public static final String PACKAGE_NAME = "com.atlassian.plugins.jira.customfields";
24      public static final String XPATH_RESOURCE = "/atlassian-plugin/*//resource";
25      public static final String XPATH_PARAM_RELATIVE = "param";
26  
27      @Before
28      public void runGenerator() throws Exception
29      {
30          setCreator(new CustomFieldModuleCreator());
31          setModuleLocation(new PluginModuleLocation.Builder(srcDir)
32                  .resourcesDirectory(resourcesDir)
33                  .testDirectory(testDir)
34                  .templateDirectory(templateDir)
35                  .build());
36  
37          setProps(new CustomFieldProperties(PACKAGE_NAME + ".MyCustomField"));
38  
39          props.setIncludeExamples(false);
40  
41      }
42  
43      @Test
44      public void allFilesAreGenerated() throws Exception
45      {
46          creator.createModule(moduleLocation, props);
47  
48          String packagePath = PACKAGE_NAME.replaceAll("\\.", Matcher.quoteReplacement(File.separator));
49          assertTrue("main class not generated", new File(srcDir, packagePath + File.separator + "MyCustomField.java").exists());
50          assertTrue("test class not generated", new File(testDir, packagePath + File.separator + "MyCustomFieldTest.java").exists());
51          assertTrue("plugin.xml not generated", new File(resourcesDir, "atlassian-plugin.xml").exists());
52      }
53  
54      @Test
55      public void moduleIsValid() throws Exception
56      {
57          String xpath = "/atlassian-plugin/customfield-type[@name='My Custom Field' and @key='my-custom-field' and @i18n-name-key='my-custom-field.name' and @class='" + PACKAGE_NAME + ".MyCustomField']";
58  
59          creator.createModule(moduleLocation, props);
60          Document pluginDoc = getXmlDocument(pluginXml);
61  
62          assertNotNull("valid customfield-type not found", pluginDoc.selectSingleNode(xpath));
63      }
64  
65      @Test
66      public void singleResourceAdded() throws Exception
67      {
68          Resource resource = new Resource();
69          resource.setName("style.css");
70          resource.setLocation("com/example/plugin/style.css");
71          resource.setType("download");
72  
73          props.getResources()
74                  .add(resource);
75  
76          creator.createModule(moduleLocation, props);
77  
78          Document pluginDoc = getXmlDocument(pluginXml);
79          List<Node> resourceList = pluginDoc.selectNodes(XPATH_RESOURCE);
80  
81          assertEquals("expected single resource", 1, resourceList.size());
82  
83          String nodeXpath = "//resource[@name='style.css' and @location='com/example/plugin/style.css' and @type='download']";
84          assertNotNull("single resource not found", pluginDoc.selectSingleNode(nodeXpath));
85  
86      }
87  
88      @Test
89      public void singleResourceNamePatternAdded() throws Exception
90      {
91          Resource resource = new Resource();
92          resource.setNamePattern("*.css");
93          resource.setLocation("com/example/plugin/style.css");
94          resource.setType("download");
95  
96          props.getResources()
97                  .add(resource);
98  
99          creator.createModule(moduleLocation, props);
100 
101         Document pluginDoc = getXmlDocument(pluginXml);
102         List<Node> resourceList = pluginDoc.selectNodes(XPATH_RESOURCE);
103 
104         assertEquals("expected single resource", 1, resourceList.size());
105 
106         String nodeXpath = "//resource[@namePattern='*.css' and @location='com/example/plugin/style.css' and @type='download']";
107         assertNotNull("single resource not found", pluginDoc.selectSingleNode(nodeXpath));
108 
109     }
110 
111     @Test
112     public void nameChosenOverPattern() throws Exception
113     {
114         Resource resource = new Resource();
115         resource.setName("style.css");
116         resource.setNamePattern("*.css");
117         resource.setLocation("com/example/plugin/style.css");
118         resource.setType("download");
119 
120         props.getResources()
121                 .add(resource);
122 
123         creator.createModule(moduleLocation, props);
124 
125         Document pluginDoc = getXmlDocument(pluginXml);
126         List<Node> resourceList = pluginDoc.selectNodes(XPATH_RESOURCE);
127 
128         assertEquals("expected single resource", 1, resourceList.size());
129 
130         String nodeXpath = "//resource[not(@namePattern) and @name='style.css' and @location='com/example/plugin/style.css' and @type='download']";
131         assertNotNull("single resource not found", pluginDoc.selectSingleNode(nodeXpath));
132 
133     }
134 
135     @Test
136     public void resourceParamsAdded() throws Exception
137     {
138         Resource resource = new Resource();
139         resource.setName("style.css");
140         resource.setLocation("com/example/plugin/style.css");
141         resource.setType("download");
142         resource.getParams()
143                 .put("content-type", "text/css");
144         resource.getParams()
145                 .put("awesome", "me");
146 
147         props.getResources()
148                 .add(resource);
149 
150         creator.createModule(moduleLocation, props);
151 
152         Document pluginDoc = getXmlDocument(pluginXml);
153         List<Node> resourceList = pluginDoc.selectNodes(XPATH_RESOURCE);
154 
155         assertEquals("expected single resource", 1, resourceList.size());
156 
157         String nodeXpath = "//resource[not(@namePattern) and @name='style.css' and @location='com/example/plugin/style.css' and @type='download']";
158         Node resourceNode = pluginDoc.selectSingleNode(nodeXpath);
159 
160         List<Node> paramList = resourceNode.selectNodes(XPATH_PARAM_RELATIVE);
161         assertEquals("expected resource params", 2, paramList.size());
162 
163         assertNotNull("missing content param", resourceNode.selectSingleNode("param[@name='content-type' and @value='text/css']"));
164         assertNotNull("missing awesome param", resourceNode.selectSingleNode("param[@name='awesome' and @value='me']"));
165 
166     }
167 
168     @Test
169     public void multipleResourcesAdded() throws Exception
170     {
171         Resource resource = new Resource();
172         resource.setName("style.css");
173         resource.setLocation("com/example/plugin/style.css");
174         resource.setType("download");
175         resource.getParams()
176                 .put("content-type", "text/css");
177         resource.getParams()
178                 .put("awesome", "me");
179 
180         Resource resource2 = new Resource();
181         resource2.setName("custom.js");
182         resource2.setLocation("com/example/plugin/custom.js");
183         resource2.setType("download");
184 
185         props.getResources()
186                 .add(resource);
187         props.getResources()
188                 .add(resource2);
189 
190         creator.createModule(moduleLocation, props);
191 
192         Document pluginDoc = getXmlDocument(pluginXml);
193         List<Node> resourceList = pluginDoc.selectNodes(XPATH_RESOURCE);
194 
195         assertEquals("expected multiple resources", 2, resourceList.size());
196 
197         String nodeXpath = "//resource[not(@namePattern) and @name='style.css' and @location='com/example/plugin/style.css' and @type='download']";
198         String node2Xpath = "//resource[not(@namePattern) and @name='custom.js' and @location='com/example/plugin/custom.js' and @type='download']";
199 
200         assertNotNull("missing css resource", pluginDoc.selectSingleNode(nodeXpath));
201         assertNotNull("missing js resource", pluginDoc.selectSingleNode(node2Xpath));
202 
203     }
204 }