/**
* Test diversion with REROUTE events.
*/
@Test
public void diversionTestInGotoState2() {
final ListenerEventHistory leh = new ListenerEventHistory();
d.stateMachine.getEventAPI().addListener(leh,
StateMachineEvent.STATE_TRANSITION);
assertEquals(0, leh.getHistory().size());
assertFalse(d.gotoState.destination.isPresent());
assertFalse(d.gotoState.prevDestination.isPresent());
d.setRoute(asList(p1));
assertEquals(diversionIsAllowed, d.isDiversionAllowed());
assertEquals(Optional.absent(), d.newRoute);
tick(5, 6);
assertEquals(d.gotoState, d.stateMachine.getCurrentState());
assertEquals(1, leh.getHistory().size());
assertEquals(p1, d.gotoState.getDestination());
assertFalse(d.gotoState.prevDestination.isPresent());
@SuppressWarnings("unchecked")
final StateTransitionEvent<StateEvent, RouteFollowingVehicle> ev1 = ((StateTransitionEvent<StateEvent, RouteFollowingVehicle>) leh
.getHistory().get(0));
assertEquals(DefaultEvent.GOTO, ev1.event);
assertEquals(d.waitState, ev1.previousState);
assertEquals(d.gotoState, ev1.newState);
assertEquals(d.gotoState, d.stateMachine.getCurrentState());
assertEquals(p1, d.getRoute().iterator().next());
boolean fail = false;
try {
d.setRoute(asList(p2));
} catch (final IllegalArgumentException e) {
fail = true;
}
assertEquals(!diversionIsAllowed && !allowDelayedRouteChanges, fail);
tick(4, 5);
assertEquals(d.gotoState, d.stateMachine.getCurrentState());
if (diversionIsAllowed) {
assertEquals(2, leh.getHistory().size());
@SuppressWarnings("unchecked")
final StateTransitionEvent<StateEvent, RouteFollowingVehicle> ev2 = ((StateTransitionEvent<StateEvent, RouteFollowingVehicle>) leh
.getHistory().get(1));
assertEquals(DefaultEvent.REROUTE, ev2.event);
assertEquals(d.gotoState, ev2.previousState);
assertEquals(d.gotoState, ev2.newState);
assertEquals(p2, d.gotoState.getDestination());
assertEquals(p1, d.gotoState.getPreviousDestination());
}
else {
assertEquals(1, leh.getHistory().size());
assertEquals(p1, d.gotoState.getDestination());
assertFalse(d.gotoState.prevDestination.isPresent());
}
}