Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for PriorityStrategy abstract class #442

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public abstract class PriorityStrategy implements ExtensionPoint, Describable<Pr
/**
* Method that checks if strategy can assign a priority to the provided {@link Item}
*
* The caller guaranties that the {@link Item#task} is a {@link Job}
* The caller guarantees that the {@link Item#task} is a {@link Job}
*
* @param item the {@link Item} to check
* @return <code>true</code> if the {@link PriorityStrategy} is applicable else <code>false</code>
Expand All @@ -52,7 +52,7 @@ public abstract class PriorityStrategy implements ExtensionPoint, Describable<Pr
* Method that that return the priority that should be used for this {@link Item}, this method is only called id
* {@link PriorityStrategy#isApplicable(Queue.Item)} returned true
*
* The caller garanties that the {@link Item#task} is a {@link Job}
* The caller guarantees that the {@link Item#task} is a {@link Job}
*
* @param item the {@link Item} to check
* @return the priority to be used by the provided {@link Item}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package jenkins.advancedqueue.priority;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import hudson.DescriptorExtensionList;
import hudson.model.Action;
import hudson.model.Descriptor;
import hudson.model.FreeStyleProject;
import hudson.model.Job;
import hudson.model.Queue;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;

public class PriorityStrategyTest {

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

private static PriorityStrategy strategy;
private static Queue.Item item;
private static FreeStyleProject project;
private static Action action;

@BeforeClass
public static void setUp() throws IOException {
project = j.createFreeStyleProject();
strategy = new TestPriorityStrategy();
}

@BeforeClass
public static void createActionAndItem() {
action = new Action() {

@Override
public String getIconFileName() {
return "";
}

@Override
public String getDisplayName() {
return "";
}

@Override
public String getUrlName() {
return "";
}
};
List<Action> actions = new ArrayList<>();
actions.add(action);
item = new Queue.WaitingItem(Calendar.getInstance(), project, actions);
}

@Test
public void testIsApplicable() {
boolean result = strategy.isApplicable(item);
assertTrue(result);
}

@Test
public void testGetPriority() {
int priority = strategy.getPriority(item);
assertEquals(5, priority);
}

@Test
public void testNumberPrioritiesUpdates() {
strategy.numberPrioritiesUpdates(3, 5);
// Add assertions to verify the behavior
TestPriorityStrategy testStrategy = new TestPriorityStrategy();
assertEquals(3, testStrategy.getOldNumberOfPriorities());
assertEquals(5, testStrategy.getNewNumberOfPriorities());
}

@Test
public void testAll() {
DescriptorExtensionList<PriorityStrategy, Descriptor<PriorityStrategy>> list = PriorityStrategy.all();
assertNotNull("DescriptorExtensionList should not be null", list);
// The list.size() method returns 7 because the DescriptorExtensionList for PriorityStrategy contains 7
// descriptors. This means there are 7 different implementations of the PriorityStrategy class registered in the
// Jenkins instance.
assertEquals(7, list.size());
}

@Test
public void testItemTaskIsInstanceOfJob() {
item = new Queue.WaitingItem(Calendar.getInstance(), project, new ArrayList<>());
assertTrue(item.task instanceof Job);
}

private static class TestPriorityStrategy extends PriorityStrategy {
private int oldNumberOfPriorities = 3;
private int newNumberOfPriorities = 5;

@Override
public boolean isApplicable(Queue.Item item) {
return item.task instanceof FreeStyleProject;
}

@Override
public int getPriority(Queue.Item item) {
return 5;
}

@Override
public void numberPrioritiesUpdates(int oldNumberOfPriorities, int newNumberOfPriorities) {
this.oldNumberOfPriorities = oldNumberOfPriorities;
this.newNumberOfPriorities = newNumberOfPriorities;
}

public int getOldNumberOfPriorities() {
return oldNumberOfPriorities;
}

public int getNewNumberOfPriorities() {
return newNumberOfPriorities;
}

@Override
public Descriptor<PriorityStrategy> getDescriptor() {
return null;
}
}
}
Loading