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
20
21 public class SearchRequestViewTest extends AbstractCodegenTestCase<SearchRequestViewProperties>
22 {
23 public static final String PACKAGE_NAME = "com.atlassian.plugins.jira.search";
24 public static final String EXT = "html";
25 public static final String CTYPE = "text/html";
26 public static final String XPATH_RESOURCE = "/atlassian-plugin/*//resource";
27 public static final String XPATH_PARAM_RELATIVE = "param";
28
29
30 @Before
31 public void runGenerator() throws Exception
32 {
33 setCreator(new SearchRequestViewModuleCreator());
34 setModuleLocation(new PluginModuleLocation.Builder(srcDir)
35 .resourcesDirectory(resourcesDir)
36 .testDirectory(testDir)
37 .templateDirectory(templateDir)
38 .build());
39
40 setProps(new SearchRequestViewProperties(PACKAGE_NAME + ".MySearchRequestView"));
41
42 props.setIncludeExamples(false);
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
53 assertTrue("main class not generated", new File(srcDir, packagePath + File.separator + "MySearchRequestView.java").exists());
54 assertTrue("test class not generated", new File(testDir, packagePath + File.separator + "MySearchRequestViewTest.java").exists());
55 assertTrue("plugin.xml not generated", new File(resourcesDir, "atlassian-plugin.xml").exists());
56
57 }
58
59 @Test
60 public void moduleIsValid() throws Exception
61 {
62 props.setFileExtension(EXT);
63 props.setContentType(CTYPE);
64
65 String xpath = "/atlassian-plugin/search-request-view[@name='My Search Request View' and @key='my-search-request-view' and @i18n-name-key='my-search-request-view.name' and @class='" + PACKAGE_NAME + ".MySearchRequestView' and @fileExtension='html' and @contentType='text/html']";
66 creator.createModule(moduleLocation, props);
67 Document pluginDoc = getXmlDocument(pluginXml);
68
69 assertNotNull("valid search-request-view not found", pluginDoc.selectSingleNode(xpath));
70
71 }
72
73 @Test
74 public void singleResourceAdded() throws Exception
75 {
76 Resource resource = new Resource();
77 resource.setName("style.css");
78 resource.setLocation("com/example/plugin/style.css");
79 resource.setType("download");
80
81 props.getResources()
82 .add(resource);
83
84 creator.createModule(moduleLocation, props);
85
86 Document pluginDoc = getXmlDocument(pluginXml);
87 List<Node> resourceList = pluginDoc.selectNodes(XPATH_RESOURCE);
88
89 assertEquals("expected single resource", 1, resourceList.size());
90
91 String nodeXpath = "//resource[@name='style.css' and @location='com/example/plugin/style.css' and @type='download']";
92 assertNotNull("single resource not found", pluginDoc.selectSingleNode(nodeXpath));
93
94 }
95
96 @Test
97 public void singleResourceNamePatternAdded() throws Exception
98 {
99 Resource resource = new Resource();
100 resource.setNamePattern("*.css");
101 resource.setLocation("com/example/plugin/style.css");
102 resource.setType("download");
103
104 props.getResources()
105 .add(resource);
106
107 creator.createModule(moduleLocation, props);
108
109 Document pluginDoc = getXmlDocument(pluginXml);
110 List<Node> resourceList = pluginDoc.selectNodes(XPATH_RESOURCE);
111
112 assertEquals("expected single resource", 1, resourceList.size());
113
114 String nodeXpath = "//resource[@namePattern='*.css' and @location='com/example/plugin/style.css' and @type='download']";
115 assertNotNull("single resource not found", pluginDoc.selectSingleNode(nodeXpath));
116
117 }
118
119 @Test
120 public void nameChosenOverPattern() throws Exception
121 {
122 Resource resource = new Resource();
123 resource.setName("style.css");
124 resource.setNamePattern("*.css");
125 resource.setLocation("com/example/plugin/style.css");
126 resource.setType("download");
127
128 props.getResources()
129 .add(resource);
130
131 creator.createModule(moduleLocation, props);
132
133 Document pluginDoc = getXmlDocument(pluginXml);
134 List<Node> resourceList = pluginDoc.selectNodes(XPATH_RESOURCE);
135
136 assertEquals("expected single resource", 1, resourceList.size());
137
138 String nodeXpath = "//resource[not(@namePattern) and @name='style.css' and @location='com/example/plugin/style.css' and @type='download']";
139 assertNotNull("single resource not found", pluginDoc.selectSingleNode(nodeXpath));
140
141 }
142
143 @Test
144 public void resourceParamsAdded() throws Exception
145 {
146 Resource resource = new Resource();
147 resource.setName("style.css");
148 resource.setLocation("com/example/plugin/style.css");
149 resource.setType("download");
150 resource.getParams()
151 .put("content-type", "text/css");
152 resource.getParams()
153 .put("awesome", "me");
154
155 props.getResources()
156 .add(resource);
157
158 creator.createModule(moduleLocation, props);
159
160 Document pluginDoc = getXmlDocument(pluginXml);
161 List<Node> resourceList = pluginDoc.selectNodes(XPATH_RESOURCE);
162
163 assertEquals("expected single resource", 1, resourceList.size());
164
165 String nodeXpath = "//resource[not(@namePattern) and @name='style.css' and @location='com/example/plugin/style.css' and @type='download']";
166 Node resourceNode = pluginDoc.selectSingleNode(nodeXpath);
167
168 List<Node> paramList = resourceNode.selectNodes(XPATH_PARAM_RELATIVE);
169 assertEquals("expected resource params", 2, paramList.size());
170
171 assertNotNull("missing content param", resourceNode.selectSingleNode("param[@name='content-type' and @value='text/css']"));
172 assertNotNull("missing awesome param", resourceNode.selectSingleNode("param[@name='awesome' and @value='me']"));
173
174 }
175
176 @Test
177 public void multipleResourcesAdded() throws Exception
178 {
179 Resource resource = new Resource();
180 resource.setName("style.css");
181 resource.setLocation("com/example/plugin/style.css");
182 resource.setType("download");
183 resource.getParams()
184 .put("content-type", "text/css");
185 resource.getParams()
186 .put("awesome", "me");
187
188 Resource resource2 = new Resource();
189 resource2.setName("custom.js");
190 resource2.setLocation("com/example/plugin/custom.js");
191 resource2.setType("download");
192
193 props.getResources()
194 .add(resource);
195 props.getResources()
196 .add(resource2);
197
198 creator.createModule(moduleLocation, props);
199
200 Document pluginDoc = getXmlDocument(pluginXml);
201 List<Node> resourceList = pluginDoc.selectNodes(XPATH_RESOURCE);
202
203 assertEquals("expected multiple resources", 2, resourceList.size());
204
205 String nodeXpath = "//resource[not(@namePattern) and @name='style.css' and @location='com/example/plugin/style.css' and @type='download']";
206 String node2Xpath = "//resource[not(@namePattern) and @name='custom.js' and @location='com/example/plugin/custom.js' and @type='download']";
207
208 assertNotNull("missing css resource", pluginDoc.selectSingleNode(nodeXpath));
209 assertNotNull("missing js resource", pluginDoc.selectSingleNode(node2Xpath));
210
211 }
212
213 }