1 package org.jbpm.bpel.def.assign; 2 3 import java.util.HashMap ; 4 5 import javax.wsdl.Definition; 6 import javax.xml.namespace.QName ; 7 8 import junit.framework.TestCase; 9 10 import org.w3c.dom.Document ; 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.data.exe.MessageVariableInstance; 19 import org.jbpm.bpel.def.BpelDefinition; 20 import org.jbpm.bpel.def.ImportsDefinition; 21 import org.jbpm.bpel.wsdl.util.WsdlUtil; 22 import org.jbpm.bpel.xml.BpelConstants; 23 import org.jbpm.bpel.xml.BpelException; 24 import org.jbpm.bpel.xml.util.NodeUtil; 25 26 30 public class ToVariableTest extends TestCase { 31 32 private Token token; 33 private MessageVariableInstance messageInstance; 34 private Snippet query; 35 private ToQuery to = new ToQuery(); 36 37 private static final String WSDL_TEXT = 38 "<definitions targetNamespace='http://jbpm.org/bpel/examples'" + 39 " xmlns:tns='http://jbpm.org/bpel/examples'" + 40 " xmlns:xsd='http://www.w3.org/2001/XMLSchema'" + 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 "</definitions>"; 47 private static final String ELEM_SURPRISE = "surpriseElement"; 48 49 public ToVariableTest(String name) { 50 super(name); 51 } 52 53 protected void setUp() throws Exception { 54 BpelDefinition pd = new BpelDefinition(); 56 ProcessInstance pi = new ProcessInstance(pd); 57 token = pi.getRootToken(); 58 Definition def = WsdlUtil.readText(WSDL_TEXT); 60 ImportsDefinition imports = pd.getImports(); 61 imports.addImport(WsdlUtil.createImport(def)); 62 VariableDefinition messageDefinition = new VariableDefinition(); 64 messageDefinition.setName("msg"); 65 messageDefinition.setTypeInfo(imports.getMessageTypeInfo(new QName (BpelConstants.NS_EXAMPLES, "request"))); 66 messageInstance = (MessageVariableInstance) messageDefinition.createInstance(token); 67 query = new Snippet(); 69 query.setUse(Snippet.Use.QUERY); 70 to.setQuery(query); 71 } 72 73 public void testAssignVariable() throws Exception { 74 query.setText("$msg"); 76 query.parseScript(); 77 Document doc = NodeUtil.createDocument(); 79 Element sourceMessage = doc.createElementNS("urn:foo", "bar"); 80 sourceMessage.appendChild(doc.createElementNS(null, "elementPart")); 81 to.assign(token, sourceMessage); 82 Element message = (Element ) messageInstance.getValue(); 84 assertEquals(BpelConstants.NS_VENDOR, message.getNamespaceURI()); 85 assertEquals(1, message.getElementsByTagName("elementPart").getLength()); 86 } 87 88 public void testAssignPart() throws Exception { 89 query.setText("$msg/elementPart"); 91 query.parseScript(); 92 Document doc = NodeUtil.createDocument(); 94 Element sourceElement = doc.createElementNS("urn:foo", "sourceElement"); 95 sourceElement.appendChild(doc.createElementNS(BpelConstants.NS_EXAMPLES, ELEM_SURPRISE)); 96 to.assign(token, sourceElement); 97 Element part = messageInstance.getPart("elementPart"); 99 assertEquals(BpelConstants.NS_EXAMPLES, part.getNamespaceURI()); 100 assertEquals(ELEM_SURPRISE, part.getLocalName()); 101 } 102 103 public void testAssignQuery() throws Exception { 104 try { 105 HashMap namespaceDeclarations = new HashMap (); 107 namespaceDeclarations.put("bpws", BpelConstants.NS_BPWS); 108 namespaceDeclarations.put("tns", BpelConstants.NS_EXAMPLES); 109 Element elementPart = messageInstance.getOrCreatePart("elementPart"); 111 Document doc = elementPart.getOwnerDocument(); 112 Element elemE = (Element ) elementPart.appendChild(doc.createElementNS(null, "e")); 113 query.setText("$msg/elementPart/tns:surpriseElement/e"); 115 query.setNamespaces(namespaceDeclarations); 116 query.parseScript(); 117 to.assign(token, "1981"); 118 assertEquals("1981", NodeUtil.getValue(elemE)); 120 } 121 catch (BpelException e) { 122 fail(e.getMessage()); 123 } 124 } 125 } 126 | Popular Tags |