KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bpel > def > assign > ToVariableTest


1 package org.jbpm.bpel.def.assign;
2
3 import java.util.HashMap JavaDoc;
4
5 import javax.wsdl.Definition;
6 import javax.xml.namespace.QName JavaDoc;
7
8 import junit.framework.TestCase;
9
10 import org.w3c.dom.Document JavaDoc;
11 import org.w3c.dom.Element JavaDoc;
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 /**
27  * @author Alejandro Guízar
28  * @version $Revision: 1.6 $ $Date: 2005/06/23 02:17:26 $
29  */

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 JavaDoc 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 JavaDoc ELEM_SURPRISE = "surpriseElement";
48   
49   public ToVariableTest(String JavaDoc name) {
50     super(name);
51   }
52   
53   protected void setUp() throws Exception JavaDoc {
54     // process and context
55
BpelDefinition pd = new BpelDefinition();
56     ProcessInstance pi = new ProcessInstance(pd);
57     token = pi.getRootToken();
58     // message type
59
Definition def = WsdlUtil.readText(WSDL_TEXT);
60     ImportsDefinition imports = pd.getImports();
61     imports.addImport(WsdlUtil.createImport(def));
62     // message variable
63
VariableDefinition messageDefinition = new VariableDefinition();
64     messageDefinition.setName("msg");
65     messageDefinition.setTypeInfo(imports.getMessageTypeInfo(new QName JavaDoc(BpelConstants.NS_EXAMPLES, "request")));
66     messageInstance = (MessageVariableInstance) messageDefinition.createInstance(token);
67     // query
68
query = new Snippet();
69     query.setUse(Snippet.Use.QUERY);
70     to.setQuery(query);
71   }
72   
73   public void testAssignVariable() throws Exception JavaDoc {
74     // query
75
query.setText("$msg");
76     query.parseScript();
77     // instance data
78
Document JavaDoc doc = NodeUtil.createDocument();
79     Element JavaDoc sourceMessage = doc.createElementNS("urn:foo", "bar");
80     sourceMessage.appendChild(doc.createElementNS(null, "elementPart"));
81     to.assign(token, sourceMessage);
82     // assertions
83
Element JavaDoc message = (Element JavaDoc) messageInstance.getValue();
84     assertEquals(BpelConstants.NS_VENDOR, message.getNamespaceURI());
85     assertEquals(1, message.getElementsByTagName("elementPart").getLength());
86   }
87   
88   public void testAssignPart() throws Exception JavaDoc {
89     // query
90
query.setText("$msg/elementPart");
91     query.parseScript();
92     // instance data
93
Document JavaDoc doc = NodeUtil.createDocument();
94     Element JavaDoc sourceElement = doc.createElementNS("urn:foo", "sourceElement");
95     sourceElement.appendChild(doc.createElementNS(BpelConstants.NS_EXAMPLES, ELEM_SURPRISE));
96     to.assign(token, sourceElement);
97     // assertions
98
Element JavaDoc 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 JavaDoc {
104     try {
105       // enclosing element
106
HashMap JavaDoc namespaceDeclarations = new HashMap JavaDoc();
107       namespaceDeclarations.put("bpws", BpelConstants.NS_BPWS);
108       namespaceDeclarations.put("tns", BpelConstants.NS_EXAMPLES);
109       // setup data
110
Element JavaDoc elementPart = messageInstance.getOrCreatePart("elementPart");
111       Document JavaDoc doc = elementPart.getOwnerDocument();
112       Element JavaDoc elemE = (Element JavaDoc) elementPart.appendChild(doc.createElementNS(null, "e"));
113       // query
114
query.setText("$msg/elementPart/tns:surpriseElement/e");
115       query.setNamespaces(namespaceDeclarations);
116       query.parseScript();
117       to.assign(token, "1981");
118       // assertions
119
assertEquals("1981", NodeUtil.getValue(elemE));
120     }
121     catch (BpelException e) {
122       fail(e.getMessage());
123     }
124   }
125 }
126
Popular Tags