KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jbpm.graph.exe;
2
3 import java.util.ArrayList JavaDoc;
4 import java.util.List JavaDoc;
5
6 import junit.framework.TestCase;
7
8 import org.jbpm.graph.def.Action;
9 import org.jbpm.graph.def.ActionHandler;
10 import org.jbpm.graph.def.Event;
11 import org.jbpm.graph.def.GraphElement;
12 import org.jbpm.graph.def.Node;
13 import org.jbpm.graph.def.ProcessDefinition;
14
15 public class EventPropagationTest extends TestCase {
16         
17   ProcessDefinition processDefinition = null;
18   ProcessInstance processInstance = null;
19
20   static List JavaDoc executedActions = null;
21   
22   public static class ExecutedAction {
23     // ExectionContext members
24
Token token = null;
25     Event event = null;
26     GraphElement eventSource = 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.eventSource = executionContext.getEventSource();
41       executedAction.action = executionContext.getAction();
42       executedAction.exception = executionContext.getException();
43       executedAction.node = executionContext.getNode();
44       executedActions.add(executedAction);
45     }
46   }
47   
48   public void setUp() {
49     executedActions = new ArrayList JavaDoc();
50   }
51
52   public void testNodeToProcessEventPropagation() {
53     processDefinition = ProcessDefinition.parseXmlString(
54       "<process-definition>" +
55       " <event type='node-enter'>" +
56       " <action class='org.jbpm.graph.exe.EventPropagationTest$Recorder' />" +
57       " </event>" +
58       " <event type='node-leave'>" +
59       " <action class='org.jbpm.graph.exe.EventPropagationTest$Recorder' />" +
60       " </event>" +
61       " <start-state name='start'>" +
62       " <transition to='state'/>" +
63       " </start-state>" +
64       " <state name='state'>" +
65       " <transition to='end'/>" +
66       " </state>" +
67       " <end-state name='end'/>" +
68       "</process-definition>"
69     );
70     // create the process instance
71
processInstance = new ProcessInstance(processDefinition);
72     assertEquals(0, executedActions.size());
73
74     processInstance.signal();
75     
76     assertEquals(2, executedActions.size());
77
78     ExecutedAction executedAction = (ExecutedAction) executedActions.get(0);
79     assertEquals("node-leave", executedAction.event.getEventType());
80     assertSame(processDefinition, executedAction.event.getGraphElement());
81     assertSame(processDefinition.getStartState(), executedAction.eventSource);
82     assertSame(processInstance.getRootToken(), executedAction.token);
83     assertSame(processDefinition.getStartState(), executedAction.node);
84
85     executedAction = (ExecutedAction) executedActions.get(1);
86     assertEquals("node-enter", executedAction.event.getEventType());
87     assertSame(processDefinition, executedAction.event.getGraphElement());
88     assertSame(processDefinition.getNode("state"), executedAction.eventSource);
89     assertSame(processInstance.getRootToken(), executedAction.token);
90     assertSame(processDefinition.getNode("state"), executedAction.node);
91
92     processInstance.signal();
93
94     assertEquals(4, executedActions.size());
95
96     executedAction = (ExecutedAction) executedActions.get(2);
97     assertEquals("node-leave", executedAction.event.getEventType());
98     assertSame(processDefinition, executedAction.event.getGraphElement());
99     assertSame(processDefinition.getNode("state"), executedAction.eventSource);
100     assertSame(processInstance.getRootToken(), executedAction.token);
101     assertSame(processDefinition.getNode("state"), executedAction.node);
102
103     executedAction = (ExecutedAction) executedActions.get(3);
104     assertEquals("node-enter", executedAction.event.getEventType());
105     assertSame(processDefinition, executedAction.event.getGraphElement());
106     assertSame(processDefinition.getNode("end"), executedAction.eventSource);
107     assertSame(processInstance.getRootToken(), executedAction.token);
108     assertSame(processDefinition.getNode("end"), executedAction.node);
109   }
110
111   public void testTransitionToProcessEventPropagation() {
112     processDefinition = ProcessDefinition.parseXmlString(
113       "<process-definition>" +
114       " <event type='transition'>" +
115       " <action class='org.jbpm.graph.exe.EventPropagationTest$Recorder' />" +
116       " </event>" +
117       " <start-state name='start'>" +
118       " <transition to='state'/>" +
119       " </start-state>" +
120       " <state name='state'>" +
121       " <transition to='end'/>" +
122       " </state>" +
123       " <end-state name='end'/>" +
124       "</process-definition>"
125     );
126     // create the process instance
127
processInstance = new ProcessInstance(processDefinition);
128     assertEquals(0, executedActions.size());
129
130     processInstance.signal();
131     
132     assertEquals(1, executedActions.size());
133
134     ExecutedAction executedAction = (ExecutedAction) executedActions.get(0);
135     assertEquals("transition", executedAction.event.getEventType());
136     assertSame(processDefinition, executedAction.event.getGraphElement());
137     assertSame(processDefinition.getStartState().getDefaultLeavingTransition(), executedAction.eventSource);
138     assertSame(processInstance.getRootToken(), executedAction.token);
139     assertNull(executedAction.node);
140
141     processInstance.signal();
142
143     assertEquals(2, executedActions.size());
144
145     executedAction = (ExecutedAction) executedActions.get(1);
146     assertEquals("transition", executedAction.event.getEventType());
147     assertSame(processDefinition, executedAction.event.getGraphElement());
148     assertSame(processDefinition.getNode("state").getDefaultLeavingTransition(), executedAction.eventSource);
149     assertSame(processInstance.getRootToken(), executedAction.token);
150     assertNull(executedAction.node);
151   }
152
153   public void testNodeToSuperStateEventPropagation() {
154     processDefinition = ProcessDefinition.parseXmlString(
155       "<process-definition>" +
156       " <start-state name='start'>" +
157       " <transition to='superstate/state'/>" +
158       " </start-state>" +
159       " <super-state name='superstate'>" +
160       " <event type='node-enter'>" +
161       " <action class='org.jbpm.graph.exe.EventPropagationTest$Recorder' />" +
162       " </event>" +
163       " <event type='node-leave'>" +
164       " <action class='org.jbpm.graph.exe.EventPropagationTest$Recorder' />" +
165       " </event>" +
166       " <state name='state'>" +
167       " <transition to='../end'/>" +
168       " </state>" +
169       " </super-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     processInstance.signal();
178     
179     assertEquals(1, executedActions.size());
180
181     ExecutedAction executedAction = (ExecutedAction) executedActions.get(0);
182     assertEquals("node-enter", executedAction.event.getEventType());
183     assertSame(processDefinition.getNode("superstate"), executedAction.event.getGraphElement());
184     assertSame(processDefinition.findNode("superstate/state"), executedAction.eventSource);
185     assertSame(processInstance.getRootToken(), executedAction.token);
186     assertSame(processDefinition.findNode("superstate/state"), executedAction.node);
187
188     processInstance.signal();
189     
190     assertEquals(2, executedActions.size());
191
192     executedAction = (ExecutedAction) executedActions.get(1);
193     assertEquals("node-leave", executedAction.event.getEventType());
194     assertSame(processDefinition.getNode("superstate"), executedAction.event.getGraphElement());
195     assertSame(processDefinition.findNode("superstate/state"), executedAction.eventSource);
196     assertSame(processInstance.getRootToken(), executedAction.token);
197     assertSame(processDefinition.findNode("superstate/state"), executedAction.node);
198   }
199
200   public void testTransitionToSuperStateEventPropagation() {
201     processDefinition = ProcessDefinition.parseXmlString(
202       "<process-definition>" +
203       " <start-state name='start'>" +
204       " <transition to='superstate/state'/>" +
205       " </start-state>" +
206       " <super-state name='superstate'>" +
207       " <event type='transition'>" +
208       " <action class='org.jbpm.graph.exe.EventPropagationTest$Recorder' />" +
209       " </event>" +
210       " <state name='state'>" +
211       " <transition to='../end'/>" +
212       " <transition name='loop' to='state'/>" +
213       " </state>" +
214       " </super-state>" +
215       " <end-state name='end'/>" +
216       "</process-definition>"
217     );
218     // create the process instance
219
processInstance = new ProcessInstance(processDefinition);
220     assertEquals(0, executedActions.size());
221
222     processInstance.signal();
223
224     assertEquals(0, executedActions.size());
225
226     processInstance.signal("loop");
227
228     assertEquals(1, executedActions.size());
229
230     ExecutedAction executedAction = (ExecutedAction) executedActions.get(0);
231     assertEquals("transition", executedAction.event.getEventType());
232     assertSame(processDefinition.getNode("superstate"), executedAction.event.getGraphElement());
233     assertSame(processDefinition.findNode("superstate/state").getLeavingTransition("loop"), executedAction.eventSource);
234     assertSame(processInstance.getRootToken(), executedAction.token);
235     assertNull(executedAction.node);
236
237     processInstance.signal();
238
239     assertEquals(1, executedActions.size());
240   }
241 }
242
Popular Tags