1 package org.jbpm.jpdl.patterns; 2 3 import junit.framework.*; 4 5 import org.jbpm.context.def.*; 6 import org.jbpm.context.exe.*; 7 import org.jbpm.graph.action.Script; 8 import org.jbpm.graph.def.*; 9 import org.jbpm.graph.exe.*; 10 import org.jbpm.graph.node.*; 11 12 15 public class Wfp06MultiChoiceTest extends TestCase { 16 17 private static ProcessDefinition multiChoiceProcessDefinition = createMultiChoiceProcessDefinition(); 18 19 public static ProcessDefinition createMultiChoiceProcessDefinition() { 20 ProcessDefinition pd = new ProcessDefinition( 21 new String []{"start-state start", 22 "state a", 23 "fork multichoice", 24 "state b", 25 "state c", 26 "join syncmerge", 27 "end-state end"}, 28 new String []{"start --> a", 29 "a --> multichoice", 30 "multichoice --to b--> b", 31 "multichoice --to c--> c", 32 "b --> syncmerge", 33 "c --> syncmerge", 34 "syncmerge --> end"}); 35 36 Script script = new Script(); 38 script.addVariableAccess(new VariableAccess("transitionNames","write",null)); 39 script.setExpression( 40 "transitionNames = new ArrayList();" + 41 "if ( scenario == 1 ) {" + 42 " transitionNames.add( \"to b\" );" + 43 "} else if ( scenario == 2 ) {" + 44 " transitionNames.add( \"to c\" );" + 45 "} else if ( scenario >= 3 ) {" + 46 " transitionNames.add( \"to b\" );" + 47 " transitionNames.add( \"to c\" );" + 48 "}" ); 49 50 Fork fork = (Fork) pd.getNode("multichoice"); 52 fork.setScript( script ); 53 54 pd.addDefinition( new ContextDefinition() ); 55 56 return pd; 57 } 58 59 public void testMultiChoiceScenario1() { 60 ProcessDefinition pd = multiChoiceProcessDefinition; 61 Token root = executeScenario(pd, 1); 62 Token tokenB = root.getChild("to b"); Token tokenC = root.getChild("to c"); assertNotNull( tokenB ); 65 assertNull( tokenC ); 66 assertEquals( 1, root.getChildren().size() ); 67 assertSame( pd.getNode("b"), tokenB.getNode() ); 68 } 69 70 public void testMultiChoiceScenario2() { 71 ProcessDefinition pd = multiChoiceProcessDefinition; 72 Token root = executeScenario(pd, 2); 73 Token tokenB = root.getChild("to b"); Token tokenC = root.getChild("to c"); assertNull( tokenB ); 76 assertNotNull( tokenC ); 77 assertEquals( 1, root.getChildren().size() ); 78 assertSame( pd.getNode("c"), tokenC.getNode() ); 79 } 80 81 public void testMultiChoiceScenario3() { 82 ProcessDefinition pd = multiChoiceProcessDefinition; 83 Token root = executeScenario(pd, 3); 84 Token tokenB = root.getChild("to b"); Token tokenC = root.getChild("to c"); assertNotNull( tokenB ); 87 assertNotNull( tokenC ); 88 assertEquals( 2, root.getChildren().size() ); 89 assertSame( pd.getNode("b"), tokenB.getNode() ); 90 assertSame( pd.getNode("c"), tokenC.getNode() ); 91 } 92 93 public static Token executeScenario(ProcessDefinition pd, int scenario) { 94 ProcessInstance pi = new ProcessInstance( pd ); 95 ContextInstance ci = (ContextInstance) pi.getInstance( ContextInstance.class ); 96 pi.signal(); 97 Token root = pi.getRootToken(); 98 assertSame( pd.getNode("a"), root.getNode() ); 99 ci.setVariable( "scenario", new Integer (scenario) ); 100 root.signal(); 101 return root; 102 } 103 } 104 | Popular Tags |