1 package org.jbpm.bpel.data.exe; 2 3 import java.util.Map ; 4 5 import javax.wsdl.Definition; 6 import javax.xml.namespace.QName ; 7 8 import junit.framework.TestCase; 9 10 import org.w3c.dom.Attr ; 11 import org.w3c.dom.Element ; 12 13 import org.jbpm.graph.exe.ProcessInstance; 14 import org.jbpm.graph.exe.Token; 15 16 import org.jbpm.bpel.data.def.Snippet; 17 import org.jbpm.bpel.data.def.VariableDefinition; 18 import org.jbpm.bpel.def.BpelDefinition; 19 import org.jbpm.bpel.def.ImportsDefinition; 20 import org.jbpm.bpel.wsdl.util.WsdlUtil; 21 import org.jbpm.bpel.xml.BpelConstants; 22 import org.jbpm.bpel.xml.BpelReader; 23 import org.jbpm.bpel.xml.util.NodeUtil; 24 25 29 public class VariableAccessTest extends TestCase { 30 31 private Token token; 32 private MessageVariableInstance messageInstance; 33 private Map namespaceDeclarations; 34 private Element partValue; 35 36 private static final String WSDL_TEXT = 37 "<definitions targetNamespace='http://jbpm.org/bpel/examples'" + 38 " xmlns:tns='http://jbpm.org/bpel/examples'" + 39 " xmlns:xsd='http://www.w3.org/2001/XMLSchema'" + 40 " xmlns:bpws='http://schemas.xmlsoap.org/ws/2004/03/business-process/'" + 41 " xmlns='http://schemas.xmlsoap.org/wsdl/'>" + 42 " <message name='request'>" + 43 " <part name='simplePart' type='xsd:string'/>" + 44 " <part name='elementPart' element='tns:surpriseElement'/>" + 45 " </message>" + 46 " <bpws:property name='nameProperty' type='xsd:string'/>" + 47 " <bpws:property name='idProperty' type='xsd:int'/>" + 48 " <bpws:propertyAlias propertyName='tns:nameProperty' messageType='tns:request'>" + 49 " <bpws:query>elementPart/tns:surpriseElement/c/@name</bpws:query>" + 50 " </bpws:propertyAlias>" + 51 " <bpws:propertyAlias propertyName='tns:idProperty' messageType='tns:request'>" + 52 " <bpws:query>elementPart/tns:surpriseElement/e</bpws:query>" + 53 " </bpws:propertyAlias>" + 54 "</definitions>"; 55 private static final String ELEM_PART_TEXT = 56 "<tns:surpriseElement xmlns:tns='http://jbpm.org/bpel/examples'>" + 57 " <b on=\"true\">true</b>" + 58 " <c name=\"venus\"/>" + 59 " <d amount=\"20\"/>" + 60 " <e>30</e>" + 61 "</tns:surpriseElement>"; 62 63 protected void setUp() throws Exception { 64 BpelDefinition pd = new BpelDefinition(); 66 token = new ProcessInstance(pd).getRootToken(); 67 Definition def = WsdlUtil.readText(WSDL_TEXT); 69 ImportsDefinition imports = pd.getImports(); 70 imports.addImport(WsdlUtil.createImport(def)); 71 BpelReader.getInstance().registerPropertyAliases(imports); 72 namespaceDeclarations = def.getNamespaces(); 74 namespaceDeclarations.remove(""); namespaceDeclarations.put("bpws11", BpelConstants.NS_BPWS_1_1); VariableDefinition messageDefinition = new VariableDefinition(); 78 messageDefinition.setName("msg"); 79 messageDefinition.setTypeInfo(imports.getMessageTypeInfo(new QName (BpelConstants.NS_EXAMPLES, "request"))); 80 messageInstance = (MessageVariableInstance) messageDefinition.createInstance(token); 81 partValue = NodeUtil.parseElement(ELEM_PART_TEXT); 83 messageInstance.setPart("elementPart", partValue); 84 } 85 86 public void testMessageSimplePart() { 87 messageInstance.setPart("simplePart", "hola"); 88 Element simplePart = (Element ) eval("$msg/simplePart"); 89 assertNull(simplePart.getNamespaceURI()); 90 assertEquals("simplePart", simplePart.getLocalName()); 91 assertEquals("hola", NodeUtil.getValue(simplePart)); 92 } 93 94 public void testMessageElementPart() { 95 Element elementPart = (Element ) eval("$msg/elementPart"); 96 assertNull(elementPart.getNamespaceURI()); 97 assertEquals("elementPart", elementPart.getLocalName()); 98 } 99 100 public void testMessageAttributeLocation() throws Exception { 101 Attr nameAttr = (Attr ) eval("$msg/elementPart/tns:surpriseElement/c/@name"); 102 assertEquals("venus", nameAttr.getValue()); 103 } 104 105 public void testMessageElementLocation() throws Exception { 106 Element e = (Element ) eval("$msg/elementPart/tns:surpriseElement/e"); 107 assertEquals("30", NodeUtil.getValue(e)); 108 } 109 110 public void testFunctionSimplePart() { 111 messageInstance.setPart("simplePart", "hola"); 112 Element simplePart = (Element ) eval("bpws11:getVariableData('msg', 'simplePart')"); 113 assertNull(simplePart.getNamespaceURI()); 114 assertEquals("simplePart", simplePart.getLocalName()); 115 assertEquals("hola", NodeUtil.getValue(simplePart)); 116 } 117 118 public void testFunctionElementPart() { 119 Element elementPart = (Element ) eval("bpws11:getVariableData('msg', 'elementPart')"); 120 assertEquals(BpelConstants.NS_EXAMPLES, elementPart.getNamespaceURI()); 121 assertEquals("surpriseElement", elementPart.getLocalName()); 122 } 123 124 public void testFunctionAttributeLocation() throws Exception { 125 Attr nameAttr = (Attr ) eval("bpws11:getVariableData('msg', 'elementPart', '/tns:surpriseElement/c/@name')"); 126 assertEquals("venus", nameAttr.getValue()); 127 } 128 129 public void testFunctionElementLocation() throws Exception { 130 Element e = (Element ) eval("bpws11:getVariableData('msg', 'elementPart', '/tns:surpriseElement/e')"); 131 assertEquals("30", NodeUtil.getValue(e)); 132 } 133 134 public void testFunctionAttributeProperty() throws Exception { 135 assertEquals("venus", eval("bpws11:getVariableProperty('msg', 'tns:nameProperty')")); 136 } 137 138 public void testFunctionElementProperty() throws Exception { 139 assertEquals("30", eval("bpws11:getVariableProperty('msg', 'tns:idProperty')")); 140 } 141 142 private Object eval(String exprText) { 143 Object value = null; 144 Snippet expr = new Snippet(); 145 expr.setText(exprText); 146 expr.setUse(Snippet.Use.EXPRESSION); 147 expr.setNamespaces(namespaceDeclarations); 148 value = expr.getScript().evaluate(token); 149 return value; 150 } 151 } 152 | Popular Tags |