KickJava   Java API By Example, From Geeks To Geeks.

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


1 package org.jbpm.bpel.data.exe;
2
3 import javax.xml.namespace.QName JavaDoc;
4
5 import junit.framework.TestCase;
6
7 import org.w3c.dom.Element JavaDoc;
8
9 import org.jbpm.context.def.ContextDefinition;
10 import org.jbpm.graph.def.ProcessDefinition;
11 import org.jbpm.graph.exe.ProcessInstance;
12 import org.jbpm.graph.exe.Token;
13
14 import org.jbpm.bpel.data.def.ElementTypeInfo;
15 import org.jbpm.bpel.data.def.SchemaTypeInfo;
16 import org.jbpm.bpel.data.def.Snippet;
17 import org.jbpm.bpel.data.def.VariableDefinition;
18 import org.jbpm.bpel.xml.BpelConstants;
19 import org.jbpm.bpel.xml.BpelException;
20 import org.jbpm.bpel.xml.util.DatatypeUtil;
21 import org.jbpm.bpel.xml.util.NodeUtil;
22
23 /**
24  * @see "WS-BPEL 2.0 §9.1.1"
25  */

26 public class BooleanExpressionTest extends TestCase {
27
28   private Token token;
29   
30   private static final String JavaDoc ELEM1_TEXT =
31     "<a>" +
32     " <b on=\"true\">true</b>" +
33     " <c name=\"venus\"/>" +
34     " <d amount=\"20\"/>" +
35     " <e>30</e>" +
36     "</a>";
37   private static final String JavaDoc ELEM2_TEXT =
38     "<a>" +
39     " <b on=\"false\">false</b>" +
40     " <c name=\"mars\"/>" +
41     " <d amount=\"30\"/>" +
42     " <e>40</e>" +
43     "</a>";
44   
45   public void setUp() throws Exception JavaDoc {
46     // process and context
47
ProcessDefinition pd = new ProcessDefinition();
48     pd.addDefinition(new ContextDefinition());
49     ProcessInstance pi = new ProcessInstance(pd);
50     token = pi.getRootToken();
51     // element variables
52
Element JavaDoc elem1 = NodeUtil.parseElement(ELEM1_TEXT);
53     VariableDefinition var1 = new VariableDefinition();
54     var1.setName("var1");
55     var1.setTypeInfo(new ElementTypeInfo(new QName JavaDoc(elem1.getNamespaceURI(), elem1.getLocalName())));
56     var1.createInstance(token).setValue(elem1);
57     Element JavaDoc elem2 = NodeUtil.parseElement(ELEM2_TEXT);
58     VariableDefinition var2 = new VariableDefinition();
59     var2.setName("var2");
60     var2.setTypeInfo(new ElementTypeInfo(new QName JavaDoc(elem2.getNamespaceURI(), elem2.getLocalName())));
61     var2.createInstance(token).setValue(elem2);
62     // simple variable
63
VariableDefinition simple = new VariableDefinition();
64     simple.setName("simple");
65     simple.setTypeInfo(new SchemaTypeInfo(new QName JavaDoc(BpelConstants.NS_XML_SCHEMA, "boolean")));
66     simple.createInstance(token).setValue(Boolean.TRUE);
67   }
68   
69   public void testSimpleExtraction() {
70     assertTrue(eval("$simple"));
71   }
72   
73   public void testAttributeExtraction() {
74     assertTrue(eval("$var1/b/@on"));
75     assertFalse(eval("$var2/b/@on"));
76   }
77
78   public void testContentExtraction() {
79     assertTrue(eval("$var1/b"));
80     assertFalse(eval("$var2/b"));
81   }
82
83   public void testStringEqual() {
84     assertTrue(eval("$var1/b/@on = $var1/b"));
85   }
86   
87   public void testStringEqualConst() {
88     assertTrue(eval("$var1/b/@on = 'true'"));
89   }
90
91   public void testStringNotEqual() {
92     assertTrue(eval(
93         "$var1/c/@name != $var2/c/@name"));
94   }
95   
96   public void testStringNotEqualConst() {
97     assertTrue(eval(
98         "$var1/c/@name != 'mars'"));
99   }
100
101   public void testNumberEqual() {
102     // number() makes the comparison numeric, not textual
103
assertTrue(eval(
104         "number($var1/e) = $var2/d/@amount"));
105   }
106   
107   public void testNumberEqualConst() {
108     // the number literal makes the comparison numeric
109
assertTrue(eval("$var1/e = 30"));
110   }
111
112   public void testNumberNotEqual() {
113     assertTrue(eval(
114         "number($var1/d/@amount) != $var2/d/@amount"));
115   }
116   
117   public void testNumberNotEqualConst() {
118     assertTrue(eval("$var1/d/@amount != 30"));
119   }
120
121   public void testNumberLess() {
122     assertTrue(eval("$var1/e < $var2/e"));
123   }
124   
125   public void testNumberLessConst() {
126     assertTrue(eval("$var1/e < 50"));
127   }
128
129   public void testNumberLessOrEqual() {
130     assertTrue(eval("$var1/e <= $var2/d/@amount"));
131     assertFalse(eval("$var1/e < $var2/d/@amount"));
132   }
133   
134   public void testNumberLessOrEqualConst() {
135     assertTrue(eval("$var1/e <= 30"));
136     assertFalse(eval("$var1/e < 30"));
137   }
138
139   public void testNumberMore() {
140     assertTrue(eval("$var2/d/@amount > $var1/d/@amount"));
141   }
142   
143   public void testNumberMoreConst() {
144     assertTrue(eval("$var2/d/@amount > 20"));
145   }
146
147   public void testNumberMoreOrEqual() {
148     assertTrue(eval("$var2/d/@amount >= $var1/e"));
149     assertFalse(eval("$var2/d/@amount > $var1/e"));
150   }
151   
152   public void testNumberMoreOrEqualConst() {
153     assertTrue(eval("$var2/d/@amount >= 30"));
154     assertFalse(eval("$var2/d/@amount > 30"));
155   }
156   
157   public void testAnd() {
158     /* the odd-looking comparison with 'true' is there because it inspects the
159      * text content of a node, whereas the boolean() function, called when an
160      * implicit conversion to boolean is required, tests for the presence of a node */

161     assertTrue(eval("$var1/b = 'true' and $var1/b/@on = 'true'"));
162     assertFalse(eval("$var1/b = 'true' and $var2/b = 'true'"));
163   }
164   
165   public void testOr() {
166     assertTrue(eval("$var1/b = 'true' or $var2/b = 'true'"));
167     assertFalse(eval("$var2/b = 'true' or $var2/b/@on = 'true'"));
168   }
169   
170   private boolean eval(String JavaDoc exprText) {
171     boolean value = false;
172     try {
173       Snippet expr = new Snippet();
174       expr.setText(exprText);
175       expr.setUse(Snippet.Use.EXPRESSION);
176       expr.parseScript();
177       value = DatatypeUtil.toBoolean(expr.getScript().evaluate(token)).booleanValue();
178     }
179     catch (BpelException e) {
180       fail(e.getMessage());
181     }
182     return value;
183   }
184 }
185
Popular Tags