Skip to content

Commit 1439faf

Browse files
committed
Test getNextLabels and deprecated constructors more deeply
The getNextLabels test needs JenkinsRule and an agent, so those are added to the test. The deprecated constructor tests now check more values that are assigned during object construction.
1 parent 14fd1ad commit 1439faf

File tree

1 file changed

+77
-6
lines changed

1 file changed

+77
-6
lines changed

src/test/java/org/jvnet/jenkins/plugins/nodelabelparameter/LabelParameterValueTest.java

Lines changed: 77 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,39 @@
2323
*/
2424
package org.jvnet.jenkins.plugins.nodelabelparameter;
2525

26-
import static org.junit.Assert.assertNotNull;
26+
import static org.hamcrest.MatcherAssert.assertThat;
27+
import static org.hamcrest.Matchers.empty;
28+
import static org.hamcrest.Matchers.is;
29+
import static org.hamcrest.Matchers.nullValue;
2730

31+
import hudson.slaves.DumbSlave;
32+
import java.util.ArrayList;
33+
import java.util.Arrays;
34+
import java.util.List;
35+
import java.util.Random;
2836
import nl.jqno.equalsverifier.EqualsVerifier;
2937
import nl.jqno.equalsverifier.Warning;
38+
import org.junit.BeforeClass;
39+
import org.junit.ClassRule;
3040
import org.junit.Test;
41+
import org.jvnet.hudson.test.JenkinsRule;
42+
import org.jvnet.jenkins.plugins.nodelabelparameter.node.AllNodeEligibility;
43+
import org.jvnet.jenkins.plugins.nodelabelparameter.node.NodeEligibility;
3144

3245
public class LabelParameterValueTest {
3346

47+
@ClassRule
48+
public static JenkinsRule j = new JenkinsRule();
49+
50+
private static DumbSlave agent;
51+
52+
@BeforeClass
53+
public static void createAgent() throws Exception {
54+
agent = j.createOnlineSlave();
55+
}
56+
57+
private final Random random = new Random();
58+
3459
public LabelParameterValueTest() {}
3560

3661
@Test
@@ -43,14 +68,60 @@ public void testEqualsContract() {
4368
}
4469

4570
@Test
46-
public void testLabelParameterValue() {
47-
LabelParameterValue labelParameterValue = new LabelParameterValue("name", "label");
48-
assertNotNull(labelParameterValue);
71+
@Deprecated
72+
public void testLabelParameterValueDeprecated2ArgConstructor() {
73+
String value = " my-label "; // Intentionally has leading and trailing spaces
74+
String trimmedValue = value.trim();
75+
// Use either value or trimmedValue randomly, does not change any assertion
76+
LabelParameterValue labelParameterValue =
77+
new LabelParameterValue("my-name", random.nextBoolean() ? value : trimmedValue);
78+
assertThat(labelParameterValue.getName(), is("my-name"));
79+
assertThat(labelParameterValue.getLabel(), is(trimmedValue));
80+
assertThat(labelParameterValue.getDescription(), is(nullValue()));
81+
assertThat(labelParameterValue.getNextLabels(), is(empty()));
82+
}
83+
84+
@Test
85+
@Deprecated
86+
public void testLabelParameterValueDeprecated2ArgConstructorNullName() {
87+
String value = " my-label "; // Intentionally has leading and trailing spaces
88+
String trimmedValue = value.trim();
89+
// Use either value or trimmedValue randomly, does not change any assertion
90+
LabelParameterValue labelParameterValue =
91+
new LabelParameterValue(null, random.nextBoolean() ? value : trimmedValue);
92+
assertThat(labelParameterValue.getName(), is("NODELABEL"));
93+
assertThat(labelParameterValue.getLabel(), is(trimmedValue));
94+
assertThat(labelParameterValue.getDescription(), is(nullValue()));
95+
assertThat(labelParameterValue.getNextLabels(), is(empty()));
96+
}
97+
98+
@Test
99+
@Deprecated
100+
public void testLabelParameterValueDeprecated3ArgConstructor() {
101+
String value = " my-label "; // Intentionally has leading and trailing spaces
102+
String trimmedValue = value.trim();
103+
String name = "my-name";
104+
String description = "My description";
105+
// Use either value or trimmedValue randomly, does not change any assertion
106+
LabelParameterValue labelParameterValue =
107+
new LabelParameterValue(name, description, random.nextBoolean() ? value : trimmedValue);
108+
assertThat(labelParameterValue.getName(), is(name));
109+
assertThat(labelParameterValue.getLabel(), is(trimmedValue));
110+
assertThat(labelParameterValue.getDescription(), is(description));
111+
assertThat(labelParameterValue.getNextLabels(), is(empty()));
49112
}
50113

51114
@Test
52115
public void testGetNextLabels() {
53-
LabelParameterValue labelParameterValue = new LabelParameterValue("name", "label");
54-
assertNotNull(labelParameterValue.getNextLabels());
116+
String name = "my-name";
117+
List<String> extraNodeNames = Arrays.asList("built-in", "not-a-valid-node-name");
118+
List<String> nodeNames = new ArrayList<>();
119+
nodeNames.add(agent.getNodeName());
120+
nodeNames.addAll(extraNodeNames);
121+
NodeEligibility eligibility = new AllNodeEligibility();
122+
LabelParameterValue labelParameterValue = new LabelParameterValue(name, nodeNames, eligibility);
123+
assertThat(labelParameterValue.getName(), is(name));
124+
assertThat(labelParameterValue.getLabel(), is(agent.getNodeName()));
125+
assertThat(labelParameterValue.getNextLabels(), is(extraNodeNames));
55126
}
56127
}

0 commit comments

Comments
 (0)