KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jbpm.taskmgmt.exe;
2
3 import java.util.Arrays JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List 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.taskmgmt.def.AssignmentHandler;
14
15 public class SwimlaneTest extends TestCase {
16   
17   static int invocationCount = 0;
18   
19   public void setUp() {
20     invocationCount = 0;
21   }
22   
23   public static class TestAssignmentHandler implements AssignmentHandler {
24     private static final long serialVersionUID = 1L;
25     public void assign(Assignable assignable, ExecutionContext executionContext) throws Exception JavaDoc {
26       invocationCount++;
27       assignable.setActorId("me");
28     }
29   }
30   
31   public void testSwimlaneActorInitialization() {
32     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
33       "<process-definition>" +
34       " <swimlane name='stalker'>" +
35       " <assignment class='org.jbpm.taskmgmt.exe.SwimlaneTest$TestAssignmentHandler' />" +
36       " </swimlane>" +
37       " <start-state>" +
38       " <transition to='a' />" +
39       " </start-state>" +
40       " <task-node name='a'>" +
41       " <task name='change nappy' swimlane='stalker' />" +
42       " </task-node>" +
43       "</process-definition>"
44     );
45     
46     ProcessInstance processInstance = new ProcessInstance(processDefinition);
47     processInstance.signal();
48     
49     TaskMgmtInstance taskMgmtInstance = processInstance.getTaskMgmtInstance();
50     TaskInstance changeNappy = (TaskInstance) taskMgmtInstance.getTaskInstances().iterator().next();
51     SwimlaneInstance stalker = taskMgmtInstance.getSwimlaneInstance("stalker");
52     assertNotNull(stalker);
53     assertEquals(1, invocationCount);
54     assertEquals("me", stalker.getActorId());
55     assertEquals("me", changeNappy.getActorId());
56   }
57   
58   public void testSwimlaneActorReassignment() {
59     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
60       "<process-definition>" +
61       " <swimlane name='hero'>" +
62       " <assignment class='org.jbpm.taskmgmt.exe.SwimlaneTest$TestAssignmentHandler' />" +
63       " </swimlane>" +
64       " <start-state>" +
65       " <transition to='a' />" +
66       " </start-state>" +
67       " <task-node name='a'>" +
68       " <task name='change nappy' swimlane='hero' />'" +
69       " <transition to='b' />" +
70       " </task-node>" +
71       " <task-node name='b'>" +
72       " <task name='make bottle' swimlane='hero' />'" +
73       " <transition to='end' />" +
74       " </task-node>" +
75       " <end-state name='end' />" +
76       "</process-definition>"
77     );
78
79     ProcessInstance processInstance = new ProcessInstance(processDefinition);
80     processInstance.signal();
81     
82     TaskMgmtInstance taskMgmtInstance = processInstance.getTaskMgmtInstance();
83     TaskInstance changeNappy = (TaskInstance) taskMgmtInstance.getTaskInstances().iterator().next();
84     assertEquals("me", changeNappy.getActorId());
85     assertEquals(1, invocationCount);
86     changeNappy.end();
87
88     TaskInstance makeBottle = (TaskInstance) taskMgmtInstance.getUnfinishedTasks(processInstance.getRootToken()).iterator().next();
89     assertEquals("me", makeBottle.getActorId());
90     assertEquals(1, invocationCount);
91   }
92
93   
94   public static class MultipleAssignmentHandler implements AssignmentHandler {
95     private static final long serialVersionUID = 1L;
96     public void assign(Assignable assignable, ExecutionContext executionContext) throws Exception JavaDoc {
97       invocationCount++;
98       assignable.setPooledActors(new String JavaDoc[]{"me", "you", "them"});
99     }
100   }
101   public void testSwimlanePoolInitialization() {
102     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
103       "<process-definition>" +
104       " <swimlane name='hero'>" +
105       " <assignment class='org.jbpm.taskmgmt.exe.SwimlaneTest$MultipleAssignmentHandler' />" +
106       " </swimlane>" +
107       " <start-state>" +
108       " <transition to='a' />" +
109       " </start-state>" +
110       " <task-node name='a'>" +
111       " <task name='change nappy' swimlane='hero' />'" +
112       " </task-node>" +
113       "</process-definition>"
114     );
115
116     ProcessInstance processInstance = new ProcessInstance(processDefinition);
117     processInstance.signal();
118     TaskMgmtInstance taskMgmtInstance = processInstance.getTaskMgmtInstance();
119     TaskInstance changeNappy = (TaskInstance) taskMgmtInstance.getTaskInstances().iterator().next();
120     assertNull(changeNappy.getActorId());
121     Set JavaDoc pooledActors = changeNappy.getPooledActors();
122     assertEquals(3, pooledActors.size());
123     assertEquals(1, invocationCount);
124     
125     List JavaDoc expectedPooledActorIds = Arrays.asList(new String JavaDoc[]{"me", "you", "them"});
126     Iterator JavaDoc iter = pooledActors.iterator();
127     while (iter.hasNext()) {
128       PooledActor pooledActor = (PooledActor) iter.next();
129       assertTrue(expectedPooledActorIds.contains(pooledActor.getActorId()));
130     }
131   }
132
133   public void testSwimlanePoolReassignmentOfNonTakenTask() {
134     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
135       "<process-definition>" +
136       " <swimlane name='hero'>" +
137       " <assignment class='org.jbpm.taskmgmt.exe.SwimlaneTest$MultipleAssignmentHandler' />" +
138       " </swimlane>" +
139       " <start-state>" +
140       " <transition to='a' />" +
141       " </start-state>" +
142       " <task-node name='a'>" +
143       " <task name='change nappy' swimlane='hero' />'" +
144       " <transition to='b' />" +
145       " </task-node>" +
146       " <task-node name='b'>" +
147       " <task name='make bottle' swimlane='hero' />'" +
148       " <transition to='end' />" +
149       " </task-node>" +
150       " <end-state name='end' />" +
151       "</process-definition>"
152     );
153
154     ProcessInstance processInstance = new ProcessInstance(processDefinition);
155     processInstance.signal();
156     TaskMgmtInstance taskMgmtInstance = processInstance.getTaskMgmtInstance();
157     SwimlaneInstance swimlaneInstance = taskMgmtInstance.getSwimlaneInstance("hero");
158     TaskInstance changeNappy = (TaskInstance) taskMgmtInstance.getTaskInstances().iterator().next();
159     
160     assertNull(changeNappy.getActorId());
161     assertEquals(3, changeNappy.getPooledActors().size());
162     assertEquals(changeNappy.getPooledActors(), swimlaneInstance.getPooledActors());
163     
164     changeNappy.end();
165
166     assertEquals(1, invocationCount);
167
168     TaskInstance makeBottle = (TaskInstance) taskMgmtInstance.getUnfinishedTasks(processInstance.getRootToken()).iterator().next();
169     assertNull(makeBottle.getActorId());
170     assertEquals(3, makeBottle.getPooledActors().size());
171     assertEquals(makeBottle.getPooledActors(), swimlaneInstance.getPooledActors());
172   }
173
174   public void testSwimlanePoolReassignmentOfTakenTask() {
175     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
176       "<process-definition>" +
177       " <swimlane name='hero'>" +
178       " <assignment class='org.jbpm.taskmgmt.exe.SwimlaneTest$MultipleAssignmentHandler' />" +
179       " </swimlane>" +
180       " <start-state>" +
181       " <transition to='a' />" +
182       " </start-state>" +
183       " <task-node name='a'>" +
184       " <task name='change nappy' swimlane='hero' />'" +
185       " <transition to='b' />" +
186       " </task-node>" +
187       " <task-node name='b'>" +
188       " <task name='make bottle' swimlane='hero' />'" +
189       " <transition to='end' />" +
190       " </task-node>" +
191       " <end-state name='end' />" +
192       "</process-definition>"
193     );
194
195     ProcessInstance processInstance = new ProcessInstance(processDefinition);
196     processInstance.signal();
197     TaskMgmtInstance taskMgmtInstance = processInstance.getTaskMgmtInstance();
198     SwimlaneInstance swimlaneInstance = taskMgmtInstance.getSwimlaneInstance("hero");
199     TaskInstance changeNappy = (TaskInstance) taskMgmtInstance.getTaskInstances().iterator().next();
200     changeNappy.setActorId("should-be-one-of-the-actors-in-the-pool-but-doesnt-have-to");
201     
202     assertEquals("should-be-one-of-the-actors-in-the-pool-but-doesnt-have-to", changeNappy.getActorId());
203     assertEquals(3, changeNappy.getPooledActors().size());
204     assertEquals(changeNappy.getPooledActors(), swimlaneInstance.getPooledActors());
205     
206     changeNappy.end();
207
208     assertEquals(1, invocationCount);
209
210     TaskInstance makeBottle = (TaskInstance) taskMgmtInstance.getUnfinishedTasks(processInstance.getRootToken()).iterator().next();
211     assertEquals("should-be-one-of-the-actors-in-the-pool-but-doesnt-have-to", makeBottle.getActorId());
212     assertEquals(3, makeBottle.getPooledActors().size());
213     assertEquals(makeBottle.getPooledActors(), swimlaneInstance.getPooledActors());
214   }
215
216   public static class NullAssignmentHandler implements AssignmentHandler {
217     private static final long serialVersionUID = 1L;
218     public void assign(Assignable assignable, ExecutionContext executionContext) throws Exception JavaDoc {
219     }
220   }
221   public void testNullActorsForSwimlaneInitialization() {
222     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
223       "<process-definition>" +
224       " <swimlane name='hero'>" +
225       " <assignment class='org.jbpm.taskmgmt.exe.SwimlaneTest$NullAssignmentHandler' />" +
226       " </swimlane>" +
227       " <start-state>" +
228       " <transition to='a' />" +
229       " </start-state>" +
230       " <task-node name='a'>" +
231       " <task name='change nappy' swimlane='hero' />'" +
232       " </task-node>" +
233       "</process-definition>"
234     );
235
236     ProcessInstance processInstance = new ProcessInstance(processDefinition);
237     processInstance.signal();
238   }
239 }
240
Popular Tags