Activiti moduler+Spring Junit环境构建(一)

阅读更多

Activiti moduler + spring + junit  环境构建(一)

软件环境:开发工具idea,jdk 1.7,tomcat 8.5。

 

具体web搭建及附件可查看:

Activiti moduler + spring web环境构建(二)

 

项目目录结构如下:


Activiti moduler+Spring Junit环境构建(一)_第1张图片

 

 

maven配置文件:

 


	4.0.0
	org.activiti.examples
	activiti-examples
	1.0-SNAPSHOT
	jar
	BPMN 2.0 with Activiti - Examples

	
		5.21.0
		4.3.3.RELEASE
	

	
		
			org.activiti
			activiti-engine
			${activiti-version}
		
		
			org.activiti
			activiti-modeler
			
				
					xalan
					xalan
				
			
			${activiti-version}
		
		
		
			org.activiti
			activiti-diagram-rest
			${activiti-version}
		
		
			org.activiti
			activiti-explorer
			
				
					vaadin
					com.vaadin
				
				
					dcharts-widget
					org.vaadin.addons
				
				
					activiti-simple-workflow
					org.activiti
				
			
			${activiti-version}
		
		
			org.activiti
			activiti-spring
			${activiti-version}
		

		
			org.springframework
			spring-context
			${spring-version}
		
		
			org.springframework
			spring-jdbc
			${spring-version}
		
		
			org.springframework
			spring-tx
			${spring-version}
		
		
			org.springframework
			spring-core
			${spring-version}
		
		
			org.springframework
			spring-beans
			${spring-version}
		
		
			org.springframework
			spring-test
			${spring-version}
		

		
			javax.servlet
			javax.servlet-api
			3.1.0
			provided
		
		
			javax.servlet
			jstl
			1.2
		

		
			mysql
			mysql-connector-java
			5.1.39
		

		
			org.slf4j
			slf4j-api
			1.7.6
		
		
			org.slf4j
			slf4j-jdk14
			1.7.6
		

		
			junit
			junit
			4.12
		
	
	
		
			
				org.apache.maven.plugins
				maven-compiler-plugin
				2.3.2
				
					1.6
					1.6
				
			
			
				org.apache.maven.plugins
				maven-eclipse-plugin
				true
				
					
						org.eclipse.jdt.USER_LIBRARY/Activiti Designer
							Extensions
					
				
			
		
	


  

spring配置文件:

 




    
        
        
        
        
        
    

    
        
        
        
        
        
        
        
        
        
    

    
        
    

    
    
    
    
    

    
        
    

 

测试的Java文件:

package com.bpm;

import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.task.Task;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.annotation.Rollback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.awt.*;
import java.io.*;
import java.util.List;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration({"classpath:spring-activiti.xml"})
@Rollback
@Transactional
public class BpmDefinitionTest {
    @Resource
    TaskService taskService;
    @Resource
    private RepositoryService repositoryService;
    @Resource
    private RuntimeService runtimeService;

    private static Color HIGHLIGHT_COLOR = Color.RED;
    private static Stroke THICK_TASK_BORDER_STROKE = new BasicStroke(3.0f);

    @Test
    public void testDeploy() throws IOException {
        String sourceFile = "holidayRequest1.bpmn";
        /** 发布流程
         方法一:流的方式
         
         InputStream is = new FileInputStream(new File(this.getClass().getResource("/holidayRequest2.bpmn").getPath()));
         Assert.assertNotNull(is);
         Deployment deployment = repositoryService.createDeployment().addInputStream("bpmn20.xml", is).name("holidayRequest").deploy();
         
         方法二:当文件在资源目录下时,可使用以下方式直接加载:
         */
        Deployment deployment = repositoryService.createDeployment()
                .name("helloRequest")
                .category("testDemo")
                .addClasspathResource(sourceFile).deploy();
        Assert.assertNotNull(deployment);
        System.out.println("deploymentId:" + deployment.getId());
        /**查询流程定义,以下查询方法会自动查询出当前流程的最高版本
         * 流程定义主要是用于部署流程的版本控制,在保存部署信息时,会自动先创建一个流程定义。系统会根据*.bpmn中process元素属性来设置数据字段值。
         * 
         *     
         *          ...
         *     
         * 
         * 其中definitions->targetNamespace会作为字段category值,process->id 会作为字段key值,用来标识流程的唯一从而进行版本的自动升级,process->name 会作为字段name值...
         * 所以取process id 时要格外注意不要重复了
         * */
        ProcessDefinitionEntity processDefinition = (ProcessDefinitionEntity) repositoryService.createProcessDefinitionQuery().deploymentId(deployment.getId()).singleResult();

        // 获取流程图并生成文件
        String diagramResourceName = processDefinition.getDiagramResourceName();
        InputStream stream = repositoryService.getResourceAsStream(processDefinition.getDeploymentId(), diagramResourceName);
        // 通过这种方式获取,一定要添加 / ,否则会找不到文件
        String path = this.getClass().getResource("/" + sourceFile).getPath();
        File file = new File(path);
        OutputStream out = new FileOutputStream(file.getParent() + "/holidayRequest.expense.png");
        int len;
        byte[] data = new byte[1024];        //可以根据实际情况调整,建议使用1024,即每次读1KB
        while ((len = stream.read(data)) != -1) {
            out.write(data, 0, len);                    //建议不要直接用os.write(bt)
        }
        out.flush();
        stream.close();
        out.close();

        /**启动流程实例
         * 还可以使用:runtimeService.startProcessInstanceByKey("myProcess");这里的key是定义在bpmn文件中的process元素的id属性
         * */
        runtimeService.startProcessInstanceById(processDefinition.getId());

        //查询任务实例
        List taskList = taskService.createTaskQuery().processDefinitionId(processDefinition.getId()).list();
        Assert.assertNotNull(taskList == null);
        Assert.assertTrue(taskList.size() > 0);
        for (Task task : taskList) {
            System.out.println("task name is " + task.getName() + " ,task key is " + task.getTaskDefinitionKey());
        }
    }

}

 

流程文件bpmn:



    
        
        
        
        
        
        
        
        
        
    
    
        
            
                
                
                    
                
            
            
                
                
                    
                
            
            
                
                
                    
                
            
            
                
                
                    
                
            
            
                
                
                    
                
            
            
                
                
                
                    
                
            
            
                
                
                
                    
                
            
            
                
                
                
                    
                
            
            
                
                
                
                    
                
            
        
    

 

  • Activiti moduler+Spring Junit环境构建(一)_第2张图片
  • 大小: 20.5 KB
  • 查看图片附件

你可能感兴趣的:(activiti,test,spring,moduler)