KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > tutorial > taskmgmt > TaskAssignmentTest


1 package org.jbpm.tutorial.taskmgmt;
2
3 import junit.framework.TestCase;
4
5 import org.jbpm.graph.def.ProcessDefinition;
6 import org.jbpm.graph.exe.ProcessInstance;
7 import org.jbpm.graph.exe.Token;
8 import org.jbpm.taskmgmt.exe.TaskInstance;
9
10 public class TaskAssignmentTest extends TestCase {
11
12   public void testTaskAssignment() {
13     // The process shown below is based on the hello world process.
14
// The state node is replaced by a task-node. The task-node
15
// is a node in JPDL that represents a wait state and generates
16
// task(s) to be completed before the process can continue to
17
// execute.
18
ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
19       "<process-definition name='the baby process'>" +
20       " <start-state>" +
21       " <transition name='baby cries' to='t' />" +
22       " </start-state>" +
23       " <task-node name='t'>" +
24       " <task name='change nappy'>" +
25       " <assignment class='org.jbpm.tutorial.taskmgmt.NappyAssignmentHandler' />" +
26       " </task>" +
27       " <transition to='end' />" +
28       " </task-node>" +
29       " <end-state name='end' />" +
30       "</process-definition>"
31     );
32     
33     // Create an execution of the process definition.
34
ProcessInstance processInstance =
35         new ProcessInstance(processDefinition);
36     Token token = processInstance.getRootToken();
37     
38     // Let's start the process execution, leaving the start-state
39
// over its default transition.
40
token.signal();
41     // The signal method will block until the process execution
42
// enters a wait state. In this case, that is the task-node.
43
assertSame(processDefinition.getNode("t"), token.getNode());
44
45     // When execution arrived in the task-node, a task 'change nappy'
46
// was created and the NappyAssignmentHandler was called to determine
47
// to whom the task should be assigned. The NappyAssignmentHandler
48
// returned 'papa'.
49

50     // In a real environment, the tasks would be fetched from the
51
// database with the methods in the org.jbpm.db.TaskMgmtSession.
52
// Since we don't want to include the persistence complexity in
53
// this example, we just take the first task-instance of this
54
// process instance (we know there is only one in this test
55
// scenario.
56
TaskInstance taskInstance = (TaskInstance)
57         processInstance
58           .getTaskMgmtInstance()
59           .getTaskInstances()
60           .iterator().next();
61
62     // Now, we check if the taskInstance was actually assigned to 'papa'.
63
assertEquals("papa", taskInstance.getActorId() );
64     
65     // Now suppose that 'papa' has done his duties and marks the task
66
// as done.
67
taskInstance.end();
68     // Since this was the last (only) task to do, the completion of this
69
// task triggered the continuation of the process instance execution.
70

71     assertSame(processDefinition.getNode("end"), token.getNode());
72   }
73
74 }
75
Popular Tags