jbpm5.1介绍(9)Junit测试调用子流程下面的示例中测试在程序中调用其它程序的子流程的示例,需要加载两个配置文件和需要指定属性下面是调用的示例程序?publicvoidtestCallActivity()throwsException{System.out.println(LoadingprocessBPMN2-CallActivity.bpmn2);KnowledgeBuilderkbuilder=KnowledgeBuilderFactory.newKnowledgeBuilder();kbuilder.add(ResourceFactory.newClassPathResource(junit/BPMN2-CallActivity.bpmn2),ResourceType.BPMN2);kbuilder.add(ResourceFactory.newClassPathResource(junit/BPMN2-CallActivitySubProcess.bpmn2),ResourceType.BPMN2);KnowledgeBasekbase=kbuilder.newKnowledgeBase();StatefulKnowledgeSessionksession=createKnowledgeSession(kbase);MapString,Objectparams=newHashMapString,Object();params.put(x,oldValue);ProcessInstanceprocessInstance=ksession.startProcess(ParentProcess,params);assertTrue(processInstance.getState()==ProcessInstance.STATE_COMPLETED);assertEquals(newvalue,((WorkflowProcessInstance)processInstance).getVariable(y));}程序输出以下结果LoadingprocessBPMN2-CallActivity.bpmn2我是子流程subX=oldValue证明子流程调用成功Junit测试子流程执行过程中触发的事件下面的示例中测试在子流程执行的过程中触发各种事件流程如下图在执行到hello1的时候变量x是没有值的,在执行hello2的时候赋值,在执行到hello3的时候变量能够输出值测试程序如下:?publicvoidtestSubProcess()throwsException{KnowledgeBasekbase=createKnowledgeBase(BPMN2-SubProcess.bpmn2);StatefulKnowledgeSessionksession=createKnowledgeSession(kbase);ksession.addEventListener(newDefaultProcessEventListener(){publicvoidafterProcessStarted(ProcessStartedEventevent){System.out.println(流程启动以后+event);}publicvoidbeforeVariableChanged(ProcessVariableChangedEventevent){System.out.println(变量改变之前+event);}publicvoidafterVariableChanged(ProcessVariableChangedEventevent){System.out.println(变量改变以后+event);}});ProcessInstanceprocessInstance=ksession.startProcess(SubProcess);assertTrue(processInstance.getState()==ProcessInstance.STATE_COMPLETED);}输出的结果如下:LoadingprocessBPMN2-SubProcess.bpmn2x=null变量改变之前==[ProcessVariableChanged(id=2:x;instanceId=1:x;oldValue=null;newValue=Hello;processName=MinimalSubProcess;processId=SubProcess)]变量改变以后==[ProcessVariableChanged(id=2:x;instanceId=1:x;oldValue=null;newValue=Hello;processName=MinimalSubProcess;processId=SubProcess)]x=HelloGoodbyeWorld流程启动以后==[ProcessStarted(name=MinimalSubProcess;id=SubProcess)]Junit测试多流程循环下面的示例中主要测试的是多流程循环的示例界面流程如下执行的测试程序如下:?publicvoidtestMultiInstanceLoopCharacteristicsProcess()throwsException{KnowledgeBasekbase=createKnowledgeBase(BPMN2-MultiInstanceLoopCharacteristicsProcess.bpmn2);StatefulKnowledgeSessionksession=createKnowledgeSession(kbase);MapString,Objectparams=newHashMapString,Object();ListStringmyList=newArrayListString();myList.add(FirstItem);myList.add(SecondItem);params.put(list,myList);ProcessInstanceprocessInstance=ksession.startProcess(MultiInstanceLoopCharacteristicsProcess,params);assertTrue(processInstance.getState()==ProcessInstance.STATE_COMPLETED);}执行结果如下LoadingprocessBPMN2-MultiInstanceLoopCharacteristicsProcess.bpmn2item=FirstItemitem=SecondItemGoodbyeWorldJunit测试异常事件处理流程下面的示例中主要测试的是在执行到子流程中的异常的时候的处理流程程序流程如下:首先执行子流程中的内容,默认遇到错误,将触发错误事件,然后执行对应的事件,输出内容测试程序如下:?publicvoidtestEscalationBoundaryEvent()throwsException{KnowledgeBasekbase=createKnowledgeBase(BPMN2-EscalationBoundaryEvent.bpmn2);StatefulKnowledgeSessionksession=createKnowledgeSession(kbase);ProcessInstanceprocessInstance=ksession.startProcess(EscalationBoundaryEvent);assertTrue(processInstance.getState()==ProcessInstance.STATE_COMPLETED);}执行完结果如下所示:LoadingprocessBPMN2-EscalationBoundaryEvent.bpmn2执行错误流程,触发事件EscalationhandledJunit测试异常事件处理流程2下面的示例中主要测试异常处理,同上例基本相同都是执行到错误结点的时候自动触发事件,但是加了一个选择分支程序流程示例如图测试程序如下所示?publicvoidtestEscalationBoundaryEventInterrupting()throwsException{KnowledgeBasekbase=createKnowledgeBase(BPMN2-EscalationBoundaryEventInterrupting.bpmn2);StatefulKnowledgeSessionksession=createKnowledgeSession(kbase);ksession.getWorkItemManager().registerWorkItemHandler(MyTask,newDoNothingWorkItemHandler());ProcessInstanceprocessInstance=ksession.startProcess(EscalationBoundaryEvent);assertTrue(processInstance.getState()==ProcessInstance.STATE_COMPLETED);//TODO:checkforcancellationoftask}执行结果如下:LoadingprocessBPMN2-EscalationBoundaryEventInterrupting.bpmn2EscalationhandledJunit测试定时器边界事件下面的示例中主要测试定时器边界事件程序流程如下所示:在属性中设置超时时间是500ms,那么在下面的程序中设置让程序暂停一下执行,触发超时事件?publicvoidtestTimerBoundaryEventDuration()throwsException{KnowledgeBasekbase=createKnowledgeBase(BPMN2-TimerBoundaryEventDuration.bpmn2);StatefulKnowledgeSessionksession=createKnowledgeSession(kbase);ksession.getWorkItemManager().registerWorkItemHandler(MyTask,newDoNothingWorkItemHandler());ProcessInstanceprocessInstance=ksession.startProcess(TimerBoundaryEvent);assertTrue(processInstance.getState()==ProcessInstance.STATE_ACTIVE);Thread.sleep(1000);ksession=restoreSession(ksession,true);assertProcessInstanceCompleted(processInstance.getId(),ksession);}设置为1000,那么就将执行错误处理事件执行的结果如下LoadingprocessBPMN2-TimerBoundaryEventDuration.bpmn2TimerhandledJunit测试定时器超时处理下面的两个示例和上面基本上没有区别,只是强调了一下超时处理的周期和边界事件直接看示例程序吧?publicvoidtestTimerBoundaryEventCycle1()throwsException{KnowledgeBasekbase=createKnowledgeBase(BPMN2-TimerBoundaryEventCycle1.bpmn2);StatefulKnowledgeSessi