-
Notifications
You must be signed in to change notification settings - Fork 35
/
SystemGroovyChoiceListProviderJenkinsTest.java
322 lines (276 loc) · 12.3 KB
/
SystemGroovyChoiceListProviderJenkinsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/*
* The MIT License
*
* Copyright (c) 2013 IKEDA Yasuyuki
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package jp.ikedam.jenkins.plugins.extensible_choice_parameter;
import static org.junit.Assert.*;
import hudson.model.FreeStyleProject;
import hudson.model.ParametersDefinitionProperty;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import java.util.Arrays;
import java.util.List;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlElement;
import com.gargoylesoftware.htmlunit.html.HtmlPage;
import com.gargoylesoftware.htmlunit.html.HtmlSelect;
import jenkins.model.Jenkins;
/**
* Tests for SystemGroovyChoiceListProvider, corresponding to Jenkins.
*/
public class SystemGroovyChoiceListProviderJenkinsTest
{
@Rule
public ExtensibleChoiceParameterJenkinsRule j = new ExtensibleChoiceParameterJenkinsRule();
static private String properScript = "[\"a\", \"b\", \"c\"]";
static private List<String> properScriptReturn = Arrays.asList("a", "b", "c");
static private String nonstringScript = "[1, 2, 3]";
static private List<String> nonstringScriptReturn = Arrays.asList("1", "2", "3");
static private String nonlistScript = "return \"abc\"";
static private String emptyListScript = "def ret = []";
static private String nullScript = "null;";
static private String emptyScript = "";
static private String blankScript = " ";
static private String syntaxBrokenScript = "1abc = []";
static private String exceptionScript = "hogehoge()";
protected SystemGroovyChoiceListProvider.DescriptorImpl getDescriptor()
{
return (SystemGroovyChoiceListProvider.DescriptorImpl)Jenkins.getInstance().getDescriptorOrDie(SystemGroovyChoiceListProvider.class);
}
@Test
public void testDescriptor_doFillDefaultChoiceItems()
{
SystemGroovyChoiceListProvider.DescriptorImpl descriptor = getDescriptor();
// Proper script
{
ListBoxModel ret = descriptor.doFillDefaultChoiceItems(properScript);
assertEquals("Script returned an unexpected list", properScriptReturn.size() + 1, ret.size());
for(int i = 0; i < properScriptReturn.size(); ++i)
{
assertEquals("Script returned an unexpected list", properScriptReturn.get(i), ret.get(i + 1).value);
}
}
// Non-string list script
{
ListBoxModel ret = descriptor.doFillDefaultChoiceItems(nonstringScript);
assertEquals("Script returned an unexpected list", nonstringScriptReturn.size() + 1, ret.size());
for(int i = 0; i < nonstringScriptReturn.size(); ++i)
{
assertEquals("Script returned an unexpected list", nonstringScriptReturn.get(i), ret.get(i + 1).value);
}
}
// non-list script
{
ListBoxModel ret = descriptor.doFillDefaultChoiceItems(nonlistScript);
assertEquals("Script returning non-list must return an empty list", 1, ret.size());
}
// Empty list script
{
ListBoxModel ret = descriptor.doFillDefaultChoiceItems(emptyListScript);
assertEquals("Script must return an empty list", 1, ret.size());
}
// Null script
{
ListBoxModel ret = descriptor.doFillDefaultChoiceItems(nullScript);
assertEquals("Script with null must return an empty list", 1, ret.size());
}
// emptyScript
{
ListBoxModel ret = descriptor.doFillDefaultChoiceItems(emptyScript);
assertEquals("empty script must return an empty list", 1, ret.size());
}
// blankScript
{
ListBoxModel ret = descriptor.doFillDefaultChoiceItems(blankScript);
assertEquals("blank script must return an empty list", 1, ret.size());
}
// Syntax broken script
{
ListBoxModel ret = descriptor.doFillDefaultChoiceItems(syntaxBrokenScript);
assertEquals("Syntax-broken-script must return an empty list", 1, ret.size());
}
// exceptionScript
{
ListBoxModel ret = descriptor.doFillDefaultChoiceItems(exceptionScript);
assertEquals("Script throwing an exception must return an empty list", 1, ret.size());
}
// null
{
ListBoxModel ret = descriptor.doFillDefaultChoiceItems(null);
assertEquals("null must return an empty list", 1, ret.size());
}
}
@Test
public void testDescriptor_doTest()
{
SystemGroovyChoiceListProvider.DescriptorImpl descriptor = getDescriptor();
// Proper script
{
FormValidation formValidation = descriptor.doTest(properScript);
assertEquals("Test for proper script must succeed", FormValidation.Kind.OK, formValidation.kind);
}
// Syntax broken script
{
FormValidation formValidation = descriptor.doTest(syntaxBrokenScript);
assertEquals("Test for broken script must fail", FormValidation.Kind.ERROR, formValidation.kind);
}
// Script raising an exception
{
FormValidation formValidation = descriptor.doTest(exceptionScript);
assertEquals("Test for script raising an exception must fail", FormValidation.Kind.ERROR, formValidation.kind);
}
// Script returning non-list
{
FormValidation formValidation = descriptor.doTest(nonlistScript);
assertEquals("Test for script returning non-list must fail", FormValidation.Kind.ERROR, formValidation.kind);
}
// Script returning null
{
FormValidation formValidation = descriptor.doTest(nullScript);
assertEquals("Test for script retuning null must fail", FormValidation.Kind.ERROR, formValidation.kind);
}
}
@Test
public void testGetChoiceList()
{
// Proper script
{
SystemGroovyChoiceListProvider target = new SystemGroovyChoiceListProvider(
properScript,
null
);
List<String> ret = target.getChoiceList();
assertEquals("Script returned an unexpected list", properScriptReturn.size(), ret.size());
for(int i = 0; i < properScriptReturn.size(); ++i)
{
assertEquals("Script returned an unexpected list", properScriptReturn.get(i), ret.get(i));
}
}
// non-string list script
{
SystemGroovyChoiceListProvider target = new SystemGroovyChoiceListProvider(
nonstringScript,
null
);
List<String> ret = target.getChoiceList();
assertEquals("Script returned an unexpected list", nonstringScriptReturn.size(), ret.size());
for(int i = 0; i < nonstringScriptReturn.size(); ++i)
{
assertEquals("Script returned an unexpected list", nonstringScriptReturn.get(i), ret.get(i));
}
}
// non-list script
{
SystemGroovyChoiceListProvider target = new SystemGroovyChoiceListProvider(
nonlistScript,
null
);
List<String> ret = target.getChoiceList();
assertEquals("Script retuning non-list must be fixed to an empty list", 0, ret.size());
}
// Empty list script
{
SystemGroovyChoiceListProvider target = new SystemGroovyChoiceListProvider(
emptyListScript,
null
);
List<String> ret = target.getChoiceList();
assertEquals("Script must return an empty list", 0, ret.size());
}
// Null script
{
SystemGroovyChoiceListProvider target = new SystemGroovyChoiceListProvider(
nullScript,
null
);
List<String> ret = target.getChoiceList();
assertEquals("Script with null must return an empty list", 0, ret.size());
}
// Empty script
{
SystemGroovyChoiceListProvider target = new SystemGroovyChoiceListProvider(
emptyScript,
null
);
List<String> ret = target.getChoiceList();
assertEquals("Empty script must return an empty list", 0, ret.size());
}
// Blank script
{
SystemGroovyChoiceListProvider target = new SystemGroovyChoiceListProvider(
blankScript,
null
);
List<String> ret = target.getChoiceList();
assertEquals("Blank script must return an empty list", 0, ret.size());
}
// Syntax broken script
{
SystemGroovyChoiceListProvider target = new SystemGroovyChoiceListProvider(
syntaxBrokenScript,
null
);
List<String> ret = target.getChoiceList();
assertEquals("Syntax-broken-script must return an empty list", 0, ret.size());
}
// exceptionScript
{
SystemGroovyChoiceListProvider target = new SystemGroovyChoiceListProvider(
exceptionScript,
null
);
List<String> ret = target.getChoiceList();
assertEquals("Script throwing an exception must return an empty list", 0, ret.size());
}
// null
{
SystemGroovyChoiceListProvider target = new SystemGroovyChoiceListProvider(
null,
null
);
List<String> ret = target.getChoiceList();
assertEquals("null must return an empty list", 0, ret.size());
}
}
@Test
public void testVariables() throws Exception
{
FreeStyleProject p = j.createFreeStyleProject();
p.addProperty(new ParametersDefinitionProperty(new ExtensibleChoiceParameterDefinition(
"test",
new SystemGroovyChoiceListProvider("[jenkins.rootDir.absolutePath, project.fullName]", null),
false,
"test"
)));
WebClient wc = j.createAllow405WebClient();
HtmlPage page = wc.getPage(p, "build");
List<HtmlElement> elements = page.getElementsByTagName("select");
assertEquals(1, elements.size());
assertTrue(elements.get(0) instanceof HtmlSelect);
HtmlSelect sel = (HtmlSelect)elements.get(0);
assertEquals(2, sel.getOptionSize());
assertEquals(j.jenkins.getRootDir().getAbsolutePath(), sel.getOption(0).getValueAttribute());
assertEquals(p.getFullName(), sel.getOption(1).getValueAttribute());
}
}