Skip to content

Commit 0050cc3

Browse files
Migrate tests to JUnit5
* Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup
1 parent 62c3398 commit 0050cc3

File tree

64 files changed

+2699
-2983
lines changed

Some content is hidden

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

64 files changed

+2699
-2983
lines changed

pom.xml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,6 @@
204204
<artifactId>test-harness</artifactId>
205205
<scope>test</scope>
206206
</dependency>
207-
<dependency>
208-
<groupId>junit</groupId>
209-
<artifactId>junit</artifactId>
210-
<scope>test</scope>
211-
</dependency>
212207
<dependency>
213208
<groupId>nl.jqno.equalsverifier</groupId>
214209
<artifactId>equalsverifier</artifactId>
@@ -236,7 +231,7 @@
236231
</dependency>
237232
<dependency>
238233
<groupId>org.mockito</groupId>
239-
<artifactId>mockito-core</artifactId>
234+
<artifactId>mockito-junit-jupiter</artifactId>
240235
<scope>test</scope>
241236
</dependency>
242237
<dependency>

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

Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -7,56 +7,45 @@
77
import org.eclipse.jgit.lib.ObjectId;
88
import org.eclipse.jgit.lib.ObjectIdRef;
99
import org.eclipse.jgit.lib.Ref;
10-
import org.junit.Test;
10+
import org.junit.jupiter.api.Test;
1111

12-
public class BranchTest {
12+
class BranchTest {
1313

14-
private final String branchSHA1;
15-
private final String branchName;
16-
private final ObjectId branchHead;
17-
private final Branch branch;
18-
private final String refPrefix;
19-
private final Ref branchRef;
20-
private final Branch branchFromRef;
21-
22-
private static final String REMOTE_BRANCH_NAME = "origin/master";
23-
24-
public BranchTest() {
25-
this.branchSHA1 = "fa71f704f9b90fa1f857d1623f3fe33fa2277ca9";
26-
this.branchName = REMOTE_BRANCH_NAME;
27-
this.branchHead = ObjectId.fromString(branchSHA1);
28-
this.refPrefix = "refs/remotes/";
29-
this.branchRef = new ObjectIdRef.PeeledNonTag(Ref.Storage.NEW, refPrefix + branchName, branchHead);
30-
this.branch = new Branch(branchName, branchHead);
31-
this.branchFromRef = new Branch(branchRef);
32-
}
14+
private static final String BRANCH_SHA_1 = "fa71f704f9b90fa1f857d1623f3fe33fa2277ca9";
15+
private static final String BRANCH_NAME = "origin/master";
16+
private static final ObjectId BRANCH_HEAD = ObjectId.fromString(BRANCH_SHA_1);
17+
private static final Branch BRANCH = new Branch(BRANCH_NAME, BRANCH_HEAD);
18+
private static final String REF_PREFIX = "refs/remotes/";
19+
private static final Ref BRANCH_REF =
20+
new ObjectIdRef.PeeledNonTag(Ref.Storage.NEW, REF_PREFIX + BRANCH_NAME, BRANCH_HEAD);
21+
private static final Branch BRANCH_FROM_REF = new Branch(BRANCH_REF);
3322

3423
@Test
35-
public void testToString() {
36-
assertThat(branch.toString(), is(branchFromRef.toString()));
24+
void testToString() {
25+
assertThat(BRANCH.toString(), is(BRANCH_FROM_REF.toString()));
3726
}
3827

3928
@Test
40-
public void testToString_Contents() {
41-
String expected = "Branch " + branchName + "(" + branchSHA1 + ")";
42-
assertThat(branch.toString(), is(expected));
29+
void testToString_Contents() {
30+
String expected = "Branch " + BRANCH_NAME + "(" + BRANCH_SHA_1 + ")";
31+
assertThat(BRANCH.toString(), is(expected));
4332
}
4433

4534
@Test
46-
public void hashCodeContract() {
47-
assertThat(branch, is(branchFromRef));
48-
assertThat(branch.hashCode(), is(branchFromRef.hashCode()));
35+
void hashCodeContract() {
36+
assertThat(BRANCH, is(BRANCH_FROM_REF));
37+
assertThat(BRANCH.hashCode(), is(BRANCH_FROM_REF.hashCode()));
4938
}
5039

5140
@Test
52-
public void constructorRefArgStripped() {
53-
Ref ref = new ObjectIdRef.PeeledNonTag(Ref.Storage.LOOSE, refPrefix + branchName, branchHead);
41+
void constructorRefArgStripped() {
42+
Ref ref = new ObjectIdRef.PeeledNonTag(Ref.Storage.LOOSE, REF_PREFIX + BRANCH_NAME, BRANCH_HEAD);
5443
Branch strippedBranch = new Branch(ref);
55-
assertThat(strippedBranch.getName(), is(branchName));
44+
assertThat(strippedBranch.getName(), is(BRANCH_NAME));
5645
}
5746

5847
@Test
59-
public void equalsContract() {
48+
void equalsContract() {
6049
EqualsVerifier.forClass(Branch.class)
6150
.usingGetClass()
6251
.withRedefinedSuperclass()

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

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
import static org.hamcrest.MatcherAssert.assertThat;
44
import static org.hamcrest.Matchers.is;
5-
import static org.junit.Assert.assertThrows;
6-
import static org.junit.Assert.assertTrue;
5+
import static org.junit.jupiter.api.Assertions.assertThrows;
6+
import static org.junit.jupiter.api.Assertions.assertTrue;
77

88
import hudson.EnvVars;
99
import hudson.model.TaskListener;
@@ -13,46 +13,50 @@
1313
import java.nio.charset.StandardCharsets;
1414
import java.nio.file.Files;
1515
import org.jenkinsci.plugins.gitclient.GitClient;
16-
import org.junit.Before;
17-
import org.junit.Rule;
18-
import org.junit.Test;
19-
import org.junit.rules.TemporaryFolder;
16+
import org.junit.jupiter.api.BeforeEach;
17+
import org.junit.jupiter.api.Test;
18+
import org.junit.jupiter.api.io.TempDir;
2019

21-
public class GitAPIBadInitTest {
20+
class GitAPIBadInitTest {
2221

23-
@Rule
24-
public TemporaryFolder tempFolder = new TemporaryFolder();
22+
@TempDir
23+
private File tempFolder;
2524

26-
private final EnvVars env;
27-
28-
public GitAPIBadInitTest() {
29-
env = new EnvVars();
30-
}
25+
private final EnvVars env = new EnvVars();
3126

3227
private File tempDir;
3328
private TaskListener listener;
3429

35-
@Before
36-
public void setUp() throws IOException, InterruptedException {
37-
tempDir = tempFolder.newFolder();
30+
@BeforeEach
31+
void setUp() throws Exception {
32+
tempDir = newFolder(tempFolder, "junit");
3833
listener = StreamTaskListener.fromStderr();
3934
}
4035

4136
@Test
42-
public void testInitExistingDirectory() throws Exception {
37+
void testInitExistingDirectory() throws Exception {
4338
GitClient git = new GitAPI("git", tempDir, listener, env);
4439
git.init();
4540
File gitDir = new File(tempDir, ".git");
46-
assertTrue(gitDir + " not created", gitDir.exists());
47-
assertTrue(gitDir + " not a directory", gitDir.isDirectory());
41+
assertTrue(gitDir.exists(), gitDir + " not created");
42+
assertTrue(gitDir.isDirectory(), gitDir + " not a directory");
4843
}
4944

5045
@Test
51-
public void testInitExistingFile() throws Exception {
46+
void testInitExistingFile() throws Exception {
5247
File existingFile = new File(tempDir, "file-exists");
5348
Files.writeString(existingFile.toPath(), "git init should fail due to this file", StandardCharsets.UTF_8);
5449
GitClient git = new GitAPI("git", existingFile, listener, env);
5550
GitException e = assertThrows(GitException.class, git::init);
5651
assertThat(e.getMessage(), is("Could not init " + existingFile.getAbsolutePath()));
5752
}
53+
54+
private static File newFolder(File root, String... subDirs) throws Exception {
55+
String subFolder = String.join("/", subDirs);
56+
File result = new File(root, subFolder);
57+
if (!result.exists() && !result.mkdirs()) {
58+
throw new IOException("Couldn't create folders " + root);
59+
}
60+
return result;
61+
}
5862
}

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

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,35 @@
55
import static org.hamcrest.Matchers.is;
66
import static org.hamcrest.Matchers.isA;
77
import static org.hamcrest.Matchers.nullValue;
8-
import static org.junit.Assert.assertNotNull;
9-
import static org.junit.Assert.assertThrows;
8+
import static org.junit.jupiter.api.Assertions.assertNotNull;
9+
import static org.junit.jupiter.api.Assertions.assertThrows;
1010

1111
import hudson.EnvVars;
1212
import hudson.model.TaskListener;
1313
import java.io.File;
1414
import java.io.IOException;
15-
import java.nio.charset.StandardCharsets;
1615
import java.nio.file.Files;
1716
import org.eclipse.jgit.api.errors.JGitInternalException;
1817
import org.jenkinsci.plugins.gitclient.Git;
1918
import org.jenkinsci.plugins.gitclient.GitClient;
20-
import org.junit.Rule;
21-
import org.junit.Test;
22-
import org.junit.rules.TemporaryFolder;
19+
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.api.io.TempDir;
2321

24-
public class GitExceptionTest {
22+
class GitExceptionTest {
2523

26-
@Rule
27-
public TemporaryFolder folder = new TemporaryFolder();
24+
@TempDir
25+
private File folder;
2826

2927
@Test
30-
public void throwsGitException() {
28+
void throwsGitException() {
3129
GitException e = assertThrows(GitException.class, () -> {
3230
throw new GitException();
3331
});
3432
assertThat(e.getMessage(), is(nullValue()));
3533
}
3634

3735
@Test
38-
public void throwsGitExceptionExpectedMessage() {
36+
void throwsGitExceptionExpectedMessage() {
3937
String message = "My custom git exception message";
4038
GitException e = assertThrows(GitException.class, () -> {
4139
throw new GitException(message);
@@ -44,7 +42,7 @@ public void throwsGitExceptionExpectedMessage() {
4442
}
4543

4644
@Test
47-
public void throwsGitExceptionExpectedMessageWithCause() {
45+
void throwsGitExceptionExpectedMessageWithCause() {
4846
String message = "My custom git exception message";
4947
GitException e = assertThrows(GitException.class, () -> {
5048
throw new GitException(message, new IOException("Custom IOException message"));
@@ -54,7 +52,7 @@ public void throwsGitExceptionExpectedMessageWithCause() {
5452
}
5553

5654
@Test
57-
public void initCliImplThrowsGitException() throws IOException, InterruptedException {
55+
void initCliImplThrowsGitException() throws Exception {
5856
if (new File("/").canWrite()) { // running as root?
5957
return;
6058
}
@@ -72,7 +70,7 @@ public void initCliImplThrowsGitException() throws IOException, InterruptedExcep
7270
}
7371

7472
@Test
75-
public void initJGitImplThrowsGitException() throws IOException, InterruptedException {
73+
void initJGitImplThrowsGitException() throws Exception {
7674
if (new File("/").canWrite()) { // running as root?
7775
return;
7876
}
@@ -91,10 +89,10 @@ public void initJGitImplThrowsGitException() throws IOException, InterruptedExce
9189
}
9290

9391
@Test
94-
public void initCliImplCollisionThrowsGitException() throws IOException, InterruptedException {
95-
File dir = folder.getRoot();
96-
File dotGit = folder.newFile(".git");
97-
Files.write(dotGit.toPath(), "file named .git".getBytes(StandardCharsets.UTF_8), APPEND);
92+
void initCliImplCollisionThrowsGitException() throws Exception {
93+
File dir = folder;
94+
File dotGit = newFile(folder, ".git");
95+
Files.writeString(dotGit.toPath(), "file named .git", APPEND);
9896
GitClient defaultClient =
9997
Git.with(TaskListener.NULL, new EnvVars()).in(dir).using("git").getClient();
10098
assertThrows(
@@ -103,10 +101,10 @@ public void initCliImplCollisionThrowsGitException() throws IOException, Interru
103101
}
104102

105103
@Test
106-
public void initJGitImplCollisionThrowsGitException() throws IOException, InterruptedException {
107-
File dir = folder.getRoot();
108-
File dotGit = folder.newFile(".git");
109-
Files.write(dotGit.toPath(), "file named .git".getBytes(StandardCharsets.UTF_8), APPEND);
104+
void initJGitImplCollisionThrowsGitException() throws Exception {
105+
File dir = folder;
106+
File dotGit = newFile(folder, ".git");
107+
Files.writeString(dotGit.toPath(), "file named .git", APPEND);
110108
GitClient defaultClient =
111109
Git.with(TaskListener.NULL, new EnvVars()).in(dir).using("jgit").getClient();
112110
JGitInternalException e = assertThrows(
@@ -122,4 +120,12 @@ public void initJGitImplCollisionThrowsGitException() throws IOException, Interr
122120
private static boolean isWindows() {
123121
return File.pathSeparatorChar == ';';
124122
}
123+
124+
private static File newFile(File parent, String child) throws Exception {
125+
File result = new File(parent, child);
126+
if (!result.createNewFile()) {
127+
throw new IOException("Couldn't create file " + result);
128+
}
129+
return result;
130+
}
125131
}

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,22 @@
33
import static org.hamcrest.MatcherAssert.assertThat;
44
import static org.hamcrest.Matchers.is;
55
import static org.hamcrest.Matchers.nullValue;
6-
import static org.junit.Assert.assertThrows;
6+
import static org.junit.jupiter.api.Assertions.assertThrows;
77

8-
import org.junit.Test;
8+
import org.junit.jupiter.api.Test;
99

10-
public class GitLockFailedExceptionTest {
10+
class GitLockFailedExceptionTest {
1111

1212
@Test
13-
public void throwsGitLockFailedException() {
13+
void throwsGitLockFailedException() {
1414
GitLockFailedException lockFailed = assertThrows(GitLockFailedException.class, () -> {
1515
throw new GitLockFailedException();
1616
});
1717
assertThat(lockFailed.getMessage(), is(nullValue()));
1818
}
1919

2020
@Test
21-
public void throwsGitLockFailedExceptionWithMessage() {
21+
void throwsGitLockFailedExceptionWithMessage() {
2222
String message = "My local exception message";
2323
GitLockFailedException lockFailed = assertThrows(GitLockFailedException.class, () -> {
2424
throw new GitLockFailedException(message);
@@ -27,7 +27,7 @@ public void throwsGitLockFailedExceptionWithMessage() {
2727
}
2828

2929
@Test
30-
public void throwsGitLockFailedExceptionWithCause() {
30+
void throwsGitLockFailedExceptionWithCause() {
3131
String message = "My git exception message";
3232
GitException e = new GitException(message);
3333
GitLockFailedException lockFailed = assertThrows(GitLockFailedException.class, () -> {
@@ -37,7 +37,7 @@ public void throwsGitLockFailedExceptionWithCause() {
3737
}
3838

3939
@Test
40-
public void throwsGitLockFailedExceptionWithCauseAndMessage() {
40+
void throwsGitLockFailedExceptionWithCauseAndMessage() {
4141
String message = "My git exception message";
4242
GitException e = new GitException(message);
4343
String lockMessage = "My lock message that is not part of the GitException";

0 commit comments

Comments
 (0)