Junit测试评估流程
评估流程的界面如下:
这个示例里边用到了Script Task,Service Task和User Task
Log执行记录日志的功能,打印出一段信息
RegisterRequest调用外部预定义的程序或者其它引擎的程序
Two调用人工任务
看一下调用的测试程序
public void testEvaluationProcess() throws Exception {
KnowledgeBase kbase = createKnowledgeBase("BPMN2-EvaluationProcess.bpmn2");
StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", new SystemOutWorkItemHandler());
ksession.getWorkItemManager().registerWorkItemHandler("RegisterRequest", new SystemOutWorkItemHandler());
Map< String , Object> params = new HashMap< String , Object>();
params.put("employee", "UserId-12345");
ProcessInstance processInstance = ksession.startProcess("Evaluation", params);
assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);
}
|
在程序中注册工作处理程序
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", new SystemOutWorkItemHandler());
ksession.getWorkItemManager().registerWorkItemHandler("RegisterRequest", new SystemOutWorkItemHandler());
看一下SystemOutWorkItemHandler中的内容
public class SystemOutWorkItemHandler implements WorkItemHandler {
public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
System.out.println("Executing work item " + workItem);
manager.completeWorkItem(workItem.getId(), null);
}
public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
System.out.println("Aborting work item " + workItem);
manager.abortWorkItem(workItem.getId());
}
}
|
看一下WorkItemHandler,这个接口
有两个方法,一个是要执行的工作任务,一个是忽略的工作任务
运行junit测试,会发现输出了
Loading process BPMN2-EvaluationProcess.bpmn2
Processing evaluation for employee UserId-12345
Executing work item WorkItem 1 [name=RegisterRequest, state=0, processInstanceId=1, parameters{employeeId=UserId-12345}]
Executing work item WorkItem 2 [name=Human Task, state=0, processInstanceId=1, parameters{}]
先是执行RegisterRequest这个里边的executeWorkItem方法
然后执行了Human Task里的executeWorkItem方法
最后使用
assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);
判断状态完成,执行成功
Junit测试评估流程2
评估流程的界面如下,同样的流程,打开BPMN2-EvaluationProcess2.bpmn2文件
同前边的示例一下,看一下测试2的示例程序
public void testEvaluationProcess2() throws Exception {
KnowledgeBase kbase = createKnowledgeBase("BPMN2-EvaluationProcess2.bpmn2");
StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", new SystemOutWorkItemHandler());
Map< String , Object> params = new HashMap< String , Object>();
params.put("employee", "UserId-12345");
ProcessInstance processInstance = ksession.startProcess("com.sample.evaluation", params);
assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);
}
|
执行完成后的结果如下:
Loading process BPMN2-EvaluationProcess2.bpmn2
Executing work item WorkItem 1 [name=Human Task, state=0, processInstanceId=1, parameters{ActorId=UserId-12345, Comment=You need to perform a self evaluation, Skippable=false, Content=, TaskName=Performance Evaluation, Priority=1}]
Executing work item WorkItem 2 [name=Human Task, state=0, processInstanceId=1, parameters{ActorId=john, Comment=You need to perform an evaluation for employee UserId-12345, Skippable=false, Content=, TaskName=Performance Evaluation, Priority=1}]
Executing work item WorkItem 3 [name=Human Task, state=0, processInstanceId=1, parameters{ActorId=mary, Comment=You need to perform an evaluation for employee UserId-12345, Skippable=false, Content=, TaskName=Performance Evaluation, Priority=1}]
每个人的工作任务自动向下执行。
Junit测试评估流程3
评估流程的界面如下,同第一个是一样的
看一下示例程序
public void testEvaluationProcess3() throws Exception {
KnowledgeBase kbase = createKnowledgeBase("BPMN2-EvaluationProcess3.bpmn2");
StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", new SystemOutWorkItemHandler());
ksession.getWorkItemManager().registerWorkItemHandler("RegisterRequest", new SystemOutWorkItemHandler());
Map< String , Object> params = new HashMap< String , Object>();
params.put("employee", "john2");
ProcessInstance processInstance = ksession.startProcess("Evaluation", params);
assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);
}
|
执行之后看结果
Loading process BPMN2-EvaluationProcess3.bpmn2
Processing evaluation for employee john2
Executing work item WorkItem 1 [name=Human Task, state=0, processInstanceId=1, parameters{}]
可以看到只执行了Human Task,默认情况下注册进来的多个Handler如果不指定的话会默认执行Human Task,如果指定了就先指定执行的,如第一个示例再执行Human Task
Junit测试用户任务
接下来的示例是测试用户任务的示例,打开BPMN2-UserTask.bpmn2文件,流程定义文件如下
指定了ActorId为john
看测试用例程序
public void testUserTask() throws Exception {
KnowledgeBase kbase = createKnowledgeBase("BPMN2-UserTask.bpmn2");
StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
ksession.getWorkItemManager().registerWorkItemHandler("Human Task", workItemHandler);
ProcessInstance processInstance = ksession.startProcess("UserTask");
assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
ksession = restoreSession(ksession, true);
WorkItem workItem = workItemHandler.getWorkItem();
assertNotNull(workItem);
assertEquals("john", workItem.getParameter("ActorId"));
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
assertProcessInstanceCompleted(processInstance.getId(), ksession);
}
|
先定义了一个TestWorkItemHandler,然后将这个handler定义为人工任务
然后判断节点状态是否是激活状态
然后判断流程中执行的是john在执行这个任务并且完成,最后判断流程完成
TestWorkItemHandler中的内容如下:
public static class TestWorkItemHandler implements WorkItemHandler {
private List< WorkItem > workItems = new ArrayList< WorkItem >();
public void executeWorkItem(WorkItem workItem, WorkItemManager manager) {
workItems.add(workItem);
}
public void abortWorkItem(WorkItem workItem, WorkItemManager manager) {
}
public WorkItem getWorkItem() {
System.out.println("执行test workitem");
if (workItems.size() == 0) {
return null;
}
if (workItems.size() == 1) {
WorkItem result = workItems.get(0);
this.workItems.clear();
return result;
} else {
throw new IllegalArgumentException("More than one work item active");
}
}
public List< WorkItem > getWorkItems() {
List< WorkItem > result = new ArrayList< WorkItem >(workItems);
workItems.clear();
return result;
}
}
|
执行结果如下显示
Loading process BPMN2-UserTask.bpmn2
执行test workitem
Junit测试泳道
接下来的示例是一个测试泳道的例子,泳道的概念可以理解成一个角色或者一个组,就是相当于一组人
看流程图
定义了泳道“MyLane”
在hello上指定了ActorId是john,在Goodbye上没有指定人
看流程示例
public void testLane() throws Exception {
KnowledgeBase kbase = createKnowledgeBase("BPMN2-Lane.bpmn2");
StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
TestWorkItemHandler workItemHandler = new TestWorkItemHandler();
ksession.getWorkItemManager().registerWorkItemHandler("Human Task",
workItemHandler);
ProcessInstance processInstance = ksession.startProcess("UserTask");
assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
ksession = restoreSession(ksession, true);
ksession.getWorkItemManager().registerWorkItemHandler("Human Task",
workItemHandler);
WorkItem workItem = workItemHandler.getWorkItem();
assertNotNull(workItem);
assertEquals("john", workItem.getParameter("ActorId"));
Map< String , Object> results = new HashMap< String , Object>();
results.put("ActorId", "mary");
ksession.getWorkItemManager().completeWorkItem(workItem.getId(),
results);
ksession = restoreSession(ksession, true);
ksession.getWorkItemManager().registerWorkItemHandler("Human Task",
workItemHandler);
workItem = workItemHandler.getWorkItem();
assertNotNull(workItem);
assertEquals("mary", workItem.getParameter("ActorId"));
ksession.getWorkItemManager().completeWorkItem(workItem.getId(), null);
assertProcessInstanceCompleted(processInstance.getId(), ksession);
}
|
定义任务并且执行,并且可以看到第一个用户任务的执行人是john,然后向里传入了第二个用户,mary
这个用户将被赋到泳道MyLane上
然后在看执行用户变成了mary,最后流程结束
看输出结果
Loading process BPMN2-Lane.bpmn2
执行test workitemWorkItem 1 [name=Human Task, state=0, processInstanceId=1, parameters{ActorId=john}]
执行test workitemWorkItem 2 [name=Human Task, state=0, processInstanceId=1, parameters{ActorId=mary}]
Junit测试异或流程
接下来的示例是测试gateway的异或流程,看一下示例
流程图如下
就是说只需要执行其中的一个流程,就可以让流程继续向下执行,这个示例是根据传入的变量X来看向哪执行
看测试代码
public void testExclusiveSplit() throws Exception {
KnowledgeBase kbase = createKnowledgeBase("BPMN2-ExclusiveSplit.bpmn2");
StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
ksession.getWorkItemManager().registerWorkItemHandler("Email",
new SystemOutWorkItemHandler());
Map< String , Object> params = new HashMap< String , Object>();
params.put("x", "First");
params.put("y", "Second");
ProcessInstance processInstance = ksession.startProcess(
"com.sample.test", params);
assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);
}
|
执行测试流程结果如下
Loading process BPMN2-ExclusiveSplit.bpmn2
x=First
Executing work item WorkItem 1 [name=Email, state=0, processInstanceId=1, parameters{Body=This is an urgent email First, Subject=Urgent email !, To=you@mail.com, From=me@mail.com}]
即当流程执行到其中有一个后则流程自动向下执行
Junit测试异或流程高级1
接下来的示例是测试,看如下流程定义
这个是根据动态传入的看结点的值是不是a如果是a则走上边的流程,如果不是则走下边的流程
public void testExclusiveSplitXPathAdvanced() throws Exception {
KnowledgeBase kbase = createKnowledgeBase("BPMN2-ExclusiveSplitXPath-advanced.bpmn2");
StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
ksession.getWorkItemManager().registerWorkItemHandler("Email",
new SystemOutWorkItemHandler());
Map< String , Object> params = new HashMap< String , Object>();
Document doc = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().newDocument();
Element hi = doc.createElement("hi");
Element ho = doc.createElement("ho");
hi.appendChild(ho);
Attr attr = doc.createAttribute("value");
ho.setAttributeNode(attr);
attr.setValue("a");
params.put("x", hi);
params.put("y", "Second");
ProcessInstance processInstance = ksession.startProcess(
"com.sample.test", params);
assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);
}
|
根据传入的结点执行流程
执行结果如下
Loading process BPMN2-ExclusiveSplitXPath-advanced.bpmn2
x=[hi: null]
Executing work item WorkItem 1 [name=Email, state=0, processInstanceId=1, parameters{Body=you@mail.com, Subject=Urgent email !, To=you@mail.com, From=me@mail.com}]
Junit测试异或流程高级2
接下来的示例是测试,如下流程图
流程结点定义
还是根据值来判断流向,这次只指定x
看测试程序
public void testExclusiveSplitXPathAdvanced2() throws Exception {
KnowledgeBase kbase = createKnowledgeBase("BPMN2-ExclusiveSplitXPath-advanced-vars-not-signaled.bpmn2");
StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
ksession.getWorkItemManager().registerWorkItemHandler("Email",
new SystemOutWorkItemHandler());
Map< String , Object> params = new HashMap< String , Object>();
Document doc = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().newDocument();
Element hi = doc.createElement("hi");
Element ho = doc.createElement("ho");
hi.appendChild(ho);
Attr attr = doc.createAttribute("value");
ho.setAttributeNode(attr);
attr.setValue("a");
params.put("x", hi);
params.put("y", "Second");
ProcessInstance processInstance = ksession.startProcess(
"com.sample.test", params);
assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);
}
|
执行的流程结果
Loading process BPMN2-ExclusiveSplitXPath-advanced-vars-not-signaled.bpmn2
y=Second
Executing work item WorkItem 1 [name=Email, state=0, processInstanceId=1, parameters{Body=you@mail.com, Subject=Urgent email !, To=you@mail.com, From=me@mail.com}]
Junit测试异或流程带参数
与上边几个程序基本一致,直接上代码吧
public void testExclusiveSplitXPathAdvancedWithVars() throws Exception {
KnowledgeBase kbase = createKnowledgeBase("BPMN2-ExclusiveSplitXPath-advanced-with-vars.bpmn2");
StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
ksession.getWorkItemManager().registerWorkItemHandler("Email",
new SystemOutWorkItemHandler());
Map< String , Object> params = new HashMap< String , Object>();
Document doc = DocumentBuilderFactory.newInstance()
.newDocumentBuilder().newDocument();
Element hi = doc.createElement("hi");
Element ho = doc.createElement("ho");
hi.appendChild(ho);
Attr attr = doc.createAttribute("value");
ho.setAttributeNode(attr);
attr.setValue("a");
params.put("x", hi);
params.put("y", "Second");
ProcessInstance processInstance = ksession.startProcess(
"com.sample.test", params);
assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);
}
|
执行结果
Loading process BPMN2-ExclusiveSplitXPath-advanced-with-vars.bpmn2
x=[hi: null]
Executing work item WorkItem 1 [name=Email, state=0, processInstanceId=1, parameters{Body=you@mail.com, Subject=Urgent email !, To=you@mail.com, From=me@mail.com}]
Junit测试异或流程优先和默认
下面的两个流程和上边基本一致只是细节上有些不同,一个是根据优先级流向一个是根据默认值流向
优先
public void testExclusiveSplitPriority() throws Exception {
KnowledgeBase kbase = createKnowledgeBase("BPMN2-ExclusiveSplitPriority.bpmn2");
StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
ksession.getWorkItemManager().registerWorkItemHandler("Email",
new SystemOutWorkItemHandler());
Map< String , Object> params = new HashMap< String , Object>();
params.put("x", "First");
params.put("y", "Second");
ProcessInstance processInstance = ksession.startProcess(
"com.sample.test", params);
assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);
}
|
默认
public void testExclusiveSplitDefault() throws Exception {
KnowledgeBase kbase = createKnowledgeBase("BPMN2-ExclusiveSplitDefault.bpmn2");
StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
ksession.getWorkItemManager().registerWorkItemHandler("Email",
new SystemOutWorkItemHandler());
Map< String , Object> params = new HashMap< String , Object>();
params.put("x", "NotFirst");
params.put("y", "Second");
ProcessInstance processInstance = ksession.startProcess(
"com.sample.test", params);
assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);
}
|
分别的执行结果如下:
优先
Loading process BPMN2-ExclusiveSplitPriority.bpmn2
y=Second
Executing work item WorkItem 1 [name=Email, state=0, processInstanceId=1, parameters{Body=This is an urgent email First, Subject=Urgent email !, To=you@mail.com, From=me@mail.com}]
默认
Loading process BPMN2-ExclusiveSplitDefault.bpmn2
y=Second
Executing work item WorkItem 1 [name=Email, state=0, processInstanceId=1, parameters{Body=This is an urgent email NotFirst, Subject=Urgent email !, To=you@mail.com, From=me@mail.com}]
时间: 2024-10-13 09:48:08