KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jbpm.bpel.data.exe;
2
3 import java.util.List JavaDoc;
4
5 import javax.xml.namespace.QName JavaDoc;
6
7 import junit.framework.TestCase;
8
9 import org.w3c.dom.Attr JavaDoc;
10 import org.w3c.dom.Element JavaDoc;
11
12 import org.jbpm.context.def.ContextDefinition;
13 import org.jbpm.graph.def.ProcessDefinition;
14 import org.jbpm.graph.exe.ProcessInstance;
15 import org.jbpm.graph.exe.Token;
16
17 import org.jbpm.bpel.data.def.ElementTypeInfo;
18 import org.jbpm.bpel.data.def.Snippet;
19 import org.jbpm.bpel.data.def.VariableDefinition;
20 import org.jbpm.bpel.xml.BpelException;
21 import org.jbpm.bpel.xml.util.NodeUtil;
22
23 /**
24  * @author Alejandro Guízar
25  * @version $Revision: 1.5 $ $Date: 2005/06/23 02:17:26 $
26  */

27 public class GeneralExpressionTest extends TestCase {
28   
29   private Token token;
30   
31   private static final String JavaDoc ELEM1_TEXT =
32     "<a>" +
33     " <b on=\"true\">true</b>" +
34     " <c name=\"venus\"/>" +
35     " <d amount=\"20\"/>" +
36     " <e>30</e>" +
37     "</a>";
38   private static final String JavaDoc ELEM2_TEXT =
39     "<a>" +
40     " <b on=\"\"/>" +
41     " <c name=\"mars\"/>" +
42     " <d amount=\"30\"/>" +
43     " <e>40</e>" +
44     "</a>";
45
46
47   public GeneralExpressionTest(String JavaDoc name) {
48     super(name);
49   }
50   
51   protected void setUp() throws Exception JavaDoc {
52     // process and context
53
ProcessDefinition pd = new ProcessDefinition();
54     pd.addDefinition(new ContextDefinition());
55     ProcessInstance pi = new ProcessInstance(pd);
56     token = pi.getRootToken();
57     // element variables
58
Element JavaDoc elem1 = NodeUtil.parseElement(ELEM1_TEXT);
59     VariableDefinition var1 = new VariableDefinition();
60     var1.setName("var1");
61     var1.setTypeInfo(new ElementTypeInfo(new QName JavaDoc(elem1.getNamespaceURI(), elem1.getLocalName())));
62     var1.createInstance(token).setValue(elem1);
63     Element JavaDoc elem2 = NodeUtil.parseElement(ELEM2_TEXT);
64     VariableDefinition var2 = new VariableDefinition();
65     var2.setName("var2");
66     var2.setTypeInfo(new ElementTypeInfo(new QName JavaDoc(elem2.getNamespaceURI(), elem2.getLocalName())));
67     var2.createInstance(token).setValue(elem2);
68   }
69   
70   public void testAdd() {
71     assertEquals(new Double JavaDoc(60), eval("$var1/d/@amount + $var2/e"));
72     assertEquals(new Double JavaDoc(60), eval("20 + $var2/e"));
73   }
74   
75   public void testSubstract() {
76     assertEquals(new Double JavaDoc(20), eval("$var2/e - $var1/d/@amount"));
77     assertEquals(new Double JavaDoc(20), eval("$var2/e - 20"));
78   }
79   
80   public void testMultiply() {
81     assertEquals(new Double JavaDoc(1200), eval("$var1/e * $var2/e"));
82     assertEquals(new Double JavaDoc(1200), eval("30 * $var2/e"));
83   }
84   
85   public void testNodeSetUnion() {
86     Object JavaDoc result = eval("$var1/c/@name | $var1/d/@amount | $var2/e");
87     assertTrue(result instanceof List JavaDoc);
88     List JavaDoc nodeset = (List JavaDoc) result;
89     assertEquals(3, nodeset.size());
90     assertEquals("venus", ((Attr JavaDoc) nodeset.get(0)).getNodeValue());
91     assertEquals("20", ((Attr JavaDoc) nodeset.get(1)).getNodeValue());
92     assertEquals("40", NodeUtil.getValue((Element JavaDoc) nodeset.get(2)));
93   }
94   
95   public void testStringFunction() {
96     assertEquals("venusandmars", eval("concat($var1/c/@name, 'and', $var2/c/@name)"));
97   }
98   
99   public void testNumberFunction() {
100     assertEquals(new Double JavaDoc(70), eval("sum($var1/e | $var2/e)"));
101   }
102   
103   private Object JavaDoc eval(String JavaDoc exprText) {
104     Object JavaDoc value = null;
105     try {
106       Snippet expr = new Snippet();
107       expr.setText(exprText);
108       expr.setUse(Snippet.Use.EXPRESSION);
109       expr.parseScript();
110       value = expr.getScript().evaluate(token);
111     }
112     catch (BpelException e) {
113       fail(e.getMessage());
114     }
115     return value;
116   }
117 }
118
Popular Tags