diff --git a/pom.xml b/pom.xml index aefd03d52..c694c83d9 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ org.jenkins-ci.plugins plugin - 2.23 + 2.27 @@ -30,8 +30,16 @@ 1.6.4 UTF-8 - 1.10 + 2.9 + 2.5 + 2.11 2.0 + 2.11 + 2.29 + 2.11 + 2.4 + 2.13 + 2.0.7 7 2 @@ -125,7 +133,7 @@ org.jenkins-ci.plugins mailer - 1.5 + 1.13 org.jvnet.hudson.plugins @@ -150,31 +158,61 @@ token-macro 2.0 + + org.jenkins-ci.plugins + structs + 1.6 + org.jenkins-ci.plugins.workflow - ${workflow.version} + ${workflow-step-api.version} workflow-step-api true org.jenkins-ci.plugins.workflow workflow-job - ${workflow.version} + ${workflow-job.version} true org.jenkins-ci.plugins.workflow - workflow-aggregator - ${workflow.version} + workflow-api + ${workflow-api.version} + test + + + org.jenkins-ci.plugins.workflow + workflow-support + ${workflow-support.version} + test + + + org.jenkins-ci.plugins.workflow + workflow-support + ${workflow-support.version} + tests + test + + + org.jenkins-ci.plugins + scm-api + ${scm-api.version} test org.jenkins-ci.plugins.workflow workflow-step-api tests - ${workflow.version} + ${workflow-step-api.version} + test + + + org.jenkins-ci.plugins.workflow + workflow-cps + ${workflow-cps.version} test @@ -218,6 +256,12 @@ powermock-module-junit4 ${powermock.version} test + + + org.javassist + javassist + + org.powermock @@ -228,20 +272,32 @@ org.javassist javassist - 3.18.1-GA + 3.20.0-GA test org.jenkins-ci.main jenkins-war war - ${jenkins.version} + ${jenkins-war.version} test org.jenkins-ci.plugins credentials - 2.1.4 + 2.1.5 + test + + + org.jenkins-ci.plugins.workflow + workflow-durable-task-step + ${workflow-durable-task-step.version} + test + + + org.jenkins-ci.plugins.workflow + workflow-basic-steps + ${workflow-basic-steps.version} test diff --git a/src/main/java/hudson/plugins/emailext/ExtendedEmailPublisherDescriptor.java b/src/main/java/hudson/plugins/emailext/ExtendedEmailPublisherDescriptor.java index c14228707..4fe88d950 100644 --- a/src/main/java/hudson/plugins/emailext/ExtendedEmailPublisherDescriptor.java +++ b/src/main/java/hudson/plugins/emailext/ExtendedEmailPublisherDescriptor.java @@ -543,12 +543,15 @@ public List getDefaultTriggerIds() { for(EmailTriggerDescriptor t : this.defaultTriggers) { // we have to do the below because a bunch of stuff is not serialized for the Descriptor EmailTriggerDescriptor d = Jenkins.getActiveInstance().getDescriptorByType(t.getClass()); - if(!defaultTriggerIds.contains(d.getId())) { + if(d != null && !defaultTriggerIds.contains(d.getId())) { defaultTriggerIds.add(d.getId()); } } } else { - defaultTriggerIds.add(Jenkins.getActiveInstance().getDescriptor(FailureTrigger.class).getId()); + FailureTrigger.DescriptorImpl f = Jenkins.getActiveInstance().getDescriptorByType(FailureTrigger.DescriptorImpl.class); + if (f != null) { + defaultTriggerIds.add(f.getId()); + } } save(); } diff --git a/src/main/java/hudson/plugins/emailext/plugins/recipients/CulpritsRecipientProvider.java b/src/main/java/hudson/plugins/emailext/plugins/recipients/CulpritsRecipientProvider.java index 62a24d42b..add492723 100644 --- a/src/main/java/hudson/plugins/emailext/plugins/recipients/CulpritsRecipientProvider.java +++ b/src/main/java/hudson/plugins/emailext/plugins/recipients/CulpritsRecipientProvider.java @@ -6,9 +6,10 @@ package hudson.plugins.emailext.plugins.recipients; +import com.google.common.base.Predicates; +import com.google.common.collect.Iterables; import hudson.EnvVars; import hudson.Extension; -import hudson.model.AbstractBuild; import hudson.model.Result; import hudson.model.Run; import hudson.model.User; @@ -17,10 +18,13 @@ import hudson.plugins.emailext.plugins.RecipientProvider; import hudson.plugins.emailext.plugins.RecipientProviderDescriptor; import jenkins.model.Jenkins; +import org.jenkinsci.plugins.workflow.job.WorkflowRun; import org.kohsuke.stapler.DataBoundConstructor; import javax.mail.internet.InternetAddress; import java.io.PrintStream; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; import java.util.ArrayList; import java.util.List; import java.util.Set; @@ -51,10 +55,25 @@ public void send(final String format, final Object... args) { } final Debug debug = new Debug(); Run run = context.getRun(); - if (run instanceof AbstractBuild) { - Set users = ((AbstractBuild)run).getCulprits(); - RecipientProviderUtilities.addUsers(users, context, env, to, cc, bcc, debug); - } else { + + boolean runHasGetCulprits = false; + + // TODO: core 2.60+, workflow-job 2.12+: Switch to checking if run is RunWithSCM and make catch an else block + try { + Method getCulprits = run.getClass().getMethod("getCulprits"); + runHasGetCulprits = true; + if (Set.class.isAssignableFrom(getCulprits.getReturnType())) { + @SuppressWarnings("unchecked") + Set users = (Set) getCulprits.invoke(run); + if (Iterables.all(users, Predicates.instanceOf(User.class))) { + RecipientProviderUtilities.addUsers(users, context, env, to, cc, bcc, debug); + } + } + } catch (NoSuchMethodException | InvocationTargetException | IllegalAccessException e) { + // Only log a debug message if the exception is not due to an older WorkflowRun without getCulprits... + if (!(run instanceof WorkflowRun && !runHasGetCulprits)) { + debug.send("Exception getting culprits for %s: %s", run, e); + } List> builds = new ArrayList<>(); Run build = run; builds.add(build); diff --git a/src/main/java/hudson/plugins/emailext/plugins/recipients/RecipientProviderUtilities.java b/src/main/java/hudson/plugins/emailext/plugins/recipients/RecipientProviderUtilities.java index 54a099d6e..629eb7a5d 100644 --- a/src/main/java/hudson/plugins/emailext/plugins/recipients/RecipientProviderUtilities.java +++ b/src/main/java/hudson/plugins/emailext/plugins/recipients/RecipientProviderUtilities.java @@ -66,6 +66,7 @@ public static Set getChangeSetAuthors(final Collection> runs, fi final Set users = new HashSet<>(); for (final Run run : runs) { debug.send(" build: %d", run.getNumber()); + // TODO: core 2.60+, workflow-job 2.12+: Switch to checking if run is an instance of RunWithSCM and call getChangeSets directly. if (run instanceof AbstractBuild) { final ChangeLogSet changeLogSet = ((AbstractBuild)run).getChangeSet(); if (changeLogSet == null) { @@ -74,6 +75,7 @@ public static Set getChangeSetAuthors(final Collection> runs, fi addChangeSetUsers(changeLogSet, users, debug); } } else { + // TODO: core 2.60+, workflow-job 2.12+: Decide whether to remove this logic since it won't be needed for Pipelines any more. try { Method getChangeSets = run.getClass().getMethod("getChangeSets"); if (List.class.isAssignableFrom(getChangeSets.getReturnType())) { diff --git a/src/main/java/hudson/plugins/emailext/plugins/trigger/FailureTrigger.java b/src/main/java/hudson/plugins/emailext/plugins/trigger/FailureTrigger.java index 2f26fb3f1..f8e1c721a 100644 --- a/src/main/java/hudson/plugins/emailext/plugins/trigger/FailureTrigger.java +++ b/src/main/java/hudson/plugins/emailext/plugins/trigger/FailureTrigger.java @@ -33,7 +33,11 @@ public FailureTrigger(boolean sendToList, boolean sendToDevs, boolean sendToRequ @Deprecated public static FailureTrigger createDefault() { DescriptorImpl descriptor = (DescriptorImpl) Jenkins.getActiveInstance().getDescriptor(FailureTrigger.class); - return (FailureTrigger) descriptor.createDefault(); + if (descriptor != null) { + return (FailureTrigger) descriptor.createDefault(); + } else { + return null; + } } @Override diff --git a/src/test/java/hudson/plugins/emailext/plugins/recipients/CulpritsRecipientProviderTest.java b/src/test/java/hudson/plugins/emailext/plugins/recipients/CulpritsRecipientProviderTest.java index 1e9139c8f..f3b204007 100644 --- a/src/test/java/hudson/plugins/emailext/plugins/recipients/CulpritsRecipientProviderTest.java +++ b/src/test/java/hudson/plugins/emailext/plugins/recipients/CulpritsRecipientProviderTest.java @@ -1,11 +1,13 @@ package hudson.plugins.emailext.plugins.recipients; import hudson.model.FreeStyleBuild; +import hudson.model.Job; import hudson.model.Result; import hudson.model.User; import hudson.plugins.emailext.ExtendedEmailPublisherDescriptor; import hudson.tasks.Mailer; import jenkins.model.Jenkins; +import org.jenkinsci.plugins.workflow.job.WorkflowJob; import org.jenkinsci.plugins.workflow.job.WorkflowRun; import org.junit.Before; import org.junit.Test; @@ -22,7 +24,9 @@ Mailer.class, Mailer.DescriptorImpl.class, User.class, - WorkflowRun.class + WorkflowRun.class, + WorkflowJob.class, + Job.class }) public class CulpritsRecipientProviderTest { @@ -46,38 +50,42 @@ public void before() throws Exception { @Test public void testAddRecipients1() throws Exception { - final WorkflowRun build1 = PowerMockito.mock(WorkflowRun.class); + final WorkflowJob j = PowerMockito.mock(WorkflowJob.class); + final WorkflowRun build1 = PowerMockito.spy(new WorkflowRun(j)); PowerMockito.when(build1.getResult()).thenReturn(Result.UNSTABLE); MockUtilities.addChangeSet(build1, "X", "V"); + PowerMockito.doReturn(null).when(build1).getPreviousBuild(); - final WorkflowRun build2 = PowerMockito.mock(WorkflowRun.class); + final WorkflowRun build2 = PowerMockito.spy(new WorkflowRun(j)); PowerMockito.when(build2.getResult()).thenReturn(Result.SUCCESS); MockUtilities.addChangeSet(build2, "Z", "V"); - PowerMockito.when(build2.getPreviousCompletedBuild()).thenReturn(build1); + PowerMockito.doReturn(build1).when(build2).getPreviousCompletedBuild(); - final WorkflowRun build3 = PowerMockito.mock(WorkflowRun.class); + final WorkflowRun build3 = PowerMockito.spy(new WorkflowRun(j)); PowerMockito.when(build3.getResult()).thenReturn(Result.UNSTABLE); MockUtilities.addChangeSet(build3, "A"); - PowerMockito.when(build3.getPreviousCompletedBuild()).thenReturn(build2); + PowerMockito.doReturn(build2).when(build3).getPreviousCompletedBuild(); - final WorkflowRun build4 = PowerMockito.mock(WorkflowRun.class); + final WorkflowRun build4 = PowerMockito.spy(new WorkflowRun(j)); PowerMockito.when(build4.getResult()).thenReturn(Result.UNSTABLE); MockUtilities.addChangeSet(build4, "B"); - PowerMockito.when(build4.getPreviousCompletedBuild()).thenReturn(build3); + PowerMockito.doReturn(build3).when(build4).getPreviousCompletedBuild(); TestUtilities.checkRecipients(build4, new CulpritsRecipientProvider(), "A", "B"); } @Test public void testAddRecipients2() throws Exception { - final WorkflowRun build1 = PowerMockito.mock(WorkflowRun.class); + final WorkflowJob j = PowerMockito.mock(WorkflowJob.class); + final WorkflowRun build1 = PowerMockito.spy(new WorkflowRun(j)); PowerMockito.when(build1.getResult()).thenReturn(Result.UNSTABLE); MockUtilities.addChangeSet(build1, "X", "V"); + PowerMockito.doReturn(null).when(build1).getPreviousBuild(); - final WorkflowRun build2 = PowerMockito.mock(WorkflowRun.class); + final WorkflowRun build2 = PowerMockito.spy(new WorkflowRun(j)); PowerMockito.when(build2.getResult()).thenReturn(Result.SUCCESS); MockUtilities.addChangeSet(build2, "Z", "V"); - PowerMockito.when(build2.getPreviousCompletedBuild()).thenReturn(build1); + PowerMockito.doReturn(build1).when(build2).getPreviousCompletedBuild(); TestUtilities.checkRecipients(build2, new CulpritsRecipientProvider(), "X", "V", "Z"); } diff --git a/src/test/java/hudson/plugins/emailext/plugins/recipients/DevelopersRecipientProviderTest.java b/src/test/java/hudson/plugins/emailext/plugins/recipients/DevelopersRecipientProviderTest.java index 0e7e05370..70e8f6d84 100644 --- a/src/test/java/hudson/plugins/emailext/plugins/recipients/DevelopersRecipientProviderTest.java +++ b/src/test/java/hudson/plugins/emailext/plugins/recipients/DevelopersRecipientProviderTest.java @@ -1,11 +1,13 @@ package hudson.plugins.emailext.plugins.recipients; import hudson.model.FreeStyleBuild; +import hudson.model.FreeStyleProject; import hudson.model.Result; import hudson.model.User; import hudson.plugins.emailext.ExtendedEmailPublisherDescriptor; import hudson.tasks.Mailer; import jenkins.model.Jenkins; +import org.jenkinsci.plugins.workflow.job.WorkflowJob; import org.jenkinsci.plugins.workflow.job.WorkflowRun; import org.junit.Before; import org.junit.Test; @@ -22,7 +24,9 @@ Mailer.class, Mailer.DescriptorImpl.class, User.class, - WorkflowRun.class + WorkflowRun.class, + WorkflowJob.class, + FreeStyleProject.class }) public class DevelopersRecipientProviderTest { @@ -46,14 +50,16 @@ public void before() throws Exception { @Test public void testAddRecipients() throws Exception { - final FreeStyleBuild build1 = PowerMockito.mock(FreeStyleBuild.class); - PowerMockito.when(build1.getResult()).thenReturn(Result.UNSTABLE); + final FreeStyleProject p = PowerMockito.mock(FreeStyleProject.class); + final FreeStyleBuild build1 = PowerMockito.spy(new FreeStyleBuild(p)); + PowerMockito.doReturn(Result.UNSTABLE).when(build1).getResult(); MockUtilities.addRequestor(build1, "A"); MockUtilities.addChangeSet(build1, "X", "V"); TestUtilities.checkRecipients(build1, new DevelopersRecipientProvider(), "X", "V"); - final WorkflowRun build2 = PowerMockito.mock(WorkflowRun.class); - PowerMockito.when(build2.getResult()).thenReturn(Result.UNSTABLE); + final WorkflowJob j = PowerMockito.mock(WorkflowJob.class); + final WorkflowRun build2 = PowerMockito.spy(new WorkflowRun(j)); + PowerMockito.doReturn(Result.UNSTABLE).when(build2).getResult(); MockUtilities.addChangeSet(build2, "X", "V"); TestUtilities.checkRecipients(build2, new DevelopersRecipientProvider(), "X", "V"); } diff --git a/src/test/java/hudson/plugins/emailext/plugins/recipients/FailingTestSuspectsRecipientProviderTest.java b/src/test/java/hudson/plugins/emailext/plugins/recipients/FailingTestSuspectsRecipientProviderTest.java index d2f97eb7b..01df8ccc5 100644 --- a/src/test/java/hudson/plugins/emailext/plugins/recipients/FailingTestSuspectsRecipientProviderTest.java +++ b/src/test/java/hudson/plugins/emailext/plugins/recipients/FailingTestSuspectsRecipientProviderTest.java @@ -23,8 +23,11 @@ */ package hudson.plugins.emailext.plugins.recipients; +import hudson.PluginManager; import hudson.model.FreeStyleBuild; +import hudson.model.FreeStyleProject; import hudson.model.Result; +import hudson.model.Run; import hudson.model.User; import hudson.plugins.emailext.ExtendedEmailPublisherDescriptor; import hudson.tasks.Mailer; @@ -35,6 +38,7 @@ import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.reflect.Whitebox; @RunWith(PowerMockRunner.class) @PrepareForTest({ @@ -43,7 +47,9 @@ Jenkins.class, Mailer.class, Mailer.DescriptorImpl.class, - User.class + User.class, + FreeStyleProject.class, + PluginManager.class }) public class FailingTestSuspectsRecipientProviderTest { @@ -56,8 +62,15 @@ public void before() throws Exception { PowerMockito.when(extendedEmailPublisherDescriptor.getExcludedCommitters()).thenReturn(""); PowerMockito.when(jenkins.getDescriptorByType(ExtendedEmailPublisherDescriptor.class)).thenReturn(extendedEmailPublisherDescriptor); + + final PluginManager pluginManager = PowerMockito.mock(PluginManager.class); + Whitebox.setInternalState(pluginManager, "uberClassLoader", this.getClass().getClassLoader()); + + PowerMockito.when(jenkins.getPluginManager()).thenReturn(pluginManager); + PowerMockito.mockStatic(Jenkins.class); PowerMockito.doReturn(jenkins).when(Jenkins.class, "getActiveInstance"); + PowerMockito.doReturn(jenkins).when(Jenkins.class, "getInstance"); final Mailer.DescriptorImpl descriptor = PowerMockito.mock(Mailer.DescriptorImpl.class); PowerMockito.when(descriptor.getDefaultSuffix()).thenReturn("DOMAIN"); @@ -73,8 +86,10 @@ public void testAddRecipients() throws Exception { * No committers. * Tests {a,b} fail. */ - final FreeStyleBuild build1 = PowerMockito.mock(FreeStyleBuild.class); - PowerMockito.when(build1.getResult()).thenReturn(Result.UNSTABLE); + final FreeStyleProject p = PowerMockito.mock(FreeStyleProject.class); + final FreeStyleBuild build1 = PowerMockito.spy(new FreeStyleBuild(p)); + PowerMockito.doReturn(Result.UNSTABLE).when(build1).getResult(); + PowerMockito.doReturn(null).when(build1).getPreviousCompletedBuild(); MockUtilities.addRequestor(build1, "A"); MockUtilities.addTestResultAction(build1, build1, build1); TestUtilities.checkRecipients(build1, new FailingTestSuspectsRecipientProvider(), "A"); @@ -84,9 +99,9 @@ public void testAddRecipients() throws Exception { * Committers {U,V}. * Tests {a,b,c} fail. */ - final FreeStyleBuild build2 = PowerMockito.mock(FreeStyleBuild.class); - PowerMockito.when(build2.getPreviousCompletedBuild()).thenReturn(build1); - PowerMockito.when(build2.getResult()).thenReturn(Result.UNSTABLE); + final FreeStyleBuild build2 = PowerMockito.spy(new FreeStyleBuild(p)); + PowerMockito.doReturn(Result.UNSTABLE).when(build2).getResult(); + PowerMockito.doReturn(build1).when(build2).getPreviousCompletedBuild(); MockUtilities.addChangeSet(build2, "U", "V"); MockUtilities.addTestResultAction(build2, build1, build1, build2); TestUtilities.checkRecipients(build2, new FailingTestSuspectsRecipientProvider(), "A", "U", "V"); @@ -96,9 +111,9 @@ public void testAddRecipients() throws Exception { * Committers {X,V}. * Tests {c,d} fail. */ - final FreeStyleBuild build3 = PowerMockito.mock(FreeStyleBuild.class); - PowerMockito.when(build3.getPreviousCompletedBuild()).thenReturn(build2); - PowerMockito.when(build3.getResult()).thenReturn(Result.UNSTABLE); + final FreeStyleBuild build3 = PowerMockito.spy(new FreeStyleBuild(p)); + PowerMockito.doReturn(Result.UNSTABLE).when(build3).getResult(); + PowerMockito.doReturn(build2).when(build3).getPreviousCompletedBuild(); MockUtilities.addChangeSet(build3, "X", "V"); MockUtilities.addTestResultAction(build3, build2, build3); TestUtilities.checkRecipients(build3, new FailingTestSuspectsRecipientProvider(), "U", "V", "X"); @@ -108,9 +123,9 @@ public void testAddRecipients() throws Exception { * Committers {K} * No tests were performed. The build failed. */ - final FreeStyleBuild build4 = PowerMockito.mock(FreeStyleBuild.class); - PowerMockito.when(build4.getPreviousCompletedBuild()).thenReturn(build3); - PowerMockito.when(build4.getResult()).thenReturn(Result.FAILURE); + final FreeStyleBuild build4 = PowerMockito.spy(new FreeStyleBuild(p)); + PowerMockito.doReturn(Result.FAILURE).when(build4).getResult(); + PowerMockito.doReturn(build3).when(build4).getPreviousCompletedBuild(); MockUtilities.addChangeSet(build4, "K"); TestUtilities.checkRecipients(build4, new FailingTestSuspectsRecipientProvider()); @@ -119,9 +134,9 @@ public void testAddRecipients() throws Exception { * Committers {X,U,V}. * No tests were performed. The build failed. */ - final FreeStyleBuild build5 = PowerMockito.mock(FreeStyleBuild.class); - PowerMockito.when(build5.getPreviousCompletedBuild()).thenReturn(build4); - PowerMockito.when(build5.getResult()).thenReturn(Result.FAILURE); + final FreeStyleBuild build5 = PowerMockito.spy(new FreeStyleBuild(p)); + PowerMockito.doReturn(Result.FAILURE).when(build5).getResult(); + PowerMockito.doReturn(build4).when(build5).getPreviousCompletedBuild(); MockUtilities.addChangeSet(build5, "U", "W"); TestUtilities.checkRecipients(build5, new FailingTestSuspectsRecipientProvider()); @@ -130,9 +145,9 @@ public void testAddRecipients() throws Exception { * Committers {W}. * Tests {a,e (new test)} fail. */ - final FreeStyleBuild build6 = PowerMockito.mock(FreeStyleBuild.class); - PowerMockito.when(build6.getPreviousCompletedBuild()).thenReturn(build5); - PowerMockito.when(build6.getResult()).thenReturn(Result.UNSTABLE); + final FreeStyleBuild build6 = PowerMockito.spy(new FreeStyleBuild(p)); + PowerMockito.doReturn(Result.UNSTABLE).when(build6).getResult(); + PowerMockito.doReturn(build5).when(build6).getPreviousCompletedBuild(); MockUtilities.addRequestor(build6, "A"); MockUtilities.addChangeSet(build6, "W"); MockUtilities.addTestResultAction(build6, build6, build6); diff --git a/src/test/java/hudson/plugins/emailext/plugins/recipients/FirstFailingBuildSuspectsRecipientProviderTest.java b/src/test/java/hudson/plugins/emailext/plugins/recipients/FirstFailingBuildSuspectsRecipientProviderTest.java index b054771c1..819578a68 100644 --- a/src/test/java/hudson/plugins/emailext/plugins/recipients/FirstFailingBuildSuspectsRecipientProviderTest.java +++ b/src/test/java/hudson/plugins/emailext/plugins/recipients/FirstFailingBuildSuspectsRecipientProviderTest.java @@ -23,7 +23,9 @@ */ package hudson.plugins.emailext.plugins.recipients; +import hudson.PluginManager; import hudson.model.FreeStyleBuild; +import hudson.model.FreeStyleProject; import hudson.model.Result; import hudson.model.User; import hudson.plugins.emailext.ExtendedEmailPublisherDescriptor; @@ -35,6 +37,7 @@ import org.powermock.api.mockito.PowerMockito; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.reflect.Whitebox; @RunWith(PowerMockRunner.class) @PrepareForTest({ @@ -43,7 +46,9 @@ Jenkins.class, Mailer.class, Mailer.DescriptorImpl.class, - User.class + User.class, + FreeStyleProject.class, + PluginManager.class }) public class FirstFailingBuildSuspectsRecipientProviderTest { @@ -55,8 +60,15 @@ public void before() throws Exception { extendedEmailPublisherDescriptor.setDebugMode(true); PowerMockito.when(jenkins.getDescriptorByType(ExtendedEmailPublisherDescriptor.class)).thenReturn(extendedEmailPublisherDescriptor); + + final PluginManager pluginManager = PowerMockito.mock(PluginManager.class); + Whitebox.setInternalState(pluginManager, "uberClassLoader", this.getClass().getClassLoader()); + + PowerMockito.when(jenkins.getPluginManager()).thenReturn(pluginManager); + PowerMockito.mockStatic(Jenkins.class); PowerMockito.doReturn(jenkins).when(Jenkins.class, "getActiveInstance"); + PowerMockito.doReturn(jenkins).when(Jenkins.class, "getInstance"); final Mailer.DescriptorImpl descriptor = PowerMockito.mock(Mailer.DescriptorImpl.class); PowerMockito.when(descriptor.getDefaultSuffix()).thenReturn("DOMAIN"); @@ -72,8 +84,10 @@ public void testAddRecipients() throws Exception { * No committers. * Failed. */ - final FreeStyleBuild build1 = PowerMockito.mock(FreeStyleBuild.class); - PowerMockito.when(build1.getResult()).thenReturn(Result.FAILURE); + final FreeStyleProject p = PowerMockito.mock(FreeStyleProject.class); + final FreeStyleBuild build1 = PowerMockito.spy(new FreeStyleBuild(p)); + PowerMockito.doReturn(Result.FAILURE).when(build1).getResult(); + PowerMockito.doReturn(null).when(build1).getPreviousCompletedBuild(); MockUtilities.addRequestor(build1, "A"); TestUtilities.checkRecipients(build1, new FirstFailingBuildSuspectsRecipientProvider(), "A"); @@ -82,9 +96,9 @@ public void testAddRecipients() throws Exception { * No committers. * Unstable. */ - final FreeStyleBuild build2 = PowerMockito.mock(FreeStyleBuild.class); - PowerMockito.when(build2.getPreviousCompletedBuild()).thenReturn(build1); - PowerMockito.when(build2.getResult()).thenReturn(Result.UNSTABLE); + final FreeStyleBuild build2 = PowerMockito.spy(new FreeStyleBuild(p)); + PowerMockito.doReturn(Result.UNSTABLE).when(build2).getResult(); + PowerMockito.doReturn(build1).when(build2).getPreviousCompletedBuild(); MockUtilities.addRequestor(build2, "A"); TestUtilities.checkRecipients(build2, new FirstFailingBuildSuspectsRecipientProvider()); @@ -93,9 +107,9 @@ public void testAddRecipients() throws Exception { * Committers {X,V}. * Failed. */ - final FreeStyleBuild build3 = PowerMockito.mock(FreeStyleBuild.class); - PowerMockito.when(build3.getPreviousCompletedBuild()).thenReturn(build2); - PowerMockito.when(build3.getResult()).thenReturn(Result.FAILURE); + final FreeStyleBuild build3 = PowerMockito.spy(new FreeStyleBuild(p)); + PowerMockito.doReturn(Result.FAILURE).when(build3).getResult(); + PowerMockito.doReturn(build2).when(build3).getPreviousCompletedBuild(); MockUtilities.addRequestor(build3, "A"); MockUtilities.addChangeSet(build3, "X", "V"); TestUtilities.checkRecipients(build3, new FirstFailingBuildSuspectsRecipientProvider(), "X", "V", "A"); @@ -105,9 +119,9 @@ public void testAddRecipients() throws Exception { * Committers {X}. * Aborted. */ - final FreeStyleBuild build4 = PowerMockito.mock(FreeStyleBuild.class); - PowerMockito.when(build4.getPreviousCompletedBuild()).thenReturn(build3); - PowerMockito.when(build4.getResult()).thenReturn(Result.ABORTED); + final FreeStyleBuild build4 = PowerMockito.spy(new FreeStyleBuild(p)); + PowerMockito.doReturn(Result.ABORTED).when(build4).getResult(); + PowerMockito.doReturn(build3).when(build4).getPreviousCompletedBuild(); MockUtilities.addRequestor(build4, "B"); MockUtilities.addChangeSet(build4, "X"); TestUtilities.checkRecipients(build4, new FirstFailingBuildSuspectsRecipientProvider()); @@ -117,9 +131,9 @@ public void testAddRecipients() throws Exception { * Committers {U,V}. * Failed. */ - final FreeStyleBuild build5 = PowerMockito.mock(FreeStyleBuild.class); - PowerMockito.when(build5.getPreviousCompletedBuild()).thenReturn(build4); - PowerMockito.when(build5.getResult()).thenReturn(Result.FAILURE); + final FreeStyleBuild build5 = PowerMockito.spy(new FreeStyleBuild(p)); + PowerMockito.doReturn(Result.FAILURE).when(build5).getResult(); + PowerMockito.doReturn(build4).when(build5).getPreviousCompletedBuild(); MockUtilities.addRequestor(build5, "B"); MockUtilities.addChangeSet(build5, "U", "V"); TestUtilities.checkRecipients(build5, new FirstFailingBuildSuspectsRecipientProvider(), "X", "V", "A"); @@ -129,9 +143,9 @@ public void testAddRecipients() throws Exception { * Committers {W}. * Success. */ - final FreeStyleBuild build6 = PowerMockito.mock(FreeStyleBuild.class); - PowerMockito.when(build6.getPreviousCompletedBuild()).thenReturn(build5); - PowerMockito.when(build6.getResult()).thenReturn(Result.UNSTABLE); + final FreeStyleBuild build6 = PowerMockito.spy(new FreeStyleBuild(p)); + PowerMockito.doReturn(Result.UNSTABLE).when(build6).getResult(); + PowerMockito.doReturn(build5).when(build6).getPreviousCompletedBuild(); MockUtilities.addRequestor(build6, "A"); MockUtilities.addChangeSet(build6, "W"); TestUtilities.checkRecipients(build6, new FirstFailingBuildSuspectsRecipientProvider()); diff --git a/src/test/java/hudson/plugins/emailext/plugins/recipients/MockUtilities.java b/src/test/java/hudson/plugins/emailext/plugins/recipients/MockUtilities.java index e81bf1af8..7247f35ff 100644 --- a/src/test/java/hudson/plugins/emailext/plugins/recipients/MockUtilities.java +++ b/src/test/java/hudson/plugins/emailext/plugins/recipients/MockUtilities.java @@ -123,7 +123,7 @@ public static void addChangeSet(final WorkflowRun build, final String... inAutho public static void addChangeSet(final AbstractBuild build, final String... inAuthors) { ChangeLogSet changeSet = makeChangeSet(build, inAuthors); - PowerMockito.when(build.getChangeSet()).thenReturn((ChangeLogSet)changeSet); + PowerMockito.doReturn(changeSet).when(build).getChangeSet(); } public static void addRequestor(final AbstractBuild build, final String requestor) throws Exception { @@ -137,7 +137,7 @@ public User answer(InvocationOnMock invocation) throws Throwable { }).when(User.class, "get", Mockito.anyString(), Mockito.anyBoolean(), Mockito.anyMap()); final Cause.UserIdCause cause = PowerMockito.mock(Cause.UserIdCause.class); PowerMockito.when(cause.getUserId()).thenReturn(requestor); - PowerMockito.when(build.getCause(Cause.UserIdCause.class)).thenReturn(cause); + PowerMockito.doReturn(cause).when(build).getCause(Cause.UserIdCause.class); } public static void addTestResultAction(final AbstractBuild build, final AbstractBuild... failedSinces) { @@ -150,7 +150,7 @@ public static void addTestResultAction(final AbstractBuild build, final Ab final TestResultAction testResultAction = PowerMockito.mock(TestResultAction.class); PowerMockito.when(testResultAction.getFailedTests()).thenReturn(failedTests); PowerMockito.when(testResultAction.getFailCount()).thenReturn(failedTests.size()); - PowerMockito.when(build.getAction(AbstractTestResultAction.class)).thenReturn(testResultAction); + PowerMockito.doReturn(testResultAction).when(build).getAction(AbstractTestResultAction.class); } }