-
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 MultiBucketStrategy class (#432)
* Add test for MultiBucketStrategy class * Made getStrategy method package-protected and condition for non-integer input. * Revert the name back to original * Remove behavior change from production object Value is set to 0 if a negative value is passed. * Adjust comment for package protected method * Use spotbugs NonNull instead of Atlassian NotNull * Inherit javadoc from the parent object * Clarify source of test values Show when default is from test vs. parent object --------- Co-authored-by: Mark Waite <[email protected]>
- Loading branch information
1 parent
ce836d1
commit b91ea54
Showing
2 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
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
109 changes: 109 additions & 0 deletions
109
src/test/java/jenkins/advancedqueue/sorter/strategy/MultiBucketStrategyTest.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,109 @@ | ||
package jenkins.advancedqueue.sorter.strategy; | ||
|
||
import static org.junit.Assert.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.jvnet.hudson.test.JenkinsRule; | ||
|
||
public class MultiBucketStrategyTest extends MultiBucketStrategy { | ||
|
||
/** @{inheritDoc} */ | ||
@Override | ||
public SorterStrategyCallback onNewItem(@NonNull Queue.Item item, SorterStrategyCallback weightCallback) { | ||
return weightCallback; | ||
} | ||
|
||
@ClassRule | ||
public static JenkinsRule j = new JenkinsRule(); | ||
|
||
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() { | ||
strategy = new MultiBucketStrategy(TEST_PRIORITY_COUNT, TEST_PRIORITY_DEFAULT) { | ||
@Override | ||
public SorterStrategyCallback onNewItem(@NonNull Queue.Item item, SorterStrategyCallback weightCallback) { | ||
return weightCallback; | ||
} | ||
}; | ||
|
||
descriptor = new MultiBucketStrategy.MultiBucketStrategyDescriptor() { | ||
@Override | ||
public String getShortName() { | ||
return ""; | ||
} | ||
|
||
@Override | ||
protected MultiBucketStrategy getStrategy() { | ||
return strategy; | ||
} | ||
}; | ||
} | ||
|
||
@Test | ||
public void getNumberOfPrioritiesReturnsCorrectValue() { | ||
assertEquals(TEST_PRIORITY_COUNT, strategy.getNumberOfPriorities()); | ||
} | ||
|
||
@Test | ||
public void getDefaultPriorityReturnsCorrectValue() { | ||
assertEquals(TEST_PRIORITY_DEFAULT, strategy.getDefaultPriority()); | ||
} | ||
|
||
@Test | ||
public void doFillDefaultPriorityItemsReturnsCorrectItems() { | ||
ListBoxModel items = descriptor.doFillDefaultPriorityItems(); | ||
assertEquals(TEST_PRIORITY_COUNT, items.size()); | ||
for (int i = 0; i < TEST_PRIORITY_COUNT; i++) { | ||
assertEquals(String.valueOf(i + 1), items.get(i).value); | ||
} | ||
} | ||
|
||
@Test | ||
public void doUpdateDefaultPriorityItemsHandlesInvalidInput() { | ||
ListBoxModel items = descriptor.doUpdateDefaultPriorityItems("invalid"); | ||
assertEquals(MultiBucketStrategy.DEFAULT_PRIORITY, items.size()); | ||
for (int i = 0; i < MultiBucketStrategy.DEFAULT_PRIORITY; i++) { | ||
assertEquals(String.valueOf(i + 1), items.get(i).value); | ||
} | ||
} | ||
|
||
@Test | ||
public void doUpdateDefaultPriorityItemsHandlesValidInput() { | ||
ListBoxModel items = descriptor.doUpdateDefaultPriorityItems("3"); | ||
assertEquals(3, items.size()); | ||
for (int i = 0; i < 3; i++) { | ||
assertEquals(String.valueOf(i + 1), items.get(i).value); | ||
} | ||
} | ||
|
||
@Test | ||
public void doUpdateDefaultPriorityItemsHandlesNegativeInput() { | ||
ListBoxModel items = descriptor.doUpdateDefaultPriorityItems("-1"); | ||
assertEquals(0, items.size()); | ||
} | ||
|
||
@Test | ||
public void doUpdateDefaultPriorityItemsHandlesZeroInput() { | ||
ListBoxModel items = descriptor.doUpdateDefaultPriorityItems("0"); | ||
assertEquals(0, items.size()); | ||
} | ||
|
||
@Test | ||
public void doUpdateDefaultPriorityItemsHandlesLargeInput() { | ||
ListBoxModel items = descriptor.doUpdateDefaultPriorityItems("100"); | ||
assertEquals(100, items.size()); | ||
for (int i = 0; i < 100; i++) { | ||
assertEquals(String.valueOf(i + 1), items.get(i).value); | ||
} | ||
} | ||
} |