jbpm5.1介绍(9)

Junit测试调用子流程

下面的示例中测试在程序中调用其它程序的子流程的示例,需要加载两个配置文件

需要指定属性

下面是调用的示例程序

public void testCallActivity() throws Exception {
		System.out.println("Loading process BPMN2-CallActivity.bpmn2");
		KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
				.newKnowledgeBuilder();
		kbuilder.add(ResourceFactory
				.newClassPathResource("junit/BPMN2-CallActivity.bpmn2"),
				ResourceType.BPMN2);
		kbuilder.add(
				ResourceFactory
						.newClassPathResource("junit/BPMN2-CallActivitySubProcess.bpmn2"),
				ResourceType.BPMN2);
		KnowledgeBase kbase = kbuilder.newKnowledgeBase();
		StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
		Map<String, Object> params = new HashMap<String, Object>();
		params.put("x", "oldValue");
		ProcessInstance processInstance = ksession.startProcess(
				"ParentProcess", params);
		assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);
		assertEquals("new value",
				((WorkflowProcessInstance) processInstance).getVariable("y"));
	}

 程序输出以下结果

Loading process BPMN2-CallActivity.bpmn2
我是子流程 subX=oldValue

证明子流程调用成功

 

Junit测试子流程执行过程中触发的事件

下面的示例中测试在子流程执行的过程中触发各种事件

流程如下图

在执行到hello1的时候变量x是没有值的,在执行hello2的时候赋值,在执行到hello3的时候变量能够输出值

测试程序如下:

public void testSubProcess() throws Exception {
		KnowledgeBase kbase = createKnowledgeBase("BPMN2-SubProcess.bpmn2");
		StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
		ksession.addEventListener(new DefaultProcessEventListener() {
			public void afterProcessStarted(ProcessStartedEvent event) {
				System.out.println("流程启动以后"+event);
			}

			public void beforeVariableChanged(ProcessVariableChangedEvent event) {
				System.out.println("变量改变之前"+event);
			}

			public void afterVariableChanged(ProcessVariableChangedEvent event) {
				System.out.println("变量改变以后"+event);
			}
		});
		ProcessInstance processInstance = ksession.startProcess("SubProcess");
		assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);
	}

 输出的结果如下:

Loading process BPMN2-SubProcess.bpmn2
x = null
变量改变之前==>[ProcessVariableChanged(id=2:x; instanceId=1:x; oldValue=null; newValue=Hello; processName=Minimal SubProcess; processId=SubProcess)]
变量改变以后==>[ProcessVariableChanged(id=2:x; instanceId=1:x; oldValue=null; newValue=Hello; processName=Minimal SubProcess; processId=SubProcess)]
x = Hello
Goodbye World
流程启动以后==>[ProcessStarted(name=Minimal SubProcess; id=SubProcess)]

 

Junit测试多流程循环

下面的示例中主要测试的是多流程循环的示例

界面流程如下


执行的测试程序如下:

public void testMultiInstanceLoopCharacteristicsProcess() throws Exception {
		KnowledgeBase kbase = createKnowledgeBase("BPMN2-MultiInstanceLoopCharacteristicsProcess.bpmn2");
		StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
		Map<String, Object> params = new HashMap<String, Object>();
		List<String> myList = new ArrayList<String>();
		myList.add("First Item");
		myList.add("Second Item");
		params.put("list", myList);
		ProcessInstance processInstance = ksession.startProcess(
				"MultiInstanceLoopCharacteristicsProcess", params);
		assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);
	}

 执行结果如下

Loading process BPMN2-MultiInstanceLoopCharacteristicsProcess.bpmn2
item = First Item
item = Second Item
Goodbye World

 

Junit测试异常事件处理流程

下面的示例中主要测试的是在执行到子流程中的异常的时候的处理流程

程序流程如下:


首先执行子流程中的内容,默认遇到错误,将触发错误事件,然后执行对应的事件,输出内容

测试程序如下:

public void testEscalationBoundaryEvent() throws Exception {
		KnowledgeBase kbase = createKnowledgeBase("BPMN2-EscalationBoundaryEvent.bpmn2");
		StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
		ProcessInstance processInstance = ksession
				.startProcess("EscalationBoundaryEvent");
		assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);
	}

 执行完结果如下所示:

Loading process BPMN2-EscalationBoundaryEvent.bpmn2
执行错误流程,触发事件 Escalation handled

 

Junit测试异常事件处理流程2

下面的示例中主要测试异常处理,同上例基本相同都是执行到错误结点的时候自动触发事件,但是加了一个选择分支

程序流程示例如图

测试程序如下所示

public void testEscalationBoundaryEventInterrupting() throws Exception {
		KnowledgeBase kbase = createKnowledgeBase("BPMN2-EscalationBoundaryEventInterrupting.bpmn2");
		StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
		ksession.getWorkItemManager().registerWorkItemHandler("MyTask",
				new DoNothingWorkItemHandler());
		ProcessInstance processInstance = ksession
				.startProcess("EscalationBoundaryEvent");
		assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);
		// TODO: check for cancellation of task
	}

 执行结果如下:

Loading process BPMN2-EscalationBoundaryEventInterrupting.bpmn2
Escalation handled

 

Junit测试定时器边界事件

下面的示例中主要测试定时器边界事件

程序流程如下所示:

在属性中设置超时时间是500ms,那么在下面的程序中设置让程序暂停一下执行,触发超时事件

public void testTimerBoundaryEventDuration() throws Exception {
		KnowledgeBase kbase = createKnowledgeBase("BPMN2-TimerBoundaryEventDuration.bpmn2");
		StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
		ksession.getWorkItemManager().registerWorkItemHandler("MyTask",
				new DoNothingWorkItemHandler());
		ProcessInstance processInstance = ksession
				.startProcess("TimerBoundaryEvent");
		assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
		Thread.sleep(1000);
		ksession = restoreSession(ksession, true);
		assertProcessInstanceCompleted(processInstance.getId(), ksession);
	}

 设置为1000,那么就将执行错误处理事件

执行的结果如下

Loading process BPMN2-TimerBoundaryEventDuration.bpmn2
Timer handled

 

Junit测试定时器超时处理

下面的两个示例和上面基本上没有区别,只是强调了一下超时处理的周期和边界事件

直接看示例程序吧

public void testTimerBoundaryEventCycle1() throws Exception {
		KnowledgeBase kbase = createKnowledgeBase("BPMN2-TimerBoundaryEventCycle1.bpmn2");
		StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
		ksession.getWorkItemManager().registerWorkItemHandler("MyTask",
				new DoNothingWorkItemHandler());
		ProcessInstance processInstance = ksession
				.startProcess("TimerBoundaryEvent");
		assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
		Thread.sleep(1000);
		ksession = restoreSession(ksession, true);
		assertProcessInstanceCompleted(processInstance.getId(), ksession);
	}

	public void testTimerBoundaryEventCycle2() throws Exception {
		KnowledgeBase kbase = createKnowledgeBase("BPMN2-TimerBoundaryEventCycle2.bpmn2");
		StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
		ksession.getWorkItemManager().registerWorkItemHandler("MyTask",
				new DoNothingWorkItemHandler());
		ProcessInstance processInstance = ksession
				.startProcess("TimerBoundaryEvent");
		assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
		Thread.sleep(1000);
		assertProcessInstanceActive(processInstance.getId(), ksession);
		Thread.sleep(1000);
		assertProcessInstanceActive(processInstance.getId(), ksession);
		ksession.abortProcessInstance(processInstance.getId());
		Thread.sleep(1000);
	}

 分别的执行结果如下:

1)同上示例基本相同

Loading process BPMN2-TimerBoundaryEventCycle1.bpmn2
Timer handled
2)执行两次超时事件

Loading process BPMN2-TimerBoundaryEventCycle2.bpmn2
Timer handled
Timer handled

posted @ 2011-11-15 08:34  skyme  阅读(2190)  评论(0编辑  收藏  举报