KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > graph > exe > ActionExecutionTest


1 package org.jbpm.graph.exe;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.List JavaDoc;
6
7 import junit.framework.TestCase;
8
9 import org.jbpm.graph.def.Action;
10 import org.jbpm.graph.def.ActionHandler;
11 import org.jbpm.graph.def.DelegationException;
12 import org.jbpm.graph.def.Event;
13 import org.jbpm.graph.def.Node;
14 import org.jbpm.graph.def.ProcessDefinition;
15
16 public class ActionExecutionTest extends TestCase {
17   
18   ProcessDefinition processDefinition = null;
19   ProcessInstance processInstance = null;
20
21   static List JavaDoc executedActions = null;
22   
23   public static class ExecutedAction {
24     // ExectionContext members
25
Token token = null;
26     Event event = null;
27     Action action = null;
28     Throwable JavaDoc exception = null;
29     // The node returned by the ExecutionContext at the time of execution
30
Node node = null;
31   }
32   
33   public static class Recorder implements ActionHandler {
34     private static final long serialVersionUID = 1L;
35
36     public void execute(ExecutionContext executionContext) throws Exception JavaDoc {
37       ExecutedAction executedAction = new ExecutedAction();
38       executedAction.token = executionContext.getToken();
39       executedAction.event = executionContext.getEvent();
40       executedAction.action = executionContext.getAction();
41       executedAction.exception = executionContext.getException();
42       executedAction.node = executionContext.getNode();
43       executedActions.add(executedAction);
44     }
45   }
46   
47   public void setUp() {
48     executedActions = new ArrayList JavaDoc();
49   }
50
51   public void testProcessStartEvent() {
52     processDefinition = ProcessDefinition.parseXmlString(
53       "<process-definition>" +
54       " <event type='process-start'>" +
55       " <action class='org.jbpm.graph.exe.ActionExecutionTest$Recorder' />" +
56       " </event>" +
57       " <start-state name='start'>" +
58       " <transition to='state'/>" +
59       " </start-state>" +
60       " <state name='state'>" +
61       " <transition to='end'/>" +
62       " </state>" +
63       " <end-state name='end'/>" +
64       "</process-definition>"
65     );
66     // create the process instance
67
processInstance = new ProcessInstance(processDefinition);
68     assertSame(getNode("start"), findExecutedAction(Event.EVENTTYPE_PROCESS_START).node);
69   }
70
71   public void testProcessEndEvent() {
72     processDefinition = ProcessDefinition.parseXmlString(
73       "<process-definition>" +
74       " <start-state name='start'>" +
75       " <transition to='state'/>" +
76       " </start-state>" +
77       " <state name='state'>" +
78       " <transition to='end'/>" +
79       " </state>" +
80       " <end-state name='end'/>" +
81       " <event type='process-end'>" +
82       " <action class='org.jbpm.graph.exe.ActionExecutionTest$Recorder' />" +
83       " </event>" +
84       "</process-definition>"
85     );
86     // create the process instance
87
processInstance = new ProcessInstance(processDefinition);
88     assertEquals(0, executedActions.size());
89     processInstance.signal();
90     assertEquals(0, executedActions.size());
91     processInstance.signal();
92     assertEquals(1, executedActions.size());
93     assertSame(getNode("end"), findExecutedAction(Event.EVENTTYPE_PROCESS_END).node);
94   }
95
96   public void testProcessBeforeSignalEvent() {
97     processDefinition = ProcessDefinition.parseXmlString(
98       "<process-definition>" +
99       " <event type='before-signal'>" +
100       " <action class='org.jbpm.graph.exe.ActionExecutionTest$Recorder' />" +
101       " </event>" +
102       " <start-state name='start'>" +
103       " <transition to='state'/>" +
104       " </start-state>" +
105       " <state name='state'>" +
106       " <transition to='end'/>" +
107       " </state>" +
108       " <end-state name='end'/>" +
109       "</process-definition>"
110     );
111     // create the process instance
112
processInstance = new ProcessInstance(processDefinition);
113     assertEquals(0, executedActions.size());
114
115     // leave the start state by sending a signal
116
processInstance.signal();
117     assertEquals(1, executedActions.size());
118     ExecutedAction executedAction = findExecutedAction(Event.EVENTTYPE_BEFORE_SIGNAL);
119     assertSame(getNode("start"), executedAction.node);
120     assertSame(processDefinition, executedAction.event.getGraphElement());
121
122     // leave the state by sending another signal
123
processInstance.signal();
124     assertEquals(2, executedActions.size());
125   }
126
127   public void testProcessAfterSignalEvent() {
128     processDefinition = ProcessDefinition.parseXmlString(
129       "<process-definition>" +
130       " <event type='after-signal'>" +
131       " <action class='org.jbpm.graph.exe.ActionExecutionTest$Recorder' />" +
132       " </event>" +
133       " <start-state name='start'>" +
134       " <transition to='state'/>" +
135       " </start-state>" +
136       " <state name='state'>" +
137       " <transition to='end'/>" +
138       " </state>" +
139       " <end-state name='end'/>" +
140       "</process-definition>"
141     );
142     // create the process instance
143
processInstance = new ProcessInstance(processDefinition);
144     assertEquals(0, executedActions.size());
145
146     // leave the start state by sending a signal
147
processInstance.signal();
148     assertEquals(1, executedActions.size());
149     ExecutedAction executedAction = findExecutedAction(Event.EVENTTYPE_AFTER_SIGNAL);
150     assertSame(getNode("state"), executedAction.node);
151     assertSame(processDefinition, executedAction.event.getGraphElement());
152
153     // leave the state by sending another signal
154
processInstance.signal();
155     assertEquals(2, executedActions.size());
156   }
157   
158   public void testNodeBeforeSignalEvent() {
159     processDefinition = ProcessDefinition.parseXmlString(
160       "<process-definition>" +
161       " <start-state name='start'>" +
162       " <event type='before-signal'>" +
163       " <action class='org.jbpm.graph.exe.ActionExecutionTest$Recorder' />" +
164       " </event>" +
165       " <transition to='state'/>" +
166       " </start-state>" +
167       " <state name='state'>" +
168       " <transition to='end'/>" +
169       " </state>" +
170       " <end-state name='end'/>" +
171       "</process-definition>"
172     );
173     // create the process instance
174
processInstance = new ProcessInstance(processDefinition);
175     assertEquals(0, executedActions.size());
176
177     // leave the start state by sending a signal
178
processInstance.signal();
179     assertEquals(1, executedActions.size());
180     ExecutedAction executedAction = findExecutedAction(Event.EVENTTYPE_BEFORE_SIGNAL);
181     assertSame(getNode("start"), executedAction.node);
182     assertSame(getNode("start"), executedAction.event.getGraphElement());
183
184     // leave the state by sending another signal
185
processInstance.signal();
186     assertEquals(1, executedActions.size());
187   }
188
189   public void testNodeAfterSignalEvent() {
190     processDefinition = ProcessDefinition.parseXmlString(
191       "<process-definition>" +
192       " <start-state name='start'>" +
193       " <event type='after-signal'>" +
194       " <action class='org.jbpm.graph.exe.ActionExecutionTest$Recorder' />" +
195       " </event>" +
196       " <transition to='state'/>" +
197       " </start-state>" +
198       " <state name='state'>" +
199       " <transition to='end'/>" +
200       " </state>" +
201       " <end-state name='end'/>" +
202       "</process-definition>"
203     );
204     // create the process instance
205
processInstance = new ProcessInstance(processDefinition);
206     assertEquals(0, executedActions.size());
207
208     // leave the start state by sending a signal
209
processInstance.signal();
210     assertEquals(1, executedActions.size());
211     ExecutedAction executedAction = findExecutedAction(Event.EVENTTYPE_AFTER_SIGNAL);
212     assertSame(getNode("state"), executedAction.node);
213     assertSame(getNode("start"), executedAction.event.getGraphElement());
214
215     // leave the state by sending another signal
216
processInstance.signal();
217     assertEquals(1, executedActions.size());
218   }
219   
220   public void testNodeEnterEvent() {
221     processDefinition = ProcessDefinition.parseXmlString(
222       "<process-definition>" +
223       " <start-state name='start'>" +
224       " <transition to='state'/>" +
225       " </start-state>" +
226       " <state name='state'>" +
227       " <event type='node-enter'>" +
228       " <action class='org.jbpm.graph.exe.ActionExecutionTest$Recorder' />" +
229       " </event>" +
230       " <transition to='end'/>" +
231       " </state>" +
232       " <end-state name='end'/>" +
233       "</process-definition>"
234     );
235     // create the process instance
236
processInstance = new ProcessInstance(processDefinition);
237     assertEquals(0, executedActions.size());
238
239     // leave the start state by sending a signal
240
processInstance.signal();
241     assertEquals(1, executedActions.size());
242     ExecutedAction executedAction = findExecutedAction(Event.EVENTTYPE_NODE_ENTER);
243     assertSame(getNode("state"), executedAction.node);
244     assertSame(getNode("state"), executedAction.event.getGraphElement());
245
246     // leave the state by sending another signal
247
processInstance.signal();
248     assertEquals(1, executedActions.size());
249   }
250
251   public void testNodeLeaveEvent() {
252     processDefinition = ProcessDefinition.parseXmlString(
253       "<process-definition>" +
254       " <start-state name='start'>" +
255       " <transition to='state'/>" +
256       " </start-state>" +
257       " <state name='state'>" +
258       " <event type='node-leave'>" +
259       " <action class='org.jbpm.graph.exe.ActionExecutionTest$Recorder' />" +
260       " </event>" +
261       " <transition to='end'/>" +
262       " </state>" +
263       " <end-state name='end'/>" +
264       "</process-definition>"
265     );
266     // create the process instance
267
processInstance = new ProcessInstance(processDefinition);
268     processInstance.signal();
269     assertEquals(0, executedActions.size());
270
271     // leave the start state by sending a signal
272
processInstance.signal();
273     assertEquals(1, executedActions.size());
274     ExecutedAction executedAction = findExecutedAction(Event.EVENTTYPE_NODE_LEAVE);
275     assertSame(getNode("state"), executedAction.node);
276     assertSame(getNode("state"), executedAction.event.getGraphElement());
277   }
278
279   public void testTransitionEvent() {
280     processDefinition = ProcessDefinition.parseXmlString(
281       "<process-definition>" +
282       " <start-state name='start'>" +
283       " <transition to='state'/>" +
284       " </start-state>" +
285       " <state name='state'>" +
286       " <transition to='end'>" +
287       " <action class='org.jbpm.graph.exe.ActionExecutionTest$Recorder' />" +
288       " </transition>" +
289       " </state>" +
290       " <end-state name='end'/>" +
291       "</process-definition>"
292     );
293     // create the process instance
294
processInstance = new ProcessInstance(processDefinition);
295     processInstance.signal();
296     assertEquals(0, executedActions.size());
297
298     // leave the start state by sending a signal
299
processInstance.signal();
300     assertEquals(1, executedActions.size());
301     ExecutedAction executedAction = findExecutedAction(Event.EVENTTYPE_TRANSITION);
302     assertNull(executedAction.node);
303     assertSame(getNode("state").getDefaultLeavingTransition(), executedAction.event.getGraphElement());
304   }
305   
306   static List JavaDoc sequence = new ArrayList JavaDoc();
307   public static class SequenceRecorder implements ActionHandler {
308     private static final long serialVersionUID = 1L;
309     public void execute(ExecutionContext executionContext) throws Exception JavaDoc {
310       Event event = executionContext.getEvent();
311       sequence.add(event.getGraphElement().getName()+" "+event.getEventType());
312     }
313   }
314   
315   public void testExecutionSequence() {
316     processDefinition = ProcessDefinition.parseXmlString(
317       "<process-definition name='process'>" +
318       " <event type='process-start'><action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' /></event>" +
319       " <start-state name='start-state'>" +
320       " <event type='node-enter'><action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' /></event>" +
321       " <event type='node-leave'><action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' /></event>" +
322       " <event type='before-signal'><action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' /></event>" +
323       " <event type='after-signal'><action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' /></event>" +
324       " <transition name='start-to-state' to='state'>" +
325       " <action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' />" +
326       " </transition>" +
327       " </start-state>" +
328       " <state name='state'>" +
329       " <event type='node-enter'><action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' /></event>" +
330       " <event type='node-leave'><action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' /></event>" +
331       " <event type='before-signal'><action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' /></event>" +
332       " <event type='after-signal'><action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' /></event>" +
333       " <transition name='state-to-end' to='end-state'>" +
334       " <action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' />" +
335       " </transition>" +
336       " </state>" +
337       " <end-state name='end-state'>" +
338       " <event type='node-enter'><action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' /></event>" +
339       " <event type='node-leave'><action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' /></event>" +
340       " <event type='before-signal'><action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' /></event>" +
341       " <event type='after-signal'><action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' /></event>" +
342       " </end-state>" +
343       " <event type='process-end'><action class='org.jbpm.graph.exe.ActionExecutionTest$SequenceRecorder' /></event>" +
344       "</process-definition>"
345     );
346     
347     // create the process instance
348
processInstance = new ProcessInstance(processDefinition);
349     processInstance.signal();
350     processInstance.signal();
351     
352     // format of the sequence messages :
353
// node-name event-type
354
// separated by a space
355
assertEquals("process process-start", sequence.get(0));
356     assertEquals("start-state before-signal", sequence.get(1));
357     assertEquals("start-state node-leave", sequence.get(2));
358     assertEquals("start-to-state transition", sequence.get(3));
359     assertEquals("state node-enter", sequence.get(4));
360     assertEquals("start-state after-signal", sequence.get(5));
361     assertEquals("state before-signal", sequence.get(6));
362     assertEquals("state node-leave", sequence.get(7));
363     assertEquals("state-to-end transition", sequence.get(8));
364     assertEquals("end-state node-enter", sequence.get(9));
365     assertEquals("process process-end", sequence.get(10));
366     assertEquals("state after-signal", sequence.get(11));
367   }
368
369   private Node getNode(String JavaDoc nodeName) {
370     return processDefinition.getNode(nodeName);
371   }
372
373   private ExecutedAction findExecutedAction(String JavaDoc eventType) {
374     Iterator JavaDoc iter = executedActions.iterator();
375     while (iter.hasNext()) {
376       ExecutedAction executedAction = (ExecutedAction) iter.next();
377       if (eventType.equals(executedAction.event.getEventType())) {
378         return executedAction;
379       }
380     }
381     throw new RuntimeException JavaDoc("no action was executed on eventtype '"+eventType+"'");
382   }
383   
384   public static class ProblematicActionHandler implements ActionHandler {
385     private static final long serialVersionUID = 1L;
386     public void execute(ExecutionContext executionContext) throws Exception JavaDoc {
387       throw new IllegalArgumentException JavaDoc("problematic problem");
388     }
389   }
390   
391   public void testProblematicReferencedAction() {
392     processDefinition = ProcessDefinition.parseXmlString(
393       "<process-definition>" +
394       " <start-state name='start'>" +
395       " <transition to='state'>" +
396       " <action ref-name='problematic action'/>" +
397       " </transition>"+
398       " </start-state>" +
399       " <state name='state' />" +
400       " <action name='problematic action' class='org.jbpm.graph.exe.ActionExecutionTest$$ProblematicActionHandler'/>"+
401       "</process-definition>");
402     
403     // create the process instance
404
processInstance = new ProcessInstance(processDefinition);
405     try {
406       processInstance.signal();
407     } catch (DelegationException e) {
408       e.printStackTrace();
409     }
410   }
411 }
Popular Tags