KickJava   Java API By Example, From Geeks To Geeks.

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


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.Set JavaDoc;
7
8 import junit.framework.TestCase;
9
10 import org.jbpm.graph.def.ProcessDefinition;
11 import org.jbpm.graph.exe.ExecutionContext;
12 import org.jbpm.graph.exe.ProcessInstance;
13 import org.jbpm.security.Authentication;
14 import org.jbpm.taskmgmt.def.AssignmentHandler;
15
16 public class TaskAssignmentTest extends TestCase {
17   
18   public static class TestAssignmentHandler implements AssignmentHandler {
19     private static final long serialVersionUID = 1L;
20     public void assign(Assignable assignable, ExecutionContext executionContext) throws Exception JavaDoc {
21       assignable.setActorId("johndoe");
22     }
23   }
24   
25   public void testTaskAssignment() {
26     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
27       "<process-definition>" +
28       " <start-state>" +
29       " <transition to='a' />" +
30       " </start-state>" +
31       " <task-node name='a'>" +
32       " <task name='change nappy'>" +
33       " <assignment class='org.jbpm.taskmgmt.exe.TaskAssignmentTest$TestAssignmentHandler' />" +
34       " </task>" +
35       " </task-node>" +
36       "</process-definition>"
37     );
38     
39     ProcessInstance processInstance = new ProcessInstance(processDefinition);
40     processInstance.signal();
41     
42     TaskMgmtInstance taskMgmtInstance =processInstance.getTaskMgmtInstance();
43     TaskInstance changeNappy = (TaskInstance) taskMgmtInstance.getTaskInstances().iterator().next();
44     
45     assertEquals("johndoe", changeNappy.getActorId());
46     assertNull(changeNappy.getPooledActors());
47   }
48   
49   public static class MultipleAssignmentHandler implements AssignmentHandler {
50     private static final long serialVersionUID = 1L;
51     public void assign(Assignable assignable, ExecutionContext executionContext) throws Exception JavaDoc {
52       assignable.setPooledActors(new String JavaDoc[]{"me", "you", "them"});
53     }
54   }
55
56   public void testMultiActorTaskAssignment() {
57     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
58       "<process-definition>" +
59       " <start-state>" +
60       " <transition to='a' />" +
61       " </start-state>" +
62       " <task-node name='a'>" +
63       " <task name='change nappy'>" +
64       " <assignment class='org.jbpm.taskmgmt.exe.TaskAssignmentTest$MultipleAssignmentHandler' />" +
65       " </task>" +
66       " </task-node>" +
67       "</process-definition>"
68     );
69     
70     ProcessInstance processInstance = new ProcessInstance(processDefinition);
71     processInstance.signal();
72     
73     TaskMgmtInstance taskMgmtInstance = processInstance.getTaskMgmtInstance();
74     TaskInstance changeNappy = (TaskInstance) taskMgmtInstance.getTaskInstances().iterator().next();
75     
76     Set JavaDoc expectedActorIds = new HashSet JavaDoc( Arrays.asList(new Object JavaDoc[]{"me", "you", "them"}) );
77     
78     // collect actor ids from the task instances
79
Set JavaDoc pooledActorIds = new HashSet JavaDoc();
80     Iterator JavaDoc iter = changeNappy.getPooledActors().iterator();
81     while (iter.hasNext()) {
82       PooledActor pooledActor = (PooledActor) iter.next();
83       pooledActorIds.add(pooledActor.getActorId());
84       assertTrue(pooledActor.getTaskInstances().contains(changeNappy));
85       assertNull(pooledActor.getSwimlaneInstance());
86     }
87     
88     assertEquals(expectedActorIds, pooledActorIds);
89   }
90
91   public static class AssignmentHandlerContextTester implements AssignmentHandler {
92     private static final long serialVersionUID = 1L;
93     public void assign(Assignable assignable, ExecutionContext executionContext) throws Exception JavaDoc {
94     }
95   }
96   
97   public void testAssignmentHandlerContext() {
98     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
99       "<process-definition>" +
100       " <start-state>" +
101       " <transition to='a' />" +
102       " </start-state>" +
103       " <task-node name='a'>" +
104       " <task name='change nappy'>" +
105       " <assignment class='org.jbpm.taskmgmt.exe.TaskAssignmentTest$AssignmentHandlerContextTester' />" +
106       " </task>" +
107       " </task-node>" +
108       "</process-definition>"
109     );
110     
111     ProcessInstance processInstance = new ProcessInstance(processDefinition);
112     processInstance.signal();
113     
114     TaskMgmtInstance taskMgmtInstance = processInstance.getTaskMgmtInstance();
115     assertEquals(1, taskMgmtInstance.getTaskInstances().size());
116     TaskInstance changeNappy = (TaskInstance) taskMgmtInstance.getTaskInstances().iterator().next();
117     assertNull(changeNappy.getActorId());
118     assertNull(changeNappy.getPooledActors());
119   }
120   
121   public void testStartTaskSwimlaneAssignmentTest() {
122     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
123       "<process-definition>" +
124       " <swimlane name='initiator' />" +
125       " <start-state>" +
126       " <task name='start this process' swimlane='initiator' />" +
127       " <transition to='wait' />" +
128       " </start-state>" +
129       " <state name='wait' />" +
130       "</process-definition>"
131     );
132
133     ProcessInstance processInstance = new ProcessInstance(processDefinition);
134     TaskMgmtInstance taskMgmtInstance = processInstance.getTaskMgmtInstance();
135     
136     Authentication.pushAuthenticatedActorId("cookie monster");
137     try {
138       taskMgmtInstance.createStartTaskInstance().end();
139     } finally {
140       Authentication.popAuthenticatedActorId();
141     }
142     
143     SwimlaneInstance swimlaneInstance = taskMgmtInstance.getSwimlaneInstance("initiator");
144     assertNotNull(swimlaneInstance);
145     assertEquals("cookie monster", swimlaneInstance.getActorId());
146   }
147 }
148
Popular Tags