From 4ae72963f38aff436280cd29613c93f955cf33de Mon Sep 17 00:00:00 2001 From: Mark Waite Date: Sat, 11 Jan 2025 06:26:35 -0700 Subject: [PATCH] Extract jobGroup creation to a separate method Avoid duplicating code that is common between uses. Use a job group name that is distinct for each test so that the single JenkinsRule instance does not risk having the tests collide in their use of the job group. --- .../strategy/PriorityJobPropertyTest.java | 32 ++++++++++++------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/src/test/java/jenkins/advancedqueue/priority/strategy/PriorityJobPropertyTest.java b/src/test/java/jenkins/advancedqueue/priority/strategy/PriorityJobPropertyTest.java index c80f9435..6d18e7c5 100644 --- a/src/test/java/jenkins/advancedqueue/priority/strategy/PriorityJobPropertyTest.java +++ b/src/test/java/jenkins/advancedqueue/priority/strategy/PriorityJobPropertyTest.java @@ -12,6 +12,7 @@ import hudson.model.ListView; import java.io.IOException; import java.util.List; +import java.util.Random; import jenkins.advancedqueue.JobGroup; import jenkins.advancedqueue.PriorityConfiguration; import jenkins.advancedqueue.PrioritySorterConfiguration; @@ -19,7 +20,9 @@ import net.sf.json.JSONObject; import org.junit.BeforeClass; import org.junit.ClassRule; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.TestName; import org.jvnet.hudson.test.JenkinsRule; import org.kohsuke.stapler.StaplerRequest; @@ -28,6 +31,9 @@ public class PriorityJobPropertyTest { @ClassRule public static JenkinsRule j = new JenkinsRule(); + @Rule + public TestName testName = new TestName(); + private static PriorityJobProperty property; private static PriorityJobProperty.DescriptorImpl descriptor; private static FreeStyleProject project; @@ -67,6 +73,18 @@ public void descriptorImpl_getPrioritiesReturnsNonEmptyList() { assertFalse(descriptor.getPriorities().isEmpty()); } + private final Random random = new Random(); + + private JobGroup createJobGroup() { + JobGroup jobGroup = new JobGroup(); + jobGroup.setDescription("testGroup-" + testName.getMethodName()); + jobGroup.setRunExclusive(random.nextBoolean()); + jobGroup.setUsePriorityStrategies(random.nextBoolean()); + jobGroup.setId(random.nextInt()); + jobGroup.setJobGroupStrategy(new ViewBasedJobInclusionStrategy("existingView")); // Use the newly created view + return jobGroup; + } + @Test public void descriptorImpl_isUsedReturnsTrueWhenJobGroupUsesPriorityStrategies() throws IOException { // Initialize PrioritySorterConfiguration @@ -86,15 +104,10 @@ public void descriptorImpl_isUsedReturnsTrueWhenJobGroupUsesPriorityStrategies() assertNotNull(j.jenkins.getView("existingView")); // Set up the PriorityJobProperty.DescriptorImpl - descriptor = new PriorityJobProperty.DescriptorImpl(); PriorityConfiguration configuration = PriorityConfiguration.get(); List jobGroups = configuration.getJobGroups(); - JobGroup jobGroup = new JobGroup(); - jobGroup.setDescription("testGroup"); - jobGroup.setRunExclusive(true); + JobGroup jobGroup = createJobGroup(); jobGroup.setUsePriorityStrategies(true); - jobGroup.setId(1); - jobGroup.setJobGroupStrategy(new ViewBasedJobInclusionStrategy("existingView")); // Use the newly created view // Add a PriorityStrategyHolder with a JobPropertyStrategy to the JobGroup JobPropertyStrategy jobPropertyStrategy = new JobPropertyStrategy(); @@ -114,12 +127,7 @@ public void descriptorImpl_isUsedReturnsFalseWhenJobGroupDoesNotUsePriorityStrat descriptor = new PriorityJobProperty.DescriptorImpl(); PriorityConfiguration configuration = PriorityConfiguration.get(); List jobGroups = configuration.getJobGroups(); - JobGroup jobGroup = new JobGroup(); - jobGroup.setDescription("testGroup"); - jobGroup.setRunExclusive(false); - jobGroup.setUsePriorityStrategies(false); - jobGroup.setId(1); - jobGroup.setJobGroupStrategy(new ViewBasedJobInclusionStrategy("defaultView")); // Set a default strategy + JobGroup jobGroup = createJobGroup(); jobGroups.add(jobGroup); configuration.setJobGroups(jobGroups); assertFalse(descriptor.isUsed(project));