Skip to content

Commit fc63c02

Browse files
committed
Broaden LabelParameterDefinitionTest
Assert more values are set by the constructors Use both deprecated constructors in a test and mark the test as deprecated so that the compiler does not warn about the use of deprecated methods. Test agent and controller doList and test a case that doList fails to find the agent label.
1 parent d19126e commit fc63c02

File tree

1 file changed

+86
-29
lines changed

1 file changed

+86
-29
lines changed
Lines changed: 86 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,113 @@
11
package org.jvnet.jenkins.plugins.nodelabelparameter;
22

3-
import static org.junit.Assert.assertEquals;
4-
import static org.junit.jupiter.api.Assertions.assertNotNull;
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.allOf;
5+
import static org.hamcrest.Matchers.containsString;
6+
import static org.hamcrest.Matchers.is;
7+
import static org.junit.Assert.assertFalse;
8+
import static org.junit.Assert.assertTrue;
59

610
import hudson.model.labels.LabelAtom;
711
import hudson.slaves.DumbSlave;
812
import hudson.util.FormValidation;
9-
import java.util.ArrayList;
10-
import java.util.List;
11-
import org.junit.Before;
12-
import org.junit.Rule;
13+
import org.junit.BeforeClass;
14+
import org.junit.ClassRule;
1315
import org.junit.Test;
1416
import org.jvnet.hudson.test.JenkinsRule;
1517

1618
public class LabelParameterDefinitionTest {
17-
@Rule
18-
public JenkinsRule j = new JenkinsRule();
1919

20-
@Before
21-
public void setUp() throws Exception {
22-
final DumbSlave node = j.createOnlineSlave(new LabelAtom("node"));
20+
@ClassRule
21+
public static JenkinsRule j = new JenkinsRule();
22+
23+
private static DumbSlave agent;
24+
25+
private static final String LABEL_NAME = "my-agent-label";
26+
private static final LabelAtom label = new LabelAtom(LABEL_NAME);
27+
28+
@BeforeClass
29+
public static void createAgent() throws Exception {
30+
agent = j.createOnlineSlave(label);
2331
}
2432

2533
@Test
26-
public void testNodeParameterDefinition() {
34+
@Deprecated
35+
public void testNodeParameterDefinitionDeprecated() {
2736
String name = "name";
28-
String description = "description";
29-
String defaultValue = "defaultValue";
30-
List<String> defaultSlaves = new ArrayList<>();
31-
List<String> allowedSlaves = new ArrayList<>();
32-
allowedSlaves.add("defaultValue");
33-
String triggerIfResult = "triggerIfResult";
34-
35-
LabelParameterDefinition nodeParameterDefinition1 =
37+
String description = "The description";
38+
String defaultValue = "built-in || master";
39+
String triggerIfResult = "The triggerIfResult value";
40+
41+
LabelParameterDefinition nodeParameterDefinition =
3642
new LabelParameterDefinition(name, description, defaultValue, true, true, triggerIfResult);
3743

38-
assertEquals(nodeParameterDefinition1.defaultValue, defaultValue);
44+
assertThat(nodeParameterDefinition.defaultValue, is(defaultValue));
45+
assertThat(nodeParameterDefinition.getName(), is(name));
46+
assertThat(nodeParameterDefinition.getDescription(), is(description));
47+
assertThat(nodeParameterDefinition.getDefaultParameterValue().getLabel(), is(defaultValue));
48+
assertThat(nodeParameterDefinition.getTriggerIfResult(), is(triggerIfResult));
49+
assertTrue(nodeParameterDefinition.isAllNodesMatchingLabel());
50+
}
51+
52+
@Test
53+
@Deprecated
54+
public void testNodeParameterDefinitionDeprecated3Arg() {
55+
String name = "name";
56+
String description = "The description";
57+
String defaultValue = "built-in || master";
3958

40-
LabelParameterDefinition nodeParameterDefinition2 =
41-
new LabelParameterDefinition(name, description, defaultValue, true, null, triggerIfResult);
59+
LabelParameterDefinition nodeParameterDefinition =
60+
new LabelParameterDefinition(name, description, defaultValue);
4261

43-
assertEquals(nodeParameterDefinition1.defaultValue, defaultValue);
62+
assertThat(nodeParameterDefinition.defaultValue, is(defaultValue));
63+
assertThat(nodeParameterDefinition.getName(), is(name));
64+
assertThat(nodeParameterDefinition.getDescription(), is(description));
65+
assertThat(nodeParameterDefinition.getDefaultParameterValue().getLabel(), is(defaultValue));
66+
assertThat(nodeParameterDefinition.getTriggerIfResult(), is("allCases"));
67+
assertFalse(nodeParameterDefinition.isAllNodesMatchingLabel());
4468
}
4569

4670
@Test
47-
public void testDoListNodesForLabel() throws Exception {
48-
LabelParameterDefinition.DescriptorImpl nodeParameterDefinition = new LabelParameterDefinition.DescriptorImpl();
71+
public void testNodeParameterDefinition() {
72+
String name = "name";
73+
String description = "The description";
74+
String defaultValue = "built-in || master";
75+
String triggerIfResult = "The triggerIfResult value";
4976

50-
String label = "node";
77+
LabelParameterDefinition nodeParameterDefinition =
78+
new LabelParameterDefinition(name, description, defaultValue, false, null, triggerIfResult);
5179

52-
FormValidation validation = nodeParameterDefinition.doListNodesForLabel(label);
80+
assertThat(nodeParameterDefinition.defaultValue, is(defaultValue));
81+
assertThat(nodeParameterDefinition.getName(), is(name));
82+
assertThat(nodeParameterDefinition.getDescription(), is(description));
83+
assertThat(nodeParameterDefinition.getDefaultParameterValue().getLabel(), is(defaultValue));
84+
assertThat(nodeParameterDefinition.getTriggerIfResult(), is(triggerIfResult));
85+
assertFalse(nodeParameterDefinition.isAllNodesMatchingLabel());
86+
}
87+
88+
@Test
89+
public void testDoListNodesForAgentLabel() throws Exception {
90+
LabelParameterDefinition.DescriptorImpl nodeParameterDefinition = new LabelParameterDefinition.DescriptorImpl();
91+
FormValidation validation = nodeParameterDefinition.doListNodesForLabel(LABEL_NAME);
92+
String msg = validation.getMessage();
93+
assertThat(msg, allOf(containsString("Matching nodes"), containsString(agent.getNodeName())));
94+
}
5395

54-
assertNotNull(validation);
96+
@Test
97+
public void testDoListNodesForControllerLabel() throws Exception {
98+
String controllerLabel = "built-in";
99+
LabelParameterDefinition.DescriptorImpl nodeParameterDefinition = new LabelParameterDefinition.DescriptorImpl();
100+
FormValidation validation = nodeParameterDefinition.doListNodesForLabel(controllerLabel);
101+
String msg = validation.getMessage();
102+
assertThat(msg, allOf(containsString("Matching nodes"), containsString(controllerLabel)));
103+
}
104+
105+
@Test
106+
public void testDoListNodesForNonExistentLabel() throws Exception {
107+
String badLabel = "this-label-does-not-exist";
108+
LabelParameterDefinition.DescriptorImpl nodeParameterDefinition = new LabelParameterDefinition.DescriptorImpl();
109+
FormValidation validation = nodeParameterDefinition.doListNodesForLabel(badLabel);
110+
String msg = validation.getMessage();
111+
assertThat(msg, allOf(containsString("The label expression"), containsString(badLabel)));
55112
}
56113
}

0 commit comments

Comments
 (0)