1
1
package org .jenkinsci .plugins .buildnamesetter ;
2
2
3
- import static org .junit .Assert .assertEquals ;
4
-
5
- import java .io .IOException ;
6
- import java .util .concurrent .ExecutionException ;
3
+ import static org .junit .jupiter .api .Assertions .assertDoesNotThrow ;
4
+ import static org .junit .jupiter .api .Assertions .assertEquals ;
7
5
8
6
import hudson .EnvVars ;
9
7
import hudson .model .FreeStyleBuild ;
10
8
import hudson .model .FreeStyleProject ;
11
9
import hudson .model .Result ;
12
10
import hudson .model .TaskListener ;
13
11
import org .jenkinsci .plugins .EnvironmentVarSetter ;
14
- import org .junit .Assert ;
15
- import org .junit .Rule ;
16
- import org .junit .Test ;
12
+ import org .junit .jupiter .api .Test ;
17
13
import org .jvnet .hudson .test .Issue ;
18
14
import org .jvnet .hudson .test .JenkinsRule ;
15
+ import org .jvnet .hudson .test .junit .jupiter .WithJenkins ;
19
16
20
- public class BuildNameSetterTest {
21
-
22
- @ Rule
23
- public JenkinsRule jenkins = new JenkinsRule ();
17
+ @ WithJenkins
18
+ class BuildNameSetterTest {
24
19
25
20
@ Test
26
- public void shouldExpand_BUILD_NUMBER_macro () throws Exception {
21
+ void shouldExpand_BUILD_NUMBER_macro (JenkinsRule jenkins ) throws Exception {
27
22
FreeStyleProject fooProj = jenkins .createFreeStyleProject ("foo" );
28
23
fooProj .getBuildWrappersList ().add (getDefaultSetter ("a_#${BUILD_NUMBER}" ));
29
24
@@ -32,7 +27,7 @@ public void shouldExpand_BUILD_NUMBER_macro() throws Exception {
32
27
}
33
28
34
29
@ Test
35
- public void shouldExpand_JOB_NAME_full_env_macro () throws InterruptedException , ExecutionException , IOException {
30
+ void shouldExpand_JOB_NAME_full_env_macro (JenkinsRule jenkins ) throws Exception {
36
31
FreeStyleProject barProj = jenkins .createFreeStyleProject ("bar" );
37
32
barProj .getBuildWrappersList ().add (getDefaultSetter ("b_${ENV,var=\" JOB_NAME\" }" ));
38
33
@@ -42,7 +37,7 @@ public void shouldExpand_JOB_NAME_full_env_macro() throws InterruptedException,
42
37
43
38
@ Issue ("13347" )
44
39
@ Test
45
- public void shouldExpand_JOB_NAME_macro () throws InterruptedException , ExecutionException , IOException {
40
+ void shouldExpand_JOB_NAME_macro (JenkinsRule jenkins ) throws Exception {
46
41
FreeStyleProject barProj = jenkins .createFreeStyleProject ("bar" );
47
42
barProj .getBuildWrappersList ().add (getDefaultSetter ("c_${JOB_NAME}" ));
48
43
@@ -52,7 +47,7 @@ public void shouldExpand_JOB_NAME_macro() throws InterruptedException, Execution
52
47
53
48
@ Issue ("13347" )
54
49
@ Test
55
- public void shouldExpand_JOB_NAME_macro_twice () throws InterruptedException , ExecutionException , IOException {
50
+ void shouldExpand_JOB_NAME_macro_twice (JenkinsRule jenkins ) throws Exception {
56
51
FreeStyleProject barProj = jenkins .createFreeStyleProject ("bar" );
57
52
barProj .getBuildWrappersList ().add (getDefaultSetter ("c_${JOB_NAME}_d_${JOB_NAME}" ));
58
53
@@ -62,7 +57,7 @@ public void shouldExpand_JOB_NAME_macro_twice() throws InterruptedException, Exe
62
57
63
58
@ Issue ("13347" )
64
59
@ Test
65
- public void shouldExpand_NODE_NAME_macro_and_JOB_NAME_full_env_macro () throws InterruptedException , ExecutionException , IOException {
60
+ void shouldExpand_NODE_NAME_macro_and_JOB_NAME_full_env_macro (JenkinsRule jenkins ) throws Exception {
66
61
FreeStyleProject fooProj = jenkins .createFreeStyleProject ("foo" );
67
62
fooProj .getBuildWrappersList ().add (getDefaultSetter ("d_${NODE_NAME}_${ENV,var=\" JOB_NAME\" }" ));
68
63
@@ -72,30 +67,26 @@ public void shouldExpand_NODE_NAME_macro_and_JOB_NAME_full_env_macro() throws In
72
67
73
68
@ Issue ("34181" )
74
69
@ Test
75
- public void shouldUse_default_config_values_if_null () throws InterruptedException , ExecutionException , IOException {
70
+ void shouldUse_default_config_values_if_null (JenkinsRule jenkins ) throws Exception {
76
71
FreeStyleProject fooProj = jenkins .createFreeStyleProject ("foo" );
77
72
fooProj .getBuildWrappersList ().add (new BuildNameSetter ("${ENV,var=\" JOB_NAME\" }" , null , null ));
78
73
79
74
FreeStyleBuild fooBuild = fooProj .scheduleBuild2 (0 ).get ();
80
75
assertDisplayName (fooBuild , "foo" );
81
76
}
82
77
83
- private void assertDisplayName (FreeStyleBuild build , String expectedName ) {
78
+ private static void assertDisplayName (FreeStyleBuild build , String expectedName ) {
84
79
assertEquals (Result .SUCCESS , build .getResult ());
85
80
assertEquals (expectedName , build .getDisplayName ());
86
81
EnvironmentVarSetter action = build .getAction (EnvironmentVarSetter .class );
87
82
assertEquals (expectedName , action .getVar (EnvironmentVarSetter .buildDisplayNameVar ));
88
- EnvVars envVars = null ;
89
- try {
90
- envVars = build .getEnvironment (TaskListener .NULL );
91
- } catch (Exception e ) {
92
- e .printStackTrace ();
93
- Assert .fail ("Exception was thrown during getting build environment:" + e .getMessage ());
94
- }
83
+ EnvVars envVars = assertDoesNotThrow (
84
+ () -> build .getEnvironment (TaskListener .NULL ),
85
+ "Exception was thrown during getting build environment" );
95
86
assertEquals (expectedName , envVars .get (EnvironmentVarSetter .buildDisplayNameVar ));
96
87
}
97
88
98
- private BuildNameSetter getDefaultSetter (String template ) {
89
+ private static BuildNameSetter getDefaultSetter (String template ) {
99
90
return new BuildNameSetter (template , true , true );
100
91
}
101
92
}
0 commit comments