public class ListenerTests extends AbstractStateMachineTests {
@Test
public void testStateEvents() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
assertTrue(ctx.containsBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE));
@SuppressWarnings("unchecked")
EnumStateMachine<TestStates,TestEvents> machine =
ctx.getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, EnumStateMachine.class);
TestStateMachineListener listener = new TestStateMachineListener();
machine.addStateListener(listener);
assertThat(machine, notNullValue());
machine.sendEvent(MessageBuilder.withPayload(TestEvents.E1).setHeader("foo", "jee1").build());
assertThat(listener.states.size(), is(1));
assertThat(listener.states.get(0).from.getId(), is(TestStates.S1));
assertThat(listener.states.get(0).to.getId(), is(TestStates.S2));
machine.sendEvent(MessageBuilder.withPayload(TestEvents.E2).setHeader("foo", "jee2").build());
assertThat(listener.states.size(), is(2));
assertThat(listener.states.get(1).from.getId(), is(TestStates.S2));
assertThat(listener.states.get(1).to.getId(), is(TestStates.S3));
machine.sendEvent(MessageBuilder.withPayload(TestEvents.E4).setHeader("foo", "jee2").build());
assertThat(listener.states.size(), is(2));
ctx.close();
}