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 CustomFieldSearcherTest extends AbstractCodegenTestCase<CustomFieldSearcherProperties>
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 CustomFieldSearcherModuleCreator());
31          setModuleLocation(new PluginModuleLocation.Builder(srcDir)
32                  .resourcesDirectory(resourcesDir)
33                  .testDirectory(testDir)
34                  .templateDirectory(templateDir)
35                  .build());
36  
37          setProps(new CustomFieldSearcherProperties(PACKAGE_NAME + ".MyCustomFieldSearcher"));
38          props.setIncludeExamples(false);
39      }
40  
41      @Test
42      public void allFilesAreGenerated() throws Exception
43      {
44          setProps(new CustomFieldSearcherProperties("com.atlassian.SomeBuiltInSearcher"));
45          props.setIncludeExamples(false);
46  
47          creator.createModule(moduleLocation, props);
48          String packagePath = "com.atlassian".replaceAll("\\.", Matcher.quoteReplacement(File.separator));
49  
50          assertFalse("main class was generated", new File(srcDir, packagePath + File.separator + "SomeBuiltInSearcher.java").exists());
51          assertFalse("test class was generated", new File(testDir, packagePath + File.separator + "SomeBuiltInSearcherTest.java").exists());
52          assertTrue("plugin.xml not generated", new File(resourcesDir, "atlassian-plugin.xml").exists());
53  
54      }
55  
56      @Test
57      public void allFilesAreGeneratedWithCustomClass() throws Exception
58      {
59          props.setGenerateClass(true);
60  
61          creator.createModule(moduleLocation, props);
62  
63          String packagePath = PACKAGE_NAME.replaceAll("\\.", Matcher.quoteReplacement(File.separator));
64          assertTrue("main class not generated", new File(srcDir, packagePath + File.separator + "MyCustomFieldSearcher.java").exists());
65          assertTrue("test class not generated", new File(testDir, packagePath + File.separator + "MyCustomFieldSearcherTest.java").exists());
66          assertTrue("plugin.xml not generated", new File(resourcesDir, "atlassian-plugin.xml").exists());
67      }
68  
69      @Test
70      public void moduleIsValid() throws Exception
71      {
72  
73          String xpath = "/atlassian-plugin/customfield-searcher[@name='My Custom Field Searcher' and @key='my-custom-field-searcher' and @i18n-name-key='my-custom-field-searcher.name' and @class='" + PACKAGE_NAME + ".MyCustomFieldSearcher']";
74  
75          creator.createModule(moduleLocation, props);
76          Document pluginDoc = getXmlDocument(pluginXml);
77  
78          assertNotNull("valid customfield-searcher not found", pluginDoc.selectSingleNode(xpath));
79      }
80  
81      @Test
82      public void moduleHasValidCustomField() throws Exception
83      {
84          props.setValidCustomFieldPackage("com.atlassian.customfields");
85          props.setValidCustomFieldKey("some-searcher");
86  
87          String xpath = "/atlassian-plugin/customfield-searcher/valid-customfield-type[@package='com.atlassian.customfields' and @key='some-searcher']";
88  
89          creator.createModule(moduleLocation, props);
90          Document pluginDoc = getXmlDocument(pluginXml);
91  
92          assertNotNull("valid custom field not found", pluginDoc.selectSingleNode(xpath));
93      }
94  
95      @Test
96      public void singleResourceAdded() throws Exception
97      {
98          Resource resource = new Resource();
99          resource.setName("style.css");
100         resource.setLocation("com/example/plugin/style.css");
101         resource.setType("download");
102 
103         props.getResources()
104                 .add(resource);
105 
106         creator.createModule(moduleLocation, props);
107 
108         Document pluginDoc = getXmlDocument(pluginXml);
109         List<Node> resourceList = pluginDoc.selectNodes(XPATH_RESOURCE);
110 
111         assertEquals("expected single resource", 1, resourceList.size());
112 
113         String nodeXpath = "//resource[@name='style.css' and @location='com/example/plugin/style.css' and @type='download']";
114         assertNotNull("single resource not found", pluginDoc.selectSingleNode(nodeXpath));
115 
116     }
117 
118     @Test
119     public void singleResourceNamePatternAdded() throws Exception
120     {
121         Resource resource = new Resource();
122         resource.setNamePattern("*.css");
123         resource.setLocation("com/example/plugin/style.css");
124         resource.setType("download");
125 
126         props.getResources()
127                 .add(resource);
128 
129         creator.createModule(moduleLocation, props);
130 
131         Document pluginDoc = getXmlDocument(pluginXml);
132         List<Node> resourceList = pluginDoc.selectNodes(XPATH_RESOURCE);
133 
134         assertEquals("expected single resource", 1, resourceList.size());
135 
136         String nodeXpath = "//resource[@namePattern='*.css' and @location='com/example/plugin/style.css' and @type='download']";
137         assertNotNull("single resource not found", pluginDoc.selectSingleNode(nodeXpath));
138 
139     }
140 
141     @Test
142     public void nameChosenOverPattern() throws Exception
143     {
144         Resource resource = new Resource();
145         resource.setName("style.css");
146         resource.setNamePattern("*.css");
147         resource.setLocation("com/example/plugin/style.css");
148         resource.setType("download");
149 
150         props.getResources()
151                 .add(resource);
152 
153         creator.createModule(moduleLocation, props);
154 
155         Document pluginDoc = getXmlDocument(pluginXml);
156         List<Node> resourceList = pluginDoc.selectNodes(XPATH_RESOURCE);
157 
158         assertEquals("expected single resource", 1, resourceList.size());
159 
160         String nodeXpath = "//resource[not(@namePattern) and @name='style.css' and @location='com/example/plugin/style.css' and @type='download']";
161         assertNotNull("single resource not found", pluginDoc.selectSingleNode(nodeXpath));
162 
163     }
164 
165     @Test
166     public void resourceParamsAdded() throws Exception
167     {
168         Resource resource = new Resource();
169         resource.setName("style.css");
170         resource.setLocation("com/example/plugin/style.css");
171         resource.setType("download");
172         resource.getParams()
173                 .put("content-type", "text/css");
174         resource.getParams()
175                 .put("awesome", "me");
176 
177         props.getResources()
178                 .add(resource);
179 
180         creator.createModule(moduleLocation, props);
181 
182         Document pluginDoc = getXmlDocument(pluginXml);
183         List<Node> resourceList = pluginDoc.selectNodes(XPATH_RESOURCE);
184 
185         assertEquals("expected single resource", 1, resourceList.size());
186 
187         String nodeXpath = "//resource[not(@namePattern) and @name='style.css' and @location='com/example/plugin/style.css' and @type='download']";
188         Node resourceNode = pluginDoc.selectSingleNode(nodeXpath);
189 
190         List<Node> paramList = resourceNode.selectNodes(XPATH_PARAM_RELATIVE);
191         assertEquals("expected resource params", 2, paramList.size());
192 
193         assertNotNull("missing content param", resourceNode.selectSingleNode("param[@name='content-type' and @value='text/css']"));
194         assertNotNull("missing awesome param", resourceNode.selectSingleNode("param[@name='awesome' and @value='me']"));
195 
196     }
197 
198     @Test
199     public void multipleResourcesAdded() throws Exception
200     {
201         Resource resource = new Resource();
202         resource.setName("style.css");
203         resource.setLocation("com/example/plugin/style.css");
204         resource.setType("download");
205         resource.getParams()
206                 .put("content-type", "text/css");
207         resource.getParams()
208                 .put("awesome", "me");
209 
210         Resource resource2 = new Resource();
211         resource2.setName("custom.js");
212         resource2.setLocation("com/example/plugin/custom.js");
213         resource2.setType("download");
214 
215         props.getResources()
216                 .add(resource);
217         props.getResources()
218                 .add(resource2);
219 
220         creator.createModule(moduleLocation, props);
221 
222         Document pluginDoc = getXmlDocument(pluginXml);
223         List<Node> resourceList = pluginDoc.selectNodes(XPATH_RESOURCE);
224 
225         assertEquals("expected multiple resources", 2, resourceList.size());
226 
227         String nodeXpath = "//resource[not(@namePattern) and @name='style.css' and @location='com/example/plugin/style.css' and @type='download']";
228         String node2Xpath = "//resource[not(@namePattern) and @name='custom.js' and @location='com/example/plugin/custom.js' and @type='download']";
229 
230         assertNotNull("missing css resource", pluginDoc.selectSingleNode(nodeXpath));
231         assertNotNull("missing js resource", pluginDoc.selectSingleNode(node2Xpath));
232 
233     }
234 
235 }