KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bpel > data > exe > VariableAccessTest


1 package org.jbpm.bpel.data.exe;
2
3 import java.util.Map 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.Attr 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.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 /**
26  * @author Alejandro Guízar
27  * @version $Revision: 1.12 $ $Date: 2005/06/23 02:17:26 $
28  */

29 public class VariableAccessTest extends TestCase {
30   
31   private Token token;
32   private MessageVariableInstance messageInstance;
33   private Map JavaDoc namespaceDeclarations;
34   private Element JavaDoc partValue;
35   
36   private static final String JavaDoc 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 JavaDoc 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 JavaDoc {
64     // process and context
65
BpelDefinition pd = new BpelDefinition();
66     token = new ProcessInstance(pd).getRootToken();
67     // read wsdl
68
Definition def = WsdlUtil.readText(WSDL_TEXT);
69     ImportsDefinition imports = pd.getImports();
70     imports.addImport(WsdlUtil.createImport(def));
71     BpelReader.getInstance().registerPropertyAliases(imports);
72     // namespace declarations
73
namespaceDeclarations = def.getNamespaces();
74     namespaceDeclarations.remove(""); // remove default namespace association
75
namespaceDeclarations.put("bpws11", BpelConstants.NS_BPWS_1_1); // add bpel 1.1 namespace
76
// message variable
77
VariableDefinition messageDefinition = new VariableDefinition();
78     messageDefinition.setName("msg");
79     messageDefinition.setTypeInfo(imports.getMessageTypeInfo(new QName JavaDoc(BpelConstants.NS_EXAMPLES, "request")));
80     messageInstance = (MessageVariableInstance) messageDefinition.createInstance(token);
81     // part value
82
partValue = NodeUtil.parseElement(ELEM_PART_TEXT);
83     messageInstance.setPart("elementPart", partValue);
84   }
85   
86   public void testMessageSimplePart() {
87     messageInstance.setPart("simplePart", "hola");
88     Element JavaDoc simplePart = (Element JavaDoc) 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 JavaDoc elementPart = (Element JavaDoc) eval("$msg/elementPart");
96     assertNull(elementPart.getNamespaceURI());
97     assertEquals("elementPart", elementPart.getLocalName());
98   }
99   
100   public void testMessageAttributeLocation() throws Exception JavaDoc {
101     Attr JavaDoc nameAttr = (Attr JavaDoc) eval("$msg/elementPart/tns:surpriseElement/c/@name");
102     assertEquals("venus", nameAttr.getValue());
103   }
104   
105   public void testMessageElementLocation() throws Exception JavaDoc {
106     Element JavaDoc e = (Element JavaDoc) 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 JavaDoc simplePart = (Element JavaDoc) 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 JavaDoc elementPart = (Element JavaDoc) 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 JavaDoc {
125     Attr JavaDoc nameAttr = (Attr JavaDoc) eval("bpws11:getVariableData('msg', 'elementPart', '/tns:surpriseElement/c/@name')");
126     assertEquals("venus", nameAttr.getValue());
127   }
128   
129   public void testFunctionElementLocation() throws Exception JavaDoc {
130     Element JavaDoc e = (Element JavaDoc) eval("bpws11:getVariableData('msg', 'elementPart', '/tns:surpriseElement/e')");
131     assertEquals("30", NodeUtil.getValue(e));
132   }
133   
134   public void testFunctionAttributeProperty() throws Exception JavaDoc {
135     assertEquals("venus", eval("bpws11:getVariableProperty('msg', 'tns:nameProperty')"));
136   }
137   
138   public void testFunctionElementProperty() throws Exception JavaDoc {
139     assertEquals("30", eval("bpws11:getVariableProperty('msg', 'tns:idProperty')"));
140   }
141   
142   private Object JavaDoc eval(String JavaDoc exprText) {
143     Object JavaDoc 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