KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > graph > node > JpdlDbTest


1 package org.jbpm.graph.node;
2
3 import java.util.HashMap JavaDoc;
4 import java.util.Iterator JavaDoc;
5 import java.util.Map JavaDoc;
6
7 import org.jbpm.db.AbstractDbTestCase;
8 import org.jbpm.graph.action.Script;
9 import org.jbpm.graph.def.ProcessDefinition;
10 import org.jbpm.graph.exe.ExecutionContext;
11 import org.jbpm.graph.node.Decision;
12 import org.jbpm.graph.node.DecisionCondition;
13 import org.jbpm.graph.node.DecisionHandler;
14 import org.jbpm.graph.node.Fork;
15 import org.jbpm.graph.node.Join;
16 import org.jbpm.graph.node.State;
17 import org.jbpm.graph.node.TaskNode;
18 import org.jbpm.instantiation.Delegation;
19 import org.jbpm.taskmgmt.def.Task;
20
21 public class JpdlDbTest extends AbstractDbTestCase {
22   
23   public void testDecision(){
24     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
25       "<process-definition>" +
26       " <decision name='d'>" +
27       " <transition name='one' to='d'>" +
28       " <condition>a == 1</condition>" +
29       " </transition>" +
30       " <transition name='two' to='d'>" +
31       " <condition>a == 2</condition>" +
32       " </transition>" +
33       " <transition name='three' to='d'>" +
34       " <condition>a == 3</condition>" +
35       " </transition>" +
36       " </decision>" +
37       "</process-definition>");
38     
39     processDefinition = saveAndReload(processDefinition);
40     
41     Decision decision = (Decision) processDefinition.getNode("d");
42     DecisionCondition decisionCondition = (DecisionCondition) decision.decisionConditions.get(0);
43     assertEquals("one", decisionCondition.transitionName);
44     assertEquals("a == 1", decisionCondition.expression);
45     
46     decisionCondition = (DecisionCondition) decision.decisionConditions.get(1);
47     assertEquals("two", decisionCondition.transitionName);
48     assertEquals("a == 2", decisionCondition.expression);
49     
50     decisionCondition = (DecisionCondition) decision.decisionConditions.get(2);
51     assertEquals("three", decisionCondition.transitionName);
52     assertEquals("a == 3", decisionCondition.expression);
53   }
54   
55   public static class MyDecisionHandler implements DecisionHandler {
56     private static final long serialVersionUID = 1L;
57     String JavaDoc decisionHandlerConfigText = null;
58     public String JavaDoc decide(ExecutionContext executionContext) throws Exception JavaDoc {
59       return "two";
60     }
61   }
62
63   public void testDecisionDelegation(){
64     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
65       "<process-definition>" +
66       " <decision name='d' >" +
67       " <handler class='org.jbpm.graph.node.JpdlDbTest$MyDecisionHandler' >" +
68       " <decisionHandlerConfigText>testing... one, two... testing</decisionHandlerConfigText>" +
69       " </handler>" +
70       " <transition name='one' to='d'/>" +
71       " <transition name='two' to='d'/>" +
72       " <transition name='three' to='d'/>" +
73       " </decision>" +
74       "</process-definition>");
75     
76     processDefinition = saveAndReload(processDefinition);
77     
78     Decision decision = (Decision) processDefinition.getNode("d");
79     Delegation decisionDelegation = decision.decisionDelegation;
80     assertNotNull(decisionDelegation);
81     assertEquals("org.jbpm.graph.node.JpdlDbTest$MyDecisionHandler", decisionDelegation.getClassName());
82     MyDecisionHandler decisionHandler = (MyDecisionHandler) decisionDelegation.instantiate();
83     assertEquals("testing... one, two... testing", decisionHandler.decisionHandlerConfigText);
84   }
85
86   public void testFork() {
87     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
88       "<process-definition>" +
89       " <fork name='f' />" +
90       "</process-definition>");
91     
92     processDefinition = saveAndReload(processDefinition);
93     
94     Fork fork = (Fork) processDefinition.getNode("f");
95     assertNotNull(fork);
96   }
97   
98   public void testJoin() {
99     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
100       "<process-definition>" +
101       " <join name='j' />" +
102       "</process-definition>");
103     
104     processDefinition = saveAndReload(processDefinition);
105     
106     Join join = (Join) processDefinition.getNode("j");
107     assertNotNull(join);
108   }
109   
110   public void testScript() {
111     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
112       "<process-definition>" +
113       " <script name='s'>" +
114       " <variable name='a' access='read' />" +
115       " <variable name='b' access='read-write' />" +
116       " <variable name='c' access='read-write' />" +
117       " <variable name='d' access='read-write-required' />" +
118       " <expression>e = m * Math.pow(c,2);</expression>" +
119       " </script>" +
120       "</process-definition>");
121     
122     processDefinition = saveAndReload(processDefinition);
123     
124     Script script = (Script) processDefinition.getAction("s");
125     assertNotNull(script);
126     assertEquals(4, script.getVariableAccesses().size() );
127     assertEquals("e = m * Math.pow(c,2);", script.getExpression() );
128   }
129   
130   public void testState() {
131     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
132       "<process-definition>" +
133       " <state name='s' />" +
134       "</process-definition>");
135     
136     processDefinition = saveAndReload(processDefinition);
137     
138     State state = (State) processDefinition.getNode("s");
139     assertNotNull(state);
140   }
141   
142   public void testTaskNode() {
143     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
144       "<process-definition>" +
145       " <task-node name='t' signal='first-wait' create-tasks='false'>" +
146       " <task name='change the world once' blocking='true'>" +
147       " <assignment class='anyonebutme' />" +
148       " </task>" +
149       " <task name='change the world twice' />" +
150       " <task name='change the world three times' />" +
151       " </task-node>" +
152       "</process-definition>"
153     );
154     
155     processDefinition = saveAndReload(processDefinition);
156     
157     TaskNode taskNode = (TaskNode) processDefinition.getNode("t");
158     assertNotNull(taskNode);
159     assertEquals("t", taskNode.getName());
160     assertEquals(TaskNode.SIGNAL_FIRST_WAIT, taskNode.getSignal());
161     assertFalse(taskNode.getCreateTasks());
162     assertEquals(3, taskNode.getTasks().size());
163     
164     Map JavaDoc tasks = new HashMap JavaDoc();
165     Iterator JavaDoc iter = taskNode.getTasks().iterator();
166     while (iter.hasNext()) {
167       Task task = (Task) iter.next();
168       tasks.put(task.getName(), task);
169     }
170     Task task = (Task) tasks.get("change the world once");
171     assertNotNull(task);
172     assertSame(taskNode, task.getTaskNode());
173     assertTrue(task.isBlocking());
174     assertEquals("anyonebutme", task.getAssignmentDelegation().getClassName());
175     
176     task = (Task) tasks.get("change the world twice");
177     assertNotNull(task);
178     assertSame(taskNode, task.getTaskNode());
179     assertFalse(task.isBlocking());
180
181     assertTrue(tasks.containsKey("change the world three times") );
182   }
183 }
184
Popular Tags