package com.activit;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.io.InputStream;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;
import org.activiti.bpmn.converter.BpmnXMLConverter;
import org.activiti.bpmn.model.BpmnModel;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.test.Deployment;
import org.junit.Before;
import org.junit.Test;
public class BpmnTest {
ProcessEngine processEngine = null;
RepositoryService repositoryService = null;
RuntimeService runtimeService = null;
TaskService taskService = null;
/** * 加载配置文件 */ @Before public void init() { processEngine = ProcessEngineConfiguration.createProcessEngineConfigurationFromResource("activiti.cfg.xml") .buildProcessEngine(); repositoryService = processEngine.getRepositoryService(); runtimeService = processEngine.getRuntimeService(); taskService = processEngine.getTaskService(); } @Test @Deployment(resources="resources/diagrams/VacationRequest.bpmn") public void testXmltoBpmn() throws XMLStreamException{ ProcessDefinition processDefinition =repositoryService.createProcessDefinitionQuery().processDefinitionKey("vacationRequest").singleResult(); //获取流程资源的名称 String sourceName = processDefinition.getResourceName(); //获取流程资源 InputStream inputStream = repositoryService.getResourceAsStream(processDefinition.getId(),sourceName); //创建转换对象 BpmnXMLConverter converter = new BpmnXMLConverter(); //读取xml文件 XMLInputFactory factory = XMLInputFactory.newInstance(); XMLStreamReader reader = factory.createXMLStreamReader(inputStream); //将xml文件转换成BpmnModel BpmnModel bpmnModel = converter.convertToBpmnModel((XMLStreamReader) reader); //验证bpmnModel是否为空 assertNotNull(bpmnModel); org.activiti.bpmn.model.Process process = bpmnModel.getMainProcess(); //验证转换的流程id assertEquals("leave",process.getId()); }
}