Skip to content

Commit fa37806

Browse files
committed
Migrate tests to JUnit5
Squashed commit of the following: commit 5567afe Author: git-client-user <[email protected]> Date: Tue Sep 9 09:28:13 2025 +0200 Do not create temporary files in project root commit d95a472 Merge: 4231ffe 3a4021b Author: Mark Waite <[email protected]> Date: Mon Sep 8 06:39:21 2025 -0600 Merge branch 'master' into migrate_to_junit5 commit 4231ffe Author: git-client-user <[email protected]> Date: Sat Sep 6 18:28:55 2025 -0600 Fix code formatting Spotless commit 357ada8 Author: Mark Waite <[email protected]> Date: Sat Sep 6 17:17:42 2025 -0600 Skip testSparseCheckoutWithCliGitLFS on CI Unreliable due to GitHub message commit e26556f Author: Mark Waite <[email protected]> Date: Sat Sep 6 12:34:13 2025 -0600 Improve JMH sample repository choices Include a wider range of small repositories in order to be more likely to detect shifts in the turning point where command line git performs better than JGit. Current data shows that JGit is faster than command line git for repositories up to 8 MB. Command line git is dramatically faster than JGit for the 450 MB repository that is the largest repository used in the test. commit f908818 Author: Mark Waite <[email protected]> Date: Sat Sep 6 07:58:11 2025 -0600 Ban JUnit 4 imports commit 9f25a4e Author: Mark Waite <[email protected]> Date: Sat Sep 6 07:56:20 2025 -0600 Correct comments in FolderForBenchmark commit b3534e1 Author: Mark Waite <[email protected]> Date: Sat Sep 6 07:23:15 2025 -0600 Use repository matching comment - cairoshell commit 73adf26 Author: Mark Waite <[email protected]> Date: Sat Sep 6 07:17:02 2025 -0600 Convert JGitAPIImplUnsupportedProtocolTest to JUnit 5 commit 97fdc99 Author: Mark Waite <[email protected]> Date: Sat Sep 6 06:57:55 2025 -0600 Remove vanilla benchmark test commit 2a893fd Merge: d86a764 10c56e5 Author: Mark Waite <[email protected]> Date: Sat Sep 6 06:47:24 2025 -0600 Merge branch 'master' into migrate_to_junit5 commit d86a764 Author: strangelookingnerd <[email protected]> Date: Mon Aug 25 13:46:26 2025 +0200 Fix file cleanup commit 690422e Author: strangelookingnerd <[email protected]> Date: Mon Aug 25 08:45:02 2025 +0200 Restore dependency declaration commit 8510a07 Merge: 6e3b929 ca8f10b Author: Mark Waite <[email protected]> Date: Sat Aug 23 10:53:56 2025 -0600 Merge branch 'master' into migrate_to_junit5 commit 6e3b929 Author: strangelookingnerd <[email protected]> Date: Thu Aug 14 11:50:43 2025 +0200 Migrate tests to JUnit5 * Migrate annotations and imports * Migrate assertions * Remove public visibility for test classes and methods * Minor code cleanup
1 parent ffecdc4 commit fa37806

File tree

69 files changed

+2751
-3196
lines changed

Some content is hidden

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

69 files changed

+2751
-3196
lines changed

.gitignore

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
target/
77
work/
8-
.@tmp/
98
.vs/
109
.vscode/
1110

pom.xml

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
<spotbugs.effort>Max</spotbugs.effort>
4747
<spotbugs.threshold>Low</spotbugs.threshold>
4848
<spotless.check.skip>false</spotless.check.skip>
49+
<ban-junit4-imports.skip>false</ban-junit4-imports.skip>
4950
</properties>
5051

5152
<dependencyManagement>
@@ -206,11 +207,6 @@
206207
<artifactId>test-harness</artifactId>
207208
<scope>test</scope>
208209
</dependency>
209-
<dependency>
210-
<groupId>junit</groupId>
211-
<artifactId>junit</artifactId>
212-
<scope>test</scope>
213-
</dependency>
214210
<dependency>
215211
<groupId>nl.jqno.equalsverifier</groupId>
216212
<artifactId>equalsverifier</artifactId>
@@ -238,7 +234,7 @@
238234
</dependency>
239235
<dependency>
240236
<groupId>org.mockito</groupId>
241-
<artifactId>mockito-core</artifactId>
237+
<artifactId>mockito-junit-jupiter</artifactId>
242238
<scope>test</scope>
243239
</dependency>
244240
<dependency>
@@ -247,12 +243,6 @@
247243
<version>3.4</version>
248244
<scope>test</scope>
249245
</dependency>
250-
<dependency>
251-
<groupId>org.testcontainers</groupId>
252-
<artifactId>testcontainers</artifactId>
253-
<version>1.21.3</version>
254-
<scope>test</scope>
255-
</dependency>
256246
</dependencies>
257247

258248
<repositories>

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.mkdirs()) {
58+
throw new IOException("Couldn't create folders " + result);
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)