public static class TestLifecycleEventHandler implements LifecycleEventHandler {
static List<LifecycleEvent> eventList = new ArrayList<>();
@Override
public void onEvent(LifecycleEvent event) {
eventList.add(event);
}
}
@Test
public void test_lifecycle_event() throws VerificationException {
@LifecycleRegistry({ CustomerObject.class, TestLifecycleEventHandler.class })
@StateMachineBuilder
class Registry extends AbsStateMachineRegistry {
protected Registry() throws VerificationException {}
}
new Registry();
assertEquals(0, TestLifecycleEventHandler.eventList.size());
final CustomerObject customer = new CustomerObject();
assertEquals(0, TestLifecycleEventHandler.eventList.size());
customer.confirm();
{
assertEquals(1, TestLifecycleEventHandler.eventList.size());
LifecycleEvent lifecycleEvent = TestLifecycleEventHandler.eventList.get(TestLifecycleEventHandler.eventList.size() - 1);
assertLifecycleEvent(customer, lifecycleEvent, CustomerStateMachine.States.Draft.class,
CustomerStateMachine.States.Confirmed.ConfirmedStates.InService.class, CustomerStateMachine.Transitions.Confirm.class);
}
customer.suspend();
{
assertEquals(2, TestLifecycleEventHandler.eventList.size());
LifecycleEvent lifecycleEvent = TestLifecycleEventHandler.eventList.get(TestLifecycleEventHandler.eventList.size() - 1);
assertLifecycleEvent(customer, lifecycleEvent, CustomerStateMachine.States.Confirmed.ConfirmedStates.InService.class,
CustomerStateMachine.States.Confirmed.ConfirmedStates.ServiceSuspended.class,
CustomerStateMachine.States.Confirmed.Transitions.Suspend.class);
}
customer.resume();
{
assertEquals(3, TestLifecycleEventHandler.eventList.size());
LifecycleEvent lifecycleEvent = TestLifecycleEventHandler.eventList.get(TestLifecycleEventHandler.eventList.size() - 1);
assertLifecycleEvent(customer, lifecycleEvent, CustomerStateMachine.States.Confirmed.ConfirmedStates.ServiceSuspended.class,
CustomerStateMachine.States.Confirmed.ConfirmedStates.InService.class, CustomerStateMachine.States.Confirmed.Transitions.Resume.class);
}
customer.terminateService();
{
assertEquals(4, TestLifecycleEventHandler.eventList.size());
LifecycleEvent lifecycleEvent = TestLifecycleEventHandler.eventList.get(TestLifecycleEventHandler.eventList.size() - 1);
assertLifecycleEvent(customer, lifecycleEvent, CustomerStateMachine.States.Confirmed.ConfirmedStates.InService.class,
CustomerStateMachine.States.Confirmed.ConfirmedStates.ServiceExpired.class,
CustomerStateMachine.States.Confirmed.Transitions.TerminateService.class);
}
customer.renew();
{
assertEquals(5, TestLifecycleEventHandler.eventList.size());
LifecycleEvent lifecycleEvent = TestLifecycleEventHandler.eventList.get(TestLifecycleEventHandler.eventList.size() - 1);
assertLifecycleEvent(customer, lifecycleEvent, CustomerStateMachine.States.Confirmed.ConfirmedStates.ServiceExpired.class,
CustomerStateMachine.States.Confirmed.ConfirmedStates.InService.class, CustomerStateMachine.States.Confirmed.Transitions.Renew.class);
}
customer.terminateService();
{
assertEquals(6, TestLifecycleEventHandler.eventList.size());
LifecycleEvent lifecycleEvent = TestLifecycleEventHandler.eventList.get(TestLifecycleEventHandler.eventList.size() - 1);
assertLifecycleEvent(customer, lifecycleEvent, CustomerStateMachine.States.Confirmed.ConfirmedStates.InService.class,
CustomerStateMachine.States.Confirmed.ConfirmedStates.ServiceExpired.class,
CustomerStateMachine.States.Confirmed.Transitions.TerminateService.class);
}
customer.abandon();
{
assertEquals(7, TestLifecycleEventHandler.eventList.size());
LifecycleEvent lifecycleEvent = TestLifecycleEventHandler.eventList.get(TestLifecycleEventHandler.eventList.size() - 1);
assertLifecycleEvent(customer, lifecycleEvent, CustomerStateMachine.States.Confirmed.ConfirmedStates.ServiceExpired.class,
CustomerStateMachine.States.Recycled.class, CustomerStateMachine.States.Confirmed.Transitions.Abandon.class);
}
customer.putBack();
{
assertEquals(8, TestLifecycleEventHandler.eventList.size());
LifecycleEvent lifecycleEvent = TestLifecycleEventHandler.eventList.get(TestLifecycleEventHandler.eventList.size() - 1);
assertLifecycleEvent(customer, lifecycleEvent, CustomerStateMachine.States.Recycled.class, CustomerStateMachine.States.Draft.class,
CustomerStateMachine.Transitions.PutBack.class);
}
try {
customer.renew();
} catch (LifecycleException e) {
assertEquals(8, TestLifecycleEventHandler.eventList.size());
}
}
private void assertLifecycleEvent(CustomerObject reactiveObject, LifecycleEvent lifecycleEvent, Class<?> fromClass, Class<?> toClass, Class<?> transition) {
assertEquals(reactiveObject, lifecycleEvent.getReactiveObject());
assertEquals(fromClass.getSimpleName(), lifecycleEvent.fromState());
assertEquals(toClass.getSimpleName(), lifecycleEvent.toState());
assertEquals(transition.getSimpleName(), lifecycleEvent.transition());
assertEquals(TransitionTypeEnum.Common, lifecycleEvent.transitionType());
}
生命周期组件框架:生命周期描述语言——生命周期事件示例
最新推荐文章于 2024-08-23 00:00:00 发布