/*
* Copyright 2014 JBoss Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.optaplanner.core.impl.constructionheuristic.placer.entity;
import java.util.Iterator;
import org.junit.Test;
import org.optaplanner.core.impl.constructionheuristic.placer.Placement;
import org.optaplanner.core.impl.constructionheuristic.placer.PooledEntityPlacer;
import org.optaplanner.core.impl.heuristic.move.DummyMove;
import org.optaplanner.core.impl.heuristic.selector.SelectorTestUtils;
import org.optaplanner.core.impl.heuristic.selector.move.MoveSelector;
import org.optaplanner.core.impl.phase.scope.AbstractPhaseScope;
import org.optaplanner.core.impl.phase.scope.AbstractStepScope;
import org.optaplanner.core.impl.solver.scope.DefaultSolverScope;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.*;
import static org.optaplanner.core.impl.testdata.util.PlannerAssert.*;
public class PooledEntityPlacerTest {
@Test
public void oneMoveSelector() {
MoveSelector moveSelector = SelectorTestUtils.mockMoveSelector(DummyMove.class,
new DummyMove("a1"), new DummyMove("a2"), new DummyMove("b1"));
PooledEntityPlacer placer = new PooledEntityPlacer(moveSelector);
DefaultSolverScope solverScope = mock(DefaultSolverScope.class);
placer.solvingStarted(solverScope);
AbstractPhaseScope phaseScopeA = mock(AbstractPhaseScope.class);
when(phaseScopeA.getSolverScope()).thenReturn(solverScope);
placer.phaseStarted(phaseScopeA);
Iterator<Placement> placementIterator = placer.iterator();
assertTrue(placementIterator.hasNext());
AbstractStepScope stepScopeA1 = mock(AbstractStepScope.class);
when(stepScopeA1.getPhaseScope()).thenReturn(phaseScopeA);
placer.stepStarted(stepScopeA1);
assertAllCodesOfIterator(placementIterator.next().iterator(),
"a1", "a2", "b1");
placer.stepEnded(stepScopeA1);
assertTrue(placementIterator.hasNext());
AbstractStepScope stepScopeA2 = mock(AbstractStepScope.class);
when(stepScopeA2.getPhaseScope()).thenReturn(phaseScopeA);
placer.stepStarted(stepScopeA2);
assertAllCodesOfIterator(placementIterator.next().iterator(),
"a1", "a2", "b1");
placer.stepEnded(stepScopeA2);
assertTrue(placementIterator.hasNext());
AbstractStepScope stepScopeA3 = mock(AbstractStepScope.class);
when(stepScopeA3.getPhaseScope()).thenReturn(phaseScopeA);
placer.stepStarted(stepScopeA3);
assertAllCodesOfIterator(placementIterator.next().iterator(),
"a1", "a2", "b1");
placer.stepEnded(stepScopeA3);
placer.phaseEnded(phaseScopeA);
AbstractPhaseScope phaseScopeB = mock(AbstractPhaseScope.class);
when(phaseScopeB.getSolverScope()).thenReturn(solverScope);
placer.phaseStarted(phaseScopeB);
placementIterator = placer.iterator();
assertTrue(placementIterator.hasNext());
AbstractStepScope stepScopeB1 = mock(AbstractStepScope.class);
when(stepScopeB1.getPhaseScope()).thenReturn(phaseScopeB);
placer.stepStarted(stepScopeB1);
assertAllCodesOfIterator(placementIterator.next().iterator(),
"a1", "a2", "b1");
placer.stepEnded(stepScopeB1);
placer.phaseEnded(phaseScopeB);
placer.solvingEnded(solverScope);
verifyPhaseLifecycle(moveSelector, 1, 2, 4);
}
}