1 package org.jbpm.bpel.service.exe; 2 3 import java.util.HashMap ; 4 import java.util.HashSet ; 5 import java.util.Set ; 6 7 import javax.wsdl.Definition; 8 import javax.xml.namespace.QName ; 9 10 import junit.framework.TestCase; 11 12 import org.w3c.dom.Element ; 13 import org.xml.sax.SAXException ; 14 15 import org.jbpm.bpel.data.def.VariableDefinition; 16 import org.jbpm.bpel.data.exe.MessageVariableInstance; 17 import org.jbpm.bpel.def.ImportsDefinition; 18 import org.jbpm.bpel.service.def.CorrelationSetDefinition; 19 import org.jbpm.bpel.wsdl.util.WsdlUtil; 20 import org.jbpm.bpel.xml.BpelConstants; 21 import org.jbpm.bpel.xml.BpelReader; 22 import org.jbpm.bpel.xml.util.NodeUtil; 23 24 28 public class CorrelationSetTest extends TestCase { 29 30 private CorrelationSetInstance correlationInstance; 31 private MessageVariableInstance messageInstance; 32 33 private static final String WSDL_TEXT = 34 "<definitions targetNamespace='http://jbpm.org/bpel/examples'" + 35 " xmlns:tns='http://jbpm.org/bpel/examples'" + 36 " xmlns:xsd='http://www.w3.org/2001/XMLSchema'" + 37 " xmlns:bpws='http://schemas.xmlsoap.org/ws/2004/03/business-process/'" + 38 " xmlns='http://schemas.xmlsoap.org/wsdl/'>" + 39 " <message name='request'>" + 40 " <part name='simplePart' type='xsd:string'/>" + 41 " <part name='elementPart' element='tns:surpriseElement'/>" + 42 " </message>" + 43 " <bpws:property name='nameProperty' type='xsd:string'/>" + 44 " <bpws:property name='idProperty' type='xsd:int'/>" + 45 " <bpws:propertyAlias propertyName='tns:nameProperty' messageType='tns:request'>" + 46 " <bpws:query>elementPart/tns:surpriseElement/c/@name</bpws:query>" + 47 " </bpws:propertyAlias>" + 48 " <bpws:propertyAlias propertyName='tns:idProperty' messageType='tns:request'>" + 49 " <bpws:query>elementPart/tns:surpriseElement/e</bpws:query>" + 50 " </bpws:propertyAlias>" + 51 "</definitions>"; 52 private static final String ELEM_PART_TEXT = 53 "<tns:surpriseElement xmlns:tns='http://jbpm.org/bpel/examples'>" + 54 " <b on=\"true\">true</b>" + 55 " <c name=\"venus\"/>" + 56 " <d amount=\"20\"/>" + 57 " <e>30</e>" + 58 "</tns:surpriseElement>"; 59 private static final QName Q_NAME_PROP = new QName (BpelConstants.NS_EXAMPLES, "nameProperty"); 60 private static final QName Q_ID_PROP = new QName (BpelConstants.NS_EXAMPLES, "idProperty"); 61 62 public CorrelationSetTest(String name) { 63 super(name); 64 } 65 66 protected void setUp() throws Exception { 67 Definition def = WsdlUtil.readText(WSDL_TEXT); 69 ImportsDefinition imports = new ImportsDefinition(); 70 imports.addImport(WsdlUtil.createImport(def)); 71 BpelReader.getInstance().registerPropertyAliases(imports); 72 VariableDefinition variableDefinition = new VariableDefinition(); 74 variableDefinition.setName("msg"); 75 variableDefinition.setTypeInfo(imports.getMessageTypeInfo(new QName (BpelConstants.NS_EXAMPLES, "request"))); 76 messageInstance = new MessageVariableInstance(); 77 messageInstance.setDefinition(variableDefinition); 78 CorrelationSetDefinition correlationDefinition = new CorrelationSetDefinition(); 80 correlationDefinition.setName("cset"); 81 Set properties = new HashSet (); 82 properties.add(WsdlUtil.getProperty(def, Q_NAME_PROP)); 83 properties.add(WsdlUtil.getProperty(def, Q_ID_PROP)); 84 correlationDefinition.setProperties(properties); 85 correlationInstance = new CorrelationSetInstance(); 87 correlationInstance.setDefinition(correlationDefinition); 88 } 89 90 public void testGetProperty() { 91 HashMap propertyValues = new HashMap (); 92 propertyValues.put(Q_NAME_PROP, "elle"); 93 propertyValues.put(Q_ID_PROP, "1981"); 94 correlationInstance.initialize(propertyValues); 95 assertEquals("elle", correlationInstance.getProperty(Q_NAME_PROP)); 96 } 97 98 public void testGetProperties() { 99 Set properties = correlationInstance.getDefinition().getProperties(); 100 assertEquals(2, properties.size()); 101 } 102 103 public void testInitializeMap() { 104 HashMap propertyValues = new HashMap (); 105 propertyValues.put(Q_NAME_PROP, "elle"); 106 propertyValues.put(Q_ID_PROP, "1981"); 107 correlationInstance.initialize(propertyValues); 108 assertEquals(propertyValues, correlationInstance.getProperties()); 109 } 110 111 public void testInitializeMessage() throws Exception { 112 Element partValue = NodeUtil.parseElement(ELEM_PART_TEXT); 113 messageInstance.setPart("elementPart", partValue); 114 115 correlationInstance.initialize(messageInstance); 116 assertEquals("venus", correlationInstance.getProperty(Q_NAME_PROP)); 117 assertEquals("30", correlationInstance.getProperty(Q_ID_PROP)); 118 } 119 120 public void testValidConstraint() throws SAXException { 121 Element partValue = NodeUtil.parseElement(ELEM_PART_TEXT); 122 messageInstance.setPart("elementPart", partValue); 123 124 HashMap propertyValues = new HashMap (); 125 propertyValues.put(Q_NAME_PROP, "venus"); 126 propertyValues.put(Q_ID_PROP, "30"); 127 correlationInstance.initialize(propertyValues); 128 try { 129 correlationInstance.validateConstraint(messageInstance); 130 } 131 catch (RuntimeException e) { 132 fail(e.toString()); 133 } 134 } 135 136 public void testInvalidConstraint() throws SAXException { 137 Element partValue = NodeUtil.parseElement(ELEM_PART_TEXT); 138 messageInstance.setPart("elementPart", partValue); 139 140 HashMap propertyValues = new HashMap (); 141 propertyValues.put(Q_NAME_PROP, "elle"); 142 propertyValues.put(Q_ID_PROP, "1981"); 143 correlationInstance.initialize(propertyValues); 144 try { 145 correlationInstance.validateConstraint(messageInstance); 146 fail("constraint validation should throw an exception"); 147 } 148 catch (RuntimeException e) { 149 } 151 } 152 } 153 | Popular Tags |