KickJava   Java API By Example, From Geeks To Geeks.

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


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.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.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 /**
26  * @author Alejandro Guízar
27  * @version $Revision: 1.8 $ $Date: 2005/06/23 02:17:26 $
28  */

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 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='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 JavaDoc 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 JavaDoc name) {
55     super(name);
56   }
57   
58   protected void setUp() throws Exception JavaDoc {
59     // process and context
60
BpelDefinition pd = new BpelDefinition();
61     ProcessInstance pi = new ProcessInstance(pd);
62     token = pi.getRootToken();
63     // message type
64
Definition def = WsdlUtil.readText(WSDL_TEXT);
65     ImportsDefinition imports = pd.getImports();
66     imports.addImport(WsdlUtil.createImport(def));
67     // message variable
68
VariableDefinition messageDefinition = new VariableDefinition();
69     messageDefinition.setName("msg");
70     messageDefinition.setTypeInfo(imports.getMessageTypeInfo(new QName JavaDoc(BpelConstants.NS_EXAMPLES, "request")));
71     messageInstance = (MessageVariableInstance) messageDefinition.createInstance(token);
72     // expression
73
expr = new Snippet();
74     expr.setUse(Snippet.Use.EXPRESSION);
75     from.setExpression(expr);
76   }
77   
78   public void testExtractVariable() throws Exception JavaDoc {
79     // expression
80
expr.setText("$msg");
81     expr.parseScript();
82     // instance data
83
messageInstance.setMessage(MessageVariableInstance.createMessageElement());
84     // assertion
85
assertSame(messageInstance.getValue(), from.extract(token));
86   }
87   
88   public void testExtractPart() throws Exception JavaDoc {
89     // expression
90
expr.setText("$msg/elementPart");
91     expr.parseScript();
92     // instance data
93
Element JavaDoc elementPart = messageInstance.getOrCreatePart("elementPart");
94     // assertion
95
assertSame(elementPart, NodeUtil.getValue((Element JavaDoc) from.extract(token)));
96   }
97   
98   public void testExtractQuery() throws Exception JavaDoc {
99     // enclosing element
100
HashMap JavaDoc namespaceDeclarations = new HashMap JavaDoc();
101     namespaceDeclarations.put("bpws", BpelConstants.NS_BPWS);
102     namespaceDeclarations.put("tns", BpelConstants.NS_EXAMPLES);
103     // expression
104
expr.setText("$msg/elementPart/tns:surpriseElement/c/@name");
105     expr.setNamespaces(namespaceDeclarations);
106     expr.parseScript();
107     // instance data
108
Element JavaDoc partValue = NodeUtil.parseElement(ELEM_PART_TEXT);
109     messageInstance.setPart("elementPart", partValue);
110     // assertion
111
assertEquals("venus", NodeUtil.getValue((Attr JavaDoc) from.extract(token)));
112   }
113 }
114
Popular Tags