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

📄 superstateactionexecutiontest.java

📁 jbpm demo 是一款非常不错的开源工作流,简单易用,适合扩张开发!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * 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.graph.exe;

import java.util.ArrayList;
import java.util.List;

import junit.framework.TestCase;

import org.jbpm.graph.def.Action;
import org.jbpm.graph.def.ActionHandler;
import org.jbpm.graph.def.Event;
import org.jbpm.graph.def.GraphElement;
import org.jbpm.graph.def.Node;
import org.jbpm.graph.def.ProcessDefinition;

public class SuperStateActionExecutionTest extends TestCase {

  ProcessDefinition processDefinition = null;
  ProcessInstance processInstance = null;

  static List executedActions = null;
  
  public static class ExecutedAction {
    // ExectionContext members
    Token token = null;
    Event event = null;
    GraphElement eventSource = null;
    Action action = null;
    Throwable exception = null;
    // The node returned by the ExecutionContext at the time of execution
    Node node = null;
  }
  
  public static class Recorder implements ActionHandler {
    private static final long serialVersionUID = 1L;

    public void execute(ExecutionContext executionContext) throws Exception {
      ExecutedAction executedAction = new ExecutedAction();
      executedAction.token = executionContext.getToken();
      executedAction.event = executionContext.getEvent();
      executedAction.eventSource = executionContext.getEventSource();
      executedAction.action = executionContext.getAction();
      executedAction.exception = executionContext.getException();
      executedAction.node = executionContext.getNode();
      executedActions.add(executedAction);
    }
  }
  
  public void setUp() {
    executedActions = new ArrayList();
  }

  public void testSuperStateEnter() {
    processDefinition = ProcessDefinition.parseXmlString(
      "<process-definition>" +
      "  <start-state name='start'>" +
      "    <transition to='superstate/insidesuperstate'/>" +
      "  </start-state>" +
      "  <super-state name='superstate'>" +
      "    <event type='superstate-enter'>" +
      "      <action class='org.jbpm.graph.exe.SuperStateActionExecutionTest$Recorder' />" +
      "    </event>" +
      "    <state name='insidesuperstate' />" +
      "  </super-state>" +
      "</process-definition>"
    );
    // create the process instance
    processInstance = new ProcessInstance(processDefinition);
    processInstance.signal();
    assertEquals(1, executedActions.size());
    ExecutedAction executedAction = (ExecutedAction) executedActions.get(0);
    assertEquals("superstate-enter", executedAction.event.getEventType());
    assertSame(processDefinition.getNode("superstate"), executedAction.event.getGraphElement());
    assertSame(processDefinition.getNode("superstate"), executedAction.eventSource);
    assertSame(processInstance.getRootToken(), executedAction.token);
    assertNull(executedAction.node);
  }

  public void testNestedSuperStateEnter() {
    processDefinition = ProcessDefinition.parseXmlString(
      "<process-definition>" +
      "  <start-state name='start'>" +
      "    <transition to='superstate/nestedsuperstate/insidenestedsuperstate'/>" +
      "  </start-state>" +
      "  <super-state name='superstate'>" +
      "    <event type='superstate-enter'>" +
      "      <action class='org.jbpm.graph.exe.SuperStateActionExecutionTest$Recorder' />" +
      "    </event>" +
      "    <super-state name='nestedsuperstate'>" +
      "      <event type='superstate-enter'>" +
      "        <action class='org.jbpm.graph.exe.SuperStateActionExecutionTest$Recorder' />" +
      "      </event>" +
      "      <state name='insidenestedsuperstate' />" +
      "    </super-state>" +
      "  </super-state>" +
      "</process-definition>"
    );
    // create the process instance
    processInstance = new ProcessInstance(processDefinition);
    processInstance.signal();
    assertEquals(3, executedActions.size());

    // the first action called is the superstate-enter on the 'superstate'
    ExecutedAction executedAction = (ExecutedAction) executedActions.get(0);
    assertEquals("superstate-enter", executedAction.event.getEventType());
    assertSame(processDefinition.getNode("superstate"), executedAction.event.getGraphElement());
    assertSame(processDefinition.getNode("superstate"), executedAction.eventSource);
    assertSame(processInstance.getRootToken(), executedAction.token);
    assertNull(executedAction.node);
    
    // the second action called is the superstate-enter on the 'nestedsuperstate'
    executedAction = (ExecutedAction) executedActions.get(1);
    assertEquals("superstate-enter", executedAction.event.getEventType());
    assertSame(processDefinition.findNode("superstate/nestedsuperstate"), executedAction.event.getGraphElement());
    assertSame(processDefinition.findNode("superstate/nestedsuperstate"), executedAction.eventSource);
    assertSame(processInstance.getRootToken(), executedAction.token);
    assertNull(executedAction.node);

    // the third action called is the *propagated* event of the 'nestedsuperstate' to the 'superstate'
    executedAction = (ExecutedAction) executedActions.get(2);
    assertEquals("superstate-enter", executedAction.event.getEventType());
    assertSame(processDefinition.findNode("superstate"), executedAction.event.getGraphElement());
    assertSame(processDefinition.findNode("superstate/nestedsuperstate"), executedAction.eventSource);
    assertSame(processInstance.getRootToken(), executedAction.token);
    assertNull(executedAction.node);
  }

  public void testSuperStateEnterViaTransitionToSuperState() {
    processDefinition = ProcessDefinition.parseXmlString(
      "<process-definition>" +
      "  <start-state name='start'>" +
      "    <transition to='superstate'/>" +
      "  </start-state>" +
      "  <super-state name='superstate'>" +
      "    <event type='superstate-enter'>" +
      "      <action class='org.jbpm.graph.exe.SuperStateActionExecutionTest$Recorder' />" +
      "    </event>" +
      "    <super-state name='nestedsuperstate'>" +
      "      <event type='superstate-enter'>" +
      "        <action class='org.jbpm.graph.exe.SuperStateActionExecutionTest$Recorder' />" +
      "      </event>" +
      "      <state name='insidenestedsuperstate' />" +
      "    </super-state>" +
      "  </super-state>" +
      "</process-definition>"
    );
    // create the process instance
    processInstance = new ProcessInstance(processDefinition);
    processInstance.signal();
    assertEquals(3, executedActions.size());

    // the first action called is the superstate-enter on the 'superstate'
    ExecutedAction executedAction = (ExecutedAction) executedActions.get(0);
    assertEquals("superstate-enter", executedAction.event.getEventType());
    assertSame(processDefinition.getNode("superstate"), executedAction.event.getGraphElement());
    assertSame(processDefinition.getNode("superstate"), executedAction.eventSource);
    assertSame(processInstance.getRootToken(), executedAction.token);
    assertNull(executedAction.node);
    
    // the second action called is the superstate-enter on the 'nestedsuperstate'
    executedAction = (ExecutedAction) executedActions.get(1);
    assertEquals("superstate-enter", executedAction.event.getEventType());
    assertSame(processDefinition.findNode("superstate/nestedsuperstate"), executedAction.event.getGraphElement());
    assertSame(processDefinition.findNode("superstate/nestedsuperstate"), executedAction.eventSource);
    assertSame(processInstance.getRootToken(), executedAction.token);
    assertNull(executedAction.node);

    // the third action called is the *propagated* event of the 'nestedsuperstate' to the 'superstate'
    executedAction = (ExecutedAction) executedActions.get(2);
    assertEquals("superstate-enter", executedAction.event.getEventType());
    assertSame(processDefinition.findNode("superstate"), executedAction.event.getGraphElement());
    assertSame(processDefinition.findNode("superstate/nestedsuperstate"), executedAction.eventSource);
    assertSame(processInstance.getRootToken(), executedAction.token);
    assertNull(executedAction.node);
  }

  public void testSuperStateLeave() {
    processDefinition = ProcessDefinition.parseXmlString(
      "<process-definition>" +
      "  <start-state name='start'>" +
      "    <transition to='superstate/stateinside'/>" +
      "  </start-state>" +
      "  <super-state name='superstate'>" +
      "    <event type='superstate-leave'>" +
      "      <action class='org.jbpm.graph.exe.SuperStateActionExecutionTest$Recorder' />" +
      "    </event>" +
      "    <state name='stateinside'>" +
      "      <transition to='../toplevelstate' />" +
      "    </state>" +
      "  </super-state>" +
      "  <state name='toplevelstate' />" +
      "</process-definition>"
    );
    // create the process instance
    processInstance = new ProcessInstance(processDefinition);
    // put the execution in the nestedsuperstate
    processInstance.signal();
    assertEquals(0, executedActions.size());
    
    // the next signal results in a node-enter internally to the superstate so it should have no effect.
    // by default, event propagation is turned on.  that is why we decided to have a separated event type for superstate leave and enter. 

⌨️ 快捷键说明

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