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

📄 actionexecutiontest.java

📁 jbpm demo 是一款非常不错的开源工作流,简单易用,适合扩张开发!
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    ExecutedAction executedAction = findExecutedAction(Event.EVENTTYPE_AFTER_SIGNAL);
    assertSame(getNode("state"), executedAction.node);
    assertSame(getNode("start"), executedAction.event.getGraphElement());

    // leave the state by sending another signal
    processInstance.signal();
    assertEquals(1, executedActions.size());
  }
  
  public void testNodeEnterEvent() {
    processDefinition = ProcessDefinition.parseXmlString(
      "<process-definition>" +
      "  <start-state name='start'>" +
      "    <transition to='state'/>" +
      "  </start-state>" +
      "  <state name='state'>" +
      "    <event type='node-enter'>" +
      "      <action class='org.jbpm.graph.exe.ActionExecutionTest$Recorder' />" +
      "    </event>" +
      "    <transition to='end'/>" +
      "  </state>" +
      "  <end-state name='end'/>" +
      "</process-definition>"
    );
    // create the process instance
    processInstance = new ProcessInstance(processDefinition);
    assertEquals(0, executedActions.size());

    // leave the start state by sending a signal
    processInstance.signal();
    assertEquals(1, executedActions.size());
    ExecutedAction executedAction = findExecutedAction(Event.EVENTTYPE_NODE_ENTER);
    assertSame(getNode("state"), executedAction.node);
    assertSame(getNode("state"), executedAction.event.getGraphElement());

    // leave the state by sending another signal
    processInstance.signal();
    assertEquals(1, executedActions.size());
  }

  public void testNodeLeaveEvent() {
    processDefinition = ProcessDefinition.parseXmlString(
      "<process-definition>" +
      "  <start-state name='start'>" +
      "    <transition to='state'/>" +
      "  </start-state>" +
      "  <state name='state'>" +
      "    <event type='node-leave'>" +
      "      <action class='org.jbpm.graph.exe.ActionExecutionTest$Recorder' />" +
      "    </event>" +
      "    <transition to='end'/>" +
      "  </state>" +
      "  <end-state name='end'/>" +
      "</process-definition>"
    );
    // create the process instance
    processInstance = new ProcessInstance(processDefinition);
    processInstance.signal();
    assertEquals(0, executedActions.size());

    // leave the start state by sending a signal
    processInstance.signal();
    assertEquals(1, executedActions.size());
    ExecutedAction executedAction = findExecutedAction(Event.EVENTTYPE_NODE_LEAVE);
    assertSame(getNode("state"), executedAction.node);
    assertSame(getNode("state"), executedAction.event.getGraphElement());
  }

  public void testTransitionEvent() {
    processDefinition = ProcessDefinition.parseXmlString(
      "<process-definition>" +
      "  <start-state name='start'>" +
      "    <transition to='state'/>" +
      "  </start-state>" +
      "  <state name='state'>" +
      "    <transition to='end'>" +
      "      <action class='org.jbpm.graph.exe.ActionExecutionTest$Recorder' />" +
      "    </transition>" +
      "  </state>" +
      "  <end-state name='end'/>" +
      "</process-definition>"
    );
    // create the process instance
    processInstance = new ProcessInstance(processDefinition);
    processInstance.signal();
    assertEquals(0, executedActions.size());

    // leave the start state by sending a signal
    processInstance.signal();
    assertEquals(1, executedActions.size());
    ExecutedAction executedAction = findExecutedAction(Event.EVENTTYPE_TRANSITION);
    assertNull(executedAction.node);
    assertSame(getNode("state").getDefaultLeavingTransition(), executedAction.event.getGraphElement());
  }
  
  static List sequence = new ArrayList();
  public static class SequenceRecorder implements ActionHandler {
    private static final long serialVersionUID = 1L;
    public void execute(ExecutionContext executionContext) throws Exception {
      Event event = executionContext.getEvent();
      sequence.add(event.getGraphElement().getName()+" "+event.getEventType());
    }
  }
  
  public void testExecutionSequence() {
    processDefinition = ProcessDefinition.parseXmlString(
      "<process-definition name='process'>" +
      "  <event type='process-start'><action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' /></event>" +
      "  <start-state name='start-state'>" +
      "    <event type='node-enter'><action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' /></event>" +
      "    <event type='node-leave'><action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' /></event>" +
      "    <event type='before-signal'><action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' /></event>" +
      "    <event type='after-signal'><action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' /></event>" +
      "    <transition name='start-to-state' to='state'>" +
      "      <action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' />" +
      "    </transition>" +
      "  </start-state>" +
      "  <state name='state'>" +
      "    <event type='node-enter'><action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' /></event>" +
      "    <event type='node-leave'><action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' /></event>" +
      "    <event type='before-signal'><action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' /></event>" +
      "    <event type='after-signal'><action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' /></event>" +
      "    <transition name='state-to-end' to='end-state'>" +
      "      <action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' />" +
      "    </transition>" +
      "  </state>" +
      "  <end-state name='end-state'>" +
      "    <event type='node-enter'><action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' /></event>" +
      "    <event type='node-leave'><action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' /></event>" +
      "    <event type='before-signal'><action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' /></event>" +
      "    <event type='after-signal'><action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' /></event>" +
      "  </end-state>" +
      "  <event type='process-end'><action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' /></event>" +
      "</process-definition>"
    );
    
    // create the process instance
    processInstance = new ProcessInstance(processDefinition);
    processInstance.signal();
    processInstance.signal();
    
    // format of the sequence messages : 
    //   node-name event-type
    // separated by a space
    assertEquals("process process-start", sequence.get(0));
    assertEquals("start-state before-signal", sequence.get(1));
    assertEquals("start-state node-leave", sequence.get(2));
    assertEquals("start-to-state transition", sequence.get(3));
    assertEquals("state node-enter", sequence.get(4));
    assertEquals("start-state after-signal", sequence.get(5));
    assertEquals("state before-signal", sequence.get(6));
    assertEquals("state node-leave", sequence.get(7));
    assertEquals("state-to-end transition", sequence.get(8));
    assertEquals("end-state node-enter", sequence.get(9));
    assertEquals("process process-end", sequence.get(10));
    assertEquals("state after-signal", sequence.get(11));
  }

  private Node getNode(String nodeName) {
    return processDefinition.getNode(nodeName);
  }

  private ExecutedAction findExecutedAction(String eventType) {
    Iterator iter = executedActions.iterator();
    while (iter.hasNext()) {
      ExecutedAction executedAction = (ExecutedAction) iter.next();
      if (eventType.equals(executedAction.event.getEventType())) {
        return executedAction;
      }
    }
    throw new RuntimeException("no action was executed on eventtype '"+eventType+"'");
  }
  
  public static class ProblematicActionHandler implements ActionHandler {
    private static final long serialVersionUID = 1L;
    public void execute(ExecutionContext executionContext) throws Exception {
      throw new IllegalArgumentException("problematic problem");
    }
  }
  
  public void testProblematicReferencedAction() {
    processDefinition = ProcessDefinition.parseXmlString(
      "<process-definition>" + 
      "  <start-state name='start'>" + 
      "    <transition to='state'>" + 
      "      <action ref-name='problematic action'/>" + 
      "    </transition>"+ 
      "  </start-state>" + 
      "  <state name='state' />" + 
      "  <action name='problematic action' class='org.jbpm.graph.exe.ActionExecutionTest$$ProblematicActionHandler'/>"+ 
      "</process-definition>");
    
    // create the process instance
    processInstance = new ProcessInstance(processDefinition);
    try {
      processInstance.signal();
      fail("expected exception");
    } catch (DelegationException e) {
      // OK
    }
  }

  public static class SignallingActionHandler implements ActionHandler {
    private static final long serialVersionUID = 1L;
    public void execute(ExecutionContext executionContext) throws Exception {
      executionContext.getToken().signal();
    }
  }
  
  public void testAttemptToSignalInAnAction() {
    processDefinition = ProcessDefinition.parseXmlString(
      "<process-definition>" + 
      "  <start-state name='start'>" + 
      "    <transition to='state'/>" +
      "    <event type='node-leave'>" + 
      "      <action class='org.jbpm.graph.exe.ActionExecutionTest$SignallingActionHandler'/>" + 
      "    </event>"+ 
      "  </start-state>" + 
      "  <state name='state' />" + 
      "</process-definition>");
    
    // create the process instance
    processInstance = new ProcessInstance(processDefinition);
    try {
      processInstance.signal();
      fail("expected exception");
    } catch (JbpmException e) {
      // OK
    }
  }
}

⌨️ 快捷键说明

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