From fca49d2814815922bcafef51fdca26c6754e3d0a Mon Sep 17 00:00:00 2001 From: strangelookingnerd <49242855+strangelookingnerd@users.noreply.github.com> Date: Thu, 6 Feb 2025 23:26:54 +0100 Subject: [PATCH] Migrate tests to JUnit5 (#453) * Migrate annotations * Migrate assertions * Cleanup assertions * Minor cleanup * Remove public visibility of test classes and methods Co-authored-by: strangelookingnerd --- .../JCasCFivePrioritiesTest.java | 12 +- .../advancedqueue/JCasCSimpleTest.java | 12 +- .../PriorityCalculationsUtilTest.java | 31 +- ...onfigurationPlaceholderTaskHelperTest.java | 59 ++-- .../FolderBasedJobInclusionStrategyTest.java | 60 ++-- .../strategy/FolderPropertyLoaderTest.java | 35 +- .../JobInclusionFolderPropertyTest.java | 31 +- .../strategy/JobInclusionJobPropertyTest.java | 61 ++-- ...PropertyBasedJobInclusionStrategyTest.java | 84 +++-- .../PrioritySorterRestrictionTest.java | 2 +- .../strategy/BuildParameterStrategyTest.java | 33 +- .../priority/strategy/HealthStrategyTest.java | 312 +++--------------- .../strategy/JobPropertyStrategyTest.java | 69 ++-- .../strategy/PriorityJobPropertyTest.java | 51 +-- .../sorter/PrioritySorterJobColumnTest.java | 32 +- .../sorter/SorterStrategyDescriptorTest.java | 27 +- .../sorter/strategy/FQStrategyTest.java | 32 +- .../strategy/MultiBucketStrategyTest.java | 40 ++- .../sorter/strategy/WFQStrategyTest.java | 29 +- .../jenkins/advancedqueue/test/BasicTest.java | 22 +- .../test/ConfigurationAsCodeTest.java | 14 +- .../advancedqueue/test/ExportCascTest.java | 16 +- .../test/JobPatternGroupTest.java | 24 +- .../advancedqueue/test/MatrixTest.java | 24 +- .../test/MultipleMatchJobGroupTest.java | 20 +- .../advancedqueue/test/NestedViewTest.java | 20 +- .../advancedqueue/test/OneJobGroupTest.java | 26 +- .../test/PrioritySorterConfigurationTest.java | 22 +- .../advancedqueue/test/SectionedViewTest.java | 20 +- .../advancedqueue/test/SimpleTest.java | 22 +- .../test/SortBlockedItemsTest.java | 12 +- .../advancedqueue/test/SubmitTest.java | 13 +- .../advancedqueue/test/UpstreamTest.java | 23 +- .../testutil/TestRunListener.java | 18 +- .../util/PrioritySorterUtilTest.java | 12 +- 35 files changed, 572 insertions(+), 748 deletions(-) diff --git a/src/test/java/jenkins/advancedqueue/JCasCFivePrioritiesTest.java b/src/test/java/jenkins/advancedqueue/JCasCFivePrioritiesTest.java index ece41c1a..9242ef93 100644 --- a/src/test/java/jenkins/advancedqueue/JCasCFivePrioritiesTest.java +++ b/src/test/java/jenkins/advancedqueue/JCasCFivePrioritiesTest.java @@ -25,20 +25,20 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertFalse; +import static org.junit.jupiter.api.Assertions.assertFalse; -import io.jenkins.plugins.casc.misc.RoundTripAbstractTest; +import io.jenkins.plugins.casc.misc.junit.jupiter.AbstractRoundTripTest; import java.util.List; import jenkins.advancedqueue.priority.PriorityStrategy; import jenkins.advancedqueue.sorter.strategy.AbsoluteStrategy; -import org.jvnet.hudson.test.RestartableJenkinsRule; +import org.jvnet.hudson.test.JenkinsRule; -public class JCasCFivePrioritiesTest extends RoundTripAbstractTest { +public class JCasCFivePrioritiesTest extends AbstractRoundTripTest { @Override - protected void assertConfiguredAsExpected(RestartableJenkinsRule j, String configContent) { + protected void assertConfiguredAsExpected(JenkinsRule j, String configContent) { PrioritySorterConfiguration globalConfig = PrioritySorterConfiguration.get(); - assertFalse("Non-admins cannot edit priority", globalConfig.getOnlyAdminsMayEditPriorityConfiguration()); + assertFalse(globalConfig.getOnlyAdminsMayEditPriorityConfiguration(), "Non-admins cannot edit priority"); assertThat("Wrong strategy class", globalConfig.getStrategy().getClass(), is(AbsoluteStrategy.class)); assertThat("Wrong number of priorities", globalConfig.getStrategy().getNumberOfPriorities(), is(5)); assertThat("Wrong default priority", globalConfig.getStrategy().getDefaultPriority(), is(3)); diff --git a/src/test/java/jenkins/advancedqueue/JCasCSimpleTest.java b/src/test/java/jenkins/advancedqueue/JCasCSimpleTest.java index e2e048cc..3464f04e 100644 --- a/src/test/java/jenkins/advancedqueue/JCasCSimpleTest.java +++ b/src/test/java/jenkins/advancedqueue/JCasCSimpleTest.java @@ -25,19 +25,19 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertFalse; +import static org.junit.jupiter.api.Assertions.assertFalse; -import io.jenkins.plugins.casc.misc.RoundTripAbstractTest; +import io.jenkins.plugins.casc.misc.junit.jupiter.AbstractRoundTripTest; import java.util.List; import jenkins.advancedqueue.sorter.strategy.AbsoluteStrategy; -import org.jvnet.hudson.test.RestartableJenkinsRule; +import org.jvnet.hudson.test.JenkinsRule; -public class JCasCSimpleTest extends RoundTripAbstractTest { +public class JCasCSimpleTest extends AbstractRoundTripTest { @Override - protected void assertConfiguredAsExpected(RestartableJenkinsRule j, String configContent) { + protected void assertConfiguredAsExpected(JenkinsRule j, String configContent) { PrioritySorterConfiguration globalConfig = PrioritySorterConfiguration.get(); - assertFalse("Non-admins cannot edit priority", globalConfig.getOnlyAdminsMayEditPriorityConfiguration()); + assertFalse(globalConfig.getOnlyAdminsMayEditPriorityConfiguration(), "Non-admins cannot edit priority"); assertThat("Wrong strategy class", globalConfig.getStrategy().getClass(), is(AbsoluteStrategy.class)); assertThat("Wrong number of priorities", globalConfig.getStrategy().getNumberOfPriorities(), is(5)); assertThat("Wrong default priority", globalConfig.getStrategy().getDefaultPriority(), is(3)); diff --git a/src/test/java/jenkins/advancedqueue/PriorityCalculationsUtilTest.java b/src/test/java/jenkins/advancedqueue/PriorityCalculationsUtilTest.java index 4dc10ced..15531c58 100644 --- a/src/test/java/jenkins/advancedqueue/PriorityCalculationsUtilTest.java +++ b/src/test/java/jenkins/advancedqueue/PriorityCalculationsUtilTest.java @@ -1,27 +1,28 @@ package jenkins.advancedqueue; -import org.junit.Assert; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; -public class PriorityCalculationsUtilTest { +import org.junit.jupiter.api.Test; + +class PriorityCalculationsUtilTest { @Test - public void testScale() { - Assert.assertEquals(10, PriorityCalculationsUtil.scale(100, 10, 100)); - Assert.assertEquals(1, PriorityCalculationsUtil.scale(100, 10, 9)); - Assert.assertEquals(5, PriorityCalculationsUtil.scale(100, 10, 50)); - Assert.assertEquals(8, PriorityCalculationsUtil.scale(100, 10, 75)); + void testScale() { + assertEquals(10, PriorityCalculationsUtil.scale(100, 10, 100)); + assertEquals(1, PriorityCalculationsUtil.scale(100, 10, 9)); + assertEquals(5, PriorityCalculationsUtil.scale(100, 10, 50)); + assertEquals(8, PriorityCalculationsUtil.scale(100, 10, 75)); - Assert.assertEquals(1, PriorityCalculationsUtil.scale(5, 10, 1)); - Assert.assertEquals(3, PriorityCalculationsUtil.scale(5, 10, 2)); - Assert.assertEquals(5, PriorityCalculationsUtil.scale(5, 10, 3)); - Assert.assertEquals(8, PriorityCalculationsUtil.scale(5, 10, 4)); - Assert.assertEquals(10, PriorityCalculationsUtil.scale(5, 10, 5)); + assertEquals(1, PriorityCalculationsUtil.scale(5, 10, 1)); + assertEquals(3, PriorityCalculationsUtil.scale(5, 10, 2)); + assertEquals(5, PriorityCalculationsUtil.scale(5, 10, 3)); + assertEquals(8, PriorityCalculationsUtil.scale(5, 10, 4)); + assertEquals(10, PriorityCalculationsUtil.scale(5, 10, 5)); } @Test - public void testScaleUseDefaultPriority() { - Assert.assertEquals( + void testScaleUseDefaultPriority() { + assertEquals( PriorityCalculationsUtil.getUseDefaultPriorityPriority(), PriorityCalculationsUtil.scale(5, 10, PriorityCalculationsUtil.getUseDefaultPriorityPriority())); } diff --git a/src/test/java/jenkins/advancedqueue/PriorityConfigurationPlaceholderTaskHelperTest.java b/src/test/java/jenkins/advancedqueue/PriorityConfigurationPlaceholderTaskHelperTest.java index 0fadef02..974a12b8 100644 --- a/src/test/java/jenkins/advancedqueue/PriorityConfigurationPlaceholderTaskHelperTest.java +++ b/src/test/java/jenkins/advancedqueue/PriorityConfigurationPlaceholderTaskHelperTest.java @@ -5,10 +5,7 @@ import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; import static org.hamcrest.Matchers.startsWith; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -23,23 +20,24 @@ import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; import org.jenkinsci.plugins.workflow.job.WorkflowJob; import org.jenkinsci.plugins.workflow.support.steps.ExecutorStepExecution; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.ClassRule; -import org.junit.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; -public class PriorityConfigurationPlaceholderTaskHelperTest { +@WithJenkins +class PriorityConfigurationPlaceholderTaskHelperTest { - @ClassRule - public static JenkinsRule j = new JenkinsRule(); + private static JenkinsRule j; private static Queue.Item pipelineItemInQuietPeriod; private static DecisionLogger decisionLogger; private static List loggedMessages; - @BeforeClass - public static void startPipelineJobWithQuietPeriod() throws Exception { + @BeforeAll + static void beforeAll(JenkinsRule rule) throws Exception { + j = rule; // Start a Pipeline with a quiet period of 37 seconds before it runs String pipelineName = "my-pipeline-in-the-quiet-period"; WorkflowJob pipeline = j.createProject(WorkflowJob.class, pipelineName); @@ -53,11 +51,19 @@ public static void startPipelineJobWithQuietPeriod() throws Exception { pipeline.setDefinition(new CpsFlowDefinition(pipelineDefinition, true)); pipeline.scheduleBuild(37, new Cause.UserIdCause()); pipelineItemInQuietPeriod = findQueueItem(pipelineName); - assertNotNull("Pipeline in quiet period not in Queue", pipelineItemInQuietPeriod); + assertNotNull(pipelineItemInQuietPeriod, "Pipeline in quiet period not in Queue"); // Check the item is blocked due to the 37 second quiet period assertThat( pipelineItemInQuietPeriod.getCauseOfBlockage().getShortDescription(), startsWith("In the quiet period.")); + + decisionLogger = new DecisionLogger() { + @Override + public DecisionLogger addDecisionLog(int indent, String log) { + loggedMessages.add(log); + return this; + } + }; } private static Queue.Item findQueueItem(String name) { @@ -71,24 +77,13 @@ private static Queue.Item findQueueItem(String name) { return found; } - @BeforeClass - public static void createDecisionLogger() { - decisionLogger = new DecisionLogger() { - @Override - public DecisionLogger addDecisionLog(int indent, String log) { - loggedMessages.add(log); - return this; - } - }; - } - - @Before - public void clearLoggedMessages() throws Exception { + @BeforeEach + void beforeEach() throws Exception { loggedMessages = new ArrayList<>(); } @Test - public void testGetPriorityAssignsGlobalDefault() { + void testGetPriorityAssignsGlobalDefault() { PriorityConfiguration configuration = new PriorityConfiguration(); PriorityConfigurationCallbackImpl callback = new PriorityConfigurationCallbackImpl(); assertThat(callback.getPrioritySelection(), is(-1)); // Before callback is used @@ -98,14 +93,14 @@ public void testGetPriorityAssignsGlobalDefault() { } @Test - public void testIsPlaceholderTask() { + void testIsPlaceholderTask() { PriorityConfigurationPlaceholderTaskHelper helper = new PriorityConfigurationPlaceholderTaskHelper(); // Pipeline task is not a placeholder task assertFalse(helper.isPlaceholderTask(pipelineItemInQuietPeriod.getTask())); } @Test - public void testGetPriority() { + void testGetPriority() { // Could not find an easy way to generate a placeholder task // Use a mock object for better test coverage ExecutorStepExecution.PlaceholderTask task = mock(ExecutorStepExecution.PlaceholderTask.class); @@ -123,7 +118,7 @@ public void testGetPriority() { } @Test - public void testGetPriorityNonJobTask() { + void testGetPriorityNonJobTask() { // Could not find an easy way to generate a placeholder task // Use a mock object for better test coverage ExecutorStepExecution.PlaceholderTask task = mock(ExecutorStepExecution.PlaceholderTask.class); @@ -148,7 +143,7 @@ public void testGetPriorityNonJobTask() { } @Test - public void testIsPlaceholderTaskUsed() { + void testIsPlaceholderTaskUsed() { assertTrue(PriorityConfigurationPlaceholderTaskHelper.isPlaceholderTaskUsed()); } diff --git a/src/test/java/jenkins/advancedqueue/jobinclusion/strategy/FolderBasedJobInclusionStrategyTest.java b/src/test/java/jenkins/advancedqueue/jobinclusion/strategy/FolderBasedJobInclusionStrategyTest.java index b4c9458b..b4ce5e58 100644 --- a/src/test/java/jenkins/advancedqueue/jobinclusion/strategy/FolderBasedJobInclusionStrategyTest.java +++ b/src/test/java/jenkins/advancedqueue/jobinclusion/strategy/FolderBasedJobInclusionStrategyTest.java @@ -3,10 +3,7 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import com.cloudbees.hudson.plugins.folder.Folder; import hudson.model.FreeStyleProject; @@ -14,29 +11,26 @@ import java.util.ArrayList; import java.util.List; import jenkins.advancedqueue.DecisionLogger; -import org.junit.After; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.ClassRule; -import org.junit.Test; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; -public class FolderBasedJobInclusionStrategyTest { +@WithJenkins +class FolderBasedJobInclusionStrategyTest { - @ClassRule - public static JenkinsRule j = new JenkinsRule(); + private static JenkinsRule j; private static FolderBasedJobInclusionStrategy strategy; private static DecisionLogger decisionLogger; private static List loggedMessages; - @BeforeClass - public static void createStrategy() throws Exception { + @BeforeAll + static void beforeAll(JenkinsRule rule) throws Exception { + j = rule; strategy = new FolderBasedJobInclusionStrategy("testFolder"); - } - - @BeforeClass - public static void createDecisionLogger() throws Exception { decisionLogger = new DecisionLogger() { @Override public DecisionLogger addDecisionLog(int indent, String log) { @@ -46,57 +40,57 @@ public DecisionLogger addDecisionLog(int indent, String log) { }; } - @Before - public void clearLoggedMessages() throws Exception { + @BeforeEach + void beforeEach() throws Exception { loggedMessages = new ArrayList<>(); } - @After - public void checkLoggedMessages() throws Exception { + @AfterEach + void afterEach() throws Exception { assertThat(loggedMessages, is(empty())); } @Test - public void getDescriptor() { + void getDescriptor() { assertNotNull(strategy.getDescriptor()); - assertTrue( - strategy.getDescriptor() - instanceof FolderBasedJobInclusionStrategy.FolderBasedJobInclusionStrategyDescriptor); + assertInstanceOf( + FolderBasedJobInclusionStrategy.FolderBasedJobInclusionStrategyDescriptor.class, + strategy.getDescriptor()); } @Test - public void all() { + void all() { assertFalse(FolderBasedJobInclusionStrategy.all().isEmpty()); } @Test - public void getFolderName() { + void getFolderName() { assertEquals("testFolder", strategy.getFolderName()); } @Test - public void contains() throws Exception { + void contains() throws Exception { FreeStyleProject project = this.j.createFreeStyleProject("testFolder_jobName"); assertTrue(strategy.contains(decisionLogger, project)); } @Test - public void containsReturnsFalseForJobNotInFolder() throws Exception { + void containsReturnsFalseForJobNotInFolder() throws Exception { FreeStyleProject project = j.createFreeStyleProject("otherFolder_jobName"); assertFalse(strategy.contains(decisionLogger, project)); } @Test - public void containsReturnsTrueForJobInSubFolder() throws Exception { + void containsReturnsTrueForJobInSubFolder() throws Exception { FreeStyleProject project = j.createFreeStyleProject("testFolder_subFolder_jobName"); assertTrue(strategy.contains(decisionLogger, project)); } @Test - public void getListFolderItemsReturnsNonNullListBoxModel() { + void getListFolderItemsReturnsNonNullListBoxModel() { FolderBasedJobInclusionStrategy.FolderBasedJobInclusionStrategyDescriptor descriptor = new FolderBasedJobInclusionStrategy.FolderBasedJobInclusionStrategyDescriptor(); ListBoxModel items = descriptor.getListFolderItems(); @@ -104,7 +98,7 @@ public void getListFolderItemsReturnsNonNullListBoxModel() { } @Test - public void getListFolderItemsReturnsCorrectFolderNames() throws Exception { + void getListFolderItemsReturnsCorrectFolderNames() throws Exception { j.jenkins.createProject(Folder.class, "folder1"); j.jenkins.createProject(Folder.class, "folder2"); diff --git a/src/test/java/jenkins/advancedqueue/jobinclusion/strategy/FolderPropertyLoaderTest.java b/src/test/java/jenkins/advancedqueue/jobinclusion/strategy/FolderPropertyLoaderTest.java index f4a29c80..4d7e5ee2 100644 --- a/src/test/java/jenkins/advancedqueue/jobinclusion/strategy/FolderPropertyLoaderTest.java +++ b/src/test/java/jenkins/advancedqueue/jobinclusion/strategy/FolderPropertyLoaderTest.java @@ -2,38 +2,39 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.hasItem; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; import com.cloudbees.hudson.plugins.folder.Folder; import hudson.model.FreeStyleProject; import java.util.ArrayList; import java.util.List; import jenkins.advancedqueue.DecisionLogger; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.ClassRule; -import org.junit.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; -public class FolderPropertyLoaderTest { +@WithJenkins +class FolderPropertyLoaderTest { - @ClassRule - public static JenkinsRule j = new JenkinsRule(); + private static JenkinsRule j; private static Folder folder; private static FreeStyleProject project; private DecisionLogger decisionLogger; private List loggedMessages; - @BeforeClass - public static void createJob() throws Exception { + @BeforeAll + static void beforeAll(JenkinsRule rule) throws Exception { + j = rule; folder = j.createProject(com.cloudbees.hudson.plugins.folder.Folder.class, "testFolder"); project = folder.createProject(FreeStyleProject.class, "testProject"); } - @Before - public void createDecisionLogger() throws Exception { + @BeforeEach + void beforeEach() throws Exception { loggedMessages = new ArrayList<>(); decisionLogger = new DecisionLogger() { @Override @@ -45,7 +46,7 @@ public DecisionLogger addDecisionLog(int indent, String log) { } @Test - public void getJobGroupName_returnsGroupName_whenJobGroupIsEnabled() throws Exception { + void getJobGroupName_returnsGroupName_whenJobGroupIsEnabled() throws Exception { JobInclusionFolderProperty property = new JobInclusionFolderProperty(true, "TestGroup"); folder.getProperties().add(property); @@ -56,7 +57,7 @@ public void getJobGroupName_returnsGroupName_whenJobGroupIsEnabled() throws Exce } @Test - public void getJobGroupName_returnsNull_whenNoJobGroupProperty() throws Exception { + void getJobGroupName_returnsNull_whenNoJobGroupProperty() throws Exception { String result = FolderPropertyLoader.getJobGroupName(decisionLogger, project); assertNull(result); @@ -64,7 +65,7 @@ public void getJobGroupName_returnsNull_whenNoJobGroupProperty() throws Exceptio } @Test - public void getJobGroupName_returnsNull_whenJobGroupIsDisabled() throws Exception { + void getJobGroupName_returnsNull_whenJobGroupIsDisabled() throws Exception { JobInclusionFolderProperty property = new JobInclusionFolderProperty(false, "TestGroup"); folder.getProperties().add(property); @@ -75,7 +76,7 @@ public void getJobGroupName_returnsNull_whenJobGroupIsDisabled() throws Exceptio } @Test - public void getJobGroupName_returnsNull_whenParentIsNotFolder() throws Exception { + void getJobGroupName_returnsNull_whenParentIsNotFolder() throws Exception { FreeStyleProject standaloneProject = j.createFreeStyleProject("standaloneProject"); String result = FolderPropertyLoader.getJobGroupName(decisionLogger, standaloneProject); diff --git a/src/test/java/jenkins/advancedqueue/jobinclusion/strategy/JobInclusionFolderPropertyTest.java b/src/test/java/jenkins/advancedqueue/jobinclusion/strategy/JobInclusionFolderPropertyTest.java index bd020769..ac64b0b3 100644 --- a/src/test/java/jenkins/advancedqueue/jobinclusion/strategy/JobInclusionFolderPropertyTest.java +++ b/src/test/java/jenkins/advancedqueue/jobinclusion/strategy/JobInclusionFolderPropertyTest.java @@ -3,27 +3,26 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import hudson.model.FreeStyleProject; import hudson.model.Run; -import org.junit.BeforeClass; -import org.junit.ClassRule; -import org.junit.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; -public class JobInclusionFolderPropertyTest { +@WithJenkins +class JobInclusionFolderPropertyTest { - @ClassRule - public static JenkinsRule j = new JenkinsRule(); + private static JenkinsRule j; private static FreeStyleProject project; private static JobInclusionFolderProperty property; - @BeforeClass - public static void createProject() throws Exception { + @BeforeAll + static void beforeAll(JenkinsRule rule) throws Exception { + j = rule; project = j.createFreeStyleProject(); Run r = project.scheduleBuild2(0).get(); // Schedule a build to ensure the queue item is created j.assertBuildStatusSuccess(r); @@ -31,22 +30,22 @@ public static void createProject() throws Exception { } @Test - public void testGetJobGroupName() { + void testGetJobGroupName() { assertEquals("testGroup", property.getJobGroupName()); } @Test - public void testIsUseJobGroup() { + void testIsUseJobGroup() { assertTrue(property.isUseJobGroup()); } @Test - public void testGetDescriptor() { + void testGetDescriptor() { assertThat(property.getDescriptor().getId(), is(JobInclusionFolderProperty.class.getName())); } @Test - public void testDescriptorImpl() { + void testDescriptorImpl() { JobInclusionFolderProperty.DescriptorImpl descriptor = new JobInclusionFolderProperty.DescriptorImpl(); assertThat(descriptor.getDisplayName(), is("XXX")); assertThat(descriptor.getJobGroups(), is(empty())); @@ -54,7 +53,7 @@ public void testDescriptorImpl() { } @Test - public void testAllJobsJobInclusionStrategy() { + void testAllJobsJobInclusionStrategy() { AllJobsJobInclusionStrategy strategy = new AllJobsJobInclusionStrategy(); assertTrue(strategy.contains(null, project)); } diff --git a/src/test/java/jenkins/advancedqueue/jobinclusion/strategy/JobInclusionJobPropertyTest.java b/src/test/java/jenkins/advancedqueue/jobinclusion/strategy/JobInclusionJobPropertyTest.java index f81c10d5..dac89c81 100644 --- a/src/test/java/jenkins/advancedqueue/jobinclusion/strategy/JobInclusionJobPropertyTest.java +++ b/src/test/java/jenkins/advancedqueue/jobinclusion/strategy/JobInclusionJobPropertyTest.java @@ -1,51 +1,50 @@ package jenkins.advancedqueue.jobinclusion.strategy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import hudson.model.FreeStyleProject; -import org.junit.After; -import org.junit.Before; -import org.junit.ClassRule; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TestName; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; -public class JobInclusionJobPropertyTest { +@WithJenkins +class JobInclusionJobPropertyTest { - @ClassRule - public static JenkinsRule j = new JenkinsRule(); - - @Rule - public TestName testName = new TestName(); + private static JenkinsRule j; private JobInclusionJobProperty jobProperty; private FreeStyleProject jobProject; private JobInclusionJobProperty.DescriptorImpl descriptor; - @Before - public void setUp() throws Exception { + @BeforeAll + static void beforeAll(JenkinsRule rule) throws Exception { + j = rule; + } + + @BeforeEach + void beforeEach(TestInfo info) throws Exception { jobProperty = new JobInclusionJobProperty(true, "TestJobGroup"); descriptor = jobProperty.getDescriptor(); - jobProject = j.createFreeStyleProject("testFolder_" + testName.getMethodName()); + jobProject = j.createFreeStyleProject( + "testFolder_" + info.getTestMethod().orElseThrow().getName()); } - @After - public void deleteProject() throws Exception { + @AfterEach + void afterEach() throws Exception { jobProject.delete(); } @Test - public void getJobGroupNameTest() { + void getJobGroupNameTest() { assertEquals("TestJobGroup", jobProperty.getJobGroupName()); } @Test - public void getJobGroupNameReturnsNullWhenNotSetAndFalse() { + void getJobGroupNameReturnsNullWhenNotSetAndFalse() { // Create a JobInclusionJobProperty with useJobGroup set to false and jobGroupName set to null jobProperty = new JobInclusionJobProperty(false, null); assertFalse(jobProperty.isUseJobGroup()); @@ -53,7 +52,7 @@ public void getJobGroupNameReturnsNullWhenNotSetAndFalse() { } @Test - public void getJobGroupNameReturnsNullWhenNotSetAndTrue() { + void getJobGroupNameReturnsNullWhenNotSetAndTrue() { // Create a JobInclusionJobProperty with useJobGroup set to true and jobGroupName set to null jobProperty = new JobInclusionJobProperty(true, null); assertTrue(jobProperty.isUseJobGroup()); @@ -61,7 +60,7 @@ public void getJobGroupNameReturnsNullWhenNotSetAndTrue() { } @Test - public void isUseJobGroupReturnsCorrectValue() { + void isUseJobGroupReturnsCorrectValue() { // Create a JobInclusionJobProperty with useJobGroup set to true jobProperty = new JobInclusionJobProperty(true, "groupName"); assertTrue(jobProperty.isUseJobGroup()); @@ -79,7 +78,7 @@ public void isUseJobGroupReturnsCorrectValue() { } @Test - public void isUseJobGroupTest() { + void isUseJobGroupTest() { assertTrue(jobProperty.isUseJobGroup()); // Create a JobInclusionJobProperty with useJobGroup set to false @@ -94,7 +93,7 @@ public void isUseJobGroupTest() { } @Test - public void getJobGroupNameReturnsNullWhenJobGroupNameNotSet() { + void getJobGroupNameReturnsNullWhenJobGroupNameNotSet() { // Create a JobInclusionJobProperty with useJobGroup set to true and jobGroupName set to null JobInclusionJobProperty jobProperty = new JobInclusionJobProperty(true, null); assertTrue(jobProperty.isUseJobGroup()); @@ -112,17 +111,17 @@ public void getJobGroupNameReturnsNullWhenJobGroupNameNotSet() { } @Test - public void getDisplayNameTest() { + void getDisplayNameTest() { assertEquals("XXX", descriptor.getDisplayName()); } @Test - public void getJobGroupsTest() { + void getJobGroupsTest() { assertNotNull(descriptor.getJobGroups()); } @Test - public void isUsedTest() { + void isUsedTest() { assertFalse(descriptor.isUsed()); } } diff --git a/src/test/java/jenkins/advancedqueue/jobinclusion/strategy/PropertyBasedJobInclusionStrategyTest.java b/src/test/java/jenkins/advancedqueue/jobinclusion/strategy/PropertyBasedJobInclusionStrategyTest.java index 1af0f6ac..9701e507 100644 --- a/src/test/java/jenkins/advancedqueue/jobinclusion/strategy/PropertyBasedJobInclusionStrategyTest.java +++ b/src/test/java/jenkins/advancedqueue/jobinclusion/strategy/PropertyBasedJobInclusionStrategyTest.java @@ -4,10 +4,7 @@ import static org.hamcrest.Matchers.empty; import static org.hamcrest.Matchers.hasItems; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import hudson.model.FreeStyleProject; import hudson.util.ListBoxModel; @@ -17,21 +14,18 @@ import jenkins.advancedqueue.JobGroup; import jenkins.advancedqueue.PriorityConfiguration; import jenkins.advancedqueue.jobinclusion.strategy.PropertyBasedJobInclusionStrategy.PropertyBasedJobInclusionStrategyDescriptor; -import org.junit.After; -import org.junit.Before; -import org.junit.ClassRule; -import org.junit.Rule; -import org.junit.Test; -import org.junit.rules.TestName; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; -public class PropertyBasedJobInclusionStrategyTest { +@WithJenkins +class PropertyBasedJobInclusionStrategyTest { - @ClassRule - public static JenkinsRule j = new JenkinsRule(); - - @Rule - public TestName testName = new TestName(); + private static JenkinsRule j; private FreeStyleProject project; private String strategyName; @@ -39,20 +33,20 @@ public class PropertyBasedJobInclusionStrategyTest { private DecisionLogger decisionLogger; private List loggedMessages; - @Before - public void createStrategy() throws Exception { - strategyName = "testGroup-" + testName.getMethodName(); - strategy = new PropertyBasedJobInclusionStrategy(strategyName); + @BeforeAll + static void beforeAll(JenkinsRule rule) throws Exception { + j = rule; } - @Before - public void createProject() throws Exception { + @BeforeEach + void beforeEach(TestInfo info) throws Exception { + strategyName = "testGroup-" + info.getTestMethod().orElseThrow().getName(); + strategy = new PropertyBasedJobInclusionStrategy(strategyName); + // Name each freestyle project with the name of the test creating it - project = j.createFreeStyleProject("freestyle-" + testName.getMethodName()); - } + project = j.createFreeStyleProject( + "freestyle-" + info.getTestMethod().orElseThrow().getName()); - @Before - public void createDecisionLogger() throws Exception { loggedMessages = new ArrayList<>(); decisionLogger = new DecisionLogger() { @Override @@ -63,40 +57,40 @@ public DecisionLogger addDecisionLog(int indent, String log) { }; } - @After - public void deleteProject() throws Exception { + @AfterEach + void afterEach() throws Exception { project.delete(); } @Test - public void all() { + void all() { List jobGroups = PriorityConfiguration.get().getJobGroups(); assertNotNull(jobGroups); assertThat(loggedMessages, is(empty())); } @Test - public void getName() { + void getName() { assertEquals(strategyName, strategy.getName()); assertThat(loggedMessages, is(empty())); } @Test - public void contains() { + void contains() { boolean result = strategy.contains(decisionLogger, project); assertFalse(result); // Assuming the project does not have the required property assertThat(loggedMessages, hasItems("No match ...")); } @Test - public void getPropertyBasesJobGroups() { + void getPropertyBasesJobGroups() { ListBoxModel jobGroups = PropertyBasedJobInclusionStrategy.getPropertyBasesJobGroups(); assertNotNull(jobGroups); assertThat(loggedMessages, is(empty())); } @Test - public void getDisplayNameContainsJobs() { + void getDisplayNameContainsJobs() { PropertyBasedJobInclusionStrategy.PropertyBasedJobInclusionStrategyDescriptor descriptor = strategy.getThisDescriptor(); assertTrue(descriptor.getDisplayName().contains("Jobs")); @@ -104,34 +98,34 @@ public void getDisplayNameContainsJobs() { } @Test - public void allReturnsNonNullJobGroups() { + void allReturnsNonNullJobGroups() { List jobGroups = PriorityConfiguration.get().getJobGroups(); assertNotNull(jobGroups); assertThat(loggedMessages, is(empty())); } @Test - public void getNameReturnsCorrectName() { + void getNameReturnsCorrectName() { assertEquals(strategyName, strategy.getName()); assertThat(loggedMessages, is(empty())); } @Test - public void containsReturnsFalseForProjectWithoutProperty() { + void containsReturnsFalseForProjectWithoutProperty() { boolean result = strategy.contains(decisionLogger, project); assertFalse(result); assertThat(loggedMessages, hasItems("No match ...")); } @Test - public void getPropertyBasesJobGroupsReturnsNonNullListBoxModel() { + void getPropertyBasesJobGroupsReturnsNonNullListBoxModel() { ListBoxModel jobGroups = PropertyBasedJobInclusionStrategy.getPropertyBasesJobGroups(); assertNotNull(jobGroups); assertThat(loggedMessages, is(empty())); } @Test - public void containsReturnsTrueForProjectWithMatchingJobGroup() throws Exception { + void containsReturnsTrueForProjectWithMatchingJobGroup() throws Exception { project.addProperty(new JobInclusionJobProperty(true, strategyName)); boolean result = strategy.contains(decisionLogger, project); @@ -144,7 +138,7 @@ public void containsReturnsTrueForProjectWithMatchingJobGroup() throws Exception } @Test - public void containsReturnsFalseForProjectWithNonMatchingJobGroup() throws Exception { + void containsReturnsFalseForProjectWithNonMatchingJobGroup() throws Exception { project.addProperty(new JobInclusionJobProperty(true, "nonMatchingGroup")); boolean result = strategy.contains(decisionLogger, project); @@ -157,7 +151,7 @@ public void containsReturnsFalseForProjectWithNonMatchingJobGroup() throws Excep } @Test - public void containsReturnsFalseWhenCloudBeesFoldersPluginNotEnabled() throws Exception { + void containsReturnsFalseWhenCloudBeesFoldersPluginNotEnabled() throws Exception { // Simulate CloudBees Folders plugin not enabled PropertyBasedJobInclusionStrategyDescriptor descriptor = strategy.getThisDescriptor(); descriptor.cloudbeesFolders = false; @@ -168,7 +162,7 @@ public void containsReturnsFalseWhenCloudBeesFoldersPluginNotEnabled() throws Ex } @Test - public void getPropertyBasesJobGroupsReturnsEmptyListBoxModelWhenNoJobGroups() { + void getPropertyBasesJobGroupsReturnsEmptyListBoxModelWhenNoJobGroups() { // Simulate no job groups PriorityConfiguration.get().getJobGroups().clear(); @@ -179,7 +173,7 @@ public void getPropertyBasesJobGroupsReturnsEmptyListBoxModelWhenNoJobGroups() { } @Test - public void containsReturnsTrueForProjectWithMatchingPropertyAndCloudBeesFoldersEnabled() throws Exception { + void containsReturnsTrueForProjectWithMatchingPropertyAndCloudBeesFoldersEnabled() throws Exception { project.addProperty(new JobInclusionJobProperty(true, strategyName)); PropertyBasedJobInclusionStrategyDescriptor descriptor = strategy.getThisDescriptor(); descriptor.cloudbeesFolders = true; @@ -194,7 +188,7 @@ public void containsReturnsTrueForProjectWithMatchingPropertyAndCloudBeesFolders } @Test - public void containsReturnsFalseForProjectWithNonMatchingPropertyAndCloudBeesFoldersEnabled() throws Exception { + void containsReturnsFalseForProjectWithNonMatchingPropertyAndCloudBeesFoldersEnabled() throws Exception { project.addProperty(new JobInclusionJobProperty(true, "nonMatchingGroup")); PropertyBasedJobInclusionStrategyDescriptor descriptor = strategy.getThisDescriptor(); descriptor.cloudbeesFolders = true; @@ -209,7 +203,7 @@ public void containsReturnsFalseForProjectWithNonMatchingPropertyAndCloudBeesFol } @Test - public void containsReturnsFalseForProjectWithoutPropertyAndCloudBeesFoldersEnabled() throws Exception { + void containsReturnsFalseForProjectWithoutPropertyAndCloudBeesFoldersEnabled() throws Exception { PropertyBasedJobInclusionStrategyDescriptor descriptor = strategy.getThisDescriptor(); descriptor.cloudbeesFolders = true; @@ -219,7 +213,7 @@ public void containsReturnsFalseForProjectWithoutPropertyAndCloudBeesFoldersEnab } @Test - public void getPropertyBasesJobGroupsReturnsNonNullListBoxModelWhenMultipleJobGroups() { + void getPropertyBasesJobGroupsReturnsNonNullListBoxModelWhenMultipleJobGroups() { PriorityConfiguration.get().getJobGroups().add(createJobGroup("group1", 1)); PriorityConfiguration.get().getJobGroups().add(createJobGroup("group2", 2)); diff --git a/src/test/java/jenkins/advancedqueue/jobrestrictions/PrioritySorterRestrictionTest.java b/src/test/java/jenkins/advancedqueue/jobrestrictions/PrioritySorterRestrictionTest.java index f8154e71..ffdc02d8 100644 --- a/src/test/java/jenkins/advancedqueue/jobrestrictions/PrioritySorterRestrictionTest.java +++ b/src/test/java/jenkins/advancedqueue/jobrestrictions/PrioritySorterRestrictionTest.java @@ -23,7 +23,7 @@ class PrioritySorterRestrictionTest { private static final int UPPER_PRIORITY = 1; @BeforeEach - void setUp() { + void beforeEach() { this.restriction = new PrioritySorterRestriction(LOWER_PRIORITY, UPPER_PRIORITY); this.mockedBuildableItem = mock(BuildableItem.class); when(mockedBuildableItem.getId()).thenReturn(MOCKED_BUILDABLE_ITEM_ID); diff --git a/src/test/java/jenkins/advancedqueue/priority/strategy/BuildParameterStrategyTest.java b/src/test/java/jenkins/advancedqueue/priority/strategy/BuildParameterStrategyTest.java index 5e44055b..420c20b5 100644 --- a/src/test/java/jenkins/advancedqueue/priority/strategy/BuildParameterStrategyTest.java +++ b/src/test/java/jenkins/advancedqueue/priority/strategy/BuildParameterStrategyTest.java @@ -1,8 +1,6 @@ package jenkins.advancedqueue.priority.strategy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import hudson.model.FreeStyleProject; import hudson.model.ParametersAction; @@ -12,27 +10,28 @@ import java.util.Calendar; import java.util.Collections; import jenkins.advancedqueue.PrioritySorterConfiguration; -import org.junit.BeforeClass; -import org.junit.ClassRule; -import org.junit.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; -public class BuildParameterStrategyTest { +@WithJenkins +class BuildParameterStrategyTest { - @ClassRule - public static JenkinsRule j = new JenkinsRule(); + private static JenkinsRule j; private static FreeStyleProject project; private static BuildParameterStrategy strategy; - @BeforeClass - public static void setUp() throws IOException { + @BeforeAll + static void beforeAll(JenkinsRule rule) throws IOException { + j = rule; project = j.createFreeStyleProject(); strategy = new BuildParameterStrategy("priority"); } @Test - public void getPriority_returnsPriorityFromParameter() { + void getPriority_returnsPriorityFromParameter() { StringParameterValue param = new StringParameterValue("priority", "5"); ParametersAction action = new ParametersAction(param); Queue.Item item = new Queue.WaitingItem(Calendar.getInstance(), project, Collections.singletonList(action)); @@ -43,7 +42,7 @@ public void getPriority_returnsPriorityFromParameter() { } @Test - public void getPriority_returnsDefaultPriorityWhenParameterIsMissing() { + void getPriority_returnsDefaultPriorityWhenParameterIsMissing() { Queue.Item item = new Queue.WaitingItem(Calendar.getInstance(), project, Collections.emptyList()); int priority = strategy.getPriority(item); @@ -52,7 +51,7 @@ public void getPriority_returnsDefaultPriorityWhenParameterIsMissing() { } @Test - public void getPriority_returnsDefaultPriorityWhenParameterIsNotANumber() { + void getPriority_returnsDefaultPriorityWhenParameterIsNotANumber() { StringParameterValue param = new StringParameterValue("priority", "not-a-number"); ParametersAction action = new ParametersAction(param); Queue.Item item = new Queue.WaitingItem(Calendar.getInstance(), project, Collections.singletonList(action)); @@ -63,7 +62,7 @@ public void getPriority_returnsDefaultPriorityWhenParameterIsNotANumber() { } @Test - public void isApplicable_returnsTrueWhenParameterIsPresentAndValid() { + void isApplicable_returnsTrueWhenParameterIsPresentAndValid() { StringParameterValue param = new StringParameterValue("priority", "5"); ParametersAction action = new ParametersAction(param); Queue.Item item = new Queue.WaitingItem(Calendar.getInstance(), project, Collections.singletonList(action)); @@ -72,14 +71,14 @@ public void isApplicable_returnsTrueWhenParameterIsPresentAndValid() { } @Test - public void isApplicable_returnsFalseWhenParameterIsMissing() { + void isApplicable_returnsFalseWhenParameterIsMissing() { Queue.Item item = new Queue.WaitingItem(Calendar.getInstance(), project, Collections.emptyList()); assertFalse(strategy.isApplicable(item)); } @Test - public void isApplicable_returnsFalseWhenParameterIsNotANumber() { + void isApplicable_returnsFalseWhenParameterIsNotANumber() { StringParameterValue param = new StringParameterValue("priority", "not-a-number"); ParametersAction action = new ParametersAction(param); Queue.Item item = new Queue.WaitingItem(Calendar.getInstance(), project, Collections.singletonList(action)); diff --git a/src/test/java/jenkins/advancedqueue/priority/strategy/HealthStrategyTest.java b/src/test/java/jenkins/advancedqueue/priority/strategy/HealthStrategyTest.java index 4aa20050..076f20f2 100644 --- a/src/test/java/jenkins/advancedqueue/priority/strategy/HealthStrategyTest.java +++ b/src/test/java/jenkins/advancedqueue/priority/strategy/HealthStrategyTest.java @@ -1,7 +1,7 @@ package jenkins.advancedqueue.priority.strategy; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import static org.mockito.Mockito.withSettings; @@ -13,266 +13,62 @@ import hudson.util.RunList; import java.lang.reflect.Field; import java.util.Iterator; -import org.junit.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.MethodSource; -public class HealthStrategyTest { +class HealthStrategyTest { private BuildableItem mockedBuildableItem; private Job mockedJob; - @Test - public void assertHealthOver80SameSelectionConcludes() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "SAME", "HEALTH_OVER_80"); - setMockedJobHealthTo(85); - initializeJobRunList(); - assertTrue(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealthOver80SameSelectionFails() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "SAME", "HEALTH_OVER_80"); - setMockedJobHealthTo(75); - initializeJobRunList(); - assertFalse(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealthOver80BetterSelectionConcludes() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "BETTER", "HEALTH_OVER_80"); - setMockedJobHealthTo(85); - initializeJobRunList(); - assertTrue(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealthOver80BetterSelectionFails() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "BETTER", "HEALTH_OVER_80"); - setMockedJobHealthTo(75); - initializeJobRunList(); - assertFalse(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealth61To80SameSelectionConcludes() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "SAME", "HEALTH_61_TO_80"); - setMockedJobHealthTo(75); - initializeJobRunList(); - assertTrue(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealth61To80SameSelectionFailsWithGreater() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "SAME", "HEALTH_61_TO_80"); - setMockedJobHealthTo(81); - initializeJobRunList(); - assertFalse(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealth61To80SameSelectionFailsWithLower() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "SAME", "HEALTH_61_TO_80"); - setMockedJobHealthTo(60); - initializeJobRunList(); - assertFalse(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealth61To80BetterSelectionConcludes() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "BETTER", "HEALTH_61_TO_80"); - setMockedJobHealthTo(75); - initializeJobRunList(); - assertTrue(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealth61To80BetterSelectionFails() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "BETTER", "HEALTH_61_TO_80"); - setMockedJobHealthTo(60); - initializeJobRunList(); - assertFalse(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealth61To80WorseSelectionConcludes() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "WORSE", "HEALTH_61_TO_80"); - setMockedJobHealthTo(80); - initializeJobRunList(); - assertTrue(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealth61To80WorseSelectionFails() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "WORSE", "HEALTH_61_TO_80"); - setMockedJobHealthTo(81); - initializeJobRunList(); - assertFalse(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealth41to60SameSelectionConcludes() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "SAME", "HEALTH_41_TO_60"); - setMockedJobHealthTo(55); - initializeJobRunList(); - assertTrue(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealth41to60SameSelectionFailsWithGreater() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "SAME", "HEALTH_41_TO_60"); - setMockedJobHealthTo(61); - initializeJobRunList(); - assertFalse(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealth41to60SameSelectionFailsWithLower() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "SAME", "HEALTH_41_TO_60"); - setMockedJobHealthTo(40); - initializeJobRunList(); - assertFalse(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealth41to60BetterSelectionConcludes() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "BETTER", "HEALTH_41_TO_60"); - setMockedJobHealthTo(50); - initializeJobRunList(); - assertTrue(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealth41to60BetterSelectionFails() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "BETTER", "HEALTH_41_TO_60"); - setMockedJobHealthTo(40); - initializeJobRunList(); - assertFalse(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealth41to60WorseSelectionConcludes() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "WORSE", "HEALTH_41_TO_60"); - setMockedJobHealthTo(60); - initializeJobRunList(); - assertTrue(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealth41to60WorseSelectionFails() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "WORSE", "HEALTH_41_TO_60"); - setMockedJobHealthTo(61); - initializeJobRunList(); - assertFalse(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealth21to40SameSelectionConcludes() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "SAME", "HEALTH_21_TO_40"); - setMockedJobHealthTo(35); - initializeJobRunList(); - assertTrue(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealth21to40SameSelectionFailsWithGreater() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "SAME", "HEALTH_21_TO_40"); - setMockedJobHealthTo(41); - initializeJobRunList(); - assertFalse(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealth21to40SameSelectionFailsWithLower() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "SAME", "HEALTH_21_TO_40"); - setMockedJobHealthTo(20); - initializeJobRunList(); - assertFalse(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealth21to40BetterSelectionConcludes() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "BETTER", "HEALTH_21_TO_40"); - setMockedJobHealthTo(30); - initializeJobRunList(); - assertTrue(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealth21to40BetterSelectionFails() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "BETTER", "HEALTH_21_TO_40"); - setMockedJobHealthTo(20); - initializeJobRunList(); - assertFalse(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealth21to40WorseSelectionConcludes() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "WORSE", "HEALTH_21_TO_40"); - setMockedJobHealthTo(40); - initializeJobRunList(); - assertTrue(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealth21to40WorseSelectionFails() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "WORSE", "HEALTH_21_TO_40"); - setMockedJobHealthTo(41); - initializeJobRunList(); - assertFalse(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealth0to20SameSelectionConcludes() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "SAME", "HEALTH_0_TO_20"); - setMockedJobHealthTo(15); - initializeJobRunList(); - assertTrue(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealth0to20SameSelectionFailsWithGreater() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "SAME", "HEALTH_0_TO_20"); - setMockedJobHealthTo(21); - initializeJobRunList(); - assertFalse(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealth0to20SameSelectionFailsWithLower() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "SAME", "HEALTH_0_TO_20"); - setMockedJobHealthTo(-1); - initializeJobRunList(); - assertFalse(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealth0to20BetterSelectionConcludes() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "BETTER", "HEALTH_0_TO_20"); - setMockedJobHealthTo(15); - initializeJobRunList(); - assertTrue(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealth0to20BetterSelectionFails() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "BETTER", "HEALTH_0_TO_20"); - setMockedJobHealthTo(-1); - initializeJobRunList(); - assertFalse(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealth0to20WorseSelectionConcludes() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "WORSE", "HEALTH_0_TO_20"); - setMockedJobHealthTo(20); - initializeJobRunList(); - assertTrue(strategy.isApplicable(this.mockedBuildableItem)); - } - - @Test - public void assertHealth0to20WorseSelectionFails() throws NoSuchFieldException, IllegalAccessException { - HealthStrategy strategy = new HealthStrategy(0, "WORSE", "HEALTH_0_TO_20"); - setMockedJobHealthTo(21); - initializeJobRunList(); - assertFalse(strategy.isApplicable(this.mockedBuildableItem)); + static Object[][] data() { + return new Object[][] { + {"SAME", "HEALTH_OVER_80", 85, true}, + {"SAME", "HEALTH_OVER_80", 75, false}, + {"BETTER", "HEALTH_OVER_80", 85, true}, + {"BETTER", "HEALTH_OVER_80", 75, false}, + {"SAME", "HEALTH_61_TO_80", 75, true}, + {"SAME", "HEALTH_61_TO_80", 81, false}, + {"SAME", "HEALTH_61_TO_80", 60, false}, + {"BETTER", "HEALTH_61_TO_80", 75, true}, + {"BETTER", "HEALTH_61_TO_80", 60, false}, + {"WORSE", "HEALTH_61_TO_80", 80, true}, + {"WORSE", "HEALTH_61_TO_80", 81, false}, + {"SAME", "HEALTH_41_TO_60", 55, true}, + {"SAME", "HEALTH_41_TO_60", 61, false}, + {"SAME", "HEALTH_41_TO_60", 40, false}, + {"BETTER", "HEALTH_41_TO_60", 50, true}, + {"BETTER", "HEALTH_41_TO_60", 40, false}, + {"WORSE", "HEALTH_41_TO_60", 60, true}, + {"WORSE", "HEALTH_41_TO_60", 61, false}, + {"SAME", "HEALTH_21_TO_40", 35, true}, + {"SAME", "HEALTH_21_TO_40", 41, false}, + {"SAME", "HEALTH_21_TO_40", 20, false}, + {"BETTER", "HEALTH_21_TO_40", 30, true}, + {"BETTER", "HEALTH_21_TO_40", 20, false}, + {"WORSE", "HEALTH_21_TO_40", 40, true}, + {"WORSE", "HEALTH_21_TO_40", 41, false}, + {"SAME", "HEALTH_0_TO_20", 15, true}, + {"SAME", "HEALTH_0_TO_20", 21, false}, + {"SAME", "HEALTH_0_TO_20", -1, false}, + {"BETTER", "HEALTH_0_TO_20", 15, true}, + {"BETTER", "HEALTH_0_TO_20", -1, false}, + {"WORSE", "HEALTH_0_TO_20", 20, true}, + {"WORSE", "HEALTH_0_TO_20", 21, false} + }; + } + + @ParameterizedTest(name = "{0} - {1} - actual: {2} - concludes: {3}") + @MethodSource("data") + void assertHealth(String selection, String health, int mockedHealth, boolean expected) throws Exception { + HealthStrategy strategy = new HealthStrategy(0, selection, health); + setMockedJobHealthTo(mockedHealth); + initializeJobRunList(); + + if (expected) { + assertTrue(strategy.isApplicable(this.mockedBuildableItem)); + } else { + assertFalse(strategy.isApplicable(this.mockedBuildableItem)); + } } private void setMockedJobHealthTo(int health) throws NoSuchFieldException, IllegalAccessException { diff --git a/src/test/java/jenkins/advancedqueue/priority/strategy/JobPropertyStrategyTest.java b/src/test/java/jenkins/advancedqueue/priority/strategy/JobPropertyStrategyTest.java index 6d90ebf6..eb5c8762 100644 --- a/src/test/java/jenkins/advancedqueue/priority/strategy/JobPropertyStrategyTest.java +++ b/src/test/java/jenkins/advancedqueue/priority/strategy/JobPropertyStrategyTest.java @@ -2,22 +2,22 @@ import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; import hudson.model.FreeStyleProject; import hudson.model.Queue; import jenkins.advancedqueue.PrioritySorterConfiguration; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.ClassRule; -import org.junit.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; -public class JobPropertyStrategyTest { +@WithJenkins +class JobPropertyStrategyTest { - @ClassRule - public static JenkinsRule j = new JenkinsRule(); + private static JenkinsRule j; private static FreeStyleProject project; private static FreeStyleProject projectWithProperty; @@ -28,68 +28,59 @@ public class JobPropertyStrategyTest { private JobPropertyStrategy strategy; - @BeforeClass - public static void startProject() throws Exception { + @BeforeAll + static void beforeAll(JenkinsRule rule) throws Exception { + j = rule; + + defaultPriority = PrioritySorterConfiguration.get().getStrategy().getDefaultPriority(); + jobPriority = defaultPriority - 1; + project = j.createFreeStyleProject("no-property"); // Schedule initial delay so job is queued but does not run project.scheduleBuild2(600); - } - @BeforeClass - public static void startProjectWithProperty() throws Exception { - boolean useJobPriority = true; - PriorityJobProperty property = new PriorityJobProperty(useJobPriority, jobPriority); + PriorityJobProperty withProperty = new PriorityJobProperty(true, jobPriority); projectWithProperty = j.createFreeStyleProject("with-property"); - projectWithProperty.addProperty(property); + projectWithProperty.addProperty(withProperty); // Schedule initial delay so job is queued but does not run projectWithProperty.scheduleBuild2(600); - } - @BeforeClass - public static void startProjectWithUnusedProperty() throws Exception { - boolean useJobPriority = false; - PriorityJobProperty property = new PriorityJobProperty(useJobPriority, jobPriority); + PriorityJobProperty withoutProperty = new PriorityJobProperty(false, jobPriority); projectWithUnusedProperty = j.createFreeStyleProject("with-unused-property"); - projectWithUnusedProperty.addProperty(property); + projectWithUnusedProperty.addProperty(withoutProperty); // Schedule initial delay so job is queued but does not run projectWithUnusedProperty.scheduleBuild2(600); } - @BeforeClass - public static void setPriorities() { - defaultPriority = PrioritySorterConfiguration.get().getStrategy().getDefaultPriority(); - jobPriority = defaultPriority - 1; - } - - @Before - public void createStrategy() { + @BeforeEach + void beforeEach() { strategy = new JobPropertyStrategy(); } @Test - public void isApplicableTestWhengetPriorityInternalReturnsNull() { + void isApplicableTestWhengetPriorityInternalReturnsNull() { Queue.Item nullItem = null; assertFalse(strategy.isApplicable(nullItem)); assertFalse(strategy.isApplicable(null)); } @Test - public void isApplicableWhenFreeStyleProject() { + void isApplicableWhenFreeStyleProject() { assertFalse(strategy.isApplicable(project.getQueueItem())); } @Test - public void isApplicableWhenFreeStyleProjectWithPriorityProperty() { + void isApplicableWhenFreeStyleProjectWithPriorityProperty() { assertTrue(strategy.isApplicable(projectWithProperty.getQueueItem())); } @Test - public void isApplicableWhenFreeStyleProjectWithUnusedPriorityProperty() { + void isApplicableWhenFreeStyleProjectWithUnusedPriorityProperty() { assertFalse(strategy.isApplicable(projectWithUnusedProperty.getQueueItem())); } @Test - public void getPriorityTestWhengetPriorityInternalReturnsNull() { + void getPriorityTestWhengetPriorityInternalReturnsNull() { Queue.Item nullItem = null; // priority is 3 when item is null assertThat(strategy.getPriority(nullItem), is(defaultPriority)); @@ -97,17 +88,17 @@ public void getPriorityTestWhengetPriorityInternalReturnsNull() { } @Test - public void getPriorityTestFreeStyleProject() { + void getPriorityTestFreeStyleProject() { assertThat(strategy.getPriority(project.getQueueItem()), is(defaultPriority)); } @Test - public void getPriorityTestFreeStyleProjectWithPriorityProperty() { + void getPriorityTestFreeStyleProjectWithPriorityProperty() { assertThat(strategy.getPriority(projectWithProperty.getQueueItem()), is(jobPriority)); } @Test - public void getPriorityTestFreeStyleProjectWithUnusedPriorityProperty() { + void getPriorityTestFreeStyleProjectWithUnusedPriorityProperty() { assertThat(strategy.getPriority(projectWithUnusedProperty.getQueueItem()), is(defaultPriority)); } } diff --git a/src/test/java/jenkins/advancedqueue/priority/strategy/PriorityJobPropertyTest.java b/src/test/java/jenkins/advancedqueue/priority/strategy/PriorityJobPropertyTest.java index c6c97977..f7f96efd 100644 --- a/src/test/java/jenkins/advancedqueue/priority/strategy/PriorityJobPropertyTest.java +++ b/src/test/java/jenkins/advancedqueue/priority/strategy/PriorityJobPropertyTest.java @@ -1,10 +1,6 @@ package jenkins.advancedqueue.priority.strategy; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.mock; import hudson.model.Descriptor; @@ -18,58 +14,63 @@ import jenkins.advancedqueue.PrioritySorterConfiguration; import jenkins.advancedqueue.jobinclusion.strategy.ViewBasedJobInclusionStrategy; 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.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInfo; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import org.kohsuke.stapler.StaplerRequest2; -public class PriorityJobPropertyTest { +@WithJenkins +class PriorityJobPropertyTest { - @ClassRule - public static JenkinsRule j = new JenkinsRule(); + private static JenkinsRule j; - @Rule - public TestName testName = new TestName(); + private String testName; private static PriorityJobProperty property; private static PriorityJobProperty.DescriptorImpl descriptor; private static final int PRIORITY = 7; - @BeforeClass - public static void setUp() throws IOException { + @BeforeAll + static void beforeAll(JenkinsRule rule) throws Exception { + j = rule; // Initialize PrioritySorterConfiguration PrioritySorterConfiguration.get().load(); property = new PriorityJobProperty(true, PRIORITY); descriptor = property.getDescriptor(); } + @BeforeEach + void beforeEach(TestInfo info) throws Exception { + testName = info.getTestMethod().orElseThrow().getName(); + } + @Test - public void priorityJobProperty_returnsCorrectPriority() { + void priorityJobProperty_returnsCorrectPriority() { assertEquals(PRIORITY, property.getPriority()); } @Test - public void priorityJobProperty_returnsCorrectUseJobPriority() { + void priorityJobProperty_returnsCorrectUseJobPriority() { assertTrue(property.getUseJobPriority()); } @Test - public void priorityJobProperty_reconfigureNullOnEmpty() throws Descriptor.FormException { + void priorityJobProperty_reconfigureNullOnEmpty() throws Descriptor.FormException { StaplerRequest2 req = mock(StaplerRequest2.class); assertNull(property.reconfigure(req, new JSONObject())); } @Test - public void descriptorImpl_getDefaultReturnsDefaultPriority() { + void descriptorImpl_getDefaultReturnsDefaultPriority() { assertEquals(PrioritySorterConfiguration.get().getStrategy().getDefaultPriority(), descriptor.getDefault()); } @Test - public void descriptorImpl_getPrioritiesReturnsNonEmptyList() { + void descriptorImpl_getPrioritiesReturnsNonEmptyList() { assertFalse(descriptor.getPriorities().isEmpty()); } @@ -77,7 +78,7 @@ public void descriptorImpl_getPrioritiesReturnsNonEmptyList() { private JobGroup createJobGroup(String viewName) { JobGroup jobGroup = new JobGroup(); - jobGroup.setDescription("testGroup-" + testName.getMethodName()); + jobGroup.setDescription("testGroup-" + testName); jobGroup.setRunExclusive(random.nextBoolean()); jobGroup.setId(random.nextInt()); jobGroup.setJobGroupStrategy(new ViewBasedJobInclusionStrategy(viewName)); @@ -85,7 +86,7 @@ private JobGroup createJobGroup(String viewName) { } @Test - public void isUsedWhenViewExists() throws IOException { + void isUsedWhenViewExists() throws IOException { // Create a new FreeStyleProject FreeStyleProject project = j.createFreeStyleProject(); @@ -128,7 +129,7 @@ public void isUsedWhenViewExists() throws IOException { } @Test - public void isUsedWhenViewDoesNotExist() throws IOException { + void isUsedWhenViewDoesNotExist() throws IOException { FreeStyleProject project = j.createFreeStyleProject(); PriorityConfiguration configuration = PriorityConfiguration.get(); List jobGroups = configuration.getJobGroups(); diff --git a/src/test/java/jenkins/advancedqueue/sorter/PrioritySorterJobColumnTest.java b/src/test/java/jenkins/advancedqueue/sorter/PrioritySorterJobColumnTest.java index c4f9eff4..c7311769 100644 --- a/src/test/java/jenkins/advancedqueue/sorter/PrioritySorterJobColumnTest.java +++ b/src/test/java/jenkins/advancedqueue/sorter/PrioritySorterJobColumnTest.java @@ -1,30 +1,30 @@ package jenkins.advancedqueue.sorter; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import hudson.model.FreeStyleProject; import hudson.model.Queue; -import java.io.IOException; import java.util.ArrayList; import java.util.Calendar; import jenkins.advancedqueue.PrioritySorterJobColumn; -import org.junit.BeforeClass; -import org.junit.ClassRule; -import org.junit.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; -public class PrioritySorterJobColumnTest { +@WithJenkins +class PrioritySorterJobColumnTest { - @ClassRule - public static JenkinsRule j = new JenkinsRule(); + private static JenkinsRule j; private static FreeStyleProject project; private static ItemInfo itemInfo; private static PrioritySorterJobColumn column; - @BeforeClass - public static void setUp() throws IOException { + @BeforeAll + static void beforeAll(JenkinsRule rule) throws Exception { + j = rule; project = j.createFreeStyleProject("test-job"); Queue.WaitingItem waitingItem = new Queue.WaitingItem(Calendar.getInstance(), project, new ArrayList<>()); itemInfo = new ItemInfo(waitingItem); @@ -32,28 +32,28 @@ public static void setUp() throws IOException { } @Test - public void getPriorityReturnsPendingWhenItemInfoIsNull() throws Exception { + void getPriorityReturnsPendingWhenItemInfoIsNull() throws Exception { assertEquals("Pending", column.getPriority(project)); } @Test - public void getPriorityReturnsCorrectPriority() throws Exception { + void getPriorityReturnsCorrectPriority() throws Exception { QueueItemCache.get().addItem(itemInfo); assertEquals("0", column.getPriority(project)); } @Test - public void getPriorityReturnsPendingForNonExistentJob() throws Exception { + void getPriorityReturnsPendingForNonExistentJob() throws Exception { assertEquals("Pending", column.getPriority(project)); } @Test - public void descriptorImplDisplayNameIsCorrect() { + void descriptorImplDisplayNameIsCorrect() { assertEquals("Priority Value", column.getDescriptor().getDisplayName()); } @Test - public void descriptorImplShownByDefaultIsFalse() { + void descriptorImplShownByDefaultIsFalse() { PrioritySorterJobColumn.DescriptorImpl descriptor = (PrioritySorterJobColumn.DescriptorImpl) column.getDescriptor(); assertFalse(descriptor.shownByDefault()); diff --git a/src/test/java/jenkins/advancedqueue/sorter/SorterStrategyDescriptorTest.java b/src/test/java/jenkins/advancedqueue/sorter/SorterStrategyDescriptorTest.java index dcef981d..7cfdd3f3 100644 --- a/src/test/java/jenkins/advancedqueue/sorter/SorterStrategyDescriptorTest.java +++ b/src/test/java/jenkins/advancedqueue/sorter/SorterStrategyDescriptorTest.java @@ -1,21 +1,21 @@ package jenkins.advancedqueue.sorter; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import edu.umd.cs.findbugs.annotations.NonNull; import hudson.Extension; import hudson.model.FreeStyleBuild; import hudson.model.FreeStyleProject; import hudson.model.Queue; -import org.junit.BeforeClass; -import org.junit.ClassRule; -import org.junit.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; -public class SorterStrategyDescriptorTest { +@WithJenkins +class SorterStrategyDescriptorTest { - @ClassRule - public static JenkinsRule j = new JenkinsRule(); + private static JenkinsRule j; private static FreeStyleProject project; private static FreeStyleBuild build; @@ -26,8 +26,9 @@ public class SorterStrategyDescriptorTest { private static final int NUMBER_OF_PRIORITIES = 9; private static final int DEFAULT_PRIORITY = 4; - @BeforeClass - public static void runJob() throws Exception { + @BeforeAll + static void beforeAll(JenkinsRule rule) throws Exception { + j = rule; project = j.createFreeStyleProject(); build = project.scheduleBuild2(0).get(); strategy = new TestSorterStrategy(); @@ -36,22 +37,22 @@ public static void runJob() throws Exception { } @Test - public void getNumberOfPriorities() { + void getNumberOfPriorities() { assertEquals(NUMBER_OF_PRIORITIES, strategy.getNumberOfPriorities()); } @Test - public void getDefaultPriority() { + void getDefaultPriority() { assertEquals(DEFAULT_PRIORITY, strategy.getDefaultPriority()); } @Test - public void getShortNameReturnsCorrectValue() { + void getShortNameReturnsCorrectValue() { assertEquals(STRATEGY_NAME, descriptor.getShortName()); } @Test - public void getKeyReturnsShortName() { + void getKeyReturnsShortName() { assertEquals(STRATEGY_NAME, descriptor.getKey()); } diff --git a/src/test/java/jenkins/advancedqueue/sorter/strategy/FQStrategyTest.java b/src/test/java/jenkins/advancedqueue/sorter/strategy/FQStrategyTest.java index 05f72fd2..c4c08e97 100644 --- a/src/test/java/jenkins/advancedqueue/sorter/strategy/FQStrategyTest.java +++ b/src/test/java/jenkins/advancedqueue/sorter/strategy/FQStrategyTest.java @@ -1,23 +1,25 @@ package jenkins.advancedqueue.sorter.strategy; -import org.junit.Assert; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; -public class FQStrategyTest { +import org.junit.jupiter.api.Test; + +class FQStrategyTest { @Test - public void testStepSize() { - Assert.assertEquals(FQBaseStrategy.MIN_STEP_SIZE, new FQStrategy().getStepSize(1), 0F); - Assert.assertEquals(FQBaseStrategy.MIN_STEP_SIZE, new FQStrategy().getStepSize(2), 0F); - Assert.assertEquals(FQBaseStrategy.MIN_STEP_SIZE, new FQStrategy().getStepSize(3), 0F); - Assert.assertEquals(FQBaseStrategy.MIN_STEP_SIZE, new FQStrategy().getStepSize(4), 0F); + void testStepSize() { + assertEquals(FQBaseStrategy.MIN_STEP_SIZE, new FQStrategy().getStepSize(1), 0F); + assertEquals(FQBaseStrategy.MIN_STEP_SIZE, new FQStrategy().getStepSize(2), 0F); + assertEquals(FQBaseStrategy.MIN_STEP_SIZE, new FQStrategy().getStepSize(3), 0F); + assertEquals(FQBaseStrategy.MIN_STEP_SIZE, new FQStrategy().getStepSize(4), 0F); } @Test - public void testGetWeightToUse() { - Assert.assertEquals(1.00000F + FQBaseStrategy.MIN_STEP_SIZE, new FQStrategy().getWeightToUse(1, 1.00000F), 0F); - Assert.assertEquals(1.00001F + FQBaseStrategy.MIN_STEP_SIZE, new FQStrategy().getWeightToUse(1, 1.00001F), 0F); - Assert.assertEquals(1F, new FQStrategy().getWeightToUse(1, Float.MAX_VALUE), 0F); + void testGetWeightToUse() { + assertEquals(1.00000F + FQBaseStrategy.MIN_STEP_SIZE, new FQStrategy().getWeightToUse(1, 1.00000F), 0F); + assertEquals(1.00001F + FQBaseStrategy.MIN_STEP_SIZE, new FQStrategy().getWeightToUse(1, 1.00001F), 0F); + assertEquals(1F, new FQStrategy().getWeightToUse(1, Float.MAX_VALUE), 0F); assertIncreasingWeight(1F); assertIncreasingWeight(100000F); } @@ -26,9 +28,9 @@ private void assertIncreasingWeight(float initialWeight) { float previousWeight = initialWeight; for (int i = 0; i < 10; ++i) { float newWeight = new FQStrategy().getWeightToUse(1, previousWeight); - Assert.assertTrue( - "New weight %s should be greater than previous weight %s".formatted(newWeight, previousWeight), - newWeight > previousWeight); + assertTrue( + newWeight > previousWeight, + "New weight %s should be greater than previous weight %s".formatted(newWeight, previousWeight)); previousWeight = newWeight; } } diff --git a/src/test/java/jenkins/advancedqueue/sorter/strategy/MultiBucketStrategyTest.java b/src/test/java/jenkins/advancedqueue/sorter/strategy/MultiBucketStrategyTest.java index cc7aede2..28d81d0e 100644 --- a/src/test/java/jenkins/advancedqueue/sorter/strategy/MultiBucketStrategyTest.java +++ b/src/test/java/jenkins/advancedqueue/sorter/strategy/MultiBucketStrategyTest.java @@ -1,17 +1,19 @@ package jenkins.advancedqueue.sorter.strategy; -import static org.junit.Assert.assertEquals; +import static org.junit.jupiter.api.Assertions.assertEquals; import edu.umd.cs.findbugs.annotations.NonNull; import hudson.model.Queue; import hudson.util.ListBoxModel; import jenkins.advancedqueue.sorter.SorterStrategyCallback; -import org.junit.Before; -import org.junit.ClassRule; -import org.junit.Test; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; -public class MultiBucketStrategyTest extends MultiBucketStrategy { +@WithJenkins +class MultiBucketStrategyTest extends MultiBucketStrategy { /** @{inheritDoc} */ @Override @@ -19,16 +21,20 @@ public SorterStrategyCallback onNewItem(@NonNull Queue.Item item, SorterStrategy return weightCallback; } - @ClassRule - public static JenkinsRule j = new JenkinsRule(); + private static JenkinsRule j; private static MultiBucketStrategy strategy; private static MultiBucketStrategy.MultiBucketStrategyDescriptor descriptor; private static final int TEST_PRIORITY_COUNT = 10; private static final int TEST_PRIORITY_DEFAULT = 5; - @Before - public void setUp() { + @BeforeAll + static void beforeAll(JenkinsRule rule) { + j = rule; + } + + @BeforeEach + void beforeEach() { strategy = new MultiBucketStrategy(TEST_PRIORITY_COUNT, TEST_PRIORITY_DEFAULT) { @Override public SorterStrategyCallback onNewItem(@NonNull Queue.Item item, SorterStrategyCallback weightCallback) { @@ -50,17 +56,17 @@ protected MultiBucketStrategy getStrategy() { } @Test - public void getNumberOfPrioritiesReturnsCorrectValue() { + void getNumberOfPrioritiesReturnsCorrectValue() { assertEquals(TEST_PRIORITY_COUNT, strategy.getNumberOfPriorities()); } @Test - public void getDefaultPriorityReturnsCorrectValue() { + void getDefaultPriorityReturnsCorrectValue() { assertEquals(TEST_PRIORITY_DEFAULT, strategy.getDefaultPriority()); } @Test - public void doFillDefaultPriorityItemsReturnsCorrectItems() { + void doFillDefaultPriorityItemsReturnsCorrectItems() { ListBoxModel items = descriptor.doFillDefaultPriorityItems(); assertEquals(TEST_PRIORITY_COUNT, items.size()); for (int i = 0; i < TEST_PRIORITY_COUNT; i++) { @@ -69,7 +75,7 @@ public void doFillDefaultPriorityItemsReturnsCorrectItems() { } @Test - public void doUpdateDefaultPriorityItemsHandlesInvalidInput() { + void doUpdateDefaultPriorityItemsHandlesInvalidInput() { ListBoxModel items = descriptor.doUpdateDefaultPriorityItems("invalid"); assertEquals(MultiBucketStrategy.DEFAULT_PRIORITY, items.size()); for (int i = 0; i < MultiBucketStrategy.DEFAULT_PRIORITY; i++) { @@ -78,7 +84,7 @@ public void doUpdateDefaultPriorityItemsHandlesInvalidInput() { } @Test - public void doUpdateDefaultPriorityItemsHandlesValidInput() { + void doUpdateDefaultPriorityItemsHandlesValidInput() { ListBoxModel items = descriptor.doUpdateDefaultPriorityItems("3"); assertEquals(3, items.size()); for (int i = 0; i < 3; i++) { @@ -87,19 +93,19 @@ public void doUpdateDefaultPriorityItemsHandlesValidInput() { } @Test - public void doUpdateDefaultPriorityItemsHandlesNegativeInput() { + void doUpdateDefaultPriorityItemsHandlesNegativeInput() { ListBoxModel items = descriptor.doUpdateDefaultPriorityItems("-1"); assertEquals(0, items.size()); } @Test - public void doUpdateDefaultPriorityItemsHandlesZeroInput() { + void doUpdateDefaultPriorityItemsHandlesZeroInput() { ListBoxModel items = descriptor.doUpdateDefaultPriorityItems("0"); assertEquals(0, items.size()); } @Test - public void doUpdateDefaultPriorityItemsHandlesLargeInput() { + void doUpdateDefaultPriorityItemsHandlesLargeInput() { ListBoxModel items = descriptor.doUpdateDefaultPriorityItems("100"); assertEquals(100, items.size()); for (int i = 0; i < 100; i++) { diff --git a/src/test/java/jenkins/advancedqueue/sorter/strategy/WFQStrategyTest.java b/src/test/java/jenkins/advancedqueue/sorter/strategy/WFQStrategyTest.java index 888e8a27..24287449 100644 --- a/src/test/java/jenkins/advancedqueue/sorter/strategy/WFQStrategyTest.java +++ b/src/test/java/jenkins/advancedqueue/sorter/strategy/WFQStrategyTest.java @@ -1,25 +1,24 @@ package jenkins.advancedqueue.sorter.strategy; -import org.junit.Assert; -import org.junit.Test; +import static org.junit.jupiter.api.Assertions.assertEquals; -public class WFQStrategyTest { +import org.junit.jupiter.api.Test; + +class WFQStrategyTest { @Test - public void testStepSize() { - Assert.assertEquals(1 * FQBaseStrategy.MIN_STEP_SIZE, new WFQStrategy().getStepSize(1), 0F); - Assert.assertEquals(2 * FQBaseStrategy.MIN_STEP_SIZE, new WFQStrategy().getStepSize(2), 0F); - Assert.assertEquals(3 * FQBaseStrategy.MIN_STEP_SIZE, new WFQStrategy().getStepSize(3), 0F); - Assert.assertEquals(4 * FQBaseStrategy.MIN_STEP_SIZE, new WFQStrategy().getStepSize(4), 0F); + void testStepSize() { + assertEquals(1 * FQBaseStrategy.MIN_STEP_SIZE, new WFQStrategy().getStepSize(1), 0F); + assertEquals(2 * FQBaseStrategy.MIN_STEP_SIZE, new WFQStrategy().getStepSize(2), 0F); + assertEquals(3 * FQBaseStrategy.MIN_STEP_SIZE, new WFQStrategy().getStepSize(3), 0F); + assertEquals(4 * FQBaseStrategy.MIN_STEP_SIZE, new WFQStrategy().getStepSize(4), 0F); } @Test - public void testGetWeightToUse() { - Assert.assertEquals(1.00000F + FQBaseStrategy.MIN_STEP_SIZE, new WFQStrategy().getWeightToUse(1, 1.00000F), 0F); - Assert.assertEquals(1.00001F + FQBaseStrategy.MIN_STEP_SIZE, new WFQStrategy().getWeightToUse(1, 1.00001F), 0F); - Assert.assertEquals( - 1.00000F + 2 * FQBaseStrategy.MIN_STEP_SIZE, new WFQStrategy().getWeightToUse(2, 1.00000F), 0F); - Assert.assertEquals( - 1.00001F + 2 * FQBaseStrategy.MIN_STEP_SIZE, new WFQStrategy().getWeightToUse(2, 1.00001F), 0F); + void testGetWeightToUse() { + assertEquals(1.00000F + FQBaseStrategy.MIN_STEP_SIZE, new WFQStrategy().getWeightToUse(1, 1.00000F), 0F); + assertEquals(1.00001F + FQBaseStrategy.MIN_STEP_SIZE, new WFQStrategy().getWeightToUse(1, 1.00001F), 0F); + assertEquals(1.00000F + 2 * FQBaseStrategy.MIN_STEP_SIZE, new WFQStrategy().getWeightToUse(2, 1.00000F), 0F); + assertEquals(1.00001F + 2 * FQBaseStrategy.MIN_STEP_SIZE, new WFQStrategy().getWeightToUse(2, 1.00001F), 0F); } } diff --git a/src/test/java/jenkins/advancedqueue/test/BasicTest.java b/src/test/java/jenkins/advancedqueue/test/BasicTest.java index 3c9ed5bf..391655cf 100644 --- a/src/test/java/jenkins/advancedqueue/test/BasicTest.java +++ b/src/test/java/jenkins/advancedqueue/test/BasicTest.java @@ -4,21 +4,27 @@ import jenkins.advancedqueue.testutil.ExpectedItem; import jenkins.advancedqueue.testutil.JobHelper; import jenkins.advancedqueue.testutil.TestRunListener; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import org.jvnet.hudson.test.recipes.LocalData; -public class BasicTest { +@WithJenkins +class BasicTest { - @Rule - public JenkinsRule j = new JenkinsRule(); + private JenkinsRule j; + private JobHelper jobHelper; - private JobHelper jobHelper = new JobHelper(j); + @BeforeEach + void beforeEach(JenkinsRule j) throws Exception { + this.j = j; + jobHelper = new JobHelper(j); + } @Test @LocalData - public void simple_two_jobs_with_basic_configuration() throws Exception { + void simple_two_jobs_with_basic_configuration() throws Exception { TestRunListener.init(new ExpectedItem("Job 0", 9), new ExpectedItem("Job 1", 9)); jobHelper.scheduleProjects(new UserIdCause(), new UserIdCause()).go(); j.waitUntilNoActivity(); @@ -27,7 +33,7 @@ public void simple_two_jobs_with_basic_configuration() throws Exception { @Test @LocalData - public void simple_with_basic_configuration() throws Exception { + void simple_with_basic_configuration() throws Exception { TestRunListener.init(new ExpectedItem("Job 0", 9)); jobHelper.scheduleProjects(new UserIdCause()).go(); j.waitUntilNoActivity(); diff --git a/src/test/java/jenkins/advancedqueue/test/ConfigurationAsCodeTest.java b/src/test/java/jenkins/advancedqueue/test/ConfigurationAsCodeTest.java index a4368247..0b63a5e3 100644 --- a/src/test/java/jenkins/advancedqueue/test/ConfigurationAsCodeTest.java +++ b/src/test/java/jenkins/advancedqueue/test/ConfigurationAsCodeTest.java @@ -17,17 +17,15 @@ import jenkins.advancedqueue.priority.strategy.JobPropertyStrategy; import jenkins.advancedqueue.priority.strategy.UpstreamCauseStrategy; import jenkins.advancedqueue.priority.strategy.UserIdCauseStrategy; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; -public class ConfigurationAsCodeTest { - - @Rule - public JenkinsRule r = new JenkinsRule(); +@WithJenkins +class ConfigurationAsCodeTest { @Test - public void PrioritySorterConfiguration() throws ConfiguratorException { + void prioritySorterConfiguration(JenkinsRule r) throws ConfiguratorException { ConfigurationAsCode.get() .configure(ConfigurationAsCodeTest.class .getResource("ConfigurationAsCodeTest/PrioritySorterConfiguration.yaml") @@ -39,7 +37,7 @@ public void PrioritySorterConfiguration() throws ConfiguratorException { } @Test - public void PriorityConfiguration() throws ConfiguratorException { + void priorityConfiguration(JenkinsRule r) throws ConfiguratorException { ConfigurationAsCode.get() .configure(ConfigurationAsCodeTest.class .getResource("ConfigurationAsCodeTest/PriorityConfiguration.yaml") diff --git a/src/test/java/jenkins/advancedqueue/test/ExportCascTest.java b/src/test/java/jenkins/advancedqueue/test/ExportCascTest.java index 42ebf52f..a1dcf540 100644 --- a/src/test/java/jenkins/advancedqueue/test/ExportCascTest.java +++ b/src/test/java/jenkins/advancedqueue/test/ExportCascTest.java @@ -1,8 +1,6 @@ package jenkins.advancedqueue.test; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import io.jenkins.plugins.casc.ConfigurationContext; import io.jenkins.plugins.casc.Configurator; @@ -16,19 +14,17 @@ import io.jenkins.plugins.casc.yaml.YamlUtils; import java.util.Collections; import jenkins.advancedqueue.PriorityConfiguration; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import org.jvnet.hudson.test.recipes.LocalData; -public class ExportCascTest { - - @Rule - public JenkinsRule r = new JenkinsRule(); +@WithJenkins +class ExportCascTest { @Test @LocalData - public void exportTest() throws Exception { + void exportTest(JenkinsRule r) throws Exception { ConfiguratorRegistry registry = ConfiguratorRegistry.get(); ConfigurationContext context = new ConfigurationContext(registry); Mapping expectedConfig = YamlUtils.loadFrom( diff --git a/src/test/java/jenkins/advancedqueue/test/JobPatternGroupTest.java b/src/test/java/jenkins/advancedqueue/test/JobPatternGroupTest.java index 40c2f76f..9bba32a1 100644 --- a/src/test/java/jenkins/advancedqueue/test/JobPatternGroupTest.java +++ b/src/test/java/jenkins/advancedqueue/test/JobPatternGroupTest.java @@ -4,21 +4,27 @@ import jenkins.advancedqueue.testutil.ExpectedItem; import jenkins.advancedqueue.testutil.JobHelper; import jenkins.advancedqueue.testutil.TestRunListener; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import org.jvnet.hudson.test.recipes.LocalData; -public class JobPatternGroupTest { +@WithJenkins +class JobPatternGroupTest { - @Rule - public JenkinsRule j = new JenkinsRule(); + private JenkinsRule j; + private JobHelper jobHelper; - private JobHelper jobHelper = new JobHelper(j); + @BeforeEach + void beforeEach(JenkinsRule j) throws Exception { + this.j = j; + jobHelper = new JobHelper(j); + } @Test @LocalData - public void test_job_pattern_1() throws Exception { + void test_job_pattern_1() throws Exception { TestRunListener.init(new ExpectedItem("Job 0", 3)); jobHelper.scheduleProjects(new UserIdCause()).go(); j.waitUntilNoActivity(); @@ -27,7 +33,7 @@ public void test_job_pattern_1() throws Exception { @Test @LocalData - public void test_job_pattern_2() throws Exception { + void test_job_pattern_2() throws Exception { TestRunListener.init(new ExpectedItem("Job 0", 3), new ExpectedItem("Job 1", 9)); jobHelper.scheduleProjects(new UserIdCause(), new UserIdCause()).go(); j.waitUntilNoActivity(); @@ -36,7 +42,7 @@ public void test_job_pattern_2() throws Exception { @Test @LocalData - public void test_job_pattern_3() throws Exception { + void test_job_pattern_3() throws Exception { TestRunListener.init( new ExpectedItem("Job 0", 3), new ExpectedItem("Job 3", 3), diff --git a/src/test/java/jenkins/advancedqueue/test/MatrixTest.java b/src/test/java/jenkins/advancedqueue/test/MatrixTest.java index 13f9632c..50d1eed4 100644 --- a/src/test/java/jenkins/advancedqueue/test/MatrixTest.java +++ b/src/test/java/jenkins/advancedqueue/test/MatrixTest.java @@ -4,29 +4,31 @@ import jenkins.advancedqueue.testutil.ExpectedItem; import jenkins.advancedqueue.testutil.JobHelper; import jenkins.advancedqueue.testutil.TestRunListener; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import org.jvnet.hudson.test.recipes.LocalData; -public class MatrixTest { +@WithJenkins +class MatrixTest { - @Rule - public JenkinsRule j = new JenkinsRule(); + private JenkinsRule j; + private JobHelper jobHelper; - private JobHelper jobHelper = new JobHelper(j); + @BeforeEach + void beforeEach(JenkinsRule j) throws Exception { + this.j = j; + jobHelper = new JobHelper(j); + } @Test @LocalData - public void simple_matrix_with_no_configuration() throws Exception { + void simple_matrix_with_no_configuration() throws Exception { TestRunListener.init( new ExpectedItem("Matrix 0", 1), new ExpectedItem("0A1=0A.", 1), new ExpectedItem("0A1=0A.", 1)); jobHelper.scheduleMatrixProjects(new UserIdCause()).go(); j.waitUntilNoActivity(); TestRunListener.assertStartedItems(); } - - private boolean isWindows() { - return java.io.File.pathSeparatorChar == ';'; - } } diff --git a/src/test/java/jenkins/advancedqueue/test/MultipleMatchJobGroupTest.java b/src/test/java/jenkins/advancedqueue/test/MultipleMatchJobGroupTest.java index cad17613..3fc46742 100644 --- a/src/test/java/jenkins/advancedqueue/test/MultipleMatchJobGroupTest.java +++ b/src/test/java/jenkins/advancedqueue/test/MultipleMatchJobGroupTest.java @@ -5,21 +5,27 @@ import jenkins.advancedqueue.testutil.ExpectedItem; import jenkins.advancedqueue.testutil.JobHelper; import jenkins.advancedqueue.testutil.TestRunListener; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import org.jvnet.hudson.test.recipes.LocalData; -public class MultipleMatchJobGroupTest { +@WithJenkins +class MultipleMatchJobGroupTest { - @Rule - public JenkinsRule j = new JenkinsRule(); + private JenkinsRule j; + private JobHelper jobHelper; - private JobHelper jobHelper = new JobHelper(j); + @BeforeEach + void beforeEach(JenkinsRule j) throws Exception { + this.j = j; + jobHelper = new JobHelper(j); + } @Test @LocalData - public void multiple_job_group_matches() throws Exception { + void multiple_job_group_matches() throws Exception { // Job 2 and 3 matches View1 and All -> View1 is before All -> priorities are 1 and 2 // Job 0 and 1 matched only All -> priorities are 3 and 4 TestRunListener.init( diff --git a/src/test/java/jenkins/advancedqueue/test/NestedViewTest.java b/src/test/java/jenkins/advancedqueue/test/NestedViewTest.java index 2c0d4c57..45036526 100644 --- a/src/test/java/jenkins/advancedqueue/test/NestedViewTest.java +++ b/src/test/java/jenkins/advancedqueue/test/NestedViewTest.java @@ -4,21 +4,27 @@ import jenkins.advancedqueue.testutil.ExpectedItem; import jenkins.advancedqueue.testutil.JobHelper; import jenkins.advancedqueue.testutil.TestRunListener; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import org.jvnet.hudson.test.recipes.LocalData; -public class NestedViewTest { +@WithJenkins +class NestedViewTest { - @Rule - public JenkinsRule j = new JenkinsRule(); + private JenkinsRule j; + private JobHelper jobHelper; - private JobHelper jobHelper = new JobHelper(j); + @BeforeEach + void beforeEach(JenkinsRule j) throws Exception { + this.j = j; + jobHelper = new JobHelper(j); + } @Test @LocalData - public void nested_view_test() throws Exception { + void nested_view_test() throws Exception { // Job 0 matches "Nested View A/Nested View B" -> priority is 1 // Job 0 matched nothing -> default priority is 9 TestRunListener.init(new ExpectedItem("Job 0", 1), new ExpectedItem("Job 1", 9)); diff --git a/src/test/java/jenkins/advancedqueue/test/OneJobGroupTest.java b/src/test/java/jenkins/advancedqueue/test/OneJobGroupTest.java index 7de18400..d92f5d1f 100644 --- a/src/test/java/jenkins/advancedqueue/test/OneJobGroupTest.java +++ b/src/test/java/jenkins/advancedqueue/test/OneJobGroupTest.java @@ -5,21 +5,27 @@ import jenkins.advancedqueue.testutil.ExpectedItem; import jenkins.advancedqueue.testutil.JobHelper; import jenkins.advancedqueue.testutil.TestRunListener; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import org.jvnet.hudson.test.recipes.LocalData; -public class OneJobGroupTest { +@WithJenkins +class OneJobGroupTest { - @Rule - public JenkinsRule j = new JenkinsRule(); + private JenkinsRule j; + private JobHelper jobHelper; - private JobHelper jobHelper = new JobHelper(j); + @BeforeEach + void beforeEach(JenkinsRule j) throws Exception { + this.j = j; + jobHelper = new JobHelper(j); + } @Test @LocalData - public void default_job_group_priority() throws Exception { + void default_job_group_priority() throws Exception { TestRunListener.init(new ExpectedItem("Job 0", 3)); jobHelper .scheduleProjects(new Cause() { @@ -36,7 +42,7 @@ public String getShortDescription() { @Test @LocalData - public void test_UserIdCause() throws Exception { + void test_UserIdCause() throws Exception { TestRunListener.init(new ExpectedItem("Job 0", 4)); jobHelper.scheduleProjects(new Cause.UserIdCause()).go(); j.waitUntilNoActivity(); @@ -45,7 +51,7 @@ public void test_UserIdCause() throws Exception { @Test @LocalData - public void test_CLICause() throws Exception { + void test_CLICause() throws Exception { TestRunListener.init(new ExpectedItem("Job 0", 5)); jobHelper.scheduleProjects(new CLICause()).go(); j.waitUntilNoActivity(); @@ -54,7 +60,7 @@ public void test_CLICause() throws Exception { @Test @LocalData - public void test_multiple_strategies() throws Exception { + void test_multiple_strategies() throws Exception { TestRunListener.init(new ExpectedItem("Job 2", 3), new ExpectedItem("Job 1", 4), new ExpectedItem("Job 0", 5)); jobHelper .scheduleProjects(new CLICause(), new Cause.UserIdCause(), new Cause() { diff --git a/src/test/java/jenkins/advancedqueue/test/PrioritySorterConfigurationTest.java b/src/test/java/jenkins/advancedqueue/test/PrioritySorterConfigurationTest.java index 91bfc5fa..f5052734 100644 --- a/src/test/java/jenkins/advancedqueue/test/PrioritySorterConfigurationTest.java +++ b/src/test/java/jenkins/advancedqueue/test/PrioritySorterConfigurationTest.java @@ -1,6 +1,6 @@ package jenkins.advancedqueue.test; -import static org.junit.Assert.*; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.Mockito.*; import hudson.util.ListBoxModel; @@ -9,28 +9,30 @@ import jenkins.advancedqueue.PrioritySorterConfiguration; import jenkins.advancedqueue.sorter.SorterStrategy; import jenkins.advancedqueue.sorter.SorterStrategyDescriptor; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import org.mockito.MockedStatic; -public class PrioritySorterConfigurationTest { - @Rule - public JenkinsRule j = new JenkinsRule(); +@WithJenkins +class PrioritySorterConfigurationTest { + + private JenkinsRule j; private PrioritySorterConfiguration prioritySorterConfiguration; private SorterStrategyDescriptor sorterStrategyDescriptor1, sorterStrategyDescriptor2; - @Before - public void setUp() { + @BeforeEach + void beforeEach(JenkinsRule j) { + this.j = j; prioritySorterConfiguration = new PrioritySorterConfiguration(); sorterStrategyDescriptor1 = mock(SorterStrategyDescriptor.class); sorterStrategyDescriptor2 = mock(SorterStrategyDescriptor.class); } @Test - public void testDoFillStrategyItems() { + void testDoFillStrategyItems() { when(sorterStrategyDescriptor1.getDisplayName()).thenReturn("DisplayName1"); when(sorterStrategyDescriptor1.getKey()).thenReturn("Key1"); when(sorterStrategyDescriptor2.getDisplayName()).thenReturn("DisplayName2"); diff --git a/src/test/java/jenkins/advancedqueue/test/SectionedViewTest.java b/src/test/java/jenkins/advancedqueue/test/SectionedViewTest.java index ab7650c6..b59589c2 100644 --- a/src/test/java/jenkins/advancedqueue/test/SectionedViewTest.java +++ b/src/test/java/jenkins/advancedqueue/test/SectionedViewTest.java @@ -4,21 +4,27 @@ import jenkins.advancedqueue.testutil.ExpectedItem; import jenkins.advancedqueue.testutil.JobHelper; import jenkins.advancedqueue.testutil.TestRunListener; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import org.jvnet.hudson.test.recipes.LocalData; -public class SectionedViewTest { +@WithJenkins +class SectionedViewTest { - @Rule - public JenkinsRule j = new JenkinsRule(); + private JenkinsRule j; + private JobHelper jobHelper; - private JobHelper jobHelper = new JobHelper(j); + @BeforeEach + void beforeEach(JenkinsRule j) throws Exception { + this.j = j; + jobHelper = new JobHelper(j); + } @Test @LocalData - public void sectioned_view_test() throws Exception { + void sectioned_view_test() throws Exception { // Job 2 matches Sectioned View and All -> Sectioned View is before All -> priority is 1 // Job 1 matches View1 and All -> View1 is before All -> priority is 2 // Job 0 matched only All -> priority is 3 diff --git a/src/test/java/jenkins/advancedqueue/test/SimpleTest.java b/src/test/java/jenkins/advancedqueue/test/SimpleTest.java index 003bbf78..3acc0d2d 100644 --- a/src/test/java/jenkins/advancedqueue/test/SimpleTest.java +++ b/src/test/java/jenkins/advancedqueue/test/SimpleTest.java @@ -4,21 +4,27 @@ import jenkins.advancedqueue.testutil.ExpectedItem; import jenkins.advancedqueue.testutil.JobHelper; import jenkins.advancedqueue.testutil.TestRunListener; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import org.jvnet.hudson.test.recipes.LocalData; -public class SimpleTest { +@WithJenkins +class SimpleTest { - @Rule - public JenkinsRule j = new JenkinsRule(); + private JenkinsRule j; + private JobHelper jobHelper; - private JobHelper jobHelper = new JobHelper(j); + @BeforeEach + void beforeEach(JenkinsRule j) throws Exception { + this.j = j; + jobHelper = new JobHelper(j); + } @Test @LocalData - public void simple_with_no_configuration() throws Exception { + void simple_with_no_configuration() throws Exception { TestRunListener.init(new ExpectedItem("Job 0", 3)); jobHelper.scheduleProjects(new UserIdCause()).go(); j.waitUntilNoActivity(); @@ -27,7 +33,7 @@ public void simple_with_no_configuration() throws Exception { @Test @LocalData - public void simple_two_jobs_with_no_configuration() throws Exception { + void simple_two_jobs_with_no_configuration() throws Exception { TestRunListener.init(new ExpectedItem("Job 0", 3), new ExpectedItem("Job 1", 3)); jobHelper.scheduleProjects(new UserIdCause(), new UserIdCause()).go(); j.waitUntilNoActivity(); diff --git a/src/test/java/jenkins/advancedqueue/test/SortBlockedItemsTest.java b/src/test/java/jenkins/advancedqueue/test/SortBlockedItemsTest.java index ed3d97d1..5a73d24a 100644 --- a/src/test/java/jenkins/advancedqueue/test/SortBlockedItemsTest.java +++ b/src/test/java/jenkins/advancedqueue/test/SortBlockedItemsTest.java @@ -5,19 +5,17 @@ import hudson.tasks.BuildTrigger; import jenkins.advancedqueue.testutil.ExpectedItem; import jenkins.advancedqueue.testutil.TestRunListener; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import org.jvnet.hudson.test.recipes.LocalData; -public class SortBlockedItemsTest { - - @Rule - public JenkinsRule j = new JenkinsRule(); +@WithJenkins +class SortBlockedItemsTest { @Test @LocalData - public void blockedItemsAreSortedByPriority() throws Exception { + void blockedItemsAreSortedByPriority(JenkinsRule j) throws Exception { // Priority value is configured in jenkins.advancedqueue.PrioritySorterConfiguration. Value is 2. FreeStyleProject upstreamProject = j.createFreeStyleProject("upstreamProject"); // Priority value is configured in jenkins.advancedqueue.PrioritySorterConfiguration. Value is 1. diff --git a/src/test/java/jenkins/advancedqueue/test/SubmitTest.java b/src/test/java/jenkins/advancedqueue/test/SubmitTest.java index 3009b095..390f56b9 100644 --- a/src/test/java/jenkins/advancedqueue/test/SubmitTest.java +++ b/src/test/java/jenkins/advancedqueue/test/SubmitTest.java @@ -15,22 +15,21 @@ import jenkins.advancedqueue.jobinclusion.strategy.ViewBasedJobInclusionStrategy; import jenkins.advancedqueue.priority.strategy.UserIdCauseStrategy; import org.apache.commons.io.IOUtils; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import org.jvnet.hudson.test.recipes.LocalData; import org.kohsuke.stapler.MockStaplerRequest; import org.kohsuke.stapler.StaplerRequest2; import org.kohsuke.stapler.StaplerResponse2; -public class SubmitTest { - - @Rule - public JenkinsRule j = new JenkinsRule(); +@WithJenkins +class SubmitTest { @Test @LocalData - public void shouldGeneratePriorityConfigurationUsingDataBoundConstructor() throws IOException, ServletException { + void shouldGeneratePriorityConfigurationUsingDataBoundConstructor(JenkinsRule j) + throws IOException, ServletException { PriorityConfiguration priorityConfiguration = (PriorityConfiguration) j.jenkins.getDescriptor(PriorityConfiguration.class); StaplerResponse2 staplerResponse = mock(StaplerResponse2.class); diff --git a/src/test/java/jenkins/advancedqueue/test/UpstreamTest.java b/src/test/java/jenkins/advancedqueue/test/UpstreamTest.java index 8e2027f6..310ec389 100644 --- a/src/test/java/jenkins/advancedqueue/test/UpstreamTest.java +++ b/src/test/java/jenkins/advancedqueue/test/UpstreamTest.java @@ -11,20 +11,27 @@ import jenkins.advancedqueue.testutil.ExpectedItem; import jenkins.advancedqueue.testutil.JobHelper; import jenkins.advancedqueue.testutil.TestRunListener; -import org.junit.Rule; -import org.junit.Test; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; import org.jvnet.hudson.test.JenkinsRule; +import org.jvnet.hudson.test.junit.jupiter.WithJenkins; import org.jvnet.hudson.test.recipes.LocalData; -public class UpstreamTest { - @Rule - public JenkinsRule j = new JenkinsRule(); +@WithJenkins +class UpstreamTest { - private final JobHelper jobHelper = new JobHelper(j); + private JenkinsRule j; + private JobHelper jobHelper; + + @BeforeEach + void beforeEach(JenkinsRule j) throws Exception { + this.j = j; + jobHelper = new JobHelper(j); + } @Test @LocalData - public void testOrphanDownstreamJob() throws Exception { + void testOrphanDownstreamJob() throws Exception { // Job 0 should run with default priority, as upstream build is unknown TestRunListener.init(new ExpectedItem("Job 0", 5)); jobHelper.scheduleProjects(createUpstreamCause("Job X", 987)).go(); @@ -35,7 +42,7 @@ public void testOrphanDownstreamJob() throws Exception { @Test @LocalData - public void testUserJobAndAssociatedDownstreamJob() throws Exception { + void testUserJobAndAssociatedDownstreamJob() throws Exception { // Upstream job should run with high priority (user triggered) TestRunListener.init(new ExpectedItem("Upstream", 1)); jobHelper.scheduleProject("Upstream", new UserIdCause()).go(); diff --git a/src/test/java/jenkins/advancedqueue/testutil/TestRunListener.java b/src/test/java/jenkins/advancedqueue/testutil/TestRunListener.java index b0fc384d..2f7e6295 100644 --- a/src/test/java/jenkins/advancedqueue/testutil/TestRunListener.java +++ b/src/test/java/jenkins/advancedqueue/testutil/TestRunListener.java @@ -1,5 +1,8 @@ package jenkins.advancedqueue.testutil; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + import hudson.Extension; import hudson.model.Run; import hudson.model.TaskListener; @@ -10,7 +13,6 @@ import java.util.logging.Logger; import jenkins.advancedqueue.sorter.ItemInfo; import jenkins.advancedqueue.sorter.QueueItemCache; -import org.junit.Assert; @Extension public class TestRunListener extends RunListener { @@ -42,17 +44,17 @@ public void onStarted(Run r, TaskListener listener) { } public static void assertStartedItems() { - Assert.assertEquals("Wrong number of started items", expected.length, actual.size()); + assertEquals(expected.length, actual.size(), "Wrong number of started items"); for (int i = 0; i < actual.size(); i++) { LOGGER.info("Validating Build " + i); - Assert.assertTrue( + assertTrue( + actual.get(i).getJobName().matches(expected[i].getJobName()), "Job mismatch at position [" + i + "] expected <" + expected[i].getJobName() + "> was <" - + actual.get(i).getJobName() + ">", - actual.get(i).getJobName().matches(expected[i].getJobName())); - Assert.assertEquals( - "Priority mismatch at position [" + i + "]", + + actual.get(i).getJobName() + ">"); + assertEquals( expected[i].getPriority(), - actual.get(i).getPriority()); + actual.get(i).getPriority(), + "Priority mismatch at position [" + i + "]"); } } } diff --git a/src/test/java/jenkins/advancedqueue/util/PrioritySorterUtilTest.java b/src/test/java/jenkins/advancedqueue/util/PrioritySorterUtilTest.java index 3971bd9f..515fc04f 100644 --- a/src/test/java/jenkins/advancedqueue/util/PrioritySorterUtilTest.java +++ b/src/test/java/jenkins/advancedqueue/util/PrioritySorterUtilTest.java @@ -1,21 +1,21 @@ package jenkins.advancedqueue.util; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; import hudson.util.ListBoxModel; -import org.junit.Test; +import org.junit.jupiter.api.Test; -public class PrioritySorterUtilTest { +class PrioritySorterUtilTest { @Test - public void assertPriorityItemsHasExpectedSize() { + void assertPriorityItemsHasExpectedSize() { ListBoxModel testList = PrioritySorterUtil.fillPriorityItems(3); assertEquals(3, testList.size()); } @Test - public void assertEmptyListWhenToParameterIsZero() { + void assertEmptyListWhenToParameterIsZero() { ListBoxModel testList = PrioritySorterUtil.fillPriorityItems(0); assertTrue(testList.isEmpty()); }