KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bpel > exe > ScopeExeTest


1 package org.jbpm.bpel.exe;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.Map JavaDoc;
5
6 import javax.xml.namespace.QName JavaDoc;
7
8 import org.jbpm.graph.exe.Token;
9
10 import org.jbpm.bpel.data.def.SchemaTypeInfo;
11 import org.jbpm.bpel.data.def.VariableDefinition;
12 import org.jbpm.bpel.def.Activity;
13 import org.jbpm.bpel.def.MessageHandler;
14 import org.jbpm.bpel.def.Receive;
15 import org.jbpm.bpel.def.Scope;
16 import org.jbpm.bpel.exe.state.ActiveState;
17 import org.jbpm.bpel.exe.state.EndedState;
18
19 /**
20  * @author Juan Cantú
21  * @version $Revision: 1.11 $ $Date: 2005/06/16 19:15:44 $
22  */

23 public class ScopeExeTest extends AbstractExeTestCase {
24
25   Scope scopeActivity;
26
27   public void testNormalExecution() throws Exception JavaDoc {
28     String JavaDoc xml =
29       "<scope>" +
30       " <receive partnerLink='aPartner' operation='o'/>" +
31       "</scope>";
32     scopeActivity = (Scope) readActivity(xml, false);
33     Receive receive =(Receive) scopeActivity.getRoot();
34     setNestedBlock(scopeActivity);
35     
36     Token token = startNested();
37     assertEquals(scopeActivity, token.getNode());
38     Token scopeToken = (Token) token.getChildren().values().iterator().next();
39     Token normalFlowToken = (Token) scopeToken.getChildren().values().iterator().next();
40     
41     assertEquals(receive, normalFlowToken.getNode());
42     
43     ScopeInstance scopeInstance = ScopeInstance.get(scopeToken);
44     assertEquals(scopeToken, scopeInstance.getToken());
45     assertNotNull(scopeInstance);
46     assertEquals(scopeInstance.getState(), ActiveState.NORMAL_PROCESSING);
47     
48     assertReceiveAndComplete(normalFlowToken, receive);
49     assertEquals( EndedState.COMPLETED, scopeInstance.getState() );
50     
51     //parent token advanced and completed
52
assertCompleted( token );
53   }
54   
55   public void testEventsExecutionFirstScenario() throws Exception JavaDoc {
56     String JavaDoc xml =
57       "<scope>" +
58       " <receive partnerLink='aPartner' operation='o'/>" +
59       "<eventHandlers>" +
60       " <onEvent partnerLink='aPartner' operation='o'>" +
61       " <receive partnerLink='aPartner' operation='o'/>" +
62       " </onEvent>" +
63       " <onEvent partnerLink='aPartner' operation='o'>" +
64       " <receive partnerLink='aPartner' operation='o'/>" +
65       " </onEvent>" +
66       "</eventHandlers>" +
67       "</scope>";
68     scopeActivity = (Scope) readActivity(xml, false);
69     setNestedBlock(scopeActivity);
70     Receive receive =(Receive) scopeActivity.getRoot();
71     Iterator JavaDoc eventsIt = scopeActivity.getMessageHandlers().iterator();
72     
73     //set a variable to receivers
74
VariableDefinition variable = new VariableDefinition();
75     variable.setName("v");
76     variable.setTypeInfo(new SchemaTypeInfo(new QName JavaDoc("v")));
77     MessageHandler firstHandler = (MessageHandler) eventsIt.next();
78     firstHandler.getReceiver().setVariable(variable);
79     MessageHandler secondHandler = (MessageHandler) eventsIt.next();
80     secondHandler.getReceiver().setVariable(variable);
81     //Receive firstActivity = (Receive) events.getNode("first");
82
//Receive secondActivity = (Receive) events.getNode("second");
83

84     Token token = startNested();
85     Token scopeToken = (Token) token.getChildren().values().iterator().next();
86     Map JavaDoc scopeTokens = scopeToken.getChildren();
87     Token normalFlowToken = (Token) scopeTokens.get(Scope.NORMAL_FLOW_TOKEN);
88     Token eventToken = (Token) scopeTokens.get(Scope.EVENTS_TOKEN);
89     ScopeInstance scopeInstance = ScopeInstance.get(scopeToken);
90     
91     firstHandler.messageReceived(firstHandler.getReceiver(), null, eventToken);
92
93     Map JavaDoc eventTokens = eventToken.getChildren();
94     Token firstEventToken = (Token) eventTokens.get(Scope.EVENT_TOKEN + 1);
95     
96     //first event activity is completed
97
assertEventAndComplete(firstEventToken, (Receive) firstHandler.getActivity());
98     
99     secondHandler.messageReceived(secondHandler.getReceiver(), null, eventToken);
100     
101     //execute scope receive, scope must reach an events pending state
102
receive.messageReceived(receive.getReceiver(), null, normalFlowToken);
103     assertEquals(ActiveState.EVENTS_PENDING, scopeInstance.getState());
104     
105     //a new message arrives to the first event. It must be rejected
106
int beforeEventCount = eventTokens.size();
107     firstHandler.messageReceived(firstHandler.getReceiver(), null, eventToken);
108     assertEquals(beforeEventCount, eventTokens.size());
109     
110     //second event activity is completed
111
Token secondEventToken = (Token) eventTokens.get(Scope.EVENT_TOKEN + 2);
112     assertEventAndComplete(secondEventToken, (Receive) secondHandler.getActivity());
113     
114     //parent token advanced and completed
115
assertEquals(EndedState.COMPLETED, scopeInstance.getState());
116     assertCompleted( token );
117   }
118
119   
120   public void testEventsExecutionSecondScenario() throws Exception JavaDoc {
121     String JavaDoc xml =
122       "<scope>" +
123       " <receive partnerLink='aPartner' operation='o'/>" +
124       "<eventHandlers>" +
125       " <onEvent partnerLink='aPartner' operation='o'>" +
126       " <receive partnerLink='aPartner' operation='o'/>" +
127       " </onEvent>" +
128       "</eventHandlers>" +
129       "</scope>";
130     scopeActivity = (Scope) readActivity(xml, false);
131     setNestedBlock(scopeActivity);
132     Receive receive =(Receive) scopeActivity.getRoot();
133     Iterator JavaDoc eventsIt = scopeActivity.getMessageHandlers().iterator();
134     
135     //set a variable to receiver
136
VariableDefinition variable = new VariableDefinition();
137     variable.setName("v");
138     variable.setTypeInfo(new SchemaTypeInfo(new QName JavaDoc("v")));
139     MessageHandler firstHandler = (MessageHandler) eventsIt.next();
140     firstHandler.getReceiver().setVariable(variable);
141     
142     Token token = startNested();
143     Token scopeToken = (Token) token.getChildren().values().iterator().next();
144     Map JavaDoc scopeTokens = scopeToken.getChildren();
145     Token normalFlowToken = (Token) scopeTokens.get(Scope.NORMAL_FLOW_TOKEN);
146     Token eventToken = (Token) scopeTokens.get(Scope.EVENTS_TOKEN);
147     ScopeInstance scopeInstance = ScopeInstance.get(scopeToken);
148     
149     firstHandler.messageReceived(firstHandler.getReceiver(), null, eventToken);
150
151     Map JavaDoc eventTokens = eventToken.getChildren();
152     Token firstEventToken = (Token) eventTokens.get(Scope.EVENT_TOKEN + 1);
153     
154     //first event activity is completed
155
assertEventAndComplete(firstEventToken, (Receive)firstHandler.getActivity());
156     
157     //execute scope receive, scope must complete
158
receive.messageReceived(receive.getReceiver(), null, normalFlowToken);
159
160     //parent token advanced and completed
161
assertEquals( EndedState.COMPLETED, scopeInstance.getState() );
162     assertCompleted( token );
163   }
164
165   /*TODO won't work until jbpm ContextInstance.getLocalVariable() fix is released
166  
167   public void testFaultWithoutHandler() throws Exception {
168     String xml =
169       "<scope>" +
170       "<sequence>" +
171       "<scope>" +
172       " <empty/>" +
173       "</scope>" +
174       "<flow>" +
175       " <throw faultName='someFault'/>" +
176       " <receive name='uselessReceive' partnerLink='aPartner' operation='o'/>" +
177       "</flow>" +
178       "</sequence>" +
179       "</scope>";
180     
181     scopeActivity = (Scope) readActivity(xml, false);
182     setNestedBlock(scopeActivity);
183     
184     Token token = startNested();
185     Token scopeToken = (Token) token.getChildren().values().iterator().next();
186     ScopeInstance scopeInstance = ScopeInstance.get(scopeToken);
187     
188     ScopeInstance.ScopeIterator childrenIt = new ScopeInstance.ScopeIterator(scopeToken);
189     assertTrue(childrenIt.hasNext());
190     assertEquals(EndedState.COMPENSATED, ((ScopeInstance)childrenIt.next()).getState());
191
192     //parent token advanced and completed abnormally
193     assertEquals( EndedState.FAULTED, scopeInstance.getState() );
194     assertNotNull( token.getEnd() );
195   }
196   
197   public void testFaultWithHandler() throws Exception {
198     String xml =
199       "<scope>" +
200       "<sequence>" +
201       "<scope>" +
202       " <empty/>" +
203       "</scope>" +
204       "<flow>" +
205       " <throw faultName='someFault'/>" +
206       " <receive name='uselessReceive' partnerLink='aPartner' operation='o'/>" +
207       "</flow>" +
208       "</sequence>" +
209       "<faultHandlers>" +
210       " <catch faultName='someFault'>" +
211       " <receive partnerLink='aPartner' operation='o'/>" +
212       " </catch>" +
213       "</faultHandlers>" +
214       "</scope>";
215     
216     scopeActivity = (Scope) readActivity(xml, false);
217     setNestedBlock(scopeActivity);
218     Flow flow =(Flow) ((Sequence) scopeActivity.getRoot()).getNodes().get(1);
219     FaultHandler handler = (FaultHandler)scopeActivity.getScope().getFaultHandlers().iterator().next();
220     Receive handlerReceive = (Receive) handler.getActivity();
221     Receive uselessReceive = (Receive) flow.getNode("uselessReceive");
222     
223     Token token = startNested();
224     Token scopeToken = (Token) token.getChildren().values().iterator().next();
225     Map scopeTokens = scopeToken.getChildren();
226     Token normalFlowToken = (Token) scopeTokens.get(Scope.NORMAL_FLOW_TOKEN);
227     ScopeInstance scopeInstance = ScopeInstance.get(scopeToken);
228     
229     assertEquals(3, normalFlowToken.getChildren().size());
230
231     ScopeInstance.ScopeIterator childrenIt = new ScopeInstance.ScopeIterator(scopeToken);
232     assertTrue(childrenIt.hasNext());
233     assertEquals(EndedState.COMPLETED, ((ScopeInstance)childrenIt.next()).getState());
234     assertFalse(childrenIt.hasNext());
235     
236     //uselessToken wasn't executed in the receive activity
237     assertEquals(0, normalFlowToken.getChildrenAtNode(uselessReceive).size());
238     
239     //fault receive is completed
240     handlerReceive.messageReceived(handlerReceive.getReceiver(), null, scopeToken);
241
242     //parent token advanced and completed abnormally
243     assertEquals( EndedState.COMPLETED_ABNORMALLY, scopeInstance.getState() );
244     assertNotNull( token.getEnd() );
245   }
246   */

247   
248   protected void assertReceiveAndAdvance(Token token, Receive sourceNode, Activity targetNode) {
249     Receive activity = (Receive) token.getNode();
250     assertEquals(sourceNode, activity);
251     activity.messageReceived(activity.getReceiver(), null, token);
252     assertSame( targetNode , token.getNode() );
253   }
254   
255   protected void assertEventAndComplete(Token token, Receive sourceNode) {
256     Receive activity = (Receive) token.getNode();
257     assertEquals(sourceNode, activity);
258     activity.messageReceived(activity.getReceiver(), null, token);
259     assertCompleted( token );
260   }
261 }
Popular Tags