1
1
package org .jenkinsci .plugins .tokenmacro ;
2
2
3
- import static junit .framework .TestCase .assertEquals ;
4
- import static org .junit .Assert .fail ;
3
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
4
+ import static org .junit .jupiter .api .Assertions .assertThrows ;
5
+ import static org .junit .jupiter .api .Assertions .fail ;
5
6
6
7
import com .google .common .collect .ListMultimap ;
7
8
import hudson .FilePath ;
23
24
import jenkins .security .MasterToSlaveCallable ;
24
25
import org .jenkinsci .plugins .workflow .cps .CpsFlowDefinition ;
25
26
import org .jenkinsci .plugins .workflow .job .WorkflowJob ;
26
- import org .junit .Before ;
27
- import org .junit .Rule ;
28
- import org .junit .Test ;
27
+ import org .junit .jupiter .api .BeforeEach ;
28
+ import org .junit .jupiter .api .Test ;
29
29
import org .jvnet .hudson .test .JenkinsRule ;
30
30
import org .jvnet .hudson .test .TestExtension ;
31
+ import org .jvnet .hudson .test .junit .jupiter .WithJenkins ;
31
32
32
33
/**
33
34
* Created by acearl on 6/14/2016.
34
35
*/
35
- public class PipelineTest {
36
+ @ WithJenkins
37
+ class PipelineTest {
36
38
private StreamTaskListener listener ;
37
39
38
- @ Rule
39
- public final JenkinsRule j = new JenkinsRule ();
40
+ private JenkinsRule j ;
40
41
41
42
private DumbSlave agent ;
42
43
43
- @ Before
44
- public void setup () throws Exception {
44
+ @ BeforeEach
45
+ void setup (JenkinsRule j ) throws Exception {
46
+ this .j = j ;
45
47
agent = j .createOnlineSlave (Label .get ("agents" ));
46
48
}
47
49
48
50
@ Test
49
- public void testEnvironmentVariables () throws Exception {
51
+ void testEnvironmentVariables () throws Exception {
50
52
WorkflowJob job = j .jenkins .createProject (WorkflowJob .class , "foo" );
51
53
job .setDefinition (new CpsFlowDefinition (getPipeline ("any" , "${ENV, var=\" VERSION\" }" ), true ));
52
54
Run <?, ?> run = j .assertBuildStatusSuccess (job .scheduleBuild2 (0 ));
53
55
j .assertLogContains ("VERSION=1.0.0" , run );
54
56
}
55
57
56
58
@ Test
57
- public void testEnvironmentVariablesNoAgent () throws Exception {
59
+ void testEnvironmentVariablesNoAgent () throws Exception {
58
60
WorkflowJob job = j .jenkins .createProject (WorkflowJob .class , "foo" );
59
61
job .setDefinition (new CpsFlowDefinition (getPipeline ("none" , "${ENV, var=\" VERSION\" }" ), true ));
60
62
Run <?, ?> run = j .assertBuildStatusSuccess (job .scheduleBuild2 (0 ));
61
63
j .assertLogContains ("VERSION=1.0.0" , run );
62
64
}
63
65
64
66
@ Test
65
- public void testWorkspaceNeededNoAgent () throws Exception {
67
+ void testWorkspaceNeededNoAgent () throws Exception {
66
68
WorkflowJob job = j .jenkins .createProject (WorkflowJob .class , "foo" );
67
69
job .setDefinition (new CpsFlowDefinition (getPipeline ("none" , "${TEST_WS}" ), true ));
68
70
Run <?, ?> run = j .assertBuildStatus (Result .FAILURE , job .scheduleBuild2 (0 ));
69
71
j .assertLogContains ("Macro 'TEST_WS' can ony be evaluated in a workspace" , run );
70
72
}
71
73
72
74
@ Test
73
- public void testWorkspaceNeededWithAgent () throws Exception {
75
+ void testWorkspaceNeededWithAgent () throws Exception {
74
76
WorkflowJob job = j .jenkins .createProject (WorkflowJob .class , "foo" );
75
77
job .setDefinition (new CpsFlowDefinition (getPipeline ("any" , "${TEST_WS}" ), true ));
76
78
Run <?, ?> run = j .assertBuildStatusSuccess (job .scheduleBuild2 (0 ));
77
79
j .assertLogContains ("Workspace: foo" , run );
78
80
}
79
81
80
82
@ Test
81
- public void testFileNeededWithAgent () throws Exception {
83
+ void testFileNeededWithAgent () throws Exception {
82
84
WorkflowJob job = j .jenkins .createProject (WorkflowJob .class , "foo" );
83
85
FilePath workspace = agent .getWorkspaceFor (job );
84
86
workspace .mkdirs ();
@@ -91,7 +93,7 @@ public void testFileNeededWithAgent() throws Exception {
91
93
}
92
94
93
95
@ Test
94
- public void testEscapedExpandAll () throws Exception {
96
+ void testEscapedExpandAll () throws Exception {
95
97
WorkflowJob job = j .jenkins .createProject (WorkflowJob .class , "foo" );
96
98
97
99
job .setDefinition (new CpsFlowDefinition ("node('agents') {\n \t echo 'Hello, world'\n }" , true ));
@@ -101,35 +103,26 @@ public void testEscapedExpandAll() throws Exception {
101
103
assertEquals (j .jenkins .getRootUrl () + "job/foo/1/" , TokenMacro .expand (run , null , listener , "${BUILD_URL}" ));
102
104
assertEquals (j .jenkins .getRootUrl () + "job/foo/1/" , TokenMacro .expand (run , null , listener , "$BUILD_URL" ));
103
105
104
- assertEquals (
105
- "{abc=[def, ghi], jkl=[true]}" ,
106
- TokenMacro .expand (run , null , listener , "${TEST,abc=\" def\" ,abc=\" ghi\" ,jkl=true}" ));
106
+ assertEquals ("{abc=[def, ghi], jkl=[true]}" , TokenMacro .expand (run , null , listener , "${TEST,abc=\" def\" ,abc=\" ghi\" ,jkl=true}" ));
107
107
}
108
108
109
109
@ Test
110
- public void testException () throws Exception {
110
+ void testException () throws Exception {
111
111
WorkflowJob job = j .jenkins .createProject (WorkflowJob .class , "foo" );
112
112
job .setDefinition (new CpsFlowDefinition ("node('agents') {\n \t echo 'Hello, world'\n }" , true ));
113
113
Run <?, ?> run = j .assertBuildStatusSuccess (job .scheduleBuild2 (0 ));
114
114
115
115
listener = StreamTaskListener .fromStdout ();
116
116
117
- try {
118
- TokenMacro .expand (run , null , listener , "${TEST_NESTEDX}" );
119
- fail ();
120
- } catch (MacroEvaluationException e ) {
121
- // do nothing, just want to catch the exception when it occurs
122
- }
117
+ assertThrows (MacroEvaluationException .class , () -> TokenMacro .expand (run , null , listener , "${TEST_NESTEDX}" ));
123
118
124
119
assertEquals (" ${TEST_NESTEDX}" , TokenMacro .expand (run , null , listener , " ${TEST_NESTEDX}" , false , null ));
125
- assertEquals (
126
- "${TEST_NESTEDX,abc=\" def\" ,abc=\" ghi\" ,jkl=true}" ,
127
- TokenMacro .expand (
128
- run , null , listener , "${TEST_NESTEDX,abc=\" def\" ,abc=\" ghi\" ,jkl=true}" , false , null ));
120
+ assertEquals ("${TEST_NESTEDX,abc=\" def\" ,abc=\" ghi\" ,jkl=true}" , TokenMacro .expand (
121
+ run , null , listener , "${TEST_NESTEDX,abc=\" def\" ,abc=\" ghi\" ,jkl=true}" , false , null ));
129
122
}
130
123
131
124
@ Test
132
- public void testUnconvertedMacro () throws Exception {
125
+ void testUnconvertedMacro () throws Exception {
133
126
WorkflowJob job = j .jenkins .createProject (WorkflowJob .class , "foo" );
134
127
job .setDefinition (new CpsFlowDefinition ("node('agents') {\n \t echo 'Hello, world'\n }" , true ));
135
128
Run <?, ?> run = j .assertBuildStatusSuccess (job .scheduleBuild2 (0 ));
@@ -174,8 +167,7 @@ public String evaluate(
174
167
TaskListener listener ,
175
168
String macroName ,
176
169
Map <String , String > arguments ,
177
- ListMultimap <String , String > argumentMultimap )
178
- throws MacroEvaluationException , IOException , InterruptedException {
170
+ ListMultimap <String , String > argumentMultimap ) {
179
171
return evaluate (context , null , listener , macroName , arguments , argumentMultimap );
180
172
}
181
173
@@ -186,8 +178,7 @@ public String evaluate(
186
178
TaskListener listener ,
187
179
String macroName ,
188
180
Map <String , String > arguments ,
189
- ListMultimap <String , String > argumentMultimap )
190
- throws MacroEvaluationException , IOException , InterruptedException {
181
+ ListMultimap <String , String > argumentMultimap ) {
191
182
return argumentMultimap .toString ();
192
183
}
193
184
}
@@ -207,8 +198,7 @@ public String evaluate(
207
198
TaskListener listener ,
208
199
String macroName ,
209
200
Map <String , String > arguments ,
210
- ListMultimap <String , String > argumentMultimap )
211
- throws MacroEvaluationException , IOException , InterruptedException {
201
+ ListMultimap <String , String > argumentMultimap ) {
212
202
return "${TEST,abc=\" def\" ,abc=\" ghi\" ,jkl=true}" ;
213
203
}
214
204
@@ -243,8 +233,7 @@ public String evaluate(
243
233
TaskListener listener ,
244
234
String macroName ,
245
235
Map <String , String > arguments ,
246
- ListMultimap <String , String > argumentMultimap )
247
- throws MacroEvaluationException , IOException , InterruptedException {
236
+ ListMultimap <String , String > argumentMultimap ) {
248
237
return argumentMultimap .toString ();
249
238
}
250
239
}
@@ -276,11 +265,11 @@ public String evaluate(
276
265
277
266
@ Override
278
267
public Callable <String , IOException > getCallable (Run <?, ?> run , String root , TaskListener listener ) {
279
- return new MasterToSlaveCallable <String , IOException >() {
280
- @ Override
281
- public String call () throws IOException {
282
- return "Workspace: " + new File (root ).getName ();
283
- }
268
+ return new MasterToSlaveCallable <>() {
269
+ @ Override
270
+ public String call () {
271
+ return "Workspace: " + new File (root ).getName ();
272
+ }
284
273
};
285
274
}
286
275
}
0 commit comments