activiti测试(部署,启动,查看状态,查看私有和公有任务)

包含部署流程,获取流程实例,查看私有任务和公有任务的抽取出来的工具类

package com.liuzm.activiti.day2;

import java.io.InputStream;
import java.util.List;

import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.DeploymentBuilder;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;


public class BPMNTestUtils {
	//获取核心对象
	private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
	//获取服务
	protected RepositoryService repositoryService = processEngine.getRepositoryService();
	protected RuntimeService runtimeService = processEngine.getRuntimeService();
	protected TaskService taskService = processEngine.getTaskService();
	
	//部署流程
	public void deployProcess(String resourceName){
		//创建部署配置对象
		DeploymentBuilder builder = repositoryService.createDeployment();
		//获取某文件夹下指定名称对应的流
		InputStream inputStream = this.getClass().getResourceAsStream(resourceName);
		//做配置
		builder.addInputStream(resourceName, inputStream );
		//部署
		builder.deploy();
	}
	
	//获取流程实例
	public ProcessInstance getProcessInstance(String processDefinitionId){
		return runtimeService.createProcessInstanceQuery().processDefinitionId(processDefinitionId).singleResult();
	}
	
	//获取私有任务列表
	public List<Task> getPersonalTask(String assignee){
		return taskService.createTaskQuery().taskAssignee(assignee).list();
	}
	
	//获取共有任务列表
	public List<Task> getCommonTask(String candidateUser){
		return taskService.createTaskQuery().taskCandidateUser(candidateUser).list();
	}
}

 简单的 部署流程,启动流程,查看状态的测试

 

package com.liuzm.activiti.day2.start;

import org.activiti.engine.runtime.ProcessInstance;
import org.junit.Test;
import org.springframework.util.Assert;

import com.liuzm.activiti.day2.BPMNTestUtils;

/**
 *	测试步骤
 *	1.部署规则
 *	2.启动流程
 *	3.查看流程状态
 */
public class StartTest extends BPMNTestUtils{
	@Test
	public void test1() throws Exception {
		//部署规则
		deployProcess("start.bpmn");
		//启动流程
		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("start");
		String pid = processInstance.getId();
		System.out.println("启动时的     processInstance : "+processInstance);
		//查看流程状态
		ProcessInstance processInstance2 = getProcessInstance(pid);
		System.out.println("流程结束时     processInstance : "+processInstance2);
		if (processInstance.isEnded()) {
			System.out.println("流程成功执行完毕!!!");
		}
	}
	
	@Test
	public void test2() throws Exception {
		//部署流程规则
		deployProcess("start2.bpmn");
		//启动流程
		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("start2");
		String pid = processInstance.getId();
		//检查流程状态
		Assert.isTrue(processInstance.isEnded());
		System.out.println("流程成功执行完毕!!!");
	}
	

}

 查看私有任务,公有任务的测试用例(包括动态分配任务)

 

package com.liuzm.activiti.day2.usertask;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.Assert;
import org.junit.Test;

import com.liuzm.activiti.day2.BPMNTestUtils;

/**
 * 私有任务和公有任务的测试
 */
public class UserTaskTest extends BPMNTestUtils {

	// 私有任务测试
	@Test
	public void personalTaskTest() throws Exception {
		// 部署流程规则
		deployProcess("task1.bpmn");
		// 启动流程
		ProcessInstance processInstance = runtimeService
				.startProcessInstanceByKey("task1");
		String pid = processInstance.getId();

		String assignee = "小明";
		List<Task> tasks = getPersonalTask(assignee);
		Assert.assertEquals(1, tasks.size());

		Task task = tasks.get(0);
		Assert.assertEquals("上交作业", task.getName());

		taskService.complete(task.getId());

		// 第二个任务
		assignee = "张老师";
		List<Task> tasks2 = getPersonalTask(assignee);
		Assert.assertEquals(1, tasks2.size());

		Task task2 = tasks2.get(0);
		Assert.assertEquals("批改作业", task2.getName());
		taskService.complete(task2.getId());

		// 检查流程状态
		Assert.assertNull(getProcessInstance(pid));
		System.out.println("私有任务成功执行!!");
	}

	@Test
	public void commonTaskTest() throws Exception {
		// 部署流程规则
		// deployProcess("task2.bpmn");
		// 启动服务
		// ProcessInstance processInstance =
		// runtimeService.startProcessInstanceByKey("task2");
		// String pid = processInstance.getId();
		String pid = "1505";
		System.out.println(pid);

		// 检查私有任务
		String assignee = "张三";
		List<Task> tasks1 = getPersonalTask(assignee);
		Assert.assertEquals(1, tasks1.size());
		System.out.println("张三的私有任务列表: " + tasks1);

		assignee = "李四";
		List<Task> tasks2 = getPersonalTask(assignee);
		Assert.assertEquals(0, tasks2.size());
		System.out.println("李四的私有任务列表: " + tasks2);

		// 检查共有任务列表
		String candidateUser = "张三";
		List<Task> tasks3 = getCommonTask(candidateUser);
		// Assert.assertEquals(1, tasks3.size());// 张三认领任务前
		Assert.assertEquals(0, tasks3.size()); // 张三认领任务后
		System.out.println("张三的公有任务列表: " + tasks3);

		candidateUser = "李四";
		List<Task> tasks4 = getCommonTask(candidateUser);
		// Assert.assertEqual(1, tasks4.size()); //张三认领任务前
		Assert.assertEquals(0, tasks4.size()); // 张三认领任务后
		System.out.println("李四的公有任务列表: " + tasks4);

		// 公有任务的认领
		System.out.println("=============张三领取任务============");
		// String taskId = tasks3.get(0).getId();
		// taskService.claim(taskId , "张三");

		// 再检查公有任务列表
		List<Task> tasks5 = getCommonTask("张三");
		Assert.assertEquals(0, tasks5.size());
		System.out.println("张三的公有任务列表: " + tasks5);

		List<Task> tasks6 = getCommonTask("李四");
		Assert.assertEquals(0, tasks6.size());
		System.out.println("李四的公有任务列表: " + tasks6);

		// 私有任务列表
		List<Task> tasks7 = getPersonalTask("张三");
		Assert.assertEquals(1, tasks7.size());
		System.out.println("张三的私有任务列表: " + tasks7);

		List<Task> tasks8 = getPersonalTask("李四");
		Assert.assertEquals(0, tasks8.size());
		System.out.println("李四的私有任务列表: " + tasks8);

		// 张三完成任务
		taskService.complete(tasks7.get(0).getId());

		// 检查流程状态
		Assert.assertNull(getProcessInstance(pid));
		System.out.println("张三领取任务<-->流程成功执行");

		// 运行结果:
		// 1505
		// 张三的私有任务列表: [Task[id=1508, name=打扫教室]]
		// 李四的私有任务列表: []
		// 张三的公有任务列表: []
		// 李四的公有任务列表: []
		// =============张三领取任务============
		// 张三的公有任务列表: []
		// 李四的公有任务列表: []
		// 张三的私有任务列表: [Task[id=1508, name=打扫教室]]
		// 李四的私有任务列表: []
		// 张三领取任务<-->流程成功执行
	}

	// 动态办理任务
	@Test
	public void completeTaskAuto() throws Exception {
		// 部署流程规则
		deployProcess("task3.bpmn");
		// 放入流程变量
		Map<String,Object> variables = new HashMap<String, Object>();
		variables.put("user", "小明");
		// 启动流程
		ProcessInstance processInstance = runtimeService
				.startProcessInstanceByKey("task3",variables);
		String pid = processInstance.getId();
		
		String assignee = "小明";
		List<Task> tasks = getPersonalTask(assignee);
		Assert.assertEquals(1, tasks.size());
	
		Task task = tasks.get(0);
		Assert.assertEquals("提交任务", task.getName());
		
		//完成任务
		taskService.complete(task.getId());
		
		//检查流程状态
		Assert.assertNull(getProcessInstance(pid));
		System.out.println("动态办理任务流程成功执行!!!");
	}

	// 动态办理任务 -->动态任务分配处理器
	@Test
	public void completeTaskAuto2() throws Exception {
		// 部署流程规则
		deployProcess("task4.bpmn");
		// 启动流程 
		ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("task4");
		String pid = processInstance.getId();
		
		String assignee = "小青";
		List<Task> tasks = getPersonalTask(assignee);
		Assert.assertEquals(1, tasks.size());
		
		Task task = tasks.get(0);
		Assert.assertEquals("经理审批", task.getName());
	
		// 完成任务
		taskService.complete(task.getId());
		
		// 检查流程状态
		Assert.assertNull(getProcessInstance(pid));
		System.out.println("动态分配任务处理器分配任务完成!!!");
	}
	
}

 上述的动态分配任务处理器需要下边的类来为其设置完成任务的对象,并且相应的节点的Listener需要监听下边的类

 

package com.liuzm.activiti.day2.usertask;

import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;

public class ManagerTaskHandler implements TaskListener{

	@Override
	public void notify(DelegateTask delegateTask) {
		// 设置任务办理者
		delegateTask.setAssignee("小青");
		// 设置任务候选者
		//delegateTask.addCandidateUser(userId);
	}

}

 

 

你可能感兴趣的:(相关文件)