1 package org.jbpm.bpel.xml; 2 3 import java.io.ByteArrayInputStream ; 4 import java.io.ByteArrayOutputStream ; 5 import java.io.IOException ; 6 import java.io.ObjectInputStream ; 7 import java.io.ObjectOutputStream ; 8 import java.io.Serializable ; 9 import java.util.Collections ; 10 import java.util.Map ; 11 12 import javax.wsdl.Output; 13 import javax.xml.namespace.QName ; 14 15 import org.w3c.dom.Element ; 16 import org.xml.sax.InputSource ; 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 35 public class BpelReaderTest extends AbstractReaderTestCase { 36 37 public void setUp() throws Exception { 38 super.setUp(); 39 initMessageProperties(); 40 } 41 42 public void testReadUrl() throws Exception { 43 BpelDefinition pd = new BpelDefinition(); 44 ProblemCollector problems = new ProblemCollector(); 46 reader.setProblemHandler(problems); 47 InputSource input = 48 new InputSource (getClass().getResource("processSample.bpel").toString()); 49 reader.read(pd, input); 50 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 { 60 BpelDefinition pd = new BpelDefinition(); 61 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 ProblemCollector problems = new ProblemCollector(); 70 reader.setProblemHandler(problems); 71 InputSource input = 72 new InputSource (getClass().getResource("processSample-1_1.bpel").toString()); 73 reader.read(pd, input); 74 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 { 83 String 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 { 90 String 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 { 97 String 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 { 104 String 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 { 111 String 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 { 118 String 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 { 125 String 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 { 132 String 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 { 139 String 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 { 146 String 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 { 153 String 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 } 162 163 public void testActivity() throws Exception { 164 String 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 175 public void testReceiverPartnerLink() throws Exception { 176 String 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 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 e) { 190 fail(e.toString()); 191 } 192 } 193 194 public void testReceiverPortTypeDefault() throws Exception { 195 String xml = 196 "<rcvr partnerLink='aPartner' operation='o' variable='iv'/>"; 197 198 try { 199 readReceiver( xml ); 200 } 201 catch (Exception e) { 202 fail(e.toString()); 203 } 204 } 205 206 public void testReceiverPortTypeNotFound() { 207 String 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 e) { 213 assertTrue(e instanceof BpelException); 214 } 215 } 216 217 public void testReceiverOperation() throws Exception { 218 String 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 { 226 MessageTypeInfo typeInfo = (MessageTypeInfo) messageVariable.getTypeInfo(); 227 Output output = new OutputImpl(); 228 output.setMessage(typeInfo.getMessage()); 229 operation.setOutput(output); 230 String 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 { 238 CorrelationSetDefinition corr = new CorrelationSetDefinition(); 239 corr.setName("corr"); 240 corr.setProperties(Collections.EMPTY_SET); 241 scope.addCorrelationSet(corr); 242 243 String 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 257 public void testAlarmFor() throws Exception { 258 String 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 { 269 String 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 { 280 String 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 { 295 String 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 307 public void testVariableName() throws Exception { 308 String xml = 309 "<variables>" + 310 " <variable name='v' type='simple'/>" + 311 "</variables>"; 312 313 Map variables = readVariables( xml ); 314 VariableDefinition variable = (VariableDefinition) variables.get("v"); 315 assertEquals("v", variable.getName()); 316 } 317 318 public void testVariableNameUntyped() throws Exception { 319 String 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 } 330 } 331 332 public void testVariableType() throws Exception { 333 String xml = 334 "<variables>" + 335 " <variable name='v' type='simple'/>" + 336 "</variables>"; 337 Map variables = readVariables( xml ); 338 VariableDefinition variable = (VariableDefinition) variables.get("v"); 339 assertEquals(new QName ("simple"), variable.getTypeInfo().getName()); 340 assertEquals(VariableDefinition.class, variable.getClass()); 341 } 342 343 public void testVariableMessageType() throws Exception { 344 initMessageProperties(); 345 String xml = 346 "<variables xmlns:tns='http://manufacturing.org/wsdl/purchase'>" + 347 " <variable name='v' messageType='tns:aQName'/>" + 348 "</variables>"; 349 Map variables = readVariables( xml ); 350 VariableDefinition variable = (VariableDefinition) variables.get("v"); 351 assertEquals(new QName ("http://manufacturing.org/wsdl/purchase", "aQName"), variable.getTypeInfo().getName()); 352 assertEquals(VariableDefinition.class, variable.getClass()); 353 } 354 355 public void testVariableSimpleType() throws Exception { 356 String xml = 357 "<variables>" + 358 " <variable name='v' type='simple'/>" + 359 "</variables>"; 360 Map variables = readVariables( xml ); 361 VariableDefinition variable = (VariableDefinition) variables.get("v"); 362 assertEquals(new QName ("simple"), variable.getTypeInfo().getName()); 363 assertEquals(VariableDefinition.class, variable.getClass()); 364 } 365 366 public void testVariableElement() throws Exception { 367 String xml = 368 "<variables>" + 369 " <variable name='v' element='element'/>" + 370 "</variables>"; 371 Map variables = readVariables( xml ); 372 VariableDefinition variable = (VariableDefinition) variables.get("v"); 373 assertEquals(new QName ("element"), variable.getTypeInfo().getName()); 374 assertEquals(VariableDefinition.class, variable.getClass()); 375 } 376 377 380 private void readProcess(String xml) throws Exception { 381 Element element = parseAsBpelElement(xml); 382 reader.readTopLevelAttributes(element, pd); 383 reader.readScope(element, pd.getScope()); 384 } 385 386 private Map readVariables(String xml) throws Exception { 387 Element element = parseAsBpelElement(xml); 388 return reader.readVariables(element, scope); 389 } 390 391 private Receiver readReceiver(String xml) throws Exception { 392 Element element = parseAsBpelElement(xml); 393 return reader.readReceiver(element, scope); 394 } 395 396 private Alarm readAlarm(String xml) throws Exception { 397 Element element = parseAsBpelElement(xml); 398 return reader.readAlarm(element, scope); 399 } 400 401 private Object writeAndRead(Serializable serializable) 402 throws IOException , ClassNotFoundException { 403 ByteArrayOutputStream baout = new ByteArrayOutputStream (); 404 ObjectOutputStream out = new ObjectOutputStream (baout); 405 out.writeObject(serializable); 406 407 ByteArrayInputStream bain = new ByteArrayInputStream (baout.toByteArray()); 408 ObjectInputStream in = new ObjectInputStream (bain); 409 return in.readObject(); 410 } 411 } 412 | Popular Tags |