首页 > 代码库 > 測试jbpm6.2使用的基础类

測试jbpm6.2使用的基础类



方便了解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公布内容
http://sourceforge.net/projects/jbpm/files/jBPM%206/jbpm-6.2.0.Final/
2. jbpm6.2 user guide
http://docs.jboss.org/jbpm/v6.2/userguide/
3. jbpm6.2 API
http://docs.jboss.org/jbpm/v6.2/javadocs/

測试jbpm6.2使用的基础类