生命周期组件框架:生命周期描述语言——生命周期事件示例_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > 生命周期组件框架:生命周期描述语言——生命周期事件示例

生命周期组件框架:生命周期描述语言——生命周期事件示例

 2013/11/24 12:26:12  barryzhong  程序员俱乐部  我要评论(0)
  • 摘要:publicstaticclassTestLifecycleEventHandlerimplementsLifecycleEventHandler{staticList<LifecycleEvent>eventList=newArrayList<>();@OverridepublicvoidonEvent(LifecycleEventevent){eventList.add(event);}}@Testpublicvoidtest_lifecycle_event(
  • 标签:事件
class="java">   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());
    }

?

发表评论
用户名: 匿名