-
Notifications
You must be signed in to change notification settings - Fork 75
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add test for BuildParameterStrategy class
- Loading branch information
1 parent
f1aa5dd
commit 6f93df9
Showing
1 changed file
with
90 additions
and
0 deletions.
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
src/test/java/jenkins/advancedqueue/priority/strategy/BuildParameterStrategyTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package jenkins.advancedqueue.priority.strategy; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
|
||
import hudson.model.FreeStyleProject; | ||
import hudson.model.ParametersAction; | ||
import hudson.model.Queue; | ||
import hudson.model.StringParameterValue; | ||
import java.io.IOException; | ||
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.jvnet.hudson.test.JenkinsRule; | ||
|
||
public class BuildParameterStrategyTest { | ||
|
||
@ClassRule | ||
public static JenkinsRule j = new JenkinsRule(); | ||
|
||
private static FreeStyleProject project; | ||
private static BuildParameterStrategy strategy; | ||
|
||
@BeforeClass | ||
public static void setUp() throws IOException { | ||
project = j.createFreeStyleProject(); | ||
strategy = new BuildParameterStrategy("priority"); | ||
} | ||
|
||
@Test | ||
public void getPriority_returnsPriorityFromParameter() throws Exception { | ||
StringParameterValue param = new StringParameterValue("priority", "5"); | ||
ParametersAction action = new ParametersAction(param); | ||
Queue.Item item = new Queue.WaitingItem(Calendar.getInstance(), project, Collections.singletonList(action)); | ||
|
||
int priority = strategy.getPriority(item); | ||
|
||
assertEquals(5, priority); | ||
} | ||
|
||
@Test | ||
public void getPriority_returnsDefaultPriorityWhenParameterIsMissing() throws Exception { | ||
Queue.Item item = new Queue.WaitingItem(Calendar.getInstance(), project, Collections.emptyList()); | ||
|
||
int priority = strategy.getPriority(item); | ||
|
||
assertEquals(PrioritySorterConfiguration.get().getStrategy().getDefaultPriority(), priority); | ||
} | ||
|
||
@Test | ||
public void getPriority_returnsDefaultPriorityWhenParameterIsNotANumber() throws Exception { | ||
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)); | ||
|
||
int priority = strategy.getPriority(item); | ||
|
||
assertEquals(PrioritySorterConfiguration.get().getStrategy().getDefaultPriority(), priority); | ||
} | ||
|
||
@Test | ||
public void isApplicable_returnsTrueWhenParameterIsPresentAndValid() throws Exception { | ||
StringParameterValue param = new StringParameterValue("priority", "5"); | ||
ParametersAction action = new ParametersAction(param); | ||
Queue.Item item = new Queue.WaitingItem(Calendar.getInstance(), project, Collections.singletonList(action)); | ||
|
||
BuildParameterStrategy strategy = new BuildParameterStrategy("priority"); | ||
assertTrue(strategy.isApplicable(item)); | ||
} | ||
|
||
@Test | ||
public void isApplicable_returnsFalseWhenParameterIsMissing() throws Exception { | ||
Queue.Item item = new Queue.WaitingItem(Calendar.getInstance(), project, Collections.emptyList()); | ||
|
||
assertFalse(strategy.isApplicable(item)); | ||
} | ||
|
||
@Test | ||
public void isApplicable_returnsFalseWhenParameterIsNotANumber() throws Exception { | ||
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)); | ||
|
||
assertFalse(strategy.isApplicable(item)); | ||
} | ||
} |