SequenceTest
csouillard Aug 10, 2007 3:14 AMAs we have problems commiting SequenceTest, we could always post the new code fo rthe current svn in this topic...
Revision 242
package org.jbpm.pvm; import java.util.HashSet; import java.util.List; import java.util.Set; import org.jbpm.JbpmTestCase; import org.jbpm.pvm.impl.ExecutionController; import org.jbpm.pvm.impl.NodeBehaviour; public class SequenceTest extends JbpmTestCase { static Set<String> recordedNodes = new HashSet<String>(); public static class Sequence implements NodeBehaviour { private static final long serialVersionUID = 1L; public void execute(ExecutionController execution) throws Exception { Node node = execution.getNode(); // get the list of nested nodes in the sequence List<Node> nesteNodes = node.getNodes(); // if this sequence has nested nodes if (nesteNodes != null && !nesteNodes.isEmpty()) { // take the first one Node firstNestedNode = nesteNodes.get(0); // indicate that firstNestedNode needs to be executed and then // this node should be signalled with signal value "1". // In this implementation, the signal value is used to encode // the index of the next nested node to be executed. String signal = "1"; execution.execute(firstNestedNode, signal); } else { execution.proceed(); } } public void signal(ExecutionController execution, String signal) throws Exception { Node node = execution.getNode(); List<Node> nestedNodes = node.getNodes(); Integer index = new Integer(signal); // if the index is equal to the size of the list of nested nodes if (nestedNodes.size() == index) { // we're done and the sequence is completed execution.proceed(); } else { // else just execute the next node and increment the signal to be // given next time to this node. Node nextNode = nestedNodes.get(index); execution.execute(nextNode, Integer.toString(index + 1)); } } } public static class Recorder implements NodeBehaviour { private static final long serialVersionUID = 1L; public void execute(ExecutionController execution) throws Exception { recordedNodes.add(execution.getNode().getName()); execution.proceed(); } public void signal(ExecutionController execution, String signal) throws Exception { throw new UnsupportedOperationException(); } } public void testThreeNodesInARow() { Process process = Process.build() .initial() .behaviour(new Sequence()) .nodes() .node("a") .behaviour(new WaitState()) .node("b") .behaviour(new WaitState()) .node("c") .behaviour(new WaitState()) .endNodes() .process(); Execution execution = process.newExecution(); assertEquals(process.getInitial().getNodesMap().get("a"), execution.getNode()); execution.signal(); assertEquals(process.getInitial().getNodesMap().get("b"), execution.getNode()); execution.signal(); assertEquals(process.getInitial().getNodesMap().get("c"), execution.getNode()); } public void testThreeNodesInARowAutomatic() { Process process = Process.build() .initial("s") .behaviour(new Sequence()) .nodes() .node("a") .behaviour(new Recorder()) .node("b") .behaviour(new Recorder()) .node("c") .behaviour(new Recorder()) .endNodes() .process(); Execution execution = process.newExecution(); Set<String> expected = new HashSet<String>(); expected.add("a"); expected.add("b"); expected.add("c"); assertEquals(expected, recordedNodes); assertTrue(execution.isEnded()); } }