1 package org.jbpm.bpel.exe; 2 3 import java.util.Iterator ; 4 import java.util.Map ; 5 6 import javax.xml.namespace.QName ; 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 23 public class ScopeExeTest extends AbstractExeTestCase { 24 25 Scope scopeActivity; 26 27 public void testNormalExecution() throws Exception { 28 String 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 assertCompleted( token ); 53 } 54 55 public void testEventsExecutionFirstScenario() throws Exception { 56 String 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 eventsIt = scopeActivity.getMessageHandlers().iterator(); 72 73 VariableDefinition variable = new VariableDefinition(); 75 variable.setName("v"); 76 variable.setTypeInfo(new SchemaTypeInfo(new QName ("v"))); 77 MessageHandler firstHandler = (MessageHandler) eventsIt.next(); 78 firstHandler.getReceiver().setVariable(variable); 79 MessageHandler secondHandler = (MessageHandler) eventsIt.next(); 80 secondHandler.getReceiver().setVariable(variable); 81 84 Token token = startNested(); 85 Token scopeToken = (Token) token.getChildren().values().iterator().next(); 86 Map 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 eventTokens = eventToken.getChildren(); 94 Token firstEventToken = (Token) eventTokens.get(Scope.EVENT_TOKEN + 1); 95 96 assertEventAndComplete(firstEventToken, (Receive) firstHandler.getActivity()); 98 99 secondHandler.messageReceived(secondHandler.getReceiver(), null, eventToken); 100 101 receive.messageReceived(receive.getReceiver(), null, normalFlowToken); 103 assertEquals(ActiveState.EVENTS_PENDING, scopeInstance.getState()); 104 105 int beforeEventCount = eventTokens.size(); 107 firstHandler.messageReceived(firstHandler.getReceiver(), null, eventToken); 108 assertEquals(beforeEventCount, eventTokens.size()); 109 110 Token secondEventToken = (Token) eventTokens.get(Scope.EVENT_TOKEN + 2); 112 assertEventAndComplete(secondEventToken, (Receive) secondHandler.getActivity()); 113 114 assertEquals(EndedState.COMPLETED, scopeInstance.getState()); 116 assertCompleted( token ); 117 } 118 119 120 public void testEventsExecutionSecondScenario() throws Exception { 121 String 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 eventsIt = scopeActivity.getMessageHandlers().iterator(); 134 135 VariableDefinition variable = new VariableDefinition(); 137 variable.setName("v"); 138 variable.setTypeInfo(new SchemaTypeInfo(new QName ("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 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 eventTokens = eventToken.getChildren(); 152 Token firstEventToken = (Token) eventTokens.get(Scope.EVENT_TOKEN + 1); 153 154 assertEventAndComplete(firstEventToken, (Receive)firstHandler.getActivity()); 156 157 receive.messageReceived(receive.getReceiver(), null, normalFlowToken); 159 160 assertEquals( EndedState.COMPLETED, scopeInstance.getState() ); 162 assertCompleted( token ); 163 } 164 165 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 |