首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 开源软件 >

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

2013-11-26 
生命周期组件框架:生命周期描述语言——生命周期事件示例public static class TestLifecycleEventHandler im

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

   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());    }

?前文:生命周期组件框架——关系型状态及服务

热点排行