Skip to content

Commit e3ffd8a

Browse files
Migrate tests to JUnit5 (jenkinsci#1818)
* Migrate tests to JUnit5 * Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup * Use deprecated call to avoid issues with JUnit dependency versions --------- Co-authored-by: Mark Waite <[email protected]>
1 parent 1ed9f03 commit e3ffd8a

File tree

151 files changed

+3611
-3353
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

151 files changed

+3611
-3353
lines changed

pom.xml

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
<linkXRef>false</linkXRef>
4646
<spotbugs.effort>Max</spotbugs.effort>
4747
<spotbugs.threshold>Low</spotbugs.threshold>
48+
<ban-junit4-imports.skip>false</ban-junit4-imports.skip>
4849
</properties>
4950

5051
<dependencyManagement>
@@ -111,16 +112,6 @@
111112
<artifactId>mailer</artifactId>
112113
</dependency>
113114

114-
<dependency>
115-
<groupId>org.jenkins-ci.plugins</groupId>
116-
<artifactId>junit</artifactId>
117-
<scope>test</scope>
118-
</dependency>
119-
<dependency>
120-
<groupId>junit</groupId>
121-
<artifactId>junit</artifactId>
122-
<scope>test</scope>
123-
</dependency>
124115
<dependency>
125116
<groupId>org.mockito</groupId>
126117
<artifactId>mockito-core</artifactId>

src/test/java/hudson/plugins/git/AbstractGitProject.java

Lines changed: 23 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -48,31 +48,40 @@
4848
import java.util.List;
4949

5050
import jenkins.MasterToSlaveFileCallable;
51+
52+
import static org.junit.jupiter.api.Assertions.assertTrue;
5153
import org.eclipse.jgit.lib.Repository;
5254

5355
import org.jenkinsci.plugins.gitclient.Git;
5456
import org.jenkinsci.plugins.gitclient.JGitTool;
5557

56-
import static org.junit.Assert.assertTrue;
57-
58-
import org.junit.Rule;
59-
58+
import org.junit.jupiter.api.AfterEach;
59+
import org.junit.jupiter.api.BeforeEach;
6060
import org.jvnet.hudson.test.CaptureEnvironmentBuilder;
61-
import org.jvnet.hudson.test.FlagRule;
6261
import org.jvnet.hudson.test.JenkinsRule;
62+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
6363

6464
/**
6565
* Abstract class that provides convenience methods to configure projects.
6666
* @author Mark Waite
6767
*/
68-
public class AbstractGitProject extends AbstractGitRepository {
68+
@WithJenkins
69+
class AbstractGitProject extends AbstractGitRepository {
70+
71+
protected JenkinsRule r;
6972

70-
@Rule
71-
public JenkinsRule r = new JenkinsRule();
73+
private String notifyCommitAccessControl;
7274

73-
@Rule
74-
public FlagRule<String> notifyCommitAccessControl =
75-
new FlagRule<>(() -> GitStatus.NOTIFY_COMMIT_ACCESS_CONTROL, x -> GitStatus.NOTIFY_COMMIT_ACCESS_CONTROL = x);
75+
@BeforeEach
76+
void beforeEach(JenkinsRule rule) {
77+
r = rule;
78+
notifyCommitAccessControl = GitStatus.NOTIFY_COMMIT_ACCESS_CONTROL;
79+
}
80+
81+
@AfterEach
82+
void afterEach() throws Exception {
83+
GitStatus.NOTIFY_COMMIT_ACCESS_CONTROL = notifyCommitAccessControl;
84+
}
7685

7786
protected FreeStyleProject setupProject(List<BranchSpec> branches, boolean authorOrCommitter) throws Exception {
7887
FreeStyleProject project = r.createFreeStyleProject();
@@ -208,7 +217,7 @@ protected FreeStyleProject setupProject(List<UserRemoteConfig> repos, List<Branc
208217
protected FreeStyleBuild build(final FreeStyleProject project, final Result expectedResult, final String... expectedNewlyCommittedFiles) throws Exception {
209218
final FreeStyleBuild build = project.scheduleBuild2(0).get();
210219
for (final String expectedNewlyCommittedFile : expectedNewlyCommittedFiles) {
211-
assertTrue(expectedNewlyCommittedFile + " file not found in workspace", build.getWorkspace().child(expectedNewlyCommittedFile).exists());
220+
assertTrue(build.getWorkspace().child(expectedNewlyCommittedFile).exists(), expectedNewlyCommittedFile + " file not found in workspace");
212221
}
213222
if (expectedResult != null) {
214223
r.assertBuildStatus(expectedResult, build);
@@ -230,7 +239,7 @@ protected FreeStyleBuild build(final FreeStyleProject project, final String pare
230239
protected MatrixBuild build(final MatrixProject project, final Result expectedResult, final String... expectedNewlyCommittedFiles) throws Exception {
231240
final MatrixBuild build = project.scheduleBuild2(0).get();
232241
for (final String expectedNewlyCommittedFile : expectedNewlyCommittedFiles) {
233-
assertTrue(expectedNewlyCommittedFile + " file not found in workspace", build.getWorkspace().child(expectedNewlyCommittedFile).exists());
242+
assertTrue(build.getWorkspace().child(expectedNewlyCommittedFile).exists(), expectedNewlyCommittedFile + " file not found in workspace");
234243
}
235244
if (expectedResult != null) {
236245
r.assertBuildStatus(expectedResult, build);
@@ -239,7 +248,7 @@ protected MatrixBuild build(final MatrixProject project, final Result expectedRe
239248
}
240249

241250
protected String getHeadRevision(AbstractBuild build, final String branch) throws IOException, InterruptedException {
242-
return build.getWorkspace().act(new MasterToSlaveFileCallable<String>() {
251+
return build.getWorkspace().act(new MasterToSlaveFileCallable<>() {
243252
@Override
244253
public String invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
245254
try (@SuppressWarnings("deprecation") // Local repository reference

src/test/java/hudson/plugins/git/AbstractGitRepository.java

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@
88
import java.util.List;
99

1010
import hudson.plugins.git.util.GitUtilsTest;
11+
import jenkins.plugins.git.junit.jupiter.WithGitSampleRepo;
1112
import org.eclipse.jgit.util.SystemReader;
12-
import org.junit.Before;
13-
import org.junit.Rule;
14-
15-
import hudson.EnvVars;
13+
import org.junit.jupiter.api.BeforeEach;
1614
import hudson.model.TaskListener;
1715
import hudson.util.StreamTaskListener;
1816

@@ -30,16 +28,17 @@
3028
*
3129
* @author Mark Waite
3230
*/
31+
@WithGitSampleRepo
3332
public abstract class AbstractGitRepository {
3433

3534
protected File testGitDir;
3635
protected GitClient testGitClient;
3736

38-
@Rule
39-
public GitSampleRepoRule repo = new GitSampleRepoRule();
37+
protected GitSampleRepoRule repo;
4038

41-
@Before
42-
public void createGitRepository() throws Exception {
39+
@BeforeEach
40+
protected void beforeEach(GitSampleRepoRule repo) throws Exception {
41+
this.repo = repo;
4342
SystemReader.getInstance().getUserConfig().clear();
4443
TaskListener listener = StreamTaskListener.fromStderr();
4544
repo.init();
@@ -78,9 +77,4 @@ protected List<UserRemoteConfig> remoteConfigs() throws IOException {
7877
list.add(new UserRemoteConfig(testGitDir.getAbsolutePath(), "origin", "", null));
7978
return list;
8079
}
81-
82-
/** inline ${@link hudson.Functions#isWindows()} to prevent a transient remote classloader issue */
83-
private boolean isWindows() {
84-
return File.pathSeparatorChar==';';
85-
}
8680
}

src/test/java/hudson/plugins/git/AbstractGitTestCase.java

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,22 @@
3838
import java.util.List;
3939

4040
import jenkins.MasterToSlaveFileCallable;
41+
import jenkins.plugins.git.junit.jupiter.WithGitSampleRepo;
4142
import org.eclipse.jgit.lib.ObjectId;
4243
import org.eclipse.jgit.lib.PersonIdent;
4344
import org.eclipse.jgit.util.SystemReader;
4445
import org.jenkinsci.plugins.gitclient.Git;
4546
import org.jenkinsci.plugins.gitclient.GitClient;
4647
import org.jenkinsci.plugins.gitclient.JGitTool;
47-
import org.junit.Before;
48-
import org.junit.Rule;
48+
import org.junit.jupiter.api.BeforeEach;
4949
import jenkins.plugins.git.GitSampleRepoRule;
5050
import org.jvnet.hudson.test.CaptureEnvironmentBuilder;
5151
import org.jvnet.hudson.test.JenkinsRule;
5252
import org.jvnet.hudson.test.TestExtension;
5353

54-
import static org.junit.Assert.*;
54+
import static org.junit.jupiter.api.Assertions.assertTrue;
55+
56+
import org.jvnet.hudson.test.junit.jupiter.WithJenkins;
5557
import org.kohsuke.stapler.DataBoundConstructor;
5658

5759
/**
@@ -60,12 +62,13 @@
6062
* @author Kohsuke Kawaguchi
6163
* @author ishaaq
6264
*/
65+
@WithJenkins
66+
@WithGitSampleRepo
6367
public abstract class AbstractGitTestCase {
64-
@Rule
65-
public JenkinsRule r = new JenkinsRule();
6668

67-
@Rule
68-
public GitSampleRepoRule sampleRepo = new GitSampleRepoRule();
69+
protected JenkinsRule r;
70+
71+
protected GitSampleRepoRule sampleRepo;
6972

7073
protected TaskListener listener;
7174

@@ -78,8 +81,11 @@ public abstract class AbstractGitTestCase {
7881
protected FilePath workspace; // aliases "gitDirPath"
7982
protected GitClient git;
8083

81-
@Before
82-
public void setUp() throws Exception {
84+
@BeforeEach
85+
protected void beforeEach(JenkinsRule rule, GitSampleRepoRule repo) throws Exception {
86+
r = rule;
87+
sampleRepo = repo;
88+
8389
SystemReader.getInstance().getUserConfig().clear();
8490
listener = StreamTaskListener.fromStderr();
8591

@@ -246,7 +252,7 @@ protected FreeStyleProject setupSimpleProject(String branchString) throws Except
246252
protected FreeStyleBuild build(final FreeStyleProject project, final Result expectedResult, final String...expectedNewlyCommittedFiles) throws Exception {
247253
final FreeStyleBuild build = project.scheduleBuild2(0).get();
248254
for(final String expectedNewlyCommittedFile : expectedNewlyCommittedFiles) {
249-
assertTrue(expectedNewlyCommittedFile + " file not found in workspace", build.getWorkspace().child(expectedNewlyCommittedFile).exists());
255+
assertTrue(build.getWorkspace().child(expectedNewlyCommittedFile).exists(), expectedNewlyCommittedFile + " file not found in workspace");
250256
}
251257
if(expectedResult != null) {
252258
r.assertBuildStatus(expectedResult, build);
@@ -268,7 +274,7 @@ protected FreeStyleBuild build(final FreeStyleProject project, final String pare
268274
protected MatrixBuild build(final MatrixProject project, final Result expectedResult, final String...expectedNewlyCommittedFiles) throws Exception {
269275
final MatrixBuild build = project.scheduleBuild2(0).get();
270276
for(final String expectedNewlyCommittedFile : expectedNewlyCommittedFiles) {
271-
assertTrue(expectedNewlyCommittedFile + " file not found in workspace", build.getWorkspace().child(expectedNewlyCommittedFile).exists());
277+
assertTrue(build.getWorkspace().child(expectedNewlyCommittedFile).exists(), expectedNewlyCommittedFile + " file not found in workspace");
272278
}
273279
if(expectedResult != null) {
274280
r.assertBuildStatus(expectedResult, build);
@@ -294,19 +300,19 @@ protected void setVariables(Node node, EnvironmentVariablesNodeProperty.Entry...
294300
}
295301

296302
protected String getHeadRevision(AbstractBuild build, final String branch) throws IOException, InterruptedException {
297-
return build.getWorkspace().act(new MasterToSlaveFileCallable<String>() {
298-
public String invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
299-
try {
300-
@SuppressWarnings("deprecation") // Local repository reference
301-
org.eclipse.jgit.lib.Repository repo = Git.with(null, null).in(f).getClient().getRepository();
302-
ObjectId oid = repo.resolve("refs/heads/" + branch);
303-
return oid.name();
304-
} catch (GitException e) {
305-
throw new RuntimeException(e);
306-
}
303+
return build.getWorkspace().act(new MasterToSlaveFileCallable<>() {
304+
public String invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
305+
try {
306+
@SuppressWarnings("deprecation") // Local repository reference
307+
org.eclipse.jgit.lib.Repository repo = Git.with(null, null).in(f).getClient().getRepository();
308+
ObjectId oid = repo.resolve("refs/heads/" + branch);
309+
return oid.name();
310+
} catch (GitException e) {
311+
throw new RuntimeException(e);
307312
}
313+
}
308314

309-
});
315+
});
310316
}
311317

312318
public static class HasCredentialBuilder extends Builder {

src/test/java/hudson/plugins/git/BranchSpecTest.java

Lines changed: 27 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
package hudson.plugins.git;
22

33
import hudson.EnvVars;
4+
import org.junit.jupiter.api.Test;
5+
46
import java.util.HashMap;
5-
import static org.junit.Assert.*;
6-
import org.junit.Test;
7+
import static org.junit.jupiter.api.Assertions.*;
8+
79
import org.jvnet.hudson.test.Issue;
810

911

10-
public class BranchSpecTest {
11-
@Test
12-
public void testMatch() {
12+
class BranchSpecTest {
1313

14+
@Test
15+
void testMatch() {
1416
BranchSpec l = new BranchSpec("master");
1517
assertTrue(l.matches("origin/master"));
1618
assertFalse(l.matches("origin/something/master"));
@@ -54,9 +56,9 @@ public void testMatch() {
5456
assertTrue(p.matches("origin/x"));
5557
assertFalse(p.matches("origin/my-branch/b1"));
5658
}
57-
59+
5860
@Test
59-
public void testMatchEnv() {
61+
void testMatchEnv() {
6062
HashMap<String, String> envMap = new HashMap<>();
6163
envMap.put("master", "master");
6264
envMap.put("origin", "origin");
@@ -116,26 +118,28 @@ public void testMatchEnv() {
116118
}
117119

118120
@Test
119-
public void testEmptyName() {
121+
void testEmptyName() {
120122
BranchSpec branchSpec = new BranchSpec("");
121123
assertEquals("**",branchSpec.getName());
122124
}
123125

124-
@Test(expected = IllegalArgumentException.class)
125-
public void testNullName() {
126-
BranchSpec branchSpec = new BranchSpec(null);
126+
@Test
127+
void testNullName() {
128+
assertThrows(IllegalArgumentException.class, () -> {
129+
BranchSpec branchSpec = new BranchSpec(null);
130+
});
127131
}
128-
132+
129133
@Test
130-
public void testNameTrimming() {
134+
void testNameTrimming() {
131135
BranchSpec branchSpec = new BranchSpec(" master ");
132136
assertEquals("master",branchSpec.getName());
133137
branchSpec.setName(" other ");
134138
assertEquals("other",branchSpec.getName());
135139
}
136-
140+
137141
@Test
138-
public void testUsesRefsHeads() {
142+
void testUsesRefsHeads() {
139143
BranchSpec m = new BranchSpec("refs/heads/j*n*");
140144
assertTrue(m.matches("refs/heads/jenkins"));
141145
assertTrue(m.matches("refs/heads/jane"));
@@ -144,9 +148,9 @@ public void testUsesRefsHeads() {
144148
assertFalse(m.matches("origin/jenkins"));
145149
assertFalse(m.matches("remote/origin/jane"));
146150
}
147-
151+
148152
@Test
149-
public void testUsesJavaPatternDirectlyIfPrefixedWithColon() {
153+
void testUsesJavaPatternDirectlyIfPrefixedWithColon() {
150154
BranchSpec m = new BranchSpec(":^(?!(origin/prefix)).*");
151155
assertTrue(m.matches("origin"));
152156
assertTrue(m.matches("origin/master"));
@@ -159,7 +163,7 @@ public void testUsesJavaPatternDirectlyIfPrefixedWithColon() {
159163

160164
@Test
161165
@Issue("JENKINS-26842")
162-
public void testUsesJavaPatternWithRepetition() {
166+
void testUsesJavaPatternWithRepetition() {
163167
// match pattern from JENKINS-26842
164168
BranchSpec m = new BranchSpec(":origin/release-\\d{8}");
165169
assertTrue(m.matches("origin/release-20150101"));
@@ -169,7 +173,7 @@ public void testUsesJavaPatternWithRepetition() {
169173
}
170174

171175
@Test
172-
public void testUsesJavaPatternToExcludeMultipleBranches() {
176+
void testUsesJavaPatternToExcludeMultipleBranches() {
173177
BranchSpec m = new BranchSpec(":^(?!origin/master$|origin/develop$).*");
174178
assertTrue(m.matches("origin/branch1"));
175179
assertTrue(m.matches("origin/branch-2"));
@@ -193,7 +197,7 @@ private EnvVars createEnvMap(String key, String value) {
193197
*/
194198
@Test
195199
@Issue("JENKINS-6856")
196-
public void testUsesEnvValueWithBraces() {
200+
void testUsesEnvValueWithBraces() {
197201
EnvVars env = createEnvMap("GIT_BRANCH", "origin/master");
198202

199203
BranchSpec withBraces = new BranchSpec("${GIT_BRANCH}");
@@ -204,7 +208,7 @@ public void testUsesEnvValueWithBraces() {
204208

205209
@Test
206210
@Issue("JENKINS-6856")
207-
public void testUsesEnvValueWithoutBraces() {
211+
void testUsesEnvValueWithoutBraces() {
208212
EnvVars env = createEnvMap("GIT_BRANCH", "origin/master");
209213

210214
BranchSpec withoutBraces = new BranchSpec("$GIT_BRANCH");
@@ -215,7 +219,7 @@ public void testUsesEnvValueWithoutBraces() {
215219

216220
@Test
217221
@Issue("JENKINS-6856")
218-
public void testUsesEnvValueWithToken() {
222+
void testUsesEnvValueWithToken() {
219223
EnvVars env = createEnvMap("GIT_BRANCH", "origin/master");
220224

221225
BranchSpec withToken = new BranchSpec("${GIT_BRANCH,fullName=True}");
@@ -226,7 +230,7 @@ public void testUsesEnvValueWithToken() {
226230

227231
@Test
228232
@Issue("JENKINS-6856")
229-
public void testUsesEnvValueWithTokenFalse() {
233+
void testUsesEnvValueWithTokenFalse() {
230234
EnvVars env = createEnvMap("GIT_BRANCH", "origin/master");
231235

232236
BranchSpec withTokenFalse = new BranchSpec("${GIT_BRANCH,fullName=false}");

0 commit comments

Comments
 (0)