KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jbpm.taskmgmt.exe;
2
3 import java.util.Arrays JavaDoc;
4 import java.util.HashSet JavaDoc;
5 import java.util.Iterator JavaDoc;
6 import java.util.List JavaDoc;
7 import java.util.Set JavaDoc;
8
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.jbpm.db.AbstractDbTestCase;
12 import org.jbpm.graph.def.ProcessDefinition;
13 import org.jbpm.graph.exe.ExecutionContext;
14 import org.jbpm.graph.exe.ProcessInstance;
15 import org.jbpm.taskmgmt.def.AssignmentHandler;
16
17 public class TaskAssignmentDbTest extends AbstractDbTestCase {
18   
19   public static class JohnAssignmentHandler implements AssignmentHandler {
20     private static final long serialVersionUID = 1L;
21     public void assign(Assignable assignable, ExecutionContext executionContext) throws Exception JavaDoc {
22       assignable.setActorId("john");
23     }
24   }
25
26   public void testPersonalTasklist() {
27     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
28       "<process-definition>" +
29       " <start-state>" +
30       " <transition to='work' />" +
31       " </start-state>" +
32       " <task-node name='work'>" +
33       " <task name='feed the chickens'>" +
34       " <assignment class='org.jbpm.taskmgmt.exe.TaskAssignmentDbTest$JohnAssignmentHandler' />" +
35       " </task>" +
36       " <task name='walk the dog'>" +
37       " <assignment class='org.jbpm.taskmgmt.exe.TaskAssignmentDbTest$JohnAssignmentHandler' />" +
38       " </task>" +
39       " <task name='play with wife'>" +
40       " <assignment class='org.jbpm.taskmgmt.exe.TaskAssignmentDbTest$JohnAssignmentHandler' />" +
41       " </task>" +
42       " </task-node>" +
43       "</process-definition>"
44     );
45     graphSession.saveProcessDefinition(processDefinition);
46     
47     ProcessInstance processInstance = new ProcessInstance(processDefinition);
48     processInstance.signal();
49     graphSession.saveProcessInstance(processInstance);
50     
51     newTransaction();
52     
53     List JavaDoc taskInstances = taskMgmtSession.findTaskInstances("john");
54     assertNotNull(taskInstances);
55     assertEquals(3, taskInstances.size());
56     
57     Set JavaDoc expectedTaskNames = new HashSet JavaDoc(Arrays.asList(new String JavaDoc[]{"feed the chickens", "walk the dog", "play with wife"}));
58     Set JavaDoc retrievedTaskNames = new HashSet JavaDoc();
59     Iterator JavaDoc iter = taskInstances.iterator();
60     while (iter.hasNext()) {
61       TaskInstance taskInstance = (TaskInstance) iter.next();
62       
63       log.debug("task instance definition: "+taskInstance.getTask().getProcessDefinition());
64       log.debug("task instance taskmgmt definition: "+taskInstance.getTask().getTaskMgmtDefinition().getProcessDefinition());
65
66       retrievedTaskNames.add(taskInstance.getName());
67     }
68     assertEquals(expectedTaskNames, retrievedTaskNames);
69   }
70
71   public static class PoolAssignmentHandler implements AssignmentHandler {
72     private static final long serialVersionUID = 1L;
73     public void assign(Assignable assignable, ExecutionContext executionContext) throws Exception JavaDoc {
74       assignable.setPooledActors(new String JavaDoc[]{"john", "joe", "homer"});
75     }
76   }
77
78   public void testPooledTasklist() {
79     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
80       "<process-definition>" +
81       " <start-state>" +
82       " <transition to='work' />" +
83       " </start-state>" +
84       " <task-node name='work'>" +
85       " <task name='feed the chickens'>" +
86       " <assignment class='org.jbpm.taskmgmt.exe.TaskAssignmentDbTest$PoolAssignmentHandler' />" +
87       " </task>" +
88       " </task-node>" +
89       "</process-definition>"
90     );
91     graphSession.saveProcessDefinition(processDefinition);
92     
93     ProcessInstance processInstance = new ProcessInstance(processDefinition);
94     processInstance.signal();
95     graphSession.saveProcessInstance(processInstance);
96     
97     newTransaction();
98     
99     List JavaDoc taskInstances = taskMgmtSession.findPooledTaskInstances("john");
100     assertNotNull(taskInstances);
101     assertEquals(1, taskInstances.size());
102     TaskInstance taskInstance = (TaskInstance) taskInstances.get(0);
103     assertEquals("feed the chickens", taskInstance.getName());
104     assertNull(taskInstance.getActorId());
105   }
106   
107   private static final Log log = LogFactory.getLog(TaskAssignmentDbTest.class);
108   
109 }
110
Popular Tags