Skip to content

Commit

Permalink
Added tests for JobPropertyStrategy class (#428)
Browse files Browse the repository at this point in the history
* added-tests

Signed-off-by: shinigami-777 <[email protected]>

* Avoid null pointer exception on null item

* Replace mocks with real objects, increase coverage

No need to use mock objects when we can use a JenkinsRule with
specifically defined jobs that exercise the desired paths.

---------

Signed-off-by: shinigami-777 <[email protected]>
Co-authored-by: Mark Waite <[email protected]>
  • Loading branch information
shinigami-777 and MarkEWaite authored Jan 1, 2025
1 parent 5bad03a commit d3dfbbd
Show file tree
Hide file tree
Showing 2 changed files with 114 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public JobPropertyStrategy() {}

@CheckForNull
private Integer getPriorityInternal(Queue.Item item) {
if (item.task instanceof Job<?, ?>) {
if (item != null && item.task instanceof Job<?, ?>) {

Check warning on line 54 in src/main/java/jenkins/advancedqueue/priority/strategy/JobPropertyStrategy.java

View check run for this annotation

ci.jenkins.io / Code Coverage

Partially covered line

Line 54 is only partially covered, one branch is missing
Job<?, ?> job = (Job<?, ?>) item.task;
PriorityJobProperty priorityProperty = job.getProperty(PriorityJobProperty.class);
if (priorityProperty != null && priorityProperty.getUseJobPriority()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package jenkins.advancedqueue.priority.strategy;

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 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.jvnet.hudson.test.JenkinsRule;

public class JobPropertyStrategyTest {

@ClassRule
public static JenkinsRule j = new JenkinsRule();

private static FreeStyleProject project;
private static FreeStyleProject projectWithProperty;
private static FreeStyleProject projectWithUnusedProperty;

private static int defaultPriority;
private static int jobPriority;

private JobPropertyStrategy strategy;

@BeforeClass
public static void startProject() throws Exception {
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);
projectWithProperty = j.createFreeStyleProject("with-property");
projectWithProperty.addProperty(property);
// 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);
projectWithUnusedProperty = j.createFreeStyleProject("with-unused-property");
projectWithUnusedProperty.addProperty(property);
// 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() {
strategy = new JobPropertyStrategy();
}

@Test
public void isApplicableTestWhengetPriorityInternalReturnsNull() {
Queue.Item nullItem = null;
assertFalse(strategy.isApplicable(nullItem));
assertFalse(strategy.isApplicable(null));
}

@Test
public void isApplicableWhenFreeStyleProject() {
assertFalse(strategy.isApplicable(project.getQueueItem()));
}

@Test
public void isApplicableWhenFreeStyleProjectWithPriorityProperty() {
assertTrue(strategy.isApplicable(projectWithProperty.getQueueItem()));
}

@Test
public void isApplicableWhenFreeStyleProjectWithUnusedPriorityProperty() {
assertFalse(strategy.isApplicable(projectWithUnusedProperty.getQueueItem()));
}

@Test
public void getPriorityTestWhengetPriorityInternalReturnsNull() {
Queue.Item nullItem = null;
// priority is 3 when item is null
assertThat(strategy.getPriority(nullItem), is(defaultPriority));
assertThat(strategy.getPriority(null), is(defaultPriority));
}

@Test
public void getPriorityTestFreeStyleProject() {
assertThat(strategy.getPriority(project.getQueueItem()), is(defaultPriority));
}

@Test
public void getPriorityTestFreeStyleProjectWithPriorityProperty() {
assertThat(strategy.getPriority(projectWithProperty.getQueueItem()), is(jobPriority));
}

@Test
public void getPriorityTestFreeStyleProjectWithUnusedPriorityProperty() {
assertThat(strategy.getPriority(projectWithUnusedProperty.getQueueItem()), is(defaultPriority));
}
}

0 comments on commit d3dfbbd

Please sign in to comment.