KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bpel > xml > BpelReaderTest


1 package org.jbpm.bpel.xml;
2
3 import java.io.ByteArrayInputStream JavaDoc;
4 import java.io.ByteArrayOutputStream JavaDoc;
5 import java.io.IOException JavaDoc;
6 import java.io.ObjectInputStream JavaDoc;
7 import java.io.ObjectOutputStream JavaDoc;
8 import java.io.Serializable JavaDoc;
9 import java.util.Collections JavaDoc;
10 import java.util.Map JavaDoc;
11
12 import javax.wsdl.Output;
13 import javax.xml.namespace.QName JavaDoc;
14
15 import org.w3c.dom.Element JavaDoc;
16 import org.xml.sax.InputSource JavaDoc;
17
18 import com.ibm.wsdl.OutputImpl;
19
20 import org.jbpm.graph.def.Node;
21
22 import org.jbpm.bpel.data.def.MessageTypeInfo;
23 import org.jbpm.bpel.data.def.VariableDefinition;
24 import org.jbpm.bpel.def.Activity;
25 import org.jbpm.bpel.def.BpelDefinition;
26 import org.jbpm.bpel.def.Import;
27 import org.jbpm.bpel.service.def.CorrelationSetDefinition;
28 import org.jbpm.bpel.service.def.Receiver;
29 import org.jbpm.bpel.timer.def.Alarm;
30
31 /**
32  * @author Juan Cantú
33  * @version $Revision: 1.19 $ $Date: 2005/06/23 20:45:04 $
34  */

35 public class BpelReaderTest extends AbstractReaderTestCase {
36   
37   public void setUp() throws Exception JavaDoc {
38     super.setUp();
39     initMessageProperties();
40   }
41
42   public void testReadUrl() throws Exception JavaDoc {
43     BpelDefinition pd = new BpelDefinition();
44     // read bpel
45
ProblemCollector problems = new ProblemCollector();
46     reader.setProblemHandler(problems);
47     InputSource JavaDoc input =
48       new InputSource JavaDoc(getClass().getResource("processSample.bpel").toString());
49     reader.read(pd, input);
50     // assertions
51
assertTrue(problems.getProblems().isEmpty());
52     assertNull(pd.getStartState());
53     Node node = (Node) pd.getNodes().get(0);
54     assertNotNull(node);
55     assertEquals(pd, node.getProcessDefinition());
56   }
57
58   
59   public void testReadUrlLegacy() throws Exception JavaDoc {
60     BpelDefinition pd = new BpelDefinition();
61     // add wsdl document
62
Import imp = new Import();
63     imp.setNamespace("http://manufacturing.org/wsdl/purchase");
64     imp.setLocation(getClass().getResource("partnerLinkTypeSample-1_1.wsdl").toString());
65     imp.setType(Import.Type.WSDL);
66     reader.readWsdlDocument(imp, new ImportWsdlLocator(""));
67     pd.getImports().addImport(imp);
68     // read bpel
69
ProblemCollector problems = new ProblemCollector();
70     reader.setProblemHandler(problems);
71     InputSource JavaDoc input =
72       new InputSource JavaDoc(getClass().getResource("processSample-1_1.bpel").toString());
73     reader.read(pd, input);
74     // assertions
75
assertTrue(problems.getProblems().isEmpty());
76     assertNull(pd.getStartState());
77     Node node = (Node) pd.getNodes().get(0);
78     assertNotNull(node);
79     assertEquals(pd, node.getProcessDefinition());
80   }
81   
82   public void testQueryLanguage() throws Exception JavaDoc {
83     String JavaDoc xml = "<pd queryLanguage='ql'>" +
84         "<receive partnerLink='aPartner' operation='o' variable='iv'/></pd>";
85     readProcess( xml );
86     assertEquals( "ql", pd.getQueryLanguage() );
87   }
88   
89   public void testExpressionLanguage() throws Exception JavaDoc {
90     String JavaDoc xml = "<pd expressionLanguage='el'>" +
91         "<receive partnerLink='aPartner' operation='o' variable='iv'/></pd>";
92     readProcess( xml );
93     assertEquals( "el", pd.getExpressionLanguage() );
94   }
95   
96   public void testSuppressJoinFailureYes() throws Exception JavaDoc {
97     String JavaDoc xml = "<pd suppressJoinFailure='yes'>" +
98         "<receive partnerLink='aPartner' operation='o' variable='iv'/></pd>";
99     readProcess( xml );
100     assertTrue( pd.getScope().getSuppressJoinFailure().booleanValue() );
101   }
102   
103   public void testSuppressJoinFailureNo() throws Exception JavaDoc {
104     String JavaDoc xml = "<pd suppressJoinFailure='no'>" +
105         "<receive partnerLink='aPartner' operation='o' variable='iv'/></pd>";
106     readProcess( xml );
107     assertFalse( pd.getScope().getSuppressJoinFailure().booleanValue() );
108   }
109   
110   public void testSuppressJoinFailureDefault() throws Exception JavaDoc {
111     String JavaDoc xml = "<pd>" +
112             "<receive partnerLink='aPartner' operation='o' variable='iv'/></pd>";
113     readProcess( xml );
114     assertFalse( pd.getScope().getSuppressJoinFailure().booleanValue() );
115   }
116   
117   public void testAbstractProcessYes() throws Exception JavaDoc {
118     String JavaDoc xml = "<pd abstractProcess='yes'>" +
119             "<receive partnerLink='aPartner' operation='o' variable='iv'/></pd>";
120     readProcess( xml );
121     assertTrue( pd.isAbstractProcess() );
122   }
123   
124   public void testAbstractProcessNo() throws Exception JavaDoc {
125     String JavaDoc xml = "<pd abstractProcess='no'>" +
126             "<receive partnerLink='aPartner' operation='o' variable='iv'/></pd>";
127     readProcess( xml );
128     assertFalse( pd.isAbstractProcess() );
129   }
130   
131   public void testAbstractProcessDefault() throws Exception JavaDoc {
132     String JavaDoc xml = "<pd>" +
133             "<receive partnerLink='aPartner' operation='o' variable='iv'/></pd>";
134     readProcess( xml );
135     assertFalse( pd.isAbstractProcess() );
136   }
137   
138   public void testEnableInstanceCompensationYes() throws Exception JavaDoc {
139     String JavaDoc xml = "<pd enableInstanceCompensation='yes'>" +
140             "<receive partnerLink='aPartner' operation='o' variable='iv'/></pd>";
141     readProcess( xml );
142     assertTrue( pd.getEnableInstanceCompensation() );
143   }
144   
145   public void testEnableInstanceCompensationNo() throws Exception JavaDoc {
146     String JavaDoc xml = "<pd enableInstanceCompensation='no'>" +
147             "<receive partnerLink='aPartner' operation='o' variable='iv'/></pd>";
148     readProcess( xml );
149     assertFalse( pd.getEnableInstanceCompensation() );
150   }
151   
152   public void testEnableInstanceCompensationDefault() throws Exception JavaDoc {
153     String JavaDoc xml = "<pd>" +
154             "<receive partnerLink='aPartner' operation='o' variable='iv'/></pd>";
155     readProcess( xml );
156     assertFalse( pd.getEnableInstanceCompensation() );
157   }
158
159   public void testScopeDefinition() {
160     //TODO
161
}
162   
163   public void testActivity() throws Exception JavaDoc {
164     String JavaDoc xml = "<pd>" +
165             "<receive partnerLink='aPartner' operation='o' variable='iv'/></pd>";
166     readProcess( xml );
167     Activity root = pd.getRoot();
168     assertNotNull(root);
169     assertEquals(pd, root.getProcessDefinition());
170   }
171   
172   // Receiver
173
/////////////////////////////////////////////////////////////////////////////
174

175   public void testReceiverPartnerLink() throws Exception JavaDoc {
176     String JavaDoc xml =
177       "<rcvr partnerLink='aPartner' operation='o' variable='iv'/>";
178     
179     Receiver receiver = readReceiver( xml );
180     assertEquals(pLink, receiver.getPartnerLink() );
181   }
182   
183   public void testReceiverPortType() {
184     String JavaDoc xml = "<receive partnerLink='aPartner' portType='tns:mpt' operation='o' variable='iv'" +
185         " xmlns:tns='http://manufacturing.org/wsdl/purchase'/>";
186     try {
187       readReceiver( xml );
188     }
189     catch (Exception JavaDoc e) {
190       fail(e.toString());
191     }
192   }
193
194   public void testReceiverPortTypeDefault() throws Exception JavaDoc {
195     String JavaDoc xml =
196       "<rcvr partnerLink='aPartner' operation='o' variable='iv'/>";
197     
198     try {
199       readReceiver( xml );
200     }
201     catch (Exception JavaDoc e) {
202       fail(e.toString());
203     }
204   }
205   
206   public void testReceiverPortTypeNotFound() {
207     String JavaDoc xml = "<rcvr partnerLink='aPartner' portType='invalidPT' operation='o'/>";
208     try {
209       readReceiver( xml );
210       fail("receive parse must fail when portType doesn't match myRole's portType");
211     }
212     catch(Exception JavaDoc e) {
213       assertTrue(e instanceof BpelException);
214     }
215   }
216     
217   public void testReceiverOperation() throws Exception JavaDoc {
218     String JavaDoc xml =
219       "<rcvr partnerLink='aPartner' operation='o' variable='iv'/>";
220     
221     Receiver receiver = readReceiver( xml );
222     assertEquals( "o", receiver.getOperation().getName() );
223   }
224   
225   public void testReceiverVariable() throws Exception JavaDoc {
226     MessageTypeInfo typeInfo = (MessageTypeInfo) messageVariable.getTypeInfo();
227     Output output = new OutputImpl();
228     output.setMessage(typeInfo.getMessage());
229     operation.setOutput(output);
230     String JavaDoc xml =
231       "<rcvr partnerLink='aPartner' operation='o' variable='iv'/>";
232
233     Receiver receiver = readReceiver( xml );
234     assertEquals(messageVariable, receiver.getVariable() );
235   }
236   
237   public void testReceiverCorrelations() throws Exception JavaDoc {
238     CorrelationSetDefinition corr = new CorrelationSetDefinition();
239     corr.setName("corr");
240     corr.setProperties(Collections.EMPTY_SET);
241     scope.addCorrelationSet(corr);
242     
243     String JavaDoc xml =
244       "<rcvr partnerLink='aPartner' operation='o' variable='iv'>" +
245       " <correlations>" +
246       " <correlation set='corr'/> " +
247       " </correlations>" +
248       "</rcvr>";
249     Receiver receiver = readReceiver( xml );
250     
251     assertNotNull( receiver.getCorrelations() );
252   }
253   
254   // Alarm
255
/////////////////////////////////////////////////////////////////////////////
256

257   public void testAlarmFor() throws Exception JavaDoc {
258     String JavaDoc xml =
259       "<alrm>" +
260       " <for>$f</for>" +
261       "</alrm>";
262     
263     Alarm alarm = readAlarm( xml );
264     assertEquals( "$f", alarm.getFor().getText() );
265     assertEquals( Alarm.AlarmKind.DURATION, alarm.getAlarmKind() );
266   }
267
268   public void testAlarmUntil() throws Exception JavaDoc {
269     String JavaDoc xml =
270       "<alrm>" +
271       " <until>$u</until>" +
272       "</alrm>";
273     
274     Alarm alarm = readAlarm( xml );
275     assertEquals( "$u", alarm.getUntil().getText() );
276     assertEquals( Alarm.AlarmKind.DEADLINE, alarm.getAlarmKind() );
277   }
278
279   public void testAlarmRepeat() throws Exception JavaDoc {
280     String JavaDoc xml =
281       "<alrm>" +
282       " <for>$f</for>" +
283       " <repeatEvery>$r</repeatEvery>" +
284       " <until>$u</until>" +
285       "</alrm>";
286     
287     Alarm alarm = readAlarm( xml );
288     assertEquals( "$u", alarm.getUntil().getText() );
289     assertEquals( "$f", alarm.getFor().getText() );
290     assertEquals( "$r", alarm.getRepeatEvery().getText() );
291     assertEquals( Alarm.AlarmKind.CYCLIC, alarm.getAlarmKind() );
292   }
293
294   public void testAlarmDefault() throws Exception JavaDoc {
295     String JavaDoc xml = "<alrm/>";
296     ProblemCollector collector = new ProblemCollector();
297     reader.setProblemHandler(collector);
298     readAlarm( xml );
299     if(!collector.hasErrors()) {
300       fail("pick parse must fail when an onAlarm doesn't have a expression");
301     }
302   }
303
304   // Variables
305
/////////////////////////////////////////////////////////////////////////////
306

307   public void testVariableName() throws Exception JavaDoc {
308     String JavaDoc xml =
309      "<variables>" +
310      " <variable name='v' type='simple'/>" +
311      "</variables>";
312
313     Map JavaDoc variables = readVariables( xml );
314     VariableDefinition variable = (VariableDefinition) variables.get("v");
315     assertEquals("v", variable.getName());
316   }
317   
318   public void testVariableNameUntyped() throws Exception JavaDoc {
319     String JavaDoc xml =
320      "<variables>" +
321      " <variable name='v'/>" +
322      "</variables>";
323     try {
324       readVariables( xml );
325       fail("scope parse must fail when a variable doesn't have a type");
326     }
327     catch(BpelException e) {
328       //test succeeded
329
}
330   }
331   
332   public void testVariableType() throws Exception JavaDoc {
333     String JavaDoc xml =
334      "<variables>" +
335      " <variable name='v' type='simple'/>" +
336      "</variables>";
337     Map JavaDoc variables = readVariables( xml );
338     VariableDefinition variable = (VariableDefinition) variables.get("v");
339     assertEquals(new QName JavaDoc("simple"), variable.getTypeInfo().getName());
340     assertEquals(VariableDefinition.class, variable.getClass());
341   }
342   
343   public void testVariableMessageType() throws Exception JavaDoc {
344     initMessageProperties();
345     String JavaDoc xml =
346      "<variables xmlns:tns='http://manufacturing.org/wsdl/purchase'>" +
347      " <variable name='v' messageType='tns:aQName'/>" +
348      "</variables>";
349     Map JavaDoc variables = readVariables( xml );
350     VariableDefinition variable = (VariableDefinition) variables.get("v");
351     assertEquals(new QName JavaDoc("http://manufacturing.org/wsdl/purchase", "aQName"), variable.getTypeInfo().getName());
352     assertEquals(VariableDefinition.class, variable.getClass());
353   }
354   
355   public void testVariableSimpleType() throws Exception JavaDoc {
356     String JavaDoc xml =
357      "<variables>" +
358      " <variable name='v' type='simple'/>" +
359      "</variables>";
360     Map JavaDoc variables = readVariables( xml );
361     VariableDefinition variable = (VariableDefinition) variables.get("v");
362     assertEquals(new QName JavaDoc("simple"), variable.getTypeInfo().getName());
363     assertEquals(VariableDefinition.class, variable.getClass());
364   }
365   
366   public void testVariableElement() throws Exception JavaDoc {
367     String JavaDoc xml =
368      "<variables>" +
369      " <variable name='v' element='element'/>" +
370      "</variables>";
371     Map JavaDoc variables = readVariables( xml );
372     VariableDefinition variable = (VariableDefinition) variables.get("v");
373     assertEquals(new QName JavaDoc("element"), variable.getTypeInfo().getName());
374     assertEquals(VariableDefinition.class, variable.getClass());
375   }
376   
377   // Helper methods
378
/////////////////////////////////////////////////////////////////////////////
379

380   private void readProcess(String JavaDoc xml) throws Exception JavaDoc {
381     Element JavaDoc element = parseAsBpelElement(xml);
382     reader.readTopLevelAttributes(element, pd);
383     reader.readScope(element, pd.getScope());
384   }
385   
386   private Map JavaDoc readVariables(String JavaDoc xml) throws Exception JavaDoc {
387     Element JavaDoc element = parseAsBpelElement(xml);
388     return reader.readVariables(element, scope);
389   }
390   
391   private Receiver readReceiver(String JavaDoc xml) throws Exception JavaDoc {
392     Element JavaDoc element = parseAsBpelElement(xml);
393     return reader.readReceiver(element, scope);
394   }
395   
396   private Alarm readAlarm(String JavaDoc xml) throws Exception JavaDoc {
397     Element JavaDoc element = parseAsBpelElement(xml);
398     return reader.readAlarm(element, scope);
399   }
400   
401   private Object JavaDoc writeAndRead(Serializable JavaDoc serializable)
402   throws IOException JavaDoc, ClassNotFoundException JavaDoc {
403     ByteArrayOutputStream JavaDoc baout = new ByteArrayOutputStream JavaDoc();
404     ObjectOutputStream JavaDoc out = new ObjectOutputStream JavaDoc(baout);
405     out.writeObject(serializable);
406     
407     ByteArrayInputStream JavaDoc bain = new ByteArrayInputStream JavaDoc(baout.toByteArray());
408     ObjectInputStream JavaDoc in = new ObjectInputStream JavaDoc(bain);
409     return in.readObject();
410   }
411 }
412
Popular Tags