Skip to content

Commit

Permalink
Add test for SorterStrategyDescriptor class (#429)
Browse files Browse the repository at this point in the history
* Add test for SorterStrategyDescriptor class

* Use spotbugs NonNull annotation, not JetBrains

The spotbugs annotation is used in the declaration of the method so
should be used in this implementation.

* Use a single JenkinsRule for the entire test class

Individual tests do not need the cost or time delay of starting a new
Jenkins controller.

* Test more of the private implementation

Allocate a strategy and test its descriptor

* Remove duplicate test of descriptor not null

Other tests will fail if descriptor is null

---------

Co-authored-by: Mark Waite <[email protected]>
  • Loading branch information
yashpal2104 and MarkEWaite authored Jan 4, 2025
1 parent 8de4326 commit f1aa5dd
Showing 1 changed file with 82 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package jenkins.advancedqueue.sorter;

import static org.junit.Assert.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.jvnet.hudson.test.JenkinsRule;

public class SorterStrategyDescriptorTest {

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

private static FreeStyleProject project;
private static FreeStyleBuild build;
private static SorterStrategy strategy;
private static SorterStrategyDescriptor descriptor;

private static final String STRATEGY_NAME = "strategy short name";
private static final int NUMBER_OF_PRIORITIES = 9;
private static final int DEFAULT_PRIORITY = 4;

@BeforeClass
public static void runJob() throws Exception {
project = j.createFreeStyleProject();
build = project.scheduleBuild2(0).get();
strategy = new TestSorterStrategy();
descriptor = strategy.getDescriptor();
j.assertBuildStatusSuccess(build);
}

@Test
public void getNumberOfPriorities() {
assertEquals(NUMBER_OF_PRIORITIES, strategy.getNumberOfPriorities());
}

@Test
public void getDefaultPriority() {
assertEquals(DEFAULT_PRIORITY, strategy.getDefaultPriority());
}

@Test
public void getShortNameReturnsCorrectValue() {
assertEquals(STRATEGY_NAME, descriptor.getShortName());
}

@Test
public void getKeyReturnsShortName() {
assertEquals(STRATEGY_NAME, descriptor.getKey());
}

private static class TestSorterStrategy extends SorterStrategy {
@Override
public SorterStrategyCallback onNewItem(@NonNull Queue.Item item, SorterStrategyCallback weightCallback) {
return weightCallback;
}

@Override
public int getNumberOfPriorities() {
return NUMBER_OF_PRIORITIES;
}

@Override
public int getDefaultPriority() {
return DEFAULT_PRIORITY;
}

@Extension
public static class DescriptorImpl extends SorterStrategyDescriptor {
@Override
public String getShortName() {
return STRATEGY_NAME;
}
}
}
}

0 comments on commit f1aa5dd

Please sign in to comment.