jbpm5.1介绍(9)

简介: Junit测试调用子流程 下面的示例中测试在程序中调用其它程序的子流程的示例,需要加载两个配置文件 和 需要指定属性 下面是调用的示例程序 public void testCallActivity() throws Exception { System.

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

目录
相关文章
|
Java 应用服务中间件 数据库连接
|
应用服务中间件 Android开发 Java
安装JBpm
原文 http://www.cnblogs.com/default/archive/2012/02/28/2370673.html   自动安装(ant start.demo)太慢,所以手动安装。
884 0
|
API
jBPM 5 的点滴
发布地址 http://sourceforge.net/projects/jbpm/files/jBPM%205/  http://www.jboss.org/jbpm/documentation   jBPM5引入了新的API、新的工具并支持BPMN 2.
1029 0
|
测试技术
jbpm5.1介绍(8)
Junit测试或流程 下面的示例中使用的是或流程,看如下流程图 判断节点的值是大于0,大于10还是大于20 看测试程序 public void testInclusiveSplit() throws Exception { KnowledgeBase kbase = createKnowledgeBase("BPMN2-InclusiveSplit.
743 0
|
JSON Java 数据格式
jbpm5.1介绍(11)
Jbpm-gwt-console源码编译 从svn下载,svn的下载地址是 http://anonsvn.jboss.org/repos/soag/bpm-console/tags/bpm-console-2.
911 0
jbpm5.1介绍(5)
看几个jbpm5中带的示例程序吧,包括了很多我们在日常生活中的场景 循环示例 本示例是一个在外部传入的变量,通过传入的变量来判断循环次数的演示程序,看一下流程定义的内容 如图: 初始化的时候设置变量i的值为0,然后进入流程结点,选择的是XOR,就是异或的意思,在循环中输出变量i的值并且加1, 在后面的选择节点上判断变量i的值,如果小于count则循环继续,如果大于i那么就进行完成结点,输出结点中的内容。
751 0
|
存储 XML Java
jbpm5.1介绍(3)
在您好的应用程序中使用一个新的流程 流程处理  (1)你需要建立一个知识库,其中包含过程定义 KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.
1084 0