码迷,mamicode.com
首页 > 其他好文 > 详细

测试jbpm6.2使用的基础类

时间:2015-07-31 01:27:49      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:



方便了解jbpm6.2功能我们可以建立一个测试项目,在其中构建一个测试基础类,具体步骤如下所示。
在集成了插件org.drools.updatesite-6.2.0.Final.zip的eclipse中新建jbpm project。插件在参考链接1中jbpm-6.2.0.Final-installer-full.zip内已包含。
将此项目引入依赖的jbpm所需要的jar——在安装的jbpm6.2-installer-full\runtime中,参见上节jbpm6.2 installer安装过程。
在项目目录src\main\resources\META-INF中增加persistence.xml文件,此JPA文件中配置测试JBPM功能的必要entity。persistence.xml文件内容如下所示。
<persistence
version="2.0"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd
http://java.sun.com/xml/ns/persistence/orm http://java.sun.com/xml/ns/persistence/orm_2_0.xsd"
xmlns:orm="http://java.sun.com/xml/ns/persistence/orm"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/persistence">
 
<persistence-unit name="org.jbpm.persistence.jpa" transaction-type="JTA">
<provider>org.hibernate.ejb.HibernatePersistence</provider>
<jta-data-source>jdbc/jbpm-ds</jta-data-source>
<mapping-file>META-INF/JBPMorm.xml</mapping-file>
<mapping-file>META-INF/Taskorm.xml</mapping-file>
<mapping-file>META-INF/TaskAuditorm.xml</mapping-file>
 
<class>org.drools.persistence.info.SessionInfo</class>
<class>org.jbpm.persistence.processinstance.ProcessInstanceInfo</class>
<class>org.drools.persistence.info.WorkItemInfo</class>
<class>org.jbpm.persistence.correlation.CorrelationKeyInfo</class>
<class>org.jbpm.persistence.correlation.CorrelationPropertyInfo</class>
 
<class>org.jbpm.process.audit.ProcessInstanceLog</class>
<class>org.jbpm.process.audit.NodeInstanceLog</class>
<class>org.jbpm.process.audit.VariableInstanceLog</class>
 
<!-- manager -->
<class>org.jbpm.runtime.manager.impl.jpa.ContextMappingInfo</class>
 
<class>org.jbpm.services.task.impl.model.AttachmentImpl</class>
<class>org.jbpm.services.task.impl.model.ContentImpl</class>
<class>org.jbpm.services.task.impl.model.BooleanExpressionImpl</class>
<class>org.jbpm.services.task.impl.model.CommentImpl</class>
<class>org.jbpm.services.task.impl.model.DeadlineImpl</class>
<class>org.jbpm.services.task.impl.model.DelegationImpl</class>
<class>org.jbpm.services.task.impl.model.EscalationImpl</class>
<class>org.jbpm.services.task.impl.model.GroupImpl</class>
<class>org.jbpm.services.task.impl.model.I18NTextImpl</class>
<class>org.jbpm.services.task.impl.model.NotificationImpl</class>
<class>org.jbpm.services.task.impl.model.EmailNotificationImpl</class>
<class>org.jbpm.services.task.impl.model.EmailNotificationHeaderImpl</class>
<class>org.jbpm.services.task.impl.model.PeopleAssignmentsImpl</class>
<class>org.jbpm.services.task.impl.model.ReassignmentImpl</class>
 
<class>org.jbpm.services.task.impl.model.TaskImpl</class>
<class>org.jbpm.services.task.impl.model.TaskDataImpl</class>
<class>org.jbpm.services.task.impl.model.UserImpl</class>
 
<!--BAM for task service -->
<class>org.jbpm.services.task.audit.impl.model.BAMTaskSummaryImpl</class>
 
<!-- Event Classes -->
<class>org.jbpm.services.task.audit.impl.model.TaskEventImpl</class>
 
<!-- Task Audit Classes -->
<class>org.jbpm.services.task.audit.impl.model.AuditTaskImpl</class>
 
<properties>
<property name="hibernate.max_fetch_depth" value="3"/>
<property name="hibernate.show_sql" value="true" />
 
<!-- BZ 841786: AS7/EAP 6/Hib 4 uses new (sequence) generators which seem to cause problems -->
<property name="hibernate.id.new_generator_mappings" value="false" />
 
<property name="hibernate.transaction.jta.platform" value="org.hibernate.service.jta.platform.internal.BitronixJtaPlatform" />
</properties>
</persistence-unit>
</persistence>
建立测试基础类。在此类中构建数据源、JBPM RuntimeManager(加载待测试流程),代码如下所示。
package com.chenwd.jbpm.test.base;
 
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
 
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
 
import org.jbpm.process.audit.strategy.StandaloneJtaStrategy;
import org.jbpm.runtime.manager.impl.RuntimeEnvironmentBuilder;
import org.jbpm.services.task.identity.JBossUserGroupCallbackImpl;
import org.junit.After;
import org.junit.Before;
import org.kie.api.io.ResourceType;
import org.kie.api.runtime.manager.RuntimeEnvironment;
import org.kie.api.runtime.manager.RuntimeManager;
import org.kie.api.runtime.manager.RuntimeManagerFactory;
import org.kie.api.task.UserGroupCallback;
import org.kie.internal.io.ResourceFactory;
 
import bitronix.tm.resource.jdbc.PoolingDataSource;
 
public class jbpmTestBase {
 
 
protected static EntityManagerFactory emf;
protected static StandaloneJtaStrategy persistenceStrategy;
protected static EntityManager em;
private static PoolingDataSource ds;
 
@Before
public void setup() {
ds = setupDataSource();
Map configOverrides = new HashMap();
configOverrides.put("hibernate.hbm2ddl.auto", "none");
configOverrides.put("hibernate.dialect", "org.hibernate.dialect.MySQL5InnoDBDialect");
emf = Persistence.createEntityManagerFactory("org.jbpm.persistence.jpa", configOverrides);
if(emf != null){
persistenceStrategy = new StandaloneJtaStrategy(emf);
em = persistenceStrategy.getEntityManager();
}
}
 
@After
public void teardown() {
if (ds != null) {
ds.close();
}
}
protected static void out(Object o){
System.out.println(String.valueOf(o));
}
private static RuntimeManager getRuntimeManager(String process) {
Properties properties = new Properties();
properties.setProperty("krisv", "");
properties.setProperty("mary", "");
properties.setProperty("john", "");
UserGroupCallback userGroupCallback = new JBossUserGroupCallbackImpl(properties);
 
RuntimeEnvironment environment = RuntimeEnvironmentBuilder.getDefault().persistence(true)
.entityManagerFactory(emf).userGroupCallback(userGroupCallback)
.addAsset(ResourceFactory.newClassPathResource(process), ResourceType.BPMN2)
.get();
return RuntimeManagerFactory.Factory.get().newPerProcessInstanceRuntimeManager(environment);
 
}
private static PoolingDataSource setupDataSource() {
PoolingDataSource pds = new PoolingDataSource();
pds.setUniqueName("jdbc/jbpm-ds");
pds.setClassName("bitronix.tm.resource.jdbc.lrc.LrcXADataSource");
pds.setMaxPoolSize(5);
pds.setAllowLocalTransactions(true);
pds.getDriverProperties().put("user", "root");
pds.getDriverProperties().put("password", "root");
pds.getDriverProperties().put("url", "jdbc:mysql://localhost:3306/jbpm2");
pds.getDriverProperties().put("driverClassName", "com.mysql.jdbc.Driver");
pds.init();
return pds;
}
}
在上面的类中,getRuntimeManager方法加载的用户的数量有限,在使用它做试验时能够动态设置task actorid的账号比较少,而且没有group。我们可以在构建RuntimeEnvironment时自己实现userGroupCallback接口,这样可以对用户和组进行java编码验证。如下所示。
private static RuntimeManager getRuntimeManager(String process) {
RuntimeEnvironment environment = RuntimeEnvironmentBuilder.getDefault().persistence(true)
.entityManagerFactory(emf).userGroupCallback(new UserGroupCallback() {
public List<String> getGroupsForUser(String userId, List<String> groupIds, List<String> allExistingGroupIds) {
List<String> result = new ArrayList<String>();
if ("chenwd".equals(userId)) {
result.add("group1");
}
return result;
}
public boolean existsUser(String arg0) {
return true;
}
public boolean existsGroup(String arg0) {
return true;
}
})
.addAsset(ResourceFactory.newClassPathResource(process), ResourceType.BPMN2)
.get();
return RuntimeManagerFactory.Factory.get().newPerProcessInstanceRuntimeManager(environment);
 
}
在此方法中chenwd为group1的成员。如果处理group task可以采用这种构建方式进行测试。之后我们的具体测试类可以继承这个类,获取kieSession和taskServcie进行流程发起和task处理。示例如下所示。
public class JbpmTest extends JbpmTestBase{
 
@Test
public void testStartProcess() throws Exception {
 
try {
 
RuntimeManager manager = getRuntimeManager("sample.bpmn");
RuntimeEngine runtime = manager.getRuntimeEngine(EmptyContext.get());
KieSession ksession = runtime.getKieSession();
 
 
//设置流程variables,发起流程
Map<String, Object> params = new HashMap<String, Object>();
ProcessInstance pi = ksession.startProcess("com.sample.bpmn.hello", params);
 
 
manager.disposeRuntimeEngine(runtime);
 
} catch (Throwable th) {
th.printStackTrace();
}
}
}

参考
1. jbpm6.2发布内容
2. jbpm6.2 user guide
3. jbpm6.2 API

版权声明:本文为博主原创文章,未经博主允许不得转载。

测试jbpm6.2使用的基础类

标签:

原文地址:http://blog.csdn.net/shierqu/article/details/47159807

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!