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.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.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.util.NodeUtil; 24 25 29 public class FromVariableTest extends TestCase { 30 31 private Token token; 32 private MessageVariableInstance messageInstance; 33 private Snippet expr; 34 private FromExpression from = new FromExpression(); 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='http://schemas.xmlsoap.org/wsdl/'>" + 41 " <message name='request'>" + 42 " <part name='simplePart' type='xsd:string'/>" + 43 " <part name='elementPart' element='tns:surpriseElement'/>" + 44 " </message>" + 45 "</definitions>"; 46 private static final String ELEM_PART_TEXT = 47 "<tns:surpriseElement xmlns:tns='http://jbpm.org/bpel/examples'>" + 48 " <b on=\"true\">true</b>" + 49 " <c name=\"venus\"/>" + 50 " <d amount=\"20\"/>" + 51 " <e>30</e>" + 52 "</tns:surpriseElement>"; 53 54 public FromVariableTest(String name) { 55 super(name); 56 } 57 58 protected void setUp() throws Exception { 59 BpelDefinition pd = new BpelDefinition(); 61 ProcessInstance pi = new ProcessInstance(pd); 62 token = pi.getRootToken(); 63 Definition def = WsdlUtil.readText(WSDL_TEXT); 65 ImportsDefinition imports = pd.getImports(); 66 imports.addImport(WsdlUtil.createImport(def)); 67 VariableDefinition messageDefinition = new VariableDefinition(); 69 messageDefinition.setName("msg"); 70 messageDefinition.setTypeInfo(imports.getMessageTypeInfo(new QName (BpelConstants.NS_EXAMPLES, "request"))); 71 messageInstance = (MessageVariableInstance) messageDefinition.createInstance(token); 72 expr = new Snippet(); 74 expr.setUse(Snippet.Use.EXPRESSION); 75 from.setExpression(expr); 76 } 77 78 public void testExtractVariable() throws Exception { 79 expr.setText("$msg"); 81 expr.parseScript(); 82 messageInstance.setMessage(MessageVariableInstance.createMessageElement()); 84 assertSame(messageInstance.getValue(), from.extract(token)); 86 } 87 88 public void testExtractPart() throws Exception { 89 expr.setText("$msg/elementPart"); 91 expr.parseScript(); 92 Element elementPart = messageInstance.getOrCreatePart("elementPart"); 94 assertSame(elementPart, NodeUtil.getValue((Element ) from.extract(token))); 96 } 97 98 public void testExtractQuery() throws Exception { 99 HashMap namespaceDeclarations = new HashMap (); 101 namespaceDeclarations.put("bpws", BpelConstants.NS_BPWS); 102 namespaceDeclarations.put("tns", BpelConstants.NS_EXAMPLES); 103 expr.setText("$msg/elementPart/tns:surpriseElement/c/@name"); 105 expr.setNamespaces(namespaceDeclarations); 106 expr.parseScript(); 107 Element partValue = NodeUtil.parseElement(ELEM_PART_TEXT); 109 messageInstance.setPart("elementPart", partValue); 110 assertEquals("venus", NodeUtil.getValue((Attr ) from.extract(token))); 112 } 113 } 114 | Popular Tags |