KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jbpm.jpdl.patterns;
2
3 import junit.framework.*;
4
5 import org.jbpm.graph.def.*;
6 import org.jbpm.graph.exe.*;
7
8 /**
9  * http://is.tm.tue.nl/research/patterns/download/swf/pat_3.swf
10  */

11 public class Wfp03SynchronizationTest extends TestCase {
12
13   public ProcessDefinition createSynchronizationProcessDefinition() {
14     ProcessDefinition pd = ProcessDefinition.parseXmlString(
15       "<process-definition>" +
16       " <start-state name='start'>" +
17       " <transition to='fork' />" +
18       " </start-state>" +
19       " <fork name='fork'>" +
20       " <transition name='first' to='one' />" +
21       " <transition name='second' to='two' />" +
22       " </fork>" +
23       " <state name='one'>" +
24       " <transition to='join' />" +
25       " </state>" +
26       " <state name='two'>" +
27       " <transition to='join' />" +
28       " </state>" +
29       " <join name='join'>" +
30       " <transition to='end' />" +
31       " </join>" +
32       " <end-state name='end' />" +
33       "</process-definition>"
34     );
35     return pd;
36   }
37
38   public void testSynchronizationFirstTokenFirst() {
39     ProcessDefinition pd = createSynchronizationProcessDefinition();
40           
41     ProcessInstance pi = new ProcessInstance( pd );
42     pi.signal();
43     Token root = pi.getRootToken();
44     Token firstToken = root.getChild( "first" );
45     Token secondToken = root.getChild( "second" );
46
47     // check that the two tokens are in the states one and two respectively
48
assertSame( pd.getNode("fork"), root.getNode() );
49     assertSame( pd.getNode("one"), firstToken.getNode() );
50     assertSame( pd.getNode("two"), secondToken.getNode() );
51
52     firstToken.signal();
53     assertSame( pd.getNode("fork"), root.getNode() );
54     assertSame( pd.getNode("join"), firstToken.getNode() );
55     assertSame( pd.getNode("two"), secondToken.getNode() );
56     
57     secondToken.signal();
58     assertSame( pd.getNode("end"), root.getNode() );
59     assertSame( pd.getNode("join"), firstToken.getNode() );
60     assertSame( pd.getNode("join"), secondToken.getNode() );
61   }
62
63   /**
64    * variation of the pattern where the second token fires first.
65    */

66   public void testSynchronizationSecondTokenFirst() {
67     ProcessDefinition pd = createSynchronizationProcessDefinition();
68           
69     ProcessInstance pi = new ProcessInstance( pd );
70     pi.signal();
71     Token root = pi.getRootToken();
72     Token firstToken = root.getChild( "first" );
73     Token secondToken = root.getChild( "second" );
74
75     // check that the two tokens are in the states one and two respectively
76
assertSame( pd.getNode("fork"), root.getNode() );
77     assertSame( pd.getNode("one"), firstToken.getNode() );
78     assertSame( pd.getNode("two"), secondToken.getNode() );
79
80     secondToken.signal();
81     assertSame( pd.getNode("fork"), root.getNode() );
82     assertSame( pd.getNode("one"), firstToken.getNode() );
83     assertSame( pd.getNode("join"), secondToken.getNode() );
84     
85     firstToken.signal();
86     assertSame( pd.getNode("end"), root.getNode() );
87     assertSame( pd.getNode("join"), firstToken.getNode() );
88     assertSame( pd.getNode("join"), secondToken.getNode() );
89   }
90
91   /**
92    * nested form of the synchronization pattern.
93    */

94   public ProcessDefinition createNestedSynchronizationProcessDefinition() {
95     // tip : draw this visually if you want to understand it.
96
ProcessDefinition pd = new ProcessDefinition(
97       new String JavaDoc[]{"start-state start",
98                    "fork fork",
99                    "fork fork1",
100                    "fork fork2",
101                    "state state1.1",
102                    "state state1.2",
103                    "state state2.1",
104                    "state state2.2",
105                    "join join2",
106                    "join join1",
107                    "join join",
108                    "end-state end"},
109       new String JavaDoc[]{"start --> fork",
110                    "fork --first--> fork1",
111                    "fork --second--> fork2",
112                    "fork1 --first--> state1.1",
113                    "fork1 --second--> state1.2",
114                    "fork2 --first--> state2.1",
115                    "fork2 --second--> state2.2",
116                    "state1.1 --> join1",
117                    "state1.2 --> join1",
118                    "state2.1 --> join2",
119                    "state2.2 --> join2",
120                    "join1 --> join",
121                    "join2 --> join",
122                    "join --> end"});
123     return pd;
124   }
125
126   public void testSynchronizationNested() {
127     ProcessDefinition pd = createNestedSynchronizationProcessDefinition();
128     ProcessInstance pi = new ProcessInstance( pd );
129     pi.signal();
130     Token rootToken = pi.getRootToken();
131     Token token1 = rootToken.getChild( "first" );
132     Token token2 = rootToken.getChild( "second" );
133     Token token11 = token1.getChild( "first" );
134     Token token12 = token1.getChild( "second" );
135     Token token21 = token2.getChild( "first" );
136     Token token22 = token2.getChild( "second" );
137     
138     assertSame( pd.getNode("fork"), rootToken.getNode() );
139     assertSame( pd.getNode("fork1"), token1.getNode() );
140     assertSame( pd.getNode("fork2"), token2.getNode() );
141     assertSame( pd.getNode("state1.1"), token11.getNode() );
142     assertSame( pd.getNode("state1.2"), token12.getNode() );
143     assertSame( pd.getNode("state2.1"), token21.getNode() );
144     assertSame( pd.getNode("state2.2"), token22.getNode() );
145     
146     token11.signal();
147
148     assertSame( pd.getNode("fork"), rootToken.getNode() );
149     assertSame( pd.getNode("fork1"), token1.getNode() );
150     assertSame( pd.getNode("fork2"), token2.getNode() );
151     assertSame( pd.getNode("join1"), token11.getNode() );
152     assertSame( pd.getNode("state1.2"), token12.getNode() );
153     assertSame( pd.getNode("state2.1"), token21.getNode() );
154     assertSame( pd.getNode("state2.2"), token22.getNode() );
155
156     token12.signal();
157
158     assertSame( pd.getNode("fork"), rootToken.getNode() );
159     assertSame( pd.getNode("join"), token1.getNode() );
160     assertSame( pd.getNode("fork2"), token2.getNode() );
161     assertSame( pd.getNode("join1"), token11.getNode() );
162     assertSame( pd.getNode("join1"), token12.getNode() );
163     assertSame( pd.getNode("state2.1"), token21.getNode() );
164     assertSame( pd.getNode("state2.2"), token22.getNode() );
165
166     token21.signal();
167
168     assertSame( pd.getNode("fork"), rootToken.getNode() );
169     assertSame( pd.getNode("join"), token1.getNode() );
170     assertSame( pd.getNode("fork2"), token2.getNode() );
171     assertSame( pd.getNode("join1"), token11.getNode() );
172     assertSame( pd.getNode("join1"), token12.getNode() );
173     assertSame( pd.getNode("join2"), token21.getNode() );
174     assertSame( pd.getNode("state2.2"), token22.getNode() );
175
176     token22.signal();
177
178     assertSame( pd.getNode("end"), rootToken.getNode() );
179     assertSame( pd.getNode("join"), token1.getNode() );
180     assertSame( pd.getNode("join"), token2.getNode() );
181     assertSame( pd.getNode("join1"), token11.getNode() );
182     assertSame( pd.getNode("join1"), token12.getNode() );
183     assertSame( pd.getNode("join2"), token21.getNode() );
184     assertSame( pd.getNode("join2"), token22.getNode() );
185   }
186 }
187
Popular Tags