23
23
*/
24
24
package org .jvnet .jenkins .plugins .nodelabelparameter ;
25
25
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 ;
27
30
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 ;
28
36
import nl .jqno .equalsverifier .EqualsVerifier ;
29
37
import nl .jqno .equalsverifier .Warning ;
38
+ import org .junit .BeforeClass ;
39
+ import org .junit .ClassRule ;
30
40
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 ;
31
44
32
45
public class LabelParameterValueTest {
33
46
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
+
34
59
public LabelParameterValueTest () {}
35
60
36
61
@ Test
@@ -43,14 +68,60 @@ public void testEqualsContract() {
43
68
}
44
69
45
70
@ 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 ()));
49
112
}
50
113
51
114
@ Test
52
115
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 ));
55
126
}
56
127
}
0 commit comments