1 package org.jbpm.bpel.data.exe; 2 3 import java.util.List ; 4 5 import javax.xml.namespace.QName ; 6 7 import junit.framework.TestCase; 8 9 import org.w3c.dom.Attr ; 10 import org.w3c.dom.Element ; 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 27 public class GeneralExpressionTest extends TestCase { 28 29 private Token token; 30 31 private static final String 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 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 name) { 48 super(name); 49 } 50 51 protected void setUp() throws Exception { 52 ProcessDefinition pd = new ProcessDefinition(); 54 pd.addDefinition(new ContextDefinition()); 55 ProcessInstance pi = new ProcessInstance(pd); 56 token = pi.getRootToken(); 57 Element elem1 = NodeUtil.parseElement(ELEM1_TEXT); 59 VariableDefinition var1 = new VariableDefinition(); 60 var1.setName("var1"); 61 var1.setTypeInfo(new ElementTypeInfo(new QName (elem1.getNamespaceURI(), elem1.getLocalName()))); 62 var1.createInstance(token).setValue(elem1); 63 Element elem2 = NodeUtil.parseElement(ELEM2_TEXT); 64 VariableDefinition var2 = new VariableDefinition(); 65 var2.setName("var2"); 66 var2.setTypeInfo(new ElementTypeInfo(new QName (elem2.getNamespaceURI(), elem2.getLocalName()))); 67 var2.createInstance(token).setValue(elem2); 68 } 69 70 public void testAdd() { 71 assertEquals(new Double (60), eval("$var1/d/@amount + $var2/e")); 72 assertEquals(new Double (60), eval("20 + $var2/e")); 73 } 74 75 public void testSubstract() { 76 assertEquals(new Double (20), eval("$var2/e - $var1/d/@amount")); 77 assertEquals(new Double (20), eval("$var2/e - 20")); 78 } 79 80 public void testMultiply() { 81 assertEquals(new Double (1200), eval("$var1/e * $var2/e")); 82 assertEquals(new Double (1200), eval("30 * $var2/e")); 83 } 84 85 public void testNodeSetUnion() { 86 Object result = eval("$var1/c/@name | $var1/d/@amount | $var2/e"); 87 assertTrue(result instanceof List ); 88 List nodeset = (List ) result; 89 assertEquals(3, nodeset.size()); 90 assertEquals("venus", ((Attr ) nodeset.get(0)).getNodeValue()); 91 assertEquals("20", ((Attr ) nodeset.get(1)).getNodeValue()); 92 assertEquals("40", NodeUtil.getValue((Element ) 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 (70), eval("sum($var1/e | $var2/e)")); 101 } 102 103 private Object eval(String exprText) { 104 Object 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 |