KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > jpdl > patterns > Wfp06MultiChoiceTest


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 /**
13  * http://is.tm.tue.nl/research/patterns/download/swf/pat_6.swf
14  */

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 JavaDoc[]{"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 JavaDoc[]{"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     // create the script
37
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     // put the script in the multichoice handler
51
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"); // the default token names are extracted from the leaving transitions
63
Token tokenC = root.getChild("to c"); // the default token names are extracted from the leaving transitions
64
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"); // the default token names are extracted from the leaving transitions
74
Token tokenC = root.getChild("to c"); // the default token names are extracted from the leaving transitions
75
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"); // the default token names are extracted from the leaving transitions
85
Token tokenC = root.getChild("to c"); // the default token names are extracted from the leaving transitions
86
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 JavaDoc(scenario) );
100     root.signal();
101     return root;
102   }
103 }
104
Popular Tags