From 09b8068eb1a967d726b18a4fdf4c0156b7395bfd Mon Sep 17 00:00:00 2001 From: Allan Burdajewicz Date: Thu, 9 Sep 2021 14:33:59 +1000 Subject: [PATCH 01/14] [JENKINS-64803] Test case retrieve tags with folder scoped credentials --- pom.xml | 10 +++ .../plugins/git/AbstractGitSCMSourceTest.java | 72 +++++++++++++++---- 2 files changed, 67 insertions(+), 15 deletions(-) diff --git a/pom.xml b/pom.xml index 594308d41a..7967afdbfd 100644 --- a/pom.xml +++ b/pom.xml @@ -156,6 +156,16 @@ mockito-core test + + org.powermock + powermock-module-junit4 + test + + + org.powermock + powermock-api-mockito2 + test + nl.jqno.equalsverifier equalsverifier diff --git a/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java b/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java index 8283e9abff..2bcaed718b 100644 --- a/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java +++ b/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java @@ -1,5 +1,13 @@ package jenkins.plugins.git; +import com.cloudbees.hudson.plugins.folder.Folder; +import com.cloudbees.hudson.plugins.folder.properties.FolderCredentialsProvider; +import com.cloudbees.plugins.credentials.CredentialsProvider; +import com.cloudbees.plugins.credentials.CredentialsScope; +import com.cloudbees.plugins.credentials.CredentialsStore; +import com.cloudbees.plugins.credentials.common.StandardCredentials; +import com.cloudbees.plugins.credentials.domains.Domain; +import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl; import edu.umd.cs.findbugs.annotations.NonNull; import hudson.EnvVars; import hudson.FilePath; @@ -53,12 +61,20 @@ import org.eclipse.jgit.transport.URIish; import org.jenkinsci.plugins.gitclient.FetchCommand; import org.jenkinsci.plugins.gitclient.Git; +import org.jenkinsci.plugins.gitclient.GitClient; import org.jenkinsci.plugins.gitclient.TestJGitAPIImpl; +import org.jenkinsci.plugins.workflow.job.WorkflowJob; import org.junit.Rule; import org.junit.Test; +import org.junit.runner.RunWith; import org.jvnet.hudson.test.Issue; import org.jvnet.hudson.test.JenkinsRule; +import org.mockito.ArgumentMatchers; import org.mockito.Mockito; +import org.powermock.api.mockito.PowerMockito; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.core.classloader.annotations.PrepareForTest; +import org.powermock.modules.junit4.PowerMockRunner; import static org.hamcrest.MatcherAssert.*; import static org.hamcrest.Matchers.*; @@ -67,11 +83,16 @@ import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; +import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.when; /** * Tests for {@link AbstractGitSCMSource} */ +@RunWith(PowerMockRunner.class) +@PrepareForTest({Git.class}) +// Need to ignore some packages due to Powermockito issue with JDK 11 https://github.com/powermock/powermock/issues/864 +@PowerMockIgnore({ "javax.xml.*" }) public class AbstractGitSCMSourceTest { static final String GitBranchSCMHead_DEV_MASTER = "[GitBranchSCMHead{name='dev', ref='refs/heads/dev'}, GitBranchSCMHead{name='master', ref='refs/heads/master'}]"; @@ -242,29 +263,50 @@ public void retrieveHeadsSupportsTagDiscovery_onlyTagsWithoutBranchDiscoveryTrai assertEquals("[SCMHead{'annotated'}, SCMHead{'lightweight'}]", source.fetch(listener).toString()); } - @Issue("JENKINS-45953") + @Issue("JENKINS-64803") @Test - public void retrieveRevisions() throws Exception { + public void retrieveTags_folderScopedCredentials() throws Exception { sampleRepo.init(); sampleRepo.git("checkout", "-b", "dev"); sampleRepo.write("file", "modified"); sampleRepo.git("commit", "--all", "--message=dev"); sampleRepo.git("tag", "lightweight"); - sampleRepo.write("file", "modified2"); - sampleRepo.git("commit", "--all", "--message=dev2"); - sampleRepo.git("tag", "-a", "annotated", "-m", "annotated"); - sampleRepo.write("file", "modified3"); - sampleRepo.git("commit", "--all", "--message=dev3"); GitSCMSource source = new GitSCMSource(sampleRepo.toString()); - source.setTraits(new ArrayList<>()); TaskListener listener = StreamTaskListener.fromStderr(); - assertThat(source.fetchRevisions(listener, null), hasSize(0)); - source.setTraits(Collections.singletonList(new BranchDiscoveryTrait())); - assertThat(source.fetchRevisions(listener, null), containsInAnyOrder("dev", "master")); - source.setTraits(Collections.singletonList(new TagDiscoveryTrait())); - assertThat(source.fetchRevisions(listener, null), containsInAnyOrder("annotated", "lightweight")); - source.setTraits(Arrays.asList(new BranchDiscoveryTrait(), new TagDiscoveryTrait())); - assertThat(source.fetchRevisions(listener, null), containsInAnyOrder("dev", "master", "annotated", "lightweight")); + + // Spy on GitClient methods + Git git = Mockito.mock(Git.class, Mockito.CALLS_REAL_METHODS); + GitClient gitClient = Mockito.spy(git.getClient()); + PowerMockito.mockStatic(Git.class, Mockito.CALLS_REAL_METHODS); + when(Git.with(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(git); + doReturn(gitClient).when(git).getClient(); + + // Create a Folder and add a folder credentials + Folder f = r.jenkins.createProject(Folder.class, "test"); + Iterable stores = CredentialsProvider.lookupStores(f); + CredentialsStore folderStore = null; + for (CredentialsStore s : stores) { + if (s.getProvider() instanceof FolderCredentialsProvider && s.getContext() == f) { + folderStore = s; + break; + } + } + assert folderStore != null; + String fCredentialsId = "fcreds"; + StandardCredentials fCredentials = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, + fCredentialsId, "fcreds", "user", "password"); + folderStore.addCredentials(Domain.global(), fCredentials); + folderStore.save(); + WorkflowJob p = f.createProject(WorkflowJob.class, "wjob"); + + source.setTraits(new ArrayList<>()); + source.setCredentialsId(fCredentialsId); + + SCMRevision rev = source.fetch("lightweight", listener, p); + assertThat(rev, notNullValue()); + assertThat(rev.getHead().toString(), equalTo("SCMHead{'lightweight'}")); + Mockito.verify(gitClient, Mockito.times(0)).addDefaultCredentials(null); + Mockito.verify(gitClient, Mockito.atLeastOnce()).addDefaultCredentials(fCredentials); } @Issue("JENKINS-47824") From 33cbe0ee15b2d004e2850316c6dde3a65c981210 Mon Sep 17 00:00:00 2001 From: Allan Burdajewicz Date: Thu, 9 Sep 2021 15:54:06 +1000 Subject: [PATCH 02/14] [JENKINS-64803] Ignore javax.crypto.* with PowerMock --- src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java b/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java index 2bcaed718b..3c623a1f4d 100644 --- a/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java +++ b/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java @@ -92,7 +92,7 @@ @RunWith(PowerMockRunner.class) @PrepareForTest({Git.class}) // Need to ignore some packages due to Powermockito issue with JDK 11 https://github.com/powermock/powermock/issues/864 -@PowerMockIgnore({ "javax.xml.*" }) +@PowerMockIgnore({ "javax.xml.*", "javax.crypto.*" }) public class AbstractGitSCMSourceTest { static final String GitBranchSCMHead_DEV_MASTER = "[GitBranchSCMHead{name='dev', ref='refs/heads/dev'}, GitBranchSCMHead{name='master', ref='refs/heads/master'}]"; From 20a02f6d7bfef4f2eed75d030c6eedb26710d7d0 Mon Sep 17 00:00:00 2001 From: Allan Burdajewicz Date: Thu, 9 Sep 2021 15:59:16 +1000 Subject: [PATCH 03/14] [JENKINS-64803] Ignore other packages with PowerMock --- src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java b/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java index 3c623a1f4d..0f37304fb7 100644 --- a/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java +++ b/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java @@ -92,7 +92,7 @@ @RunWith(PowerMockRunner.class) @PrepareForTest({Git.class}) // Need to ignore some packages due to Powermockito issue with JDK 11 https://github.com/powermock/powermock/issues/864 -@PowerMockIgnore({ "javax.xml.*", "javax.crypto.*" }) +@PowerMockIgnore({ "com.sun.org.apache.xerces.*", "javax.management.*", "javax.net.ssl.*", "javax.xml.*", "org.xml.*" }) public class AbstractGitSCMSourceTest { static final String GitBranchSCMHead_DEV_MASTER = "[GitBranchSCMHead{name='dev', ref='refs/heads/dev'}, GitBranchSCMHead{name='master', ref='refs/heads/master'}]"; From 00d34dd5efebaabfe8c8b0e51696a6d6e21c34dd Mon Sep 17 00:00:00 2001 From: Allan Burdajewicz Date: Thu, 9 Sep 2021 16:01:06 +1000 Subject: [PATCH 04/14] [JENKINS-64803] Pass Item context to doRetrieve when possible --- .../plugins/git/AbstractGitSCMSource.java | 32 ++++++++++++++++--- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java b/src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java index b00d2c5676..7322f079ef 100644 --- a/src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java +++ b/src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java @@ -326,15 +326,37 @@ private , R extends GitSCMSourceRequest> @NonNull C context, @NonNull TaskListener listener, boolean prune) + throws IOException, InterruptedException { + return doRetrieve(retriever, context, listener, prune, getOwner(), false); + } + + @NonNull + private , R extends GitSCMSourceRequest> T doRetrieve(Retriever retriever, + @NonNull C context, + @NonNull TaskListener listener, + boolean prune, + @CheckForNull Item retrieveContext) + throws IOException, InterruptedException { + return doRetrieve(retriever, context, listener, prune, retrieveContext, false); + } + + @NonNull + private , R extends GitSCMSourceRequest> T doRetrieve(Retriever retriever, + @NonNull C context, + @NonNull TaskListener listener, + boolean prune, + boolean delayFetch) throws IOException, InterruptedException { - return doRetrieve(retriever, context, listener, prune, false); + return doRetrieve(retriever, context, listener, prune, getOwner(), delayFetch); } @NonNull private , R extends GitSCMSourceRequest> T doRetrieve(Retriever retriever, @NonNull C context, @NonNull TaskListener listener, - boolean prune, boolean delayFetch) + boolean prune, + @CheckForNull Item retrieveContext, + boolean delayFetch) throws IOException, InterruptedException { String cacheEntry = getCacheEntry(); Lock cacheLock = getCacheLock(cacheEntry); @@ -347,7 +369,7 @@ private , R extends GitSCMSourceRequest> git.using(tool.getGitExe()); } GitClient client = git.getClient(); - client.addDefaultCredentials(getCredentials()); + client.addDefaultCredentials(getCredentials(retrieveContext)); if (!client.hasGitRepo(false)) { listener.getLogger().println("Creating git repository in " + cacheDir); client.init(); @@ -971,7 +993,7 @@ public SCMRevision run(GitClient client, String remoteName) throws IOException, } }, context, - listener, pruneRefs); + listener, pruneRefs, retrieveContext); } // Pokémon!... Got to catch them all listener.getLogger().printf("Could not find %s in remote references. " @@ -1017,7 +1039,7 @@ public SCMRevision run(GitClient client, String remoteName) throws IOException, } }, context, - listener, pruneRefs); + listener, pruneRefs, retrieveContext); } /** From 205a6c0b0982d557bc174a0aa9ce8579ea441ea1 Mon Sep 17 00:00:00 2001 From: Allan Burdajewicz Date: Thu, 9 Sep 2021 18:18:18 +1000 Subject: [PATCH 05/14] [JENKINS-64803] Re-add javax.crypto packages to PowerMockIgnore --- src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java b/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java index 0f37304fb7..b4c241973b 100644 --- a/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java +++ b/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java @@ -92,7 +92,7 @@ @RunWith(PowerMockRunner.class) @PrepareForTest({Git.class}) // Need to ignore some packages due to Powermockito issue with JDK 11 https://github.com/powermock/powermock/issues/864 -@PowerMockIgnore({ "com.sun.org.apache.xerces.*", "javax.management.*", "javax.net.ssl.*", "javax.xml.*", "org.xml.*" }) +@PowerMockIgnore({ "com.sun.org.apache.xerces.*", "javax.management.*", "javax.net.ssl.*", "javax.xml.*", "org.xml.*", "javax.crypto.*" }) public class AbstractGitSCMSourceTest { static final String GitBranchSCMHead_DEV_MASTER = "[GitBranchSCMHead{name='dev', ref='refs/heads/dev'}, GitBranchSCMHead{name='master', ref='refs/heads/master'}]"; From 1e7542938e9d07f7c59c5822c43fb29061d76148 Mon Sep 17 00:00:00 2001 From: Allan Burdajewicz Date: Mon, 20 Sep 2021 16:19:18 +1000 Subject: [PATCH 06/14] [JENKINS-64803] Use Mockito instead of PowerMockito --- pom.xml | 10 ++---- .../plugins/git/AbstractGitSCMSourceTest.java | 36 ++++++++----------- 2 files changed, 17 insertions(+), 29 deletions(-) diff --git a/pom.xml b/pom.xml index 7967afdbfd..3b45f69b59 100644 --- a/pom.xml +++ b/pom.xml @@ -157,13 +157,9 @@ test - org.powermock - powermock-module-junit4 - test - - - org.powermock - powermock-api-mockito2 + org.mockito + mockito-inline + 3.8.0 test diff --git a/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java b/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java index b4c241973b..27be72937d 100644 --- a/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java +++ b/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java @@ -66,15 +66,11 @@ import org.jenkinsci.plugins.workflow.job.WorkflowJob; import org.junit.Rule; import org.junit.Test; -import org.junit.runner.RunWith; import org.jvnet.hudson.test.Issue; import org.jvnet.hudson.test.JenkinsRule; import org.mockito.ArgumentMatchers; +import org.mockito.MockedStatic; import org.mockito.Mockito; -import org.powermock.api.mockito.PowerMockito; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.core.classloader.annotations.PrepareForTest; -import org.powermock.modules.junit4.PowerMockRunner; import static org.hamcrest.MatcherAssert.*; import static org.hamcrest.Matchers.*; @@ -89,10 +85,6 @@ /** * Tests for {@link AbstractGitSCMSource} */ -@RunWith(PowerMockRunner.class) -@PrepareForTest({Git.class}) -// Need to ignore some packages due to Powermockito issue with JDK 11 https://github.com/powermock/powermock/issues/864 -@PowerMockIgnore({ "com.sun.org.apache.xerces.*", "javax.management.*", "javax.net.ssl.*", "javax.xml.*", "org.xml.*", "javax.crypto.*" }) public class AbstractGitSCMSourceTest { static final String GitBranchSCMHead_DEV_MASTER = "[GitBranchSCMHead{name='dev', ref='refs/heads/dev'}, GitBranchSCMHead{name='master', ref='refs/heads/master'}]"; @@ -274,13 +266,6 @@ public void retrieveTags_folderScopedCredentials() throws Exception { GitSCMSource source = new GitSCMSource(sampleRepo.toString()); TaskListener listener = StreamTaskListener.fromStderr(); - // Spy on GitClient methods - Git git = Mockito.mock(Git.class, Mockito.CALLS_REAL_METHODS); - GitClient gitClient = Mockito.spy(git.getClient()); - PowerMockito.mockStatic(Git.class, Mockito.CALLS_REAL_METHODS); - when(Git.with(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(git); - doReturn(gitClient).when(git).getClient(); - // Create a Folder and add a folder credentials Folder f = r.jenkins.createProject(Folder.class, "test"); Iterable stores = CredentialsProvider.lookupStores(f); @@ -293,7 +278,7 @@ public void retrieveTags_folderScopedCredentials() throws Exception { } assert folderStore != null; String fCredentialsId = "fcreds"; - StandardCredentials fCredentials = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, + StandardCredentials fCredentials = new UsernamePasswordCredentialsImpl(CredentialsScope.GLOBAL, fCredentialsId, "fcreds", "user", "password"); folderStore.addCredentials(Domain.global(), fCredentials); folderStore.save(); @@ -302,11 +287,18 @@ public void retrieveTags_folderScopedCredentials() throws Exception { source.setTraits(new ArrayList<>()); source.setCredentialsId(fCredentialsId); - SCMRevision rev = source.fetch("lightweight", listener, p); - assertThat(rev, notNullValue()); - assertThat(rev.getHead().toString(), equalTo("SCMHead{'lightweight'}")); - Mockito.verify(gitClient, Mockito.times(0)).addDefaultCredentials(null); - Mockito.verify(gitClient, Mockito.atLeastOnce()).addDefaultCredentials(fCredentials); + Git git = Mockito.mock(Git.class, Mockito.CALLS_REAL_METHODS); + GitClient gitClient = Mockito.spy(git.getClient()); + // Spy on GitClient methods + try (MockedStatic gitMock = Mockito.mockStatic(Git.class, Mockito.CALLS_REAL_METHODS)) { + gitMock.when(() -> Git.with(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(git); + doReturn(gitClient).when(git).getClient(); + SCMRevision rev = source.fetch("lightweight", listener, p); + assertThat(rev, notNullValue()); + assertThat(rev.getHead().toString(), equalTo("SCMHead{'lightweight'}")); + Mockito.verify(gitClient, Mockito.times(0)).addDefaultCredentials(null); + Mockito.verify(gitClient, Mockito.atLeastOnce()).addDefaultCredentials(fCredentials); + } } @Issue("JENKINS-47824") From 3172bf32e4f6a2f521c07c1de147445ddfcb275c Mon Sep 17 00:00:00 2001 From: Allan Burdajewicz Date: Mon, 20 Sep 2021 16:23:15 +1000 Subject: [PATCH 07/14] [JENKINS-64803] Restore test that was mistakenly removed --- .../plugins/git/AbstractGitSCMSourceTest.java | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java b/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java index 27be72937d..f49a4e22fc 100644 --- a/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java +++ b/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java @@ -255,6 +255,31 @@ public void retrieveHeadsSupportsTagDiscovery_onlyTagsWithoutBranchDiscoveryTrai assertEquals("[SCMHead{'annotated'}, SCMHead{'lightweight'}]", source.fetch(listener).toString()); } + @Issue("JENKINS-45953") + @Test + public void retrieveRevisions() throws Exception { + sampleRepo.init(); + sampleRepo.git("checkout", "-b", "dev"); + sampleRepo.write("file", "modified"); + sampleRepo.git("commit", "--all", "--message=dev"); + sampleRepo.git("tag", "lightweight"); + sampleRepo.write("file", "modified2"); + sampleRepo.git("commit", "--all", "--message=dev2"); + sampleRepo.git("tag", "-a", "annotated", "-m", "annotated"); + sampleRepo.write("file", "modified3"); + sampleRepo.git("commit", "--all", "--message=dev3"); + GitSCMSource source = new GitSCMSource(sampleRepo.toString()); + source.setTraits(new ArrayList<>()); + TaskListener listener = StreamTaskListener.fromStderr(); + assertThat(source.fetchRevisions(listener, null), hasSize(0)); + source.setTraits(Collections.singletonList(new BranchDiscoveryTrait())); + assertThat(source.fetchRevisions(listener, null), containsInAnyOrder("dev", "master")); + source.setTraits(Collections.singletonList(new TagDiscoveryTrait())); + assertThat(source.fetchRevisions(listener, null), containsInAnyOrder("annotated", "lightweight")); + source.setTraits(Arrays.asList(new BranchDiscoveryTrait(), new TagDiscoveryTrait())); + assertThat(source.fetchRevisions(listener, null), containsInAnyOrder("dev", "master", "annotated", "lightweight")); + } + @Issue("JENKINS-64803") @Test public void retrieveTags_folderScopedCredentials() throws Exception { From f97826e061f98a8e6518660760c9feabedf8d77c Mon Sep 17 00:00:00 2001 From: Allan Burdajewicz Date: Mon, 20 Sep 2021 17:53:38 +1000 Subject: [PATCH 08/14] [JENKINS-64803] Add Mockito import static --- .../plugins/git/AbstractGitSCMSourceTest.java | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java b/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java index f49a4e22fc..5e5bbec997 100644 --- a/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java +++ b/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java @@ -79,7 +79,15 @@ import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNull; import static org.junit.Assert.fail; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.CALLS_REAL_METHODS; +import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.spy; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; /** @@ -312,17 +320,17 @@ public void retrieveTags_folderScopedCredentials() throws Exception { source.setTraits(new ArrayList<>()); source.setCredentialsId(fCredentialsId); - Git git = Mockito.mock(Git.class, Mockito.CALLS_REAL_METHODS); - GitClient gitClient = Mockito.spy(git.getClient()); + Git git = mock(Git.class, CALLS_REAL_METHODS); + GitClient gitClient = spy(git.getClient()); // Spy on GitClient methods - try (MockedStatic gitMock = Mockito.mockStatic(Git.class, Mockito.CALLS_REAL_METHODS)) { - gitMock.when(() -> Git.with(ArgumentMatchers.any(), ArgumentMatchers.any())).thenReturn(git); + try (MockedStatic gitMock = mockStatic(Git.class, CALLS_REAL_METHODS)) { + gitMock.when(() -> Git.with(any(), any())).thenReturn(git); doReturn(gitClient).when(git).getClient(); SCMRevision rev = source.fetch("lightweight", listener, p); assertThat(rev, notNullValue()); assertThat(rev.getHead().toString(), equalTo("SCMHead{'lightweight'}")); - Mockito.verify(gitClient, Mockito.times(0)).addDefaultCredentials(null); - Mockito.verify(gitClient, Mockito.atLeastOnce()).addDefaultCredentials(fCredentials); + verify(gitClient, times(0)).addDefaultCredentials(null); + verify(gitClient, atLeastOnce()).addDefaultCredentials(fCredentials); } } From be8f349808d66c7bddc02f25dd232165d67f534c Mon Sep 17 00:00:00 2001 From: Mark Waite Date: Thu, 23 Sep 2021 18:29:40 -0600 Subject: [PATCH 09/14] Reduce trailing white space Prefer no trailing white space in new code --- src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java | 2 +- src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java b/src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java index 7322f079ef..a9e51aa5d3 100644 --- a/src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java +++ b/src/main/java/jenkins/plugins/git/AbstractGitSCMSource.java @@ -345,7 +345,7 @@ private , R extends GitSCMSourceRequest> @NonNull C context, @NonNull TaskListener listener, boolean prune, - boolean delayFetch) + boolean delayFetch) throws IOException, InterruptedException { return doRetrieve(retriever, context, listener, prune, getOwner(), delayFetch); } diff --git a/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java b/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java index 5e5bbec997..25b062eac5 100644 --- a/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java +++ b/src/test/java/jenkins/plugins/git/AbstractGitSCMSourceTest.java @@ -298,7 +298,7 @@ public void retrieveTags_folderScopedCredentials() throws Exception { sampleRepo.git("tag", "lightweight"); GitSCMSource source = new GitSCMSource(sampleRepo.toString()); TaskListener listener = StreamTaskListener.fromStderr(); - + // Create a Folder and add a folder credentials Folder f = r.jenkins.createProject(Folder.class, "test"); Iterable stores = CredentialsProvider.lookupStores(f); From 64224d1cb75dec9ad4587dbb7275956d1df4cb3f Mon Sep 17 00:00:00 2001 From: Allan Burdajewicz Date: Tue, 5 Oct 2021 10:35:45 +1000 Subject: [PATCH 10/14] Fix TFS Browser tests --- .../TFS2013GitRepositoryBrowserTest.java | 26 +++++++++---------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/src/test/java/hudson/plugins/git/browser/TFS2013GitRepositoryBrowserTest.java b/src/test/java/hudson/plugins/git/browser/TFS2013GitRepositoryBrowserTest.java index b15314d2a3..5206593bf6 100644 --- a/src/test/java/hudson/plugins/git/browser/TFS2013GitRepositoryBrowserTest.java +++ b/src/test/java/hudson/plugins/git/browser/TFS2013GitRepositoryBrowserTest.java @@ -6,6 +6,7 @@ import hudson.scm.ChangeLogSet; import hudson.scm.EditType; import org.jenkinsci.plugins.gitclient.JGitTool; +import org.junit.BeforeClass; import org.junit.Test; import java.net.URL; @@ -20,24 +21,21 @@ public class TFS2013GitRepositoryBrowserTest { - private static final String projectName = "fisheyeProjectName"; - - private final String repoUrl; - private final GitChangeSetSample sample; - - public TFS2013GitRepositoryBrowserTest() { - this.repoUrl = "http://tfs/tfs/project/_git/repo"; - sample = new GitChangeSetSample(false); + private static final String repoUrl = "http://tfs/tfs/project/_git/repo"; + private static final GitChangeSetSample sample = new GitChangeSetSample(false); + + @BeforeClass + public static void testSetUp() { GitSCM scm = new GitSCM( - Collections.singletonList(new UserRemoteConfig(repoUrl, null, null, null)), - new ArrayList<>(), - null, JGitTool.MAGIC_EXENAME, - Collections.emptyList()); - + Collections.singletonList(new UserRemoteConfig(repoUrl, null, null, null)), + new ArrayList<>(), + null, JGitTool.MAGIC_EXENAME, + Collections.emptyList()); + AbstractProject project = mock(AbstractProject.class); AbstractBuild build = mock(AbstractBuild.class); - + when(project.getScm()).thenReturn(scm); when(build.getProject()).thenReturn(project); From cb7377d72b37edd889000f449c6200bca6c12182 Mon Sep 17 00:00:00 2001 From: Allan Burdajewicz Date: Tue, 5 Oct 2021 13:34:41 +1000 Subject: [PATCH 11/14] Add missing mocked method --- pom.xml | 1 - .../plugins/git/browser/TFS2013GitRepositoryBrowserTest.java | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index b7450f69db..90bbf76267 100644 --- a/pom.xml +++ b/pom.xml @@ -159,7 +159,6 @@ org.mockito mockito-inline - 3.8.0 test diff --git a/src/test/java/hudson/plugins/git/browser/TFS2013GitRepositoryBrowserTest.java b/src/test/java/hudson/plugins/git/browser/TFS2013GitRepositoryBrowserTest.java index 5206593bf6..ac60ac936b 100644 --- a/src/test/java/hudson/plugins/git/browser/TFS2013GitRepositoryBrowserTest.java +++ b/src/test/java/hudson/plugins/git/browser/TFS2013GitRepositoryBrowserTest.java @@ -16,6 +16,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertEquals; import static org.hamcrest.MatcherAssert.assertThat; +import static org.mockito.Mockito.CALLS_REAL_METHODS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -38,6 +39,7 @@ public static void testSetUp() { when(project.getScm()).thenReturn(scm); when(build.getProject()).thenReturn(project); + when(build.getParent()).thenReturn(project); sample.changeSet.setParent(ChangeLogSet.createEmpty((Run) build)); } From 2900ee34f463763a032404df413e0c104e62b510 Mon Sep 17 00:00:00 2001 From: Allan Burdajewicz Date: Fri, 8 Oct 2021 21:28:33 +1000 Subject: [PATCH 12/14] [JENKINS-64803] Remove trailing space Co-authored-by: Mark Waite --- .../plugins/git/browser/TFS2013GitRepositoryBrowserTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/test/java/hudson/plugins/git/browser/TFS2013GitRepositoryBrowserTest.java b/src/test/java/hudson/plugins/git/browser/TFS2013GitRepositoryBrowserTest.java index ac60ac936b..8b20c3cb9e 100644 --- a/src/test/java/hudson/plugins/git/browser/TFS2013GitRepositoryBrowserTest.java +++ b/src/test/java/hudson/plugins/git/browser/TFS2013GitRepositoryBrowserTest.java @@ -24,7 +24,6 @@ public class TFS2013GitRepositoryBrowserTest { private static final String repoUrl = "http://tfs/tfs/project/_git/repo"; private static final GitChangeSetSample sample = new GitChangeSetSample(false); - @BeforeClass public static void testSetUp() { From 38934354ca721ebb97279ee9711f4f0e4d10df8c Mon Sep 17 00:00:00 2001 From: Allan Burdajewicz Date: Fri, 8 Oct 2021 21:36:19 +1000 Subject: [PATCH 13/14] [JENKINS-64803] Preserve existing formatting --- .../TFS2013GitRepositoryBrowserTest.java | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/src/test/java/hudson/plugins/git/browser/TFS2013GitRepositoryBrowserTest.java b/src/test/java/hudson/plugins/git/browser/TFS2013GitRepositoryBrowserTest.java index 8b20c3cb9e..b30d348678 100644 --- a/src/test/java/hudson/plugins/git/browser/TFS2013GitRepositoryBrowserTest.java +++ b/src/test/java/hudson/plugins/git/browser/TFS2013GitRepositoryBrowserTest.java @@ -16,7 +16,6 @@ import static org.hamcrest.CoreMatchers.is; import static org.junit.Assert.assertEquals; import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Mockito.CALLS_REAL_METHODS; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -24,18 +23,18 @@ public class TFS2013GitRepositoryBrowserTest { private static final String repoUrl = "http://tfs/tfs/project/_git/repo"; private static final GitChangeSetSample sample = new GitChangeSetSample(false); + @BeforeClass - public static void testSetUp() { - + public static void setUp() { GitSCM scm = new GitSCM( - Collections.singletonList(new UserRemoteConfig(repoUrl, null, null, null)), - new ArrayList<>(), - null, JGitTool.MAGIC_EXENAME, - Collections.emptyList()); - + Collections.singletonList(new UserRemoteConfig(repoUrl, null, null, null)), + new ArrayList<>(), + null, JGitTool.MAGIC_EXENAME, + Collections.emptyList()); + AbstractProject project = mock(AbstractProject.class); AbstractBuild build = mock(AbstractBuild.class); - + when(project.getScm()).thenReturn(scm); when(build.getProject()).thenReturn(project); when(build.getParent()).thenReturn(project); From 2c44edbaa86b8bc5859dce7cba2f37f3a7ed5250 Mon Sep 17 00:00:00 2001 From: Mark Waite Date: Fri, 22 Oct 2021 09:54:02 -0600 Subject: [PATCH 14/14] Remove trailing white space from test class --- .../git/browser/TFS2013GitRepositoryBrowserTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/test/java/hudson/plugins/git/browser/TFS2013GitRepositoryBrowserTest.java b/src/test/java/hudson/plugins/git/browser/TFS2013GitRepositoryBrowserTest.java index b30d348678..3ba80db5c4 100644 --- a/src/test/java/hudson/plugins/git/browser/TFS2013GitRepositoryBrowserTest.java +++ b/src/test/java/hudson/plugins/git/browser/TFS2013GitRepositoryBrowserTest.java @@ -23,7 +23,7 @@ public class TFS2013GitRepositoryBrowserTest { private static final String repoUrl = "http://tfs/tfs/project/_git/repo"; private static final GitChangeSetSample sample = new GitChangeSetSample(false); - + @BeforeClass public static void setUp() { GitSCM scm = new GitSCM( @@ -31,10 +31,10 @@ public static void setUp() { new ArrayList<>(), null, JGitTool.MAGIC_EXENAME, Collections.emptyList()); - + AbstractProject project = mock(AbstractProject.class); AbstractBuild build = mock(AbstractBuild.class); - + when(project.getScm()).thenReturn(scm); when(build.getProject()).thenReturn(project); when(build.getParent()).thenReturn(project);