1 package com.atlassian.plugins.codegen.modules.jira;
2
3 import java.io.File;
4 import java.util.List;
5 import java.util.Properties;
6 import java.util.regex.Matcher;
7
8 import com.atlassian.plugins.codegen.AbstractCodegenTestCase;
9 import com.atlassian.plugins.codegen.modules.PluginModuleLocation;
10 import com.atlassian.plugins.codegen.modules.common.Label;
11 import com.atlassian.plugins.codegen.modules.common.Resource;
12
13 import org.dom4j.Document;
14 import org.dom4j.Node;
15 import org.junit.Before;
16 import org.junit.Test;
17
18 import static org.junit.Assert.*;
19
20
21
22
23 public class ReportTest extends AbstractCodegenTestCase<ReportProperties>
24 {
25 public static final String PACKAGE_NAME = "com.atlassian.plugins.jira.reports";
26 public static final String XPATH_RESOURCE = "/atlassian-plugin/*//resource";
27 public static final String XPATH_PARAM_RELATIVE = "param";
28
29 @Before
30 public void runGenerator() throws Exception
31 {
32 setCreator(new ReportModuleCreator());
33 setModuleLocation(new PluginModuleLocation.Builder(srcDir)
34 .resourcesDirectory(resourcesDir)
35 .testDirectory(testDir)
36 .templateDirectory(templateDir)
37 .build());
38
39 setProps(new ReportProperties(PACKAGE_NAME + ".MyReport"));
40
41 props.setIncludeExamples(false);
42
43
44 }
45
46 @Test
47 public void allFilesAreGenerated() throws Exception
48 {
49 creator.createModule(moduleLocation, props);
50
51 String packagePath = PACKAGE_NAME.replaceAll("\\.", Matcher.quoteReplacement(File.separator));
52 assertTrue("main class not generated", new File(srcDir, packagePath + File.separator + "MyReport.java").exists());
53 assertTrue("test class not generated", new File(testDir, packagePath + File.separator + "MyReportTest.java").exists());
54 assertTrue("plugin.xml not generated", new File(resourcesDir, "atlassian-plugin.xml").exists());
55
56 }
57
58 @Test
59 public void i18nGenerated() throws Exception
60 {
61 Resource resource = new Resource();
62 resource.setName("i18n");
63 resource.setLocation("MyReport");
64 resource.setType("i18n");
65
66 props.getResources()
67 .add(resource);
68
69 creator.createModule(moduleLocation, props);
70
71 assertTrue("i18n not generated", new File(resourcesDir, "MyReport.properties").exists());
72 }
73
74 @Test
75 public void moduleIsValid() throws Exception
76 {
77 String xpath = "/atlassian-plugin/report[@name='My Report' and @key='my-report' and @i18n-name-key='my-report.name' and @class='" + PACKAGE_NAME + ".MyReport']";
78
79 creator.createModule(moduleLocation, props);
80 Document pluginDoc = getXmlDocument(pluginXml);
81
82 assertNotNull("valid report not found", pluginDoc.selectSingleNode(xpath));
83 }
84
85 @Test
86 public void labelAdded() throws Exception
87 {
88 String paramVal0 = "$helper.project.name";
89 String paramVal1 = "$helper.project.description";
90 Label label = new Label("common.concepts.create.new.issue", "create new issue");
91 label.addParam(paramVal0);
92 label.addParam(paramVal1);
93
94 props.setLabel(label);
95
96 String labelXpath = "/atlassian-plugin/report/label[@key='common.concepts.create.new.issue']";
97
98 creator.createModule(moduleLocation, props);
99 Document pluginDoc = getXmlDocument(pluginXml);
100
101 Node labelNode = pluginDoc.selectSingleNode(labelXpath);
102
103 assertNotNull("label not found", labelNode);
104
105 Node param0 = labelNode.selectSingleNode("param[@name='param0' and @value='" + paramVal0 + "']");
106 Node param1 = labelNode.selectSingleNode("param[@name='param1' and @value='" + paramVal1 + "']");
107
108 assertNotNull("param 0 not found", param0);
109 assertNotNull("param 1 not found", param1);
110
111 Properties i18nprops = loadI18nProperties();
112 assertTrue("label i18n not found", i18nprops.containsKey(label.getKey()));
113 assertEquals("label i18n has wrong value", label.getValue(), i18nprops.getProperty(label.getKey()));
114
115 }
116
117 @Test
118 public void singleResourceAdded() throws Exception
119 {
120 Resource resource = new Resource();
121 resource.setName("style.css");
122 resource.setLocation("com/example/plugin/style.css");
123 resource.setType("download");
124
125 props.getResources()
126 .add(resource);
127
128 creator.createModule(moduleLocation, props);
129
130 Document pluginDoc = getXmlDocument(pluginXml);
131 List<Node> resourceList = pluginDoc.selectNodes(XPATH_RESOURCE);
132
133 assertEquals("expected single resource", 1, resourceList.size());
134
135 String nodeXpath = "//resource[@name='style.css' and @location='com/example/plugin/style.css' and @type='download']";
136 assertNotNull("single resource not found", pluginDoc.selectSingleNode(nodeXpath));
137
138 }
139
140 @Test
141 public void singleResourceNamePatternAdded() throws Exception
142 {
143 Resource resource = new Resource();
144 resource.setNamePattern("*.css");
145 resource.setLocation("com/example/plugin/style.css");
146 resource.setType("download");
147
148 props.getResources()
149 .add(resource);
150
151 creator.createModule(moduleLocation, props);
152
153 Document pluginDoc = getXmlDocument(pluginXml);
154 List<Node> resourceList = pluginDoc.selectNodes(XPATH_RESOURCE);
155
156 assertEquals("expected single resource", 1, resourceList.size());
157
158 String nodeXpath = "//resource[@namePattern='*.css' and @location='com/example/plugin/style.css' and @type='download']";
159 assertNotNull("single resource not found", pluginDoc.selectSingleNode(nodeXpath));
160
161 }
162
163 @Test
164 public void nameChosenOverPattern() throws Exception
165 {
166 Resource resource = new Resource();
167 resource.setName("style.css");
168 resource.setNamePattern("*.css");
169 resource.setLocation("com/example/plugin/style.css");
170 resource.setType("download");
171
172 props.getResources()
173 .add(resource);
174
175 creator.createModule(moduleLocation, props);
176
177 Document pluginDoc = getXmlDocument(pluginXml);
178 List<Node> resourceList = pluginDoc.selectNodes(XPATH_RESOURCE);
179
180 assertEquals("expected single resource", 1, resourceList.size());
181
182 String nodeXpath = "//resource[not(@namePattern) and @name='style.css' and @location='com/example/plugin/style.css' and @type='download']";
183 assertNotNull("single resource not found", pluginDoc.selectSingleNode(nodeXpath));
184
185 }
186
187 @Test
188 public void resourceParamsAdded() throws Exception
189 {
190 Resource resource = new Resource();
191 resource.setName("style.css");
192 resource.setLocation("com/example/plugin/style.css");
193 resource.setType("download");
194 resource.getParams()
195 .put("content-type", "text/css");
196 resource.getParams()
197 .put("awesome", "me");
198
199 props.getResources()
200 .add(resource);
201
202 creator.createModule(moduleLocation, props);
203
204 Document pluginDoc = getXmlDocument(pluginXml);
205 List<Node> resourceList = pluginDoc.selectNodes(XPATH_RESOURCE);
206
207 assertEquals("expected single resource", 1, resourceList.size());
208
209 String nodeXpath = "//resource[not(@namePattern) and @name='style.css' and @location='com/example/plugin/style.css' and @type='download']";
210 Node resourceNode = pluginDoc.selectSingleNode(nodeXpath);
211
212 List<Node> paramList = resourceNode.selectNodes(XPATH_PARAM_RELATIVE);
213 assertEquals("expected resource params", 2, paramList.size());
214
215 assertNotNull("missing content param", resourceNode.selectSingleNode("param[@name='content-type' and @value='text/css']"));
216 assertNotNull("missing awesome param", resourceNode.selectSingleNode("param[@name='awesome' and @value='me']"));
217
218 }
219
220 @Test
221 public void multipleResourcesAdded() throws Exception
222 {
223 Resource resource = new Resource();
224 resource.setName("style.css");
225 resource.setLocation("com/example/plugin/style.css");
226 resource.setType("download");
227 resource.getParams()
228 .put("content-type", "text/css");
229 resource.getParams()
230 .put("awesome", "me");
231
232 Resource resource2 = new Resource();
233 resource2.setName("custom.js");
234 resource2.setLocation("com/example/plugin/custom.js");
235 resource2.setType("download");
236
237 props.getResources()
238 .add(resource);
239 props.getResources()
240 .add(resource2);
241
242 creator.createModule(moduleLocation, props);
243
244 Document pluginDoc = getXmlDocument(pluginXml);
245 List<Node> resourceList = pluginDoc.selectNodes(XPATH_RESOURCE);
246
247 assertEquals("expected multiple resources", 2, resourceList.size());
248
249 String nodeXpath = "//resource[not(@namePattern) and @name='style.css' and @location='com/example/plugin/style.css' and @type='download']";
250 String node2Xpath = "//resource[not(@namePattern) and @name='custom.js' and @location='com/example/plugin/custom.js' and @type='download']";
251
252 assertNotNull("missing css resource", pluginDoc.selectSingleNode(nodeXpath));
253 assertNotNull("missing js resource", pluginDoc.selectSingleNode(node2Xpath));
254
255 }
256 }