KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > graph > action > ScriptTest


1 package org.jbpm.graph.action;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.HashSet JavaDoc;
5 import java.util.Map JavaDoc;
6 import java.util.Set JavaDoc;
7
8 import junit.framework.TestCase;
9
10 import org.jbpm.context.def.ContextDefinition;
11 import org.jbpm.context.def.VariableAccess;
12 import org.jbpm.context.exe.ContextInstance;
13 import org.jbpm.graph.def.ProcessDefinition;
14 import org.jbpm.graph.exe.ExecutionContext;
15 import org.jbpm.graph.exe.ProcessInstance;
16 import org.jbpm.graph.exe.Token;
17
18 public class ScriptTest extends TestCase {
19
20   public void testActionScript() {
21     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
22       "<process-definition>" +
23       " <start-state>" +
24       " <transition to='a' />" +
25       " </start-state>" +
26       " <state name='a'>" +
27       " <event type='node-enter'>" +
28       " <script>" +
29       " <variable name='a' access='write' />" +
30       " <expression>" +
31       " a = b + c;" +
32       " </expression>" +
33       " </script>" +
34       " </event>" +
35       " </state>" +
36       "</process-definition>"
37     );
38     processDefinition.addDefinition(new ContextDefinition());
39
40     ProcessInstance processInstance = new ProcessInstance(processDefinition);
41     ContextInstance contextInstance = (ContextInstance) processInstance.getInstance(ContextInstance.class);
42     contextInstance.setVariable("b", new Integer JavaDoc(3));
43     contextInstance.setVariable("c", new Integer JavaDoc(9));
44     processInstance.signal();
45
46     assertEquals(new Integer JavaDoc(12), contextInstance.getVariable("a"));
47   }
48
49   public void testNodeScript() {
50     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
51       "<process-definition>" +
52       " <start-state>" +
53       " <transition to='a' />" +
54       " </start-state>" +
55       " <node name='a'>" +
56       " <script>" +
57       " executionContext.leaveNode(\"c\");" +
58       " </script>" +
59       " <transition name='b' to='b' />" +
60       " <transition name='c' to='c' />" +
61       " </node>" +
62       " <state name='b' />" +
63       " <state name='c' />" +
64       "</process-definition>"
65     );
66     
67     ProcessInstance processInstance = new ProcessInstance(processDefinition);
68     processInstance.signal();
69     assertSame(processDefinition.getNode("c"), processInstance.getRootToken().getNode());
70   }
71
72   public void testScriptEvaluation() {
73     Map JavaDoc inputMap = new HashMap JavaDoc();
74     inputMap.put("a", new Integer JavaDoc(1));
75     inputMap.put("b", new Integer JavaDoc(1));
76     
77     Set JavaDoc outputNames = new HashSet JavaDoc();
78     outputNames.add("c");
79     outputNames.add("d");
80
81     Script script = new Script();
82     script.setExpression(
83       "c = a + b;" +
84       "d = a + b + c"
85     );
86     Map JavaDoc outputMap = script.eval(inputMap, outputNames);
87     
88     assertEquals(2, outputMap.size());
89     assertEquals(new Integer JavaDoc(2), outputMap.get("c"));
90     assertEquals(new Integer JavaDoc(4), outputMap.get("d"));
91   }
92   
93   public void testExecute() {
94     ProcessDefinition processDefinition = ProcessDefinition.createNewProcessDefinition();
95     ProcessInstance processInstance = new ProcessInstance(processDefinition);
96     Token token = processInstance.getRootToken();
97     ExecutionContext executionContext = new ExecutionContext(token);
98     ContextInstance contextInstance = executionContext.getContextInstance();
99     contextInstance.setVariable("a", new Integer JavaDoc(1));
100     contextInstance.setVariable("b", new Integer JavaDoc(1));
101     
102     Script script = new Script();
103     script.addVariableAccess(new VariableAccess("a", "read,write", null));
104                                                 // b is READ-ONLY !
105
script.addVariableAccess(new VariableAccess("b", "read", null));
106     script.addVariableAccess(new VariableAccess("c", "write", null));
107     script.setExpression(
108       "if (a!=1) throw new RuntimeException(\"a is not 1\");" +
109       "if (b!=1) throw new RuntimeException(\"b is not 1\");" +
110       "a = 2;" +
111       "b = 3;" +
112       "c = 4;"
113     );
114     script.execute(executionContext);
115     
116     assertEquals(new Integer JavaDoc(2), contextInstance.getVariable("a"));
117     // b was READ-ONLY !
118
assertEquals(new Integer JavaDoc(1), contextInstance.getVariable("b"));
119     assertEquals(new Integer JavaDoc(4), contextInstance.getVariable("c"));
120   }
121
122   
123   public void testCreateInputMapWithoutContext() {
124     ProcessDefinition processDefinition = ProcessDefinition.createNewProcessDefinition();
125     ProcessInstance processInstance = new ProcessInstance(processDefinition);
126     Token token = processInstance.getRootToken();
127     
128     Script script = new Script();
129     ExecutionContext executionContext = new ExecutionContext(token);
130     Map JavaDoc inputMap = script.createInputMap(executionContext);
131     assertSame( executionContext, inputMap.get("executionContext"));
132     assertSame( token, inputMap.get("token"));
133     assertNull( inputMap.get("node"));
134     assertNull( inputMap.get("task"));
135     assertNull( inputMap.get("taskInstance"));
136   }
137
138   public void testCreateInputMapWithoutVariableAccesses() {
139     ProcessDefinition processDefinition = ProcessDefinition.createNewProcessDefinition();
140     ProcessInstance processInstance = new ProcessInstance(processDefinition);
141     Token token = processInstance.getRootToken();
142     ContextInstance contextInstance = processInstance.getContextInstance();
143     contextInstance.setVariable("a", new Integer JavaDoc(1) );
144     contextInstance.setVariable("b", new Integer JavaDoc(1) );
145     contextInstance.setVariable("c", new Integer JavaDoc(1) );
146     
147     Script script = new Script();
148     Map JavaDoc inputMap = script.createInputMap(new ExecutionContext(token));
149     assertEquals(new Integer JavaDoc(1), inputMap.get("a"));
150     assertEquals(new Integer JavaDoc(1), inputMap.get("b"));
151     assertEquals(new Integer JavaDoc(1), inputMap.get("c"));
152   }
153
154   public void testCreateInputMapWithOnlyWriteVariableAccesses() {
155     ProcessDefinition processDefinition = ProcessDefinition.createNewProcessDefinition();
156     ProcessInstance processInstance = new ProcessInstance(processDefinition);
157     Token token = processInstance.getRootToken();
158     ContextInstance contextInstance = processInstance.getContextInstance();
159     contextInstance.setVariable("a", new Integer JavaDoc(1) );
160     contextInstance.setVariable("b", new Integer JavaDoc(1) );
161     contextInstance.setVariable("c", new Integer JavaDoc(1) );
162     
163     Script script = new Script();
164     script.addVariableAccess(new VariableAccess("a", "write", null));
165     script.addVariableAccess(new VariableAccess("b", "write-required", null));
166     Map JavaDoc inputMap = script.createInputMap(new ExecutionContext(token));
167     assertEquals(new Integer JavaDoc(1), inputMap.get("a"));
168     assertEquals(new Integer JavaDoc(1), inputMap.get("b"));
169     assertEquals(new Integer JavaDoc(1), inputMap.get("c"));
170   }
171
172   public void testCreateInputMapWithOnlyReadVariableAccesses() {
173     ProcessDefinition processDefinition = ProcessDefinition.createNewProcessDefinition();
174     ProcessInstance processInstance = new ProcessInstance(processDefinition);
175     Token token = processInstance.getRootToken();
176     ContextInstance contextInstance = processInstance.getContextInstance();
177     contextInstance.setVariable("a", new Integer JavaDoc(1) );
178     contextInstance.setVariable("b", new Integer JavaDoc(1) );
179     contextInstance.setVariable("c", new Integer JavaDoc(1) );
180     
181     Script script = new Script();
182     script.addVariableAccess(new VariableAccess("a", "read", null));
183     Map JavaDoc inputMap = script.createInputMap(new ExecutionContext(token));
184     assertEquals(new Integer JavaDoc(1), inputMap.get("a"));
185     assertFalse(inputMap.containsKey("b"));
186     assertFalse(inputMap.containsKey("c"));
187   }
188
189   public void testMappedNameTest() {
190     ProcessDefinition processDefinition = ProcessDefinition.createNewProcessDefinition();
191     ProcessInstance processInstance = new ProcessInstance(processDefinition);
192     Token token = processInstance.getRootToken();
193     ContextInstance contextInstance = processInstance.getContextInstance();
194     contextInstance.setVariable("a", new Integer JavaDoc(1) );
195     
196     Script script = new Script();
197     script.setExpression("AAA++;");
198     script.addVariableAccess(new VariableAccess("a", "read-write", "AAA"));
199     script.execute(new ExecutionContext(token));
200
201     assertEquals(new Integer JavaDoc(2), contextInstance.getVariable("a"));
202   }
203 }
204
Popular Tags