KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jbpm.bpel.exe;
2
3 import java.net.URL JavaDoc;
4
5 import javax.wsdl.Operation;
6 import javax.wsdl.PortType;
7 import javax.xml.namespace.QName JavaDoc;
8 import javax.xml.parsers.DocumentBuilder JavaDoc;
9
10 import junit.framework.TestCase;
11
12 import org.w3c.dom.Document JavaDoc;
13 import org.w3c.dom.Element JavaDoc;
14
15 import com.ibm.wsdl.OperationImpl;
16 import com.ibm.wsdl.PortTypeImpl;
17
18 import org.jbpm.context.exe.ContextInstance;
19 import org.jbpm.graph.def.Node;
20 import org.jbpm.graph.exe.ExecutionContext;
21 import org.jbpm.graph.exe.ProcessInstance;
22 import org.jbpm.graph.exe.Token;
23
24 import org.jbpm.bpel.def.*;
25 import org.jbpm.bpel.exe.state.EndedState;
26 import org.jbpm.bpel.service.def.PartnerLinkDefinition;
27 import org.jbpm.bpel.service.def.Receiver;
28 import org.jbpm.bpel.service.exe.NonStartListener;
29 import org.jbpm.bpel.wsdl.def.PartnerLinkType;
30 import org.jbpm.bpel.wsdl.def.PartnerLinkType.Role;
31 import org.jbpm.bpel.wsdl.impl.PartnerLinkTypeImpl;
32 import org.jbpm.bpel.xml.BpelConstants;
33 import org.jbpm.bpel.xml.BpelReader;
34 import org.jbpm.bpel.xml.util.NodeUtil;
35
36 /**
37  * @author Juan Cantú
38  * @version $Revision: 1.12 $ $Date: 2005/06/23 02:17:26 $
39  */

40 public abstract class AbstractExeTestCase extends TestCase {
41
42   private BpelReader reader;
43   protected ProcessInstance pi;
44   private BpelDefinition pd;
45   protected TestScope scope;
46
47   private Receive start;
48   
49   public void setUp() throws Exception JavaDoc {
50     reader = new BpelReader() {
51       public Receiver readReceiver(Element JavaDoc element, CompositeActivity parent) {
52         return new TestReceiver();
53       }
54     };
55     
56     pd = new BpelDefinition("pd");
57     pd.getNodes().clear();
58     scope = new TestScope();
59     pd.addNode(scope);
60     
61     Operation operation = new OperationImpl();
62     operation.setName("o");
63     
64     PortType partnerPortType = new PortTypeImpl();
65     partnerPortType.setQName(new QName JavaDoc("ppt"));
66     partnerPortType.addOperation(operation);
67     
68     PortType myPortType = new PortTypeImpl();
69     myPortType.setQName(new QName JavaDoc("mpt"));
70     myPortType.addOperation(operation);
71     
72     PartnerLinkType pLinkType = new PartnerLinkTypeImpl();
73     Role role = pLinkType.createRole();
74     role.setName("role1");
75     role.setPortType(partnerPortType);
76     pLinkType.setFirstRole(role);
77     
78     role = pLinkType.createRole();
79     role.setName("role2");
80     role.setPortType(myPortType);
81     pLinkType.setSecondRole(role);
82
83     PartnerLinkDefinition pLink = new PartnerLinkDefinition();
84     pLink.setName("aPartner");
85     pLink.setPartnerLinkType(pLinkType);
86     pLink.setMyRole(role);
87     
88     scope.addPartnerLink(pLink);
89     pi = new ProcessInstance(pd);
90   }
91   
92   //////////////////// activity read methods
93

94   protected Activity readActivity(String JavaDoc xml, boolean isInitial) throws Exception JavaDoc {
95     String JavaDoc textToParse =
96       "<parent xmlns='" + BpelConstants.NS_BPWS + "'>" + xml + "</parent>";
97     Element JavaDoc element = (Element JavaDoc) NodeUtil.parseElement(textToParse).getChildNodes().item(0);
98     return readActivity(element, isInitial);
99   }
100   
101   protected Activity readActivity(URL JavaDoc xmlFile, boolean isInitial) throws Exception JavaDoc {
102     DocumentBuilder JavaDoc builder = NodeUtil.getDocumentBuilder();
103     Document JavaDoc document = builder.parse(xmlFile.openStream());
104     return readActivity(document.getDocumentElement(), isInitial);
105   }
106   
107   private Activity readActivity(Element JavaDoc element, boolean isInitial) {
108     scope.initial = isInitial;
109     Activity activity = reader.readActivity(element, scope);
110     return activity;
111   }
112   
113   //////////////////// load activity into the definition
114

115   protected void setNestedBlock(Activity activity) {
116     Sequence sequence = new Sequence("rootSequence");
117     scope.addNode(sequence);
118
119     start = new Receive();
120     start.setReceiver(new TestReceiver(start));
121     start.setName("start");
122     start.connect(activity);
123     activity.connect(sequence.getEnd());
124   }
125   
126   protected void setRootBlock(Activity activity) {
127     scope.addNode(activity);
128   }
129   
130   //////////////////// execution methods
131

132   protected Token startNested() {
133     Token root = pi.getRootToken();
134     scope.initScopeData(root);
135     start.leave(scope.createScopeContext(root));
136     return (Token) root.getChildren().values().iterator().next();
137   }
138
139   protected Token startInitial( Receiver trigger ) {
140     Token root = pi.getRootToken();
141     scope.initScopeData(root);
142     pd.triggerProcessInstance(pi, trigger);
143     //return the first children of the scope token (normalFlowToken)
144
return (Token) root.getChildren().values().iterator().next();
145   }
146   
147   //////////////////// execution assertions
148

149   protected void assertReceiveDisabled(Token token, Receive receive) {
150     assertReceiverDisabled(token, receive.getReceiver());
151   }
152   
153   protected void assertReceiveEnabled(Token token, Receive receive) {
154     assertReceiverEnabled(token, receive.getReceiver());
155   }
156   
157   protected void assertReceiverDisabled(Token token, Receiver receiver) {
158     assertFalse(hasInFlowListener(token, receiver));
159   }
160   
161   protected void assertReceiverEnabled(Token token, Receiver receiver) {
162     assertTrue(hasInFlowListener(token, receiver));
163   }
164   
165   protected void assertReceiveAndAdvance(Token token, Receive sourceNode, Activity targetNode) {
166     Receive activity = (Receive) token.getNode();
167     assertEquals(sourceNode, activity);
168     activity.messageReceived(activity.getReceiver(), null, token);
169     assertSame( targetNode , token.getNode() );
170   }
171   
172   protected void assertReceiveAndComplete(Token token, Receive sourceNode) {
173     Receive activity = (Receive) token.getNode();
174     assertEquals(sourceNode, activity);
175     activity.messageReceived(activity.getReceiver(), null, token);
176     assertCompleted(token);
177   }
178   
179   protected void assertCompleted(Token token) {
180     assertEquals(EndedState.COMPLETED, ScopeInstance.get(token).getState());
181   }
182   
183   protected Token findToken(Token parent, Node node) {
184     return (Token) parent.getChildrenAtNode(node).iterator().next();
185   }
186   
187   private boolean hasInFlowListener(Token token, Receiver receiver) {
188     ContextInstance contextInstance = (ContextInstance) token.getProcessInstance().getInstance(ContextInstance.class);
189     return contextInstance.getVariable(NonStartListener.RECEIVER_NAME + receiver.getId(), token) != null ? true : false;
190   }
191   
192   protected static class TestScope extends BpelDefinition.RootScope {
193     boolean initial = false;
194     
195     public boolean isChildInitial(Activity activity) {
196       return initial;
197     }
198   }
199  
200   public static class TestReceiver extends Receiver {
201     
202     public TestReceiver() {
203       setId(hashCode());
204     }
205     
206     public TestReceiver(Activity activity) {
207       super(activity);
208       setId(hashCode());
209     }
210     
211     public void close(Token token) {
212       ContextInstance contextInstance = (ContextInstance) token.getProcessInstance().getInstance(ContextInstance.class);
213       contextInstance.deleteVariable(NonStartListener.RECEIVER_NAME + this.getId(), token);
214     }
215     
216     public void receive(Token token) {
217       NonStartListener.create(this, token);
218     }
219     
220     public void readMessage(Element JavaDoc element, Token token) {
221       //nooooooothinnnnnng
222
}
223   }
224   
225   public static class TestLink extends Link {
226
227     public TestLink(String JavaDoc name) {
228       super(name);
229     }
230     public void setNegativeStatus(Token token) {
231       LinkInstance mi = LinkInstance.get(token, getName());
232       mi.setReached(Boolean.FALSE);
233     }
234     public void execute(ExecutionContext context) {
235       LinkInstance mi = LinkInstance.get(context.getToken(), getName());
236       mi.setReached(Boolean.TRUE);
237     }
238   }
239   
240 }
241
Popular Tags