From 6e3b9294fdf42d2b530cfb446511583c5dbdc6b9 Mon Sep 17 00:00:00 2001
From: strangelookingnerd
<49242855+strangelookingnerd@users.noreply.github.com>
Date: Thu, 14 Aug 2025 11:50:43 +0200
Subject: [PATCH 01/12] Migrate tests to JUnit5
* Migrate annotations and imports
* Migrate assertions
* Remove public visibility for test classes and methods
* Minor code cleanup
---
pom.xml | 19 +-
.../java/hudson/plugins/git/BranchTest.java | 55 +-
.../hudson/plugins/git/GitAPIBadInitTest.java | 46 +-
.../hudson/plugins/git/GitExceptionTest.java | 50 +-
.../git/GitLockFailedExceptionTest.java | 14 +-
.../hudson/plugins/git/GitObjectTest.java | 55 +-
.../plugins/git/GitToolResolverTest.java | 20 +-
.../java/hudson/plugins/git/GitToolTest.java | 39 +-
.../hudson/plugins/git/IndexEntryTest.java | 55 +-
.../java/hudson/plugins/git/RevisionTest.java | 68 +-
src/test/java/hudson/plugins/git/TagTest.java | 23 +-
.../java/jmh/benchmark/BenchmarkRunner.java | 11 +-
.../plugins/gitclient/CliGitAPIImplTest.java | 38 +-
.../gitclient/CliGitAPITempFileTest.java | 108 +--
.../CliGitAPIWindowsFilePermissionsTest.java | 19 +-
.../plugins/gitclient/CliGitCommand.java | 29 +-
.../plugins/gitclient/CredentialsTest.java | 319 ++++---
.../gitclient/FilePermissionsTest.java | 92 +-
...va => GitAPICliGitNotInitializedTest.java} | 97 +-
.../gitclient/GitAPIForCliGitTest.java | 79 +-
.../GitAPIJGitNotInitializedTest.java | 65 +-
...est.java => GitAPINotInitializedTest.java} | 141 +--
.../plugins/gitclient/GitAPITest.java | 526 ++++++-----
.../plugins/gitclient/GitAPITestUpdate.java | 369 ++++----
.../gitclient/GitAPITestUpdateCliGit.java | 68 +-
.../gitclient/GitClientCliCloneTest.java | 46 +-
.../plugins/gitclient/GitClientCloneTest.java | 100 +--
.../plugins/gitclient/GitClientFetchTest.java | 101 +--
.../gitclient/GitClientMaintenanceTest.java | 128 +--
.../gitclient/GitClientSampleRepoRule.java | 103 +--
.../gitclient/GitClientSecurityTest.java | 107 +--
.../plugins/gitclient/GitClientTest.java | 848 +++++++++---------
...tHostKeyVerificationConfigurationTest.java | 14 +-
.../plugins/gitclient/GitJenkinsRuleTest.java | 21 +-
.../jenkinsci/plugins/gitclient/GitTest.java | 89 +-
.../GitToolConfiguratorJenkinsRuleTest.java | 24 +-
.../gitclient/GitToolConfiguratorTest.java | 64 +-
.../GitURIRequirementsBuilderTest.java | 6 +-
.../plugins/gitclient/JGitAPIImplTest.java | 8 +-
.../gitclient/JGitApacheAPIImplTest.java | 8 +-
.../gitclient/JGitLightweightTagTest.java | 34 +-
.../plugins/gitclient/JcascTest.java | 14 +-
.../LegacyCompatibleGitAPIImplJGitTest.java | 7 +-
.../LegacyCompatibleGitAPIImplTest.java | 109 +--
.../plugins/gitclient/LogHandlerTest.java | 46 +-
.../plugins/gitclient/MergeCommandTest.java | 305 ++++---
.../plugins/gitclient/NetrcTest.java | 115 +--
.../plugins/gitclient/PushSimpleTest.java | 32 +-
.../jenkinsci/plugins/gitclient/PushTest.java | 135 ++-
.../plugins/gitclient/RemoteGitImplTest.java | 83 +-
.../gitclient/SubmodulePatternStringTest.java | 47 +-
.../gitclient/UnsupportedCommandTest.java | 79 +-
.../gitclient/WarnTempDirValueTest.java | 72 +-
.../plugins/gitclient/WorkspaceWithRepo.java | 21 +-
.../cgit/GitCommandsExecutorTest.java | 119 ++-
.../CredentialsProviderImplTest.java | 52 +-
.../SmartCredentialsProviderTest.java | 46 +-
.../plugins/gitclient/jgit/AuthzTest.java | 45 +-
...reemptiveAuthHttpClientConnectionTest.java | 30 +-
.../AcceptFirstConnectionVerifierTest.java | 61 +-
.../verifier/KnownHostsFileVerifierTest.java | 41 +-
.../verifier/KnownHostsTestUtil.java | 42 +-
.../ManuallyProvidedKeyVerifierTest.java | 62 +-
.../verifier/NoHostKeyVerifierTest.java | 19 +-
64 files changed, 2696 insertions(+), 2992 deletions(-)
rename src/test/java/org/jenkinsci/plugins/gitclient/{GitAPICliGitNotIntializedTest.java => GitAPICliGitNotInitializedTest.java} (77%)
rename src/test/java/org/jenkinsci/plugins/gitclient/{GitAPINotIntializedTest.java => GitAPINotInitializedTest.java} (66%)
diff --git a/pom.xml b/pom.xml
index ec966361dd..7c4740ee24 100644
--- a/pom.xml
+++ b/pom.xml
@@ -204,11 +204,6 @@
test-harness
test
-
- junit
- junit
- test
-
nl.jqno.equalsverifier
equalsverifier
@@ -236,19 +231,7 @@
org.mockito
- mockito-core
- test
-
-
- org.objenesis
- objenesis
- 3.4
- test
-
-
- org.testcontainers
- testcontainers
- 1.21.3
+ mockito-junit-jupiter
test
diff --git a/src/test/java/hudson/plugins/git/BranchTest.java b/src/test/java/hudson/plugins/git/BranchTest.java
index febb240d2e..5851ee3236 100644
--- a/src/test/java/hudson/plugins/git/BranchTest.java
+++ b/src/test/java/hudson/plugins/git/BranchTest.java
@@ -7,56 +7,45 @@
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectIdRef;
import org.eclipse.jgit.lib.Ref;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
-public class BranchTest {
+class BranchTest {
- private final String branchSHA1;
- private final String branchName;
- private final ObjectId branchHead;
- private final Branch branch;
- private final String refPrefix;
- private final Ref branchRef;
- private final Branch branchFromRef;
-
- private static final String REMOTE_BRANCH_NAME = "origin/master";
-
- public BranchTest() {
- this.branchSHA1 = "fa71f704f9b90fa1f857d1623f3fe33fa2277ca9";
- this.branchName = REMOTE_BRANCH_NAME;
- this.branchHead = ObjectId.fromString(branchSHA1);
- this.refPrefix = "refs/remotes/";
- this.branchRef = new ObjectIdRef.PeeledNonTag(Ref.Storage.NEW, refPrefix + branchName, branchHead);
- this.branch = new Branch(branchName, branchHead);
- this.branchFromRef = new Branch(branchRef);
- }
+ private static final String BRANCH_SHA_1 = "fa71f704f9b90fa1f857d1623f3fe33fa2277ca9";
+ private static final String BRANCH_NAME = "origin/master";
+ private static final ObjectId BRANCH_HEAD = ObjectId.fromString(BRANCH_SHA_1);
+ private static final Branch BRANCH = new Branch(BRANCH_NAME, BRANCH_HEAD);
+ private static final String REF_PREFIX = "refs/remotes/";
+ private static final Ref BRANCH_REF =
+ new ObjectIdRef.PeeledNonTag(Ref.Storage.NEW, REF_PREFIX + BRANCH_NAME, BRANCH_HEAD);
+ private static final Branch BRANCH_FROM_REF = new Branch(BRANCH_REF);
@Test
- public void testToString() {
- assertThat(branch.toString(), is(branchFromRef.toString()));
+ void testToString() {
+ assertThat(BRANCH.toString(), is(BRANCH_FROM_REF.toString()));
}
@Test
- public void testToString_Contents() {
- String expected = "Branch " + branchName + "(" + branchSHA1 + ")";
- assertThat(branch.toString(), is(expected));
+ void testToString_Contents() {
+ String expected = "Branch " + BRANCH_NAME + "(" + BRANCH_SHA_1 + ")";
+ assertThat(BRANCH.toString(), is(expected));
}
@Test
- public void hashCodeContract() {
- assertThat(branch, is(branchFromRef));
- assertThat(branch.hashCode(), is(branchFromRef.hashCode()));
+ void hashCodeContract() {
+ assertThat(BRANCH, is(BRANCH_FROM_REF));
+ assertThat(BRANCH.hashCode(), is(BRANCH_FROM_REF.hashCode()));
}
@Test
- public void constructorRefArgStripped() {
- Ref ref = new ObjectIdRef.PeeledNonTag(Ref.Storage.LOOSE, refPrefix + branchName, branchHead);
+ void constructorRefArgStripped() {
+ Ref ref = new ObjectIdRef.PeeledNonTag(Ref.Storage.LOOSE, REF_PREFIX + BRANCH_NAME, BRANCH_HEAD);
Branch strippedBranch = new Branch(ref);
- assertThat(strippedBranch.getName(), is(branchName));
+ assertThat(strippedBranch.getName(), is(BRANCH_NAME));
}
@Test
- public void equalsContract() {
+ void equalsContract() {
EqualsVerifier.forClass(Branch.class)
.usingGetClass()
.withRedefinedSuperclass()
diff --git a/src/test/java/hudson/plugins/git/GitAPIBadInitTest.java b/src/test/java/hudson/plugins/git/GitAPIBadInitTest.java
index f3fb672e68..07bc91fe3c 100644
--- a/src/test/java/hudson/plugins/git/GitAPIBadInitTest.java
+++ b/src/test/java/hudson/plugins/git/GitAPIBadInitTest.java
@@ -2,8 +2,8 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThrows;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
import hudson.EnvVars;
import hudson.model.TaskListener;
@@ -13,46 +13,50 @@
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import org.jenkinsci.plugins.gitclient.GitClient;
-import org.junit.Before;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
-public class GitAPIBadInitTest {
+class GitAPIBadInitTest {
- @Rule
- public TemporaryFolder tempFolder = new TemporaryFolder();
+ @TempDir
+ private File tempFolder;
- private final EnvVars env;
-
- public GitAPIBadInitTest() {
- env = new EnvVars();
- }
+ private final EnvVars env = new EnvVars();
private File tempDir;
private TaskListener listener;
- @Before
- public void setUp() throws IOException, InterruptedException {
- tempDir = tempFolder.newFolder();
+ @BeforeEach
+ void setUp() throws Exception {
+ tempDir = newFolder(tempFolder, "junit");
listener = StreamTaskListener.fromStderr();
}
@Test
- public void testInitExistingDirectory() throws Exception {
+ void testInitExistingDirectory() throws Exception {
GitClient git = new GitAPI("git", tempDir, listener, env);
git.init();
File gitDir = new File(tempDir, ".git");
- assertTrue(gitDir + " not created", gitDir.exists());
- assertTrue(gitDir + " not a directory", gitDir.isDirectory());
+ assertTrue(gitDir.exists(), gitDir + " not created");
+ assertTrue(gitDir.isDirectory(), gitDir + " not a directory");
}
@Test
- public void testInitExistingFile() throws Exception {
+ void testInitExistingFile() throws Exception {
File existingFile = new File(tempDir, "file-exists");
Files.writeString(existingFile.toPath(), "git init should fail due to this file", StandardCharsets.UTF_8);
GitClient git = new GitAPI("git", existingFile, listener, env);
GitException e = assertThrows(GitException.class, git::init);
assertThat(e.getMessage(), is("Could not init " + existingFile.getAbsolutePath()));
}
+
+ private static File newFolder(File root, String... subDirs) throws Exception {
+ String subFolder = String.join("/", subDirs);
+ File result = new File(root, subFolder);
+ if (!result.mkdirs()) {
+ throw new IOException("Couldn't create folders " + result);
+ }
+ return result;
+ }
}
diff --git a/src/test/java/hudson/plugins/git/GitExceptionTest.java b/src/test/java/hudson/plugins/git/GitExceptionTest.java
index cebdd5d28f..7cf328d059 100644
--- a/src/test/java/hudson/plugins/git/GitExceptionTest.java
+++ b/src/test/java/hudson/plugins/git/GitExceptionTest.java
@@ -5,29 +5,27 @@
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.isA;
import static org.hamcrest.Matchers.nullValue;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
import hudson.EnvVars;
import hudson.model.TaskListener;
import java.io.File;
import java.io.IOException;
-import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.jenkinsci.plugins.gitclient.Git;
import org.jenkinsci.plugins.gitclient.GitClient;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
-public class GitExceptionTest {
+class GitExceptionTest {
- @Rule
- public TemporaryFolder folder = new TemporaryFolder();
+ @TempDir
+ private File folder;
@Test
- public void throwsGitException() {
+ void throwsGitException() {
GitException e = assertThrows(GitException.class, () -> {
throw new GitException();
});
@@ -35,7 +33,7 @@ public void throwsGitException() {
}
@Test
- public void throwsGitExceptionExpectedMessage() {
+ void throwsGitExceptionExpectedMessage() {
String message = "My custom git exception message";
GitException e = assertThrows(GitException.class, () -> {
throw new GitException(message);
@@ -44,7 +42,7 @@ public void throwsGitExceptionExpectedMessage() {
}
@Test
- public void throwsGitExceptionExpectedMessageWithCause() {
+ void throwsGitExceptionExpectedMessageWithCause() {
String message = "My custom git exception message";
GitException e = assertThrows(GitException.class, () -> {
throw new GitException(message, new IOException("Custom IOException message"));
@@ -54,7 +52,7 @@ public void throwsGitExceptionExpectedMessageWithCause() {
}
@Test
- public void initCliImplThrowsGitException() throws IOException, InterruptedException {
+ void initCliImplThrowsGitException() throws Exception {
if (new File("/").canWrite()) { // running as root?
return;
}
@@ -72,7 +70,7 @@ public void initCliImplThrowsGitException() throws IOException, InterruptedExcep
}
@Test
- public void initJGitImplThrowsGitException() throws IOException, InterruptedException {
+ void initJGitImplThrowsGitException() throws Exception {
if (new File("/").canWrite()) { // running as root?
return;
}
@@ -91,10 +89,10 @@ public void initJGitImplThrowsGitException() throws IOException, InterruptedExce
}
@Test
- public void initCliImplCollisionThrowsGitException() throws IOException, InterruptedException {
- File dir = folder.getRoot();
- File dotGit = folder.newFile(".git");
- Files.write(dotGit.toPath(), "file named .git".getBytes(StandardCharsets.UTF_8), APPEND);
+ void initCliImplCollisionThrowsGitException() throws Exception {
+ File dir = folder;
+ File dotGit = newFile(folder, ".git");
+ Files.writeString(dotGit.toPath(), "file named .git", APPEND);
GitClient defaultClient =
Git.with(TaskListener.NULL, new EnvVars()).in(dir).using("git").getClient();
assertThrows(
@@ -103,10 +101,10 @@ public void initCliImplCollisionThrowsGitException() throws IOException, Interru
}
@Test
- public void initJGitImplCollisionThrowsGitException() throws IOException, InterruptedException {
- File dir = folder.getRoot();
- File dotGit = folder.newFile(".git");
- Files.write(dotGit.toPath(), "file named .git".getBytes(StandardCharsets.UTF_8), APPEND);
+ void initJGitImplCollisionThrowsGitException() throws Exception {
+ File dir = folder;
+ File dotGit = newFile(folder, ".git");
+ Files.writeString(dotGit.toPath(), "file named .git", APPEND);
GitClient defaultClient =
Git.with(TaskListener.NULL, new EnvVars()).in(dir).using("jgit").getClient();
JGitInternalException e = assertThrows(
@@ -122,4 +120,12 @@ public void initJGitImplCollisionThrowsGitException() throws IOException, Interr
private static boolean isWindows() {
return File.pathSeparatorChar == ';';
}
+
+ private static File newFile(File parent, String child) throws Exception {
+ File result = new File(parent, child);
+ if (!result.createNewFile()) {
+ throw new IOException("Couldn't create file " + result);
+ }
+ return result;
+ }
}
diff --git a/src/test/java/hudson/plugins/git/GitLockFailedExceptionTest.java b/src/test/java/hudson/plugins/git/GitLockFailedExceptionTest.java
index ba01459319..e53ba8089e 100644
--- a/src/test/java/hudson/plugins/git/GitLockFailedExceptionTest.java
+++ b/src/test/java/hudson/plugins/git/GitLockFailedExceptionTest.java
@@ -3,14 +3,14 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
-import static org.junit.Assert.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertThrows;
-import org.junit.Test;
+import org.junit.jupiter.api.Test;
-public class GitLockFailedExceptionTest {
+class GitLockFailedExceptionTest {
@Test
- public void throwsGitLockFailedException() {
+ void throwsGitLockFailedException() {
GitLockFailedException lockFailed = assertThrows(GitLockFailedException.class, () -> {
throw new GitLockFailedException();
});
@@ -18,7 +18,7 @@ public void throwsGitLockFailedException() {
}
@Test
- public void throwsGitLockFailedExceptionWithMessage() {
+ void throwsGitLockFailedExceptionWithMessage() {
String message = "My local exception message";
GitLockFailedException lockFailed = assertThrows(GitLockFailedException.class, () -> {
throw new GitLockFailedException(message);
@@ -27,7 +27,7 @@ public void throwsGitLockFailedExceptionWithMessage() {
}
@Test
- public void throwsGitLockFailedExceptionWithCause() {
+ void throwsGitLockFailedExceptionWithCause() {
String message = "My git exception message";
GitException e = new GitException(message);
GitLockFailedException lockFailed = assertThrows(GitLockFailedException.class, () -> {
@@ -37,7 +37,7 @@ public void throwsGitLockFailedExceptionWithCause() {
}
@Test
- public void throwsGitLockFailedExceptionWithCauseAndMessage() {
+ void throwsGitLockFailedExceptionWithCauseAndMessage() {
String message = "My git exception message";
GitException e = new GitException(message);
String lockMessage = "My lock message that is not part of the GitException";
diff --git a/src/test/java/hudson/plugins/git/GitObjectTest.java b/src/test/java/hudson/plugins/git/GitObjectTest.java
index d3087f4e4a..e6ea4d48aa 100644
--- a/src/test/java/hudson/plugins/git/GitObjectTest.java
+++ b/src/test/java/hudson/plugins/git/GitObjectTest.java
@@ -1,65 +1,66 @@
package hudson.plugins.git;
-import static org.junit.Assert.*;
+import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.ArrayList;
-import java.util.Collection;
import java.util.List;
import nl.jqno.equalsverifier.EqualsVerifier;
import org.eclipse.jgit.lib.ObjectId;
-import org.junit.Test;
-import org.junit.runner.RunWith;
-import org.junit.runners.Parameterized;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.Parameter;
+import org.junit.jupiter.params.ParameterizedClass;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.MethodSource;
-@RunWith(Parameterized.class)
-public class GitObjectTest {
+@ParameterizedClass(name = "{0}-{1}")
+@MethodSource("gitObjects")
+class GitObjectTest {
- private final String sha1String;
- private final String name;
- private final ObjectId sha1;
+ @Parameter(0)
+ private String name;
- private final GitObject gitObject;
+ @Parameter(1)
+ private String sha1String;
- public GitObjectTest(String name, String sha1String) {
- this.name = name;
- this.sha1String = sha1String;
- this.sha1 = sha1String != null ? ObjectId.fromString(sha1String) : null;
- gitObject = new GitObject(name, sha1);
- }
+ @Parameter(2)
+ private ObjectId sha1;
+
+ @Parameter(3)
+ private GitObject gitObject;
- @Parameterized.Parameters(name = "{0}-{1}")
- public static Collection gitObjects() {
- List