public void cacheTypeJustInTime() {
runCacheType(SelectionCacheType.JUST_IN_TIME, 5);
}
public void runCacheType(SelectionCacheType cacheType, int timesCalled) {
MoveSelector childMoveSelector = SelectorTestUtils.mockMoveSelector(DummyMove.class,
new DummyMove("jan"), new DummyMove("feb"), new DummyMove("mar"),
new DummyMove("apr"), new DummyMove("may"), new DummyMove("jun"));
SelectionSorter<DummyMove> sorter = new SelectionSorter<DummyMove>() {
public void sort(ScoreDirector scoreDirector, List<DummyMove> selectionList) {
Collections.sort(selectionList, new Comparator<DummyMove>() {
public int compare(DummyMove a, DummyMove b) {
return a.getCode().compareTo(b.getCode());
}
});
}
};
MoveSelector moveSelector = new SortingMoveSelector(childMoveSelector, cacheType, sorter);
DefaultSolverScope solverScope = mock(DefaultSolverScope.class);
moveSelector.solvingStarted(solverScope);
AbstractPhaseScope phaseScopeA = mock(AbstractPhaseScope.class);
when(phaseScopeA.getSolverScope()).thenReturn(solverScope);
moveSelector.phaseStarted(phaseScopeA);
AbstractStepScope stepScopeA1 = mock(AbstractStepScope.class);
when(stepScopeA1.getPhaseScope()).thenReturn(phaseScopeA);
moveSelector.stepStarted(stepScopeA1);
assertAllCodesOfMoveSelector(moveSelector, "apr", "feb", "jan", "jun", "mar", "may");
moveSelector.stepEnded(stepScopeA1);
AbstractStepScope stepScopeA2 = mock(AbstractStepScope.class);
when(stepScopeA2.getPhaseScope()).thenReturn(phaseScopeA);
moveSelector.stepStarted(stepScopeA2);
assertAllCodesOfMoveSelector(moveSelector, "apr", "feb", "jan", "jun", "mar", "may");
moveSelector.stepEnded(stepScopeA2);
moveSelector.phaseEnded(phaseScopeA);
AbstractPhaseScope phaseScopeB = mock(AbstractPhaseScope.class);
when(phaseScopeB.getSolverScope()).thenReturn(solverScope);
moveSelector.phaseStarted(phaseScopeB);
AbstractStepScope stepScopeB1 = mock(AbstractStepScope.class);
when(stepScopeB1.getPhaseScope()).thenReturn(phaseScopeB);
moveSelector.stepStarted(stepScopeB1);
assertAllCodesOfMoveSelector(moveSelector, "apr", "feb", "jan", "jun", "mar", "may");
moveSelector.stepEnded(stepScopeB1);
AbstractStepScope stepScopeB2 = mock(AbstractStepScope.class);
when(stepScopeB2.getPhaseScope()).thenReturn(phaseScopeB);
moveSelector.stepStarted(stepScopeB2);
assertAllCodesOfMoveSelector(moveSelector, "apr", "feb", "jan", "jun", "mar", "may");
moveSelector.stepEnded(stepScopeB2);
AbstractStepScope stepScopeB3 = mock(AbstractStepScope.class);
when(stepScopeB3.getPhaseScope()).thenReturn(phaseScopeB);
moveSelector.stepStarted(stepScopeB3);
assertAllCodesOfMoveSelector(moveSelector, "apr", "feb", "jan", "jun", "mar", "may");
moveSelector.stepEnded(stepScopeB3);
moveSelector.phaseEnded(phaseScopeB);
moveSelector.solvingEnded(solverScope);
verifyPhaseLifecycle(childMoveSelector, 1, 2, 5);
verify(childMoveSelector, times(timesCalled)).iterator();
verify(childMoveSelector, times(timesCalled)).getSize();
}