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