KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > taskmgmt > exe > TaskExecutionTest


1 package org.jbpm.taskmgmt.exe;
2
3 import java.util.Iterator JavaDoc;
4
5 import junit.framework.TestCase;
6
7 import org.jbpm.graph.def.ProcessDefinition;
8 import org.jbpm.graph.def.Transition;
9 import org.jbpm.graph.exe.ProcessInstance;
10 import org.jbpm.graph.exe.Token;
11
12 public class TaskExecutionTest extends TestCase {
13
14   public void testSignalLast() {
15     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
16       "<process-definition>" +
17       " <start-state>" +
18       " <transition to='a' />" +
19       " </start-state>" +
20       " <task-node name='a'>" +
21       " <task name='laundry' />" +
22       " <task name='dishes' />" +
23       " <task name='change nappy' />" +
24       " <transition to='b' />" +
25       " </task-node>" +
26       " <state name='b' />" +
27       "</process-definition>"
28     );
29     ProcessInstance processInstance = new ProcessInstance(processDefinition);
30     Token token = processInstance.getRootToken();
31     processInstance.signal();
32
33     TaskMgmtInstance tmi = (TaskMgmtInstance) processInstance.getInstance(TaskMgmtInstance.class);
34     assertNotNull(tmi);
35     assertEquals(3, tmi.getTaskInstances().size());
36     
37     Iterator JavaDoc iter = tmi.getTaskInstances().iterator();
38     while (iter.hasNext()) {
39       // before every task is completed, check if the token is still in node a
40
assertSame(processDefinition.getNode("a"), token.getNode());
41       TaskInstance taskInstance = (TaskInstance) iter.next();
42       taskInstance.end();
43     }
44
45     // after the 3 tasks have been completed, check if the token has moved to b
46
assertSame(processDefinition.getNode("b"), token.getNode());
47   }
48
49   public void testSignalFirst() {
50     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
51       "<process-definition>" +
52       " <start-state>" +
53       " <transition to='a' />" +
54       " </start-state>" +
55       " <task-node name='a' signal='first'>" +
56       " <task name='laundry' />" +
57       " <task name='dishes' />" +
58       " <task name='change nappy' />" +
59       " <transition to='b' />" +
60       " </task-node>" +
61       " <state name='b' />" +
62       "</process-definition>"
63     );
64     ProcessInstance processInstance = new ProcessInstance(processDefinition);
65     Token token = processInstance.getRootToken();
66     processInstance.signal();
67
68     TaskMgmtInstance tmi = (TaskMgmtInstance) processInstance.getInstance(TaskMgmtInstance.class);
69     assertNotNull(tmi);
70     assertEquals(3, tmi.getTaskInstances().size());
71     
72     // only before the first task, the token should be in a
73
assertSame(processDefinition.getNode("a"), token.getNode());
74
75     Iterator JavaDoc iter = tmi.getTaskInstances().iterator();
76     while (iter.hasNext()) {
77       // before every task is completed, check if the token is still in node a
78
TaskInstance taskInstance = (TaskInstance) iter.next();
79       taskInstance.end();
80
81       // after each task, check if the token has moved to b
82
assertSame(processDefinition.getNode("b"), token.getNode());
83     }
84
85   }
86
87   public void testSignalNever() {
88     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
89       "<process-definition>" +
90       " <start-state>" +
91       " <transition to='a' />" +
92       " </start-state>" +
93       " <task-node name='a' signal='never'>" +
94       " <task name='laundry' />" +
95       " <task name='dishes' />" +
96       " <task name='change nappy' />" +
97       " <transition to='b' />" +
98       " </task-node>" +
99       " <state name='b' />" +
100       "</process-definition>"
101     );
102     ProcessInstance processInstance = new ProcessInstance(processDefinition);
103     Token token = processInstance.getRootToken();
104     processInstance.signal();
105
106     TaskMgmtInstance tmi = (TaskMgmtInstance) processInstance.getInstance(TaskMgmtInstance.class);
107     assertNotNull(tmi);
108     assertEquals(3, tmi.getTaskInstances().size());
109     
110     // only before the first task, the token should be in a
111
assertSame(processDefinition.getNode("a"), token.getNode());
112
113     Iterator JavaDoc iter = tmi.getTaskInstances().iterator();
114     while (iter.hasNext()) {
115       // before every task is completed, check if the token is still in node a
116
TaskInstance taskInstance = (TaskInstance) iter.next();
117       taskInstance.end();
118
119       // after each task, check if the token remains in a
120
assertSame(processDefinition.getNode("a"), token.getNode());
121     }
122     // signal='never' is used when an external trigger should trigger execution, without
123
// any relation to the tasks finishing
124
processInstance.signal();
125     assertSame(processDefinition.getNode("b"), token.getNode());
126   }
127
128   public void testSignalUnsynchronized() {
129     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
130       "<process-definition>" +
131       " <start-state>" +
132       " <transition to='a' />" +
133       " </start-state>" +
134       " <task-node name='a' signal='unsynchronized'>" +
135       " <task name='laundry' />" +
136       " <task name='dishes' />" +
137       " <task name='change nappy' />" +
138       " <transition to='b' />" +
139       " </task-node>" +
140       " <state name='b' />" +
141       "</process-definition>"
142     );
143     ProcessInstance processInstance = new ProcessInstance(processDefinition);
144     Token token = processInstance.getRootToken();
145     processInstance.signal();
146
147     TaskMgmtInstance tmi = (TaskMgmtInstance) processInstance.getInstance(TaskMgmtInstance.class);
148     assertNotNull(tmi);
149     assertEquals(3, tmi.getTaskInstances().size());
150     
151     // unsyncronized means that after creation of the tasks, execution should just continue.
152
assertSame(processDefinition.getNode("b"), token.getNode());
153
154     Iterator JavaDoc iter = tmi.getTaskInstances().iterator();
155     while (iter.hasNext()) {
156       // before every task is completed, check if the token is still in node a
157
TaskInstance taskInstance = (TaskInstance) iter.next();
158       taskInstance.end();
159
160       // after each task, check if the token remains in b
161
assertSame(processDefinition.getNode("b"), token.getNode());
162     }
163   }
164
165   public void testCreateTasksDisabled() {
166     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
167       "<process-definition>" +
168       " <start-state>" +
169       " <transition to='a' />" +
170       " </start-state>" +
171       " <task-node name='a' create-tasks='false'>" +
172       " <task name='laundry' />" +
173       " <task name='dishes' />" +
174       " <task name='change nappy' />" +
175       " <transition to='b' />" +
176       " </task-node>" +
177       " <state name='b' />" +
178       "</process-definition>"
179     );
180     ProcessInstance processInstance = new ProcessInstance(processDefinition);
181     Token token = processInstance.getRootToken();
182     processInstance.signal();
183
184     TaskMgmtInstance tmi = (TaskMgmtInstance) processInstance.getInstance(TaskMgmtInstance.class);
185     assertNotNull(tmi);
186     assertNull(tmi.getTaskInstances());
187     
188     // if no tasks are created and signal is last (default) then execution continues
189
assertSame(processDefinition.getNode("b"), token.getNode());
190   }
191
192   public void testNonBlockingTask() {
193     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
194       "<process-definition>" +
195       " <start-state>" +
196       " <transition to='a' />" +
197       " </start-state>" +
198       " <task-node name='a'>" +
199       " <task name='laundry' />" +
200       " <transition to='b' />" +
201       " </task-node>" +
202       " <state name='b' />" +
203       "</process-definition>"
204     );
205     ProcessInstance processInstance = new ProcessInstance(processDefinition);
206     Token token = processInstance.getRootToken();
207     processInstance.signal();
208     assertSame(processDefinition.getNode("a"), token.getNode());
209     processInstance.signal();
210     assertSame(processDefinition.getNode("b"), token.getNode());
211   }
212
213   public void testBlockingTask() {
214     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
215       "<process-definition>" +
216       " <start-state>" +
217       " <transition to='a' />" +
218       " </start-state>" +
219       " <task-node name='a'>" +
220       " <task name='laundry' blocking='true' />" +
221       " <transition to='b' />" +
222       " </task-node>" +
223       " <state name='b' />" +
224       "</process-definition>"
225     );
226     ProcessInstance processInstance = new ProcessInstance(processDefinition);
227     Token token = processInstance.getRootToken();
228     processInstance.signal();
229     assertSame(processDefinition.getNode("a"), token.getNode());
230     try {
231       processInstance.signal();
232       fail("expected exception");
233     } catch (IllegalStateException JavaDoc e) {
234       // OK
235
}
236   }
237
238   public void testTransitionNameInTaskEnd() {
239     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
240       "<process-definition>" +
241       " <start-state>" +
242       " <transition to='t' />" +
243       " </start-state>" +
244       " <task-node name='t'>" +
245       " <task name='change nappy' />" +
246       " <transition name='ok' to='a' />" +
247       " <transition name='messed all over the floor' to='b' />" +
248       " </task-node>" +
249       " <state name='a' />" +
250       " <state name='b' />" +
251       "</process-definition>"
252     );
253     ProcessInstance processInstance = new ProcessInstance(processDefinition);
254     Token token = processInstance.getRootToken();
255     processInstance.signal();
256     assertSame(processDefinition.getNode("t"), token.getNode());
257
258     TaskMgmtInstance tmi = (TaskMgmtInstance) processInstance.getInstance(TaskMgmtInstance.class);
259     TaskInstance taskInstance = (TaskInstance) tmi.getUnfinishedTasks(token).iterator().next();
260     taskInstance.end("messed all over the floor");
261     
262     assertSame(processDefinition.getNode("b"), token.getNode());
263   }
264
265   public void testTransitionInTaskEnd() {
266     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
267       "<process-definition>" +
268       " <start-state>" +
269       " <transition to='t' />" +
270       " </start-state>" +
271       " <task-node name='t'>" +
272       " <task name='change nappy' />" +
273       " <transition name='ok' to='a' />" +
274       " <transition name='messed all over the floor' to='b' />" +
275       " </task-node>" +
276       " <state name='a' />" +
277       " <state name='b' />" +
278       "</process-definition>"
279     );
280     ProcessInstance processInstance = new ProcessInstance(processDefinition);
281     Token token = processInstance.getRootToken();
282     processInstance.signal();
283     assertSame(processDefinition.getNode("t"), token.getNode());
284
285     TaskMgmtInstance tmi = (TaskMgmtInstance) processInstance.getInstance(TaskMgmtInstance.class);
286     TaskInstance taskInstance = (TaskInstance) tmi.getUnfinishedTasks(token).iterator().next();
287     
288     Transition messedTransition = processDefinition.getNode("t").getLeavingTransition("messed all over the floor");
289     taskInstance.end(messedTransition);
290     
291     assertSame(processDefinition.getNode("b"), token.getNode());
292   }
293 }
294
Popular Tags