jbpm5.1介绍(10)

Junit测试异常事件触发

下面的示例中测试在程序中触发异常事件的流程,流程如下

测试程序

public void testTimerBoundaryEventInterrupting() throws Exception {
		KnowledgeBase kbase = createKnowledgeBase("BPMN2-TimerBoundaryEventInterrupting.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);
		System.out.println("Firing timer");
		ksession.fireAllRules();
		assertProcessInstanceCompleted(processInstance.getId(), ksession);
	}

 结果如下

Loading process BPMN2-TimerBoundaryEventInterrupting.bpmn2
Timer handled
Firing timer

 

Junit测试根据规则执行子流程

下面的示例中测试在程序中加入固定规则,然后如果发现符合规则那么向下执行子流程

流程界面如下

规则文件如下

package org.drools.bpmn2

import org.drools.runtime.process.ProcessContext

rule "Auto-activate Hello1" ruleflow-group "Hello"
  when
  then
    System.out.println(drools);
    System.out.println(drools.getContext(ProcessContext.class));
    System.out.println(drools.getContext(ProcessContext.class).getProcessInstance());
    drools.getContext(ProcessContext.class).getProcessInstance().signalEvent("Hello1", null);
end

 测试程序

public void testAdHocSubProcess() throws Exception {
		System.out.println("Loading process BPMN2-AdHocSubProcess.bpmn2");
		KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
				.newKnowledgeBuilder();
		kbuilder.add(ResourceFactory
				.newClassPathResource("junit/BPMN2-AdHocSubProcess.bpmn2"),
				ResourceType.BPMN2);
		kbuilder.add(ResourceFactory
				.newClassPathResource("junit/BPMN2-AdHocSubProcess.drl"),
				ResourceType.DRL);
		KnowledgeBase kbase = kbuilder.newKnowledgeBase();
		StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
		TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
		ksession.getWorkItemManager().registerWorkItemHandler("Human Task",
				workItemHandler);
		ProcessInstance processInstance = ksession
				.startProcess("AdHocSubProcess");
		assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
		WorkItem workItem = workItemHandler.getWorkItem();
		assertNull(workItem);
		ksession = restoreSession(ksession, true);
		ksession.getWorkItemManager().registerWorkItemHandler("Human Task",
				workItemHandler);
		ksession.fireAllRules();
		System.out.println("Signaling Hello2");
		ksession.signalEvent("Hello2", null, processInstance.getId());
		workItem = workItemHandler.getWorkItem();
		assertNotNull(workItem);
		ksession = restoreSession(ksession, true);
		ksession.getWorkItemManager().registerWorkItemHandler("Human Task",
				workItemHandler);
		ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
	}

 执行结果如下

Loading process BPMN2-AdHocSubProcess.bpmn2
org.drools.base.DefaultKnowledgeHelper@1bdb58
org.drools.spi.ProcessContext@1568654
WorkflowProcessInstance1 [processId=AdHocSubProcess,state=1]
Hello World 1
Signaling Hello2
Hello World 2
执行test workitemWorkItem 1 [name=Human Task, state=0, processInstanceId=1, parameters{}]

 

Junit测试根据规则执行子流程并自动完成

下面的示例中测试在程序中如下图

执行流程如下,执行完成之后自动完成并且向下走

测试程序

public void testAdHocSubProcessAutoComplete() throws Exception {
		System.out
				.println("Loading process BPMN2-AdHocSubProcessAutoComplete.bpmn2");
		KnowledgeBuilder kbuilder = KnowledgeBuilderFactory
				.newKnowledgeBuilder();
		kbuilder.add(
				ResourceFactory
						.newClassPathResource("junit/BPMN2-AdHocSubProcessAutoComplete.bpmn2"),
				ResourceType.BPMN2);
		kbuilder.add(ResourceFactory
				.newClassPathResource("junit/BPMN2-AdHocSubProcess.drl"),
				ResourceType.DRL);
		KnowledgeBase kbase = kbuilder.newKnowledgeBase();
		StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
		TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
		ksession.getWorkItemManager().registerWorkItemHandler("Human Task",
				workItemHandler);
		ProcessInstance processInstance = ksession
				.startProcess("AdHocSubProcess");
		assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
		WorkItem workItem = workItemHandler.getWorkItem();
		assertNull(workItem);
		ksession = restoreSession(ksession, true);
		ksession.getWorkItemManager().registerWorkItemHandler("Human Task",
				workItemHandler);
		ksession.fireAllRules();
		workItem = workItemHandler.getWorkItem();
		assertNotNull(workItem);
		ksession = restoreSession(ksession, true);
		ksession.getWorkItemManager().registerWorkItemHandler("Human Task",
				workItemHandler);
		ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
		assertProcessInstanceCompleted(processInstance.getId(), ksession);
	}

 执行结果

Loading process BPMN2-AdHocSubProcessAutoComplete.bpmn2
org.drools.base.DefaultKnowledgeHelper@6833f2
org.drools.spi.ProcessContext@1bdb58
WorkflowProcessInstance1 [processId=AdHocSubProcess,state=1]
执行test workitemWorkItem 1 [name=Human Task, state=0, processInstanceId=1, parameters{}]
Goodbye World

 

Junit测试触发任务

下面的示例中测试在程序中执行并且触发任务

流程如下

测试程序

public void testAdHocProcess() throws Exception {
		KnowledgeBase kbase = createKnowledgeBase("BPMN2-AdHocProcess.bpmn2");
		StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
		ProcessInstance processInstance = ksession.startProcess("AdHocProcess");
		assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
		ksession = restoreSession(ksession, true);
		ksession.getWorkItemManager().registerWorkItemHandler("Human Task",
				new DoNothingWorkItemHandler());
		System.out.println("Triggering node");
		ksession.signalEvent("Task1", null, processInstance.getId());
		assertProcessInstanceActive(processInstance.getId(), ksession);
		ksession.signalEvent("User1", null, processInstance.getId());
		assertProcessInstanceActive(processInstance.getId(), ksession);
		ksession.insert(new Person());
		ksession.signalEvent("Task3", null, processInstance.getId());
		assertProcessInstanceCompleted(processInstance.getId(), ksession);
	}

 测试结果

Loading process BPMN2-AdHocProcess.bpmn2
Triggering node
Task1
Task3

 

Junit测试获取异常事件

下面的示例中测试在程序中得到异常事件

流程如图

程序中触发事件

public void testIntermediateCatchEventSignal() throws Exception {
		KnowledgeBase kbase = createKnowledgeBase("BPMN2-IntermediateCatchEventSignal.bpmn2");
		StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
		ksession.getWorkItemManager().registerWorkItemHandler("Human Task",
				new DoNothingWorkItemHandler());
		ProcessInstance processInstance = ksession
				.startProcess("IntermediateCatchEvent");
		assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
		ksession = restoreSession(ksession, true);
		// now signal process instance
		ksession.signalEvent("MyMessage", "SomeValue", processInstance.getId());
		assertProcessInstanceCompleted(processInstance.getId(), ksession);
	}

 执行结果

Loading process BPMN2-AdHocProcess.bpmn2
Triggering node
Task1
Task3

 

Junit测试获取事件消息

下面的示例中测试在程序中得到事件消息

测试程序

public void testIntermediateCatchEventMessage() throws Exception {
		KnowledgeBase kbase = createKnowledgeBase("BPMN2-IntermediateCatchEventMessage.bpmn2");
		StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
		ksession.getWorkItemManager().registerWorkItemHandler("Human Task",
				new DoNothingWorkItemHandler());
		ProcessInstance processInstance = ksession
				.startProcess("IntermediateCatchEvent");
		assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
		ksession = restoreSession(ksession, true);
		// now signal process instance
		ksession.signalEvent("Message-HelloMessage", "SomeValue",
				processInstance.getId());
		assertProcessInstanceCompleted(processInstance.getId(), ksession);
	}

 执行结果

Loading process BPMN2-IntermediateCatchEventMessage.bpmn2
SomeValue

 

Junit测试超时事件

下面的示例中测试在流程中超时触发事件

流程如下

测试程序如下

public void testIntermediateCatchEventTimerDuration() throws Exception {
		KnowledgeBase kbase = createKnowledgeBase("BPMN2-IntermediateCatchEventTimerDuration.bpmn2");
		StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
		ksession.getWorkItemManager().registerWorkItemHandler("Human Task",
				new DoNothingWorkItemHandler());
		ProcessInstance processInstance = ksession
				.startProcess("IntermediateCatchEvent");
		assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
		// now wait for 1 second for timer to trigger
		Thread.sleep(1000);
		ksession = restoreSession(ksession, true);
		ksession.getWorkItemManager().registerWorkItemHandler("Human Task",
				new DoNothingWorkItemHandler());
		ksession.fireAllRules();
		assertProcessInstanceCompleted(processInstance.getId(), ksession);
	}

 超时触发事件,结果如下

Loading process BPMN2-IntermediateCatchEventTimerDuration.bpmn2
Timer triggered

 

Junit测试超时事件1

下面的示例中测试在流程中超时触发事件,基本操作同上

示例如果同上,不再贴出


Junit测试超时事件2

下面的示例中测试在流程中执行两次超时事件并且都触发对应的事件执行

测试程序

public void testIntermediateCatchEventTimerCycle2() throws Exception {
		KnowledgeBase kbase = createKnowledgeBase("BPMN2-IntermediateCatchEventTimerCycle2.bpmn2");
		StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
		ksession.getWorkItemManager().registerWorkItemHandler("Human Task",
				new DoNothingWorkItemHandler());
		ProcessInstance processInstance = ksession
				.startProcess("IntermediateCatchEvent");
		assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
		// now wait for 1 second for timer to trigger
		Thread.sleep(1000);
		assertProcessInstanceActive(processInstance.getId(), ksession);
		Thread.sleep(1000);
		assertProcessInstanceActive(processInstance.getId(), ksession);
		ksession.abortProcessInstance(processInstance.getId());
		Thread.sleep(1000);
	}

 执行结果

Loading process BPMN2-IntermediateCatchEventTimerCycle2.bpmn2
Timer triggered
Timer triggered

 

Junit测试条件触发事件执行

下面的示例中测试在流程中当条件等于jack时触发事件执行

流程如下

测试程序

public void testIntermediateCatchEventCondition() throws Exception {
		KnowledgeBase kbase = createKnowledgeBase("BPMN2-IntermediateCatchEventCondition.bpmn2");
		StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
		ProcessInstance processInstance = ksession
				.startProcess("IntermediateCatchEvent");
		assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
		ksession = restoreSession(ksession, true);
		// now activate condition
		Person person = new Person();
		person.setName("Jack");
		ksession.insert(person);
		assertProcessInstanceCompleted(processInstance.getId(), ksession);
	}

 执行结果

Loading process BPMN2-IntermediateCatchEventCondition.bpmn2
Condition triggered

 

Junit测试错误事件

下面的示例中测试错误事件的执行

测试程序

public void testErrorEndEventProcess() throws Exception {
		KnowledgeBase kbase = createKnowledgeBase("BPMN2-ErrorEndEvent.bpmn2");
		StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
		ProcessInstance processInstance = ksession
				.startProcess("ErrorEndEvent");
		assertProcessInstanceAborted(processInstance.getId(), ksession);
	}

 执行结果

Loading process BPMN2-ErrorEndEvent.bpmn2

执行到错误流程流程自动结束

posted @ 2011-11-15 10:01  skyme  阅读(1543)  评论(1编辑  收藏  举报