KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > websale > WebsaleTest


1 package org.jbpm.websale;
2
3 import java.util.Arrays JavaDoc;
4 import java.util.HashMap JavaDoc;
5 import java.util.HashSet JavaDoc;
6 import java.util.Iterator JavaDoc;
7 import java.util.List JavaDoc;
8 import java.util.Map JavaDoc;
9 import java.util.Set JavaDoc;
10
11 import org.jbpm.context.exe.ContextInstance;
12 import org.jbpm.db.AbstractDbTestCase;
13 import org.jbpm.graph.def.ProcessDefinition;
14 import org.jbpm.graph.exe.ProcessInstance;
15 import org.jbpm.identity.Entity;
16 import org.jbpm.identity.hibernate.IdentitySession;
17 import org.jbpm.identity.xml.IdentityXmlParser;
18 import org.jbpm.security.Authentication;
19 import org.jbpm.taskmgmt.exe.TaskFormParameter;
20 import org.jbpm.taskmgmt.exe.TaskInstance;
21 import org.jbpm.taskmgmt.exe.TaskMgmtInstance;
22
23 public class WebsaleTest extends AbstractDbTestCase {
24
25   ProcessDefinition processDefinition =
26     ProcessDefinition.parseXmlResource("websale.par/processdefinition.xml");
27   
28   ProcessInstance processInstance = null;
29   ContextInstance contextInstance = null;
30   TaskMgmtInstance taskMgmtInstance = null;
31
32   public void setUp() {
33     super.setUp();
34     
35     graphSession.saveProcessDefinition(processDefinition);
36     
37     processInstance = new ProcessInstance(processDefinition);
38     graphSession.saveProcessInstance(processInstance);
39
40     // load the identities
41
Entity[] entities = IdentityXmlParser.parseEntitiesResource("hsqldb/identity.db.xml");
42     IdentitySession identitySession = new IdentitySession(jbpmSession.getSession());
43     for (int i=0; i<entities.length; i++) {
44       identitySession.saveEntity(entities[i]);
45     }
46
47     newTransaction();
48     
49     processDefinition = graphSession.loadProcessDefinition(processDefinition.getId());
50     processInstance = graphSession.loadProcessInstance(processInstance.getId());
51     contextInstance = processInstance.getContextInstance();
52     taskMgmtInstance = processInstance.getTaskMgmtInstance();
53   }
54
55   public void testWebSaleOrderTaskParameters() {
56     TaskInstance taskInstance = processInstance.getTaskMgmtInstance().createStartTaskInstance();
57     assertEquals("create new web sale order", taskInstance.getName());
58
59     Set JavaDoc expectedParameters = new HashSet JavaDoc(Arrays.asList(new String JavaDoc[]{"item", "address", "quantity"}));
60
61     List JavaDoc taskFormParameters = taskInstance.getTaskFormParameters();
62     Iterator JavaDoc iter = taskFormParameters.iterator();
63     while (iter.hasNext()) {
64       TaskFormParameter tfp = (TaskFormParameter) iter.next();
65       String JavaDoc label = tfp.getLabel();
66       assertTrue("taskFormParameter '"+label+"' not in "+expectedParameters, (expectedParameters.contains(label)));
67       expectedParameters.remove(label);
68     }
69     
70     if (!expectedParameters.isEmpty()) {
71       fail("following task form parameters were not present: "+expectedParameters);
72     }
73   }
74
75   public void testPerformWebSaleOrderTask() {
76     TaskInstance taskInstance = null;
77     
78     // in an authenticated block,
79
Authentication.pushAuthenticatedActorId("cookie monster");
80     try {
81       // create a task to start the websale process
82
taskInstance = processInstance.getTaskMgmtInstance().createStartTaskInstance();
83     } finally {
84       Authentication.popAuthenticatedActorId();
85     }
86     
87     Map JavaDoc parameters = new HashMap JavaDoc();
88     parameters.put("item", "cookies");
89     parameters.put("quantity", "lots of them");
90     parameters.put("address", "sesamestreet 46");
91     
92     taskInstance.submitParameters(parameters);
93     
94     assertEquals("cookies", contextInstance.getVariable("item"));
95     assertEquals("lots of them", contextInstance.getVariable("quantity"));
96     assertEquals("sesamestreet 46", contextInstance.getVariable("address"));
97     assertEquals("cookie monster", taskMgmtInstance.getSwimlaneInstance("buyer").getActorId());
98   }
99
100   public void testEvaluateAssignment() {
101     TaskInstance taskInstance = processInstance.getTaskMgmtInstance().createStartTaskInstance();
102     taskInstance.end();
103     graphSession.saveProcessInstance(processInstance);
104     
105     newTransaction();
106     
107     List JavaDoc erniesTasks = taskMgmtSession.findTaskInstances("ernie");
108     assertEquals(1, erniesTasks.size());
109
110     TaskInstance evaluateTaskInstance = (TaskInstance) erniesTasks.get(0);
111     assertEquals("ernie", evaluateTaskInstance.getActorId());
112     assertEquals("evaluate web order", evaluateTaskInstance.getName());
113     assertNotNull(evaluateTaskInstance.getToken());
114     assertNotNull(evaluateTaskInstance.getCreate());
115     assertNull(evaluateTaskInstance.getStart());
116     assertNull(evaluateTaskInstance.getEnd());
117   }
118
119   public void testEvaluateOk() {
120     TaskInstance taskInstance = processInstance.getTaskMgmtInstance().createStartTaskInstance();
121     taskInstance.end();
122     graphSession.saveProcessInstance(processInstance);
123     
124     newTransaction();
125     
126     TaskInstance evaluateTaskInstance = (TaskInstance) taskMgmtSession.findTaskInstances("ernie").get(0);
127     evaluateTaskInstance.end("ok");
128     graphSession.saveProcessInstance(evaluateTaskInstance.getTaskMgmtInstance().getProcessInstance());
129
130     newTransaction();
131     
132     List JavaDoc erniesTasks = taskMgmtSession.findTaskInstances("bert");
133     assertEquals(1, erniesTasks.size());
134
135     TaskInstance waitForMoneyTaskInstance = (TaskInstance) erniesTasks.get(0);
136     assertEquals("bert", waitForMoneyTaskInstance.getActorId());
137     assertEquals("wait for money", waitForMoneyTaskInstance.getName());
138     assertNotNull(waitForMoneyTaskInstance.getToken());
139     assertNotNull(waitForMoneyTaskInstance.getCreate());
140     assertNull(waitForMoneyTaskInstance.getStart());
141     assertNull(waitForMoneyTaskInstance.getEnd());
142   }
143
144   public void testMoreInfoNeeded() {
145     TaskInstance taskInstance = null;
146     // in an authenticated block,
147
Authentication.pushAuthenticatedActorId("cookie monster");
148     try {
149       // create a task to start the websale process
150
taskInstance = processInstance.getTaskMgmtInstance().createStartTaskInstance();
151     } finally {
152       Authentication.popAuthenticatedActorId();
153     }
154     
155     taskInstance.end();
156     graphSession.saveProcessInstance(processInstance);
157     
158     newTransaction();
159     
160     TaskInstance evaluateTaskInstance = (TaskInstance) taskMgmtSession.findTaskInstances("ernie").get(0);
161     evaluateTaskInstance.end("more info needed");
162     graphSession.saveProcessInstance(evaluateTaskInstance.getTaskMgmtInstance().getProcessInstance());
163 /*
164     newTransaction();
165     
166     List cookieMonsterTasks = taskMgmtSession.findTaskInstances("cookie monster");
167     assertEquals(1, cookieMonsterTasks.size());
168
169     TaskInstance fixWebOrderDataTaskInstance = (TaskInstance) cookieMonsterTasks.get(0);
170     assertEquals("cookie monster", fixWebOrderDataTaskInstance.getActorId());
171     assertEquals("fix web order data", fixWebOrderDataTaskInstance.getName());
172     assertNotNull(fixWebOrderDataTaskInstance.getToken());
173     assertNotNull(fixWebOrderDataTaskInstance.getCreate());
174     assertNull(fixWebOrderDataTaskInstance.getStart());
175     assertNull(fixWebOrderDataTaskInstance.getEnd());
176 */

177   }
178 }
179
Popular Tags