Package org.jbpm.taskmgmt.exe

Source Code of org.jbpm.taskmgmt.exe.TaskInstanceEndTest

/*
* 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.taskmgmt.exe;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import junit.framework.TestCase;

import org.jbpm.graph.def.Node;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.graph.exe.Token;

public class TaskInstanceEndTest extends TestCase {

  public void testStartTaskInstanceStressed() {
    ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
      "<process-definition>" +
      "  <start-state>" +
      "    <task name='get it going' />" +
      "    <transition name='stressed' to='calm down' />" +
      "    <transition name='relaxed' to='task first things first' />" +
      "  </start-state>" +
      "  <state name='calm down' />" +
      "  <state name='first things first' />" +
      "</process-definition>"
    );
   
    ProcessInstance processInstance = new ProcessInstance(processDefinition);
    TaskInstance startTaskInstance = processInstance.getTaskMgmtInstance().createStartTaskInstance();
    startTaskInstance.end("stressed");

    Node calmDown = processDefinition.getNode("calm down");
    assertEquals(calmDown, processInstance.getRootToken().getNode());
  }

  public void testStartTaskInstanceRelaxed() {
    ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
      "<process-definition>" +
      "  <start-state>" +
      "    <task name='get it going' />" +
      "    <transition name='stressed' to='calm down' />" +
      "    <transition name='relaxed' to='first things first' />" +
      "  </start-state>" +
      "  <state name='calm down' />" +
      "  <state name='first things first' />" +
      "</process-definition>"
    );
   
    ProcessInstance processInstance = new ProcessInstance(processDefinition);
    TaskInstance startTaskInstance = processInstance.getTaskMgmtInstance().createStartTaskInstance();
    startTaskInstance.end("relaxed");

    Node ftf = processDefinition.getNode("first things first");
    assertEquals(ftf, processInstance.getRootToken().getNode());
  }

  public void testTaskNodeStressed() {
    ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
      "<process-definition>" +
      "  <start-state>" +
      "    <transition to='get it going' />" +
      "  </start-state>" +
      "  <task-node name='get it going'>" +
      "    <task name='get it going' />" +
      "    <transition name='stressed' to='calm down' />" +
      "    <transition name='relaxed' to='first things first' />" +
      "  </task-node>" +
      "  <state name='calm down' />" +
      "  <state name='first things first' />" +
      "</process-definition>"
    );
   
    ProcessInstance processInstance = new ProcessInstance(processDefinition);
    Token rootToken = processInstance.getRootToken();
    processInstance.signal();
    List unfinishedTasks = new ArrayList(processInstance.getTaskMgmtInstance().getUnfinishedTasks(rootToken));
    assertNotNull(unfinishedTasks);
    assertEquals(1, unfinishedTasks.size());
    TaskInstance startTaskInstance = (TaskInstance) unfinishedTasks.get(0);
    startTaskInstance.end("stressed");

    Node calmDown = processDefinition.getNode("calm down");
    assertEquals(calmDown, processInstance.getRootToken().getNode());
  }

  public void testTaskNodeRelaxed() {
    ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
      "<process-definition>" +
      "  <start-state>" +
      "    <transition to='get it going' />" +
      "  </start-state>" +
      "  <task-node name='get it going'>" +
      "    <task name='get it going' />" +
      "    <transition name='stressed' to='calm down' />" +
      "    <transition name='relaxed' to='first things first' />" +
      "  </task-node>" +
      "  <state name='calm down' />" +
      "  <state name='first things first' />" +
      "</process-definition>"
    );
   
    ProcessInstance processInstance = new ProcessInstance(processDefinition);
    Token rootToken = processInstance.getRootToken();
    processInstance.signal();
    List unfinishedTasks = new ArrayList(processInstance.getTaskMgmtInstance().getUnfinishedTasks(rootToken));
    assertNotNull(unfinishedTasks);
    assertEquals(1, unfinishedTasks.size());
    TaskInstance startTaskInstance = (TaskInstance) unfinishedTasks.get(0);
    startTaskInstance.end("relaxed");

    Node ftf = processDefinition.getNode("first things first");
    assertEquals(ftf, rootToken.getNode());
  }

  public void testLastTaskNotSignalling() {
    ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
      "<process-definition>" +
      "  <start-state>" +
      "    <transition to='review' />" +
      "  </start-state>" +
      "  <task-node name='review'>" +
      "    <task name='primary review' />" +
      "    <task name='required secondary review' />" +
      "    <task name='optional secondary review' signalling='false' />" +
      "    <transition to='next phase' />" +
      "  </task-node>" +
      "  <state name='next phase' />" +
      "</process-definition>"
    );
   
    ProcessInstance processInstance = new ProcessInstance(processDefinition);
    Token rootToken = processInstance.getRootToken();
    processInstance.signal();

    assertEquals("review", rootToken.getNode().getName());
   
    Collection unfinishedTasks = processInstance.getTaskMgmtInstance().getUnfinishedTasks(rootToken);
    assertNotNull(unfinishedTasks);
    assertEquals(3, unfinishedTasks.size());
    TaskInstance taskInstance = findTaskInstance("primary review", unfinishedTasks);
    taskInstance.end();

    assertEquals("review", rootToken.getNode().getName());

    unfinishedTasks = processInstance.getTaskMgmtInstance().getUnfinishedTasks(rootToken);
    assertNotNull(unfinishedTasks);
    assertEquals(2, unfinishedTasks.size());
    taskInstance = findTaskInstance("required secondary review", unfinishedTasks);
    taskInstance.end();

    assertEquals("next phase", rootToken.getNode().getName());
  }

  private TaskInstance findTaskInstance(String taskName, Collection taskInstances) {
    TaskInstance taskInstance = null;
    Iterator iter = taskInstances.iterator();
    while (iter.hasNext()) {
      TaskInstance candidate = (TaskInstance) iter.next();
      if (taskName.equals(candidate.getName())) {
        taskInstance = candidate;
      }
    }
    return taskInstance;
  }
}
TOP

Related Classes of org.jbpm.taskmgmt.exe.TaskInstanceEndTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.