KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jbpm.bpel.exe;
2
3 import java.util.HashSet JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.Set JavaDoc;
6
7 import org.jbpm.graph.exe.Token;
8
9 import org.jbpm.bpel.def.Activity;
10 import org.jbpm.bpel.def.Pick;
11 import org.jbpm.bpel.def.Receive;
12 import org.jbpm.bpel.service.def.Receiver;
13 import org.jbpm.bpel.timer.def.Alarm;
14
15 /**
16  * @author Juan Cantú
17  * @version $Revision: 1.7 $ $Date: 2005/05/13 20:07:06 $
18  */

19 public class PickExeTest extends AbstractExeTestCase {
20
21   Pick pick;
22   Set JavaDoc activities;
23   String JavaDoc initialXml =
24     "<pick createInstance='yes'>" +
25       " <onMessage partnerLink='aPartner' operation='o'>" +
26       " <receive name='a' partnerLink='aPartner' operation='o'/>" +
27       " </onMessage>" +
28       " <onMessage partnerLink='aPartner' operation='o'>" +
29       " <receive name='b' partnerLink='aPartner' operation='o'/>" +
30       " </onMessage>" +
31     "</pick>";
32     
33   public void initActivities() {
34     activities = new HashSet JavaDoc();
35     activities.addAll(pick.getNodes());
36     
37     Token root = pi.getRootToken();
38     for(Iterator JavaDoc it = activities.iterator(); it.hasNext();) {
39       Activity activity = ((Activity)it.next());
40       TestLink link = new TestLink(activity.getName());
41       activity.addSource(link);
42       LinkInstance.create(root, link.getName());
43     }
44   }
45   
46   public void testInitialFirstReceiver() throws Exception JavaDoc {
47     pick = (Pick) readActivity(initialXml, true);
48     setRootBlock(pick);
49     initActivities();
50     Iterator JavaDoc msgIt = pick.getOnMessages().iterator();
51     validateInitial((Receiver) msgIt.next());
52   }
53   
54   public void testInitialSecondReceiver() throws Exception JavaDoc {
55     pick = (Pick) readActivity(initialXml, true);
56     setRootBlock(pick);
57     initActivities();
58     Iterator JavaDoc msgIt = pick.getOnMessages().iterator();
59     msgIt.next();
60     validateInitial((Receiver) msgIt.next());
61   }
62   
63   public void validateInitial(Receiver receiver) {
64     Token token = startInitial(receiver);
65     Receive pickedReceive = (Receive)pick.getOnMessage(receiver);
66     assertReceiveAndComplete(token, pickedReceive);
67     assertReceiversDisabled(token, receiver);
68     assertLinksDetermined(token, pickedReceive);
69   }
70   
71   String JavaDoc xml =
72     "<pick>" +
73     " <onMessage partnerLink='aPartner' operation='o'>" +
74     " <receive name='a' partnerLink='aPartner' operation='o'/>" +
75     " </onMessage>" +
76     " <onMessage partnerLink='aPartner' operation='o'>" +
77     " <receive name='b' partnerLink='aPartner' operation='o'/>" +
78     " </onMessage>" +
79     " <onAlarm>" +
80     " <for>$f</for>" +
81     " <receive name='c' partnerLink='aPartner' operation='o'/>" +
82     " </onAlarm>" +
83     " <onAlarm>" +
84     " <until>$u</until>" +
85     " <receive name='d' partnerLink='aPartner' operation='o'/>" +
86     " </onAlarm>" +
87     "</pick>";
88   
89   public void testNestedFirstReceive() throws Exception JavaDoc {
90     pick = (Pick) readActivity(xml, false);
91     setNestedBlock(pick);
92     initActivities();
93     Iterator JavaDoc msgIt = pick.getOnMessages().iterator();
94     validateNestedMsg((Receiver) msgIt.next());
95   }
96   
97   public void testNestedSecondReceive() throws Exception JavaDoc {
98     pick = (Pick) readActivity(xml, false);
99     setNestedBlock(pick);
100     initActivities();
101     Iterator JavaDoc msgIt = pick.getOnMessages().iterator();
102     msgIt.next();
103     validateNestedMsg((Receiver) msgIt.next());
104   }
105   
106   public void testNestedFirstAlarm() throws Exception JavaDoc {
107     pick = (Pick) readActivity(xml, false);
108     setNestedBlock(pick);
109     initActivities();
110     Iterator JavaDoc alarmIt = pick.getOnAlarms().iterator();
111     validateNestedAlarm((Alarm) alarmIt.next());
112   }
113   
114   public void testNestedSecondAlarm() throws Exception JavaDoc {
115     pick = (Pick) readActivity(xml, false);
116     setNestedBlock(pick);
117     initActivities();
118     Iterator JavaDoc alarmIt = pick.getOnAlarms().iterator();
119     alarmIt.next();
120     validateNestedAlarm((Alarm) alarmIt.next());
121   }
122   
123   private void validateNestedMsg(Receiver receiver) {
124     Token token = startNested();
125     Receive pickedReceive = (Receive) pick.getOnMessage(receiver);
126     assertEquals(pick.getStart(), token.getNode());
127     pick.messageReceived(receiver, null, token);
128     assertSame( pickedReceive , token.getNode() );
129
130     assertReceiveAndComplete(token, pickedReceive);
131     assertReceiversDisabled(token, receiver);
132     assertLinksDetermined(token, pickedReceive);
133   }
134   
135   private void validateNestedAlarm(Alarm alarm) {
136     Token token = startNested();
137     assertEquals(pick.getStart(), token.getNode());
138     alarm.onAlarm(token);
139     Receive pickedReceive = (Receive) pick.getOnAlarm(alarm);
140
141     assertReceiveAndComplete(token, pickedReceive);
142     assertReceiversDisabled(token, null);
143     assertLinksDetermined(token, pickedReceive);
144   }
145   
146   private void assertReceiversDisabled(Token token, Receiver event) {
147     for(Iterator JavaDoc it = pick.getOnMessages().iterator(); it.hasNext();) {
148       Receiver other = (Receiver) it.next();
149       if(!other.equals(event)) assertReceiverDisabled(token, other);
150     }
151   }
152   
153   private void assertLinksDetermined(Token token, Activity eventActivity) {
154     for(Iterator JavaDoc it = activities.iterator(); it.hasNext();) {
155       Activity activity = ((Activity)it.next());
156       String JavaDoc name = activity.getName();
157       Boolean JavaDoc reached = LinkInstance.get(token, name).getReached();
158       
159       if(activity.equals(eventActivity)) {
160         assertNotNull( reached );
161       }
162       else {
163         assertEquals( Boolean.FALSE, reached );
164       }
165     }
166   }
167 }
Popular Tags