⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 websaletest.java

📁 一个部署好的网上销售jbpm实例,对学习和入门很有帮助
💻 JAVA
字号:
/*
 * JBoss, Home of Professional Open Source
 * Copyright 2005, JBoss Inc., and individual contributors as indicated
 * by the @authors tag. See the copyright.txt in the distribution for a
 * full listing of individual contributors.
 *
 * This is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */
package org.jbpm.websale;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jbpm.JbpmConfiguration;
import org.jbpm.context.exe.ContextInstance;
import org.jbpm.db.AbstractDbTestCase;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.identity.Entity;
import org.jbpm.identity.hibernate.IdentitySession;
import org.jbpm.identity.xml.IdentityXmlParser;
import org.jbpm.taskmgmt.exe.TaskInstance;
import org.jbpm.taskmgmt.exe.TaskMgmtInstance;

public class WebsaleTest extends AbstractDbTestCase {
  
  JbpmConfiguration jbpmConfiguration = JbpmConfiguration.getInstance();
  
  ProcessDefinition processDefinition = null;
  ProcessInstance processInstance = null;
  ContextInstance contextInstance = null;
  TaskMgmtInstance taskMgmtInstance = null;
  long processInstanceId = -1;

  public void setUp() throws Exception {
    super.setUp();
    deployProcess();
    loadIdentities();
    newTransaction();
    createNewProcessInstance();
  }

  private void deployProcess() {
		// 后来改的 by 陈刚
		try {
			InputStream is = new FileInputStream("processes/websale.par/processdefinition.xml");
			processDefinition = ProcessDefinition.parseXmlInputStream(is);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		// jBPM的原语句
		// ProcessDefinition processDefinition =
		// ProcessDefinition.parseXmlResource("websale.par/processdefinition.xml");
		jbpmContext.deployProcessDefinition(processDefinition);
	}

  private void loadIdentities() {
    // load the identities
    Entity[] entities = IdentityXmlParser.parseEntitiesResource("hsqldb/identity.db.xml");
    IdentitySession identitySession = new IdentitySession(session);
    for (int i=0; i<entities.length; i++) {
      identitySession.saveEntity(entities[i]);
    }
    newTransaction();
  }

  private void createNewProcessInstance() {
    processDefinition = graphSession.findLatestProcessDefinition("websale");
    processInstance = new ProcessInstance(processDefinition);
    contextInstance = processInstance.getContextInstance();
    taskMgmtInstance = processInstance.getTaskMgmtInstance();
  }


  public void testWebSaleOrderTaskParameters() {
    TaskInstance taskInstance = processInstance.getTaskMgmtInstance().createStartTaskInstance();
    assertEquals("create new web sale order", taskInstance.getName());
    assertEquals(3, taskInstance.getVariables().size());
  }

  public void testPerformWebSaleOrderTask() {
    TaskInstance taskInstance = null;
    
    jbpmContext.setActorId("cookie monster");
    // create a task to start the websale process
    taskInstance = processInstance.getTaskMgmtInstance().createStartTaskInstance();

    Map taskVariables = new HashMap();
    taskVariables.put("item", "cookies");
    taskVariables.put("quantity", "lots of them");
    taskVariables.put("address", "sesamestreet 46");
    
    taskInstance.addVariables(taskVariables);
    taskInstance.end();
    
    assertEquals("cookies", contextInstance.getVariable("item"));
    assertEquals("lots of them", contextInstance.getVariable("quantity"));
    assertEquals("sesamestreet 46", contextInstance.getVariable("address"));
    assertEquals("cookie monster", taskMgmtInstance.getSwimlaneInstance("buyer").getActorId());
  }

  public void testEvaluateAssignment() {
    TaskInstance taskInstance = null;

    jbpmContext.setActorId("cookie monster");
    // create a task to start the websale process
    taskInstance = processInstance.getTaskMgmtInstance().createStartTaskInstance();
    taskInstance.setVariable("item", "cookies");
    taskInstance.end();
    jbpmContext.save(processInstance);
    processInstanceId = processInstance.getId();
    
    newTransaction();
    
    List erniesTasks = taskMgmtSession.findTaskInstances("ernie");
    assertEquals(1, erniesTasks.size());

    TaskInstance evaluateTaskInstance = (TaskInstance) erniesTasks.get(0);
    assertEquals("ernie", evaluateTaskInstance.getActorId());
    assertEquals("evaluate web order", evaluateTaskInstance.getName());
    assertNotNull(evaluateTaskInstance.getToken());
    assertNotNull(evaluateTaskInstance.getCreate());
    assertNull(evaluateTaskInstance.getStart());
    assertNull(evaluateTaskInstance.getEnd());
  }

  public void testEvaluateOk() {
    TaskInstance taskInstance = processInstance.getTaskMgmtInstance().createStartTaskInstance();
    taskInstance.end();
    jbpmContext.save(processInstance);
    
    newTransaction();
    
    TaskInstance evaluateTaskInstance = (TaskInstance) taskMgmtSession.findTaskInstances("ernie").get(0);
    evaluateTaskInstance.end("ok");
    jbpmContext.save(evaluateTaskInstance);

    newTransaction();
    
    List erniesTasks = taskMgmtSession.findTaskInstances("bert");
    assertEquals(1, erniesTasks.size());

    TaskInstance waitForMoneyTaskInstance = (TaskInstance) erniesTasks.get(0);
    assertEquals("bert", waitForMoneyTaskInstance.getActorId());
    assertEquals("wait for money", waitForMoneyTaskInstance.getName());
    assertNotNull(waitForMoneyTaskInstance.getToken());
    assertNotNull(waitForMoneyTaskInstance.getCreate());
    assertNull(waitForMoneyTaskInstance.getStart());
    assertNull(waitForMoneyTaskInstance.getEnd());
  }

  public void testUnwritableVariableException() {
    testEvaluateAssignment();
    newTransaction();
    List erniesTasks = taskMgmtSession.findTaskInstances("ernie");
    TaskInstance evaluateTaskInstance = (TaskInstance) erniesTasks.get(0);
    // this variable is set in the task instance variables, but 
    // should not be submitted to the process context variables.
    evaluateTaskInstance.setVariable("item", "this is not allowed");
    evaluateTaskInstance.end();

    newTransaction();
    
    processInstance = graphSession.loadProcessInstance(processInstanceId);
    contextInstance = processInstance.getContextInstance();
    // so the cookies should still be in the item process variable.
    assertEquals("cookies", contextInstance.getVariable("item"));
  }

  public void testEvaluateNok() {
    testEvaluateAssignment();
    newTransaction();
    
    List erniesTasks = taskMgmtSession.findTaskInstances("ernie");
    TaskInstance evaluateTaskInstance = (TaskInstance) erniesTasks.get(0);
    evaluateTaskInstance.setVariable("comment", "wtf");
    evaluateTaskInstance.end("more info needed");
    jbpmContext.save(evaluateTaskInstance);
    
    newTransaction();

    List cookieMonsterTasks = taskMgmtSession.findTaskInstances("cookie monster");
    assertEquals(1, cookieMonsterTasks.size());
    TaskInstance fixWebOrderDataTaskInstance = (TaskInstance) cookieMonsterTasks.get(0);
    assertEquals("cookie monster", fixWebOrderDataTaskInstance.getActorId());
    assertEquals("wtf", fixWebOrderDataTaskInstance.getVariable("comment"));
  }

  public void testMoreInfoNeeded() {
    TaskInstance taskInstance = null; 

    jbpmContext.setActorId("cookie monster");
      
    // create a task to start the websale process
    taskInstance = processInstance.getTaskMgmtInstance().createStartTaskInstance();
    taskInstance.end();
    jbpmContext.save(processInstance);
    
    newTransaction();
    
    TaskInstance evaluateTaskInstance = (TaskInstance) taskMgmtSession.findTaskInstances("ernie").get(0);
    evaluateTaskInstance.end("more info needed");
    jbpmContext.save(evaluateTaskInstance);

    newTransaction();
    
    List cookieMonsterTasks = taskMgmtSession.findTaskInstances("cookie monster");
    assertEquals(1, cookieMonsterTasks.size());

    TaskInstance fixWebOrderDataTaskInstance = (TaskInstance) cookieMonsterTasks.get(0);
    assertEquals("cookie monster", fixWebOrderDataTaskInstance.getActorId());
    assertEquals("fix web order data", fixWebOrderDataTaskInstance.getName());
    assertNotNull(fixWebOrderDataTaskInstance.getToken());
    assertNotNull(fixWebOrderDataTaskInstance.getCreate());
    assertNull(fixWebOrderDataTaskInstance.getStart());
    assertNull(fixWebOrderDataTaskInstance.getEnd());
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -