KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > db > GraphSessionDbTest


1 package org.jbpm.db;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.List JavaDoc;
5
6 import org.jbpm.graph.def.ProcessDefinition;
7 import org.jbpm.graph.exe.ProcessInstance;
8 import org.jbpm.jpdl.par.ProcessArchiveDeployer;
9
10 public class GraphSessionDbTest extends AbstractDbTestCase {
11
12   public void testProcessDefinitionSaveAndLoad() {
13     // create a process definition
14
ProcessDefinition processDefinition = new ProcessDefinition("auction");
15     // save it in the database
16
graphSession.saveProcessDefinition( processDefinition );
17     // get the assigned id
18
long processDefinitionId = processDefinition.getId();
19     // start a new transaction
20
newTransaction();
21     // load the process definition by the id
22
processDefinition = graphSession.loadProcessDefinition( processDefinitionId );
23     // check the result
24
assertEquals("auction", processDefinition.getName());
25   }
26   
27   public void testFindProcessDefinitionByNameAndVersion() {
28     // put 3 process definitions in the database with the same name, but different versions
29
ProcessDefinition processDefinition = new ProcessDefinition("auction");
30     processDefinition.setVersion(1);
31     graphSession.saveProcessDefinition( processDefinition );
32     
33     processDefinition = new ProcessDefinition("auction");
34     processDefinition.setVersion(2);
35     graphSession.saveProcessDefinition( processDefinition );
36     // get the assigned id of the second verions
37
long secondVersionProcessDefinitionId = processDefinition.getId();
38
39     processDefinition = new ProcessDefinition("auction");
40     processDefinition.setVersion(3);
41     graphSession.saveProcessDefinition( processDefinition );
42     
43     // start a new transaction
44
newTransaction();
45     
46     // load the process definition by the id
47
processDefinition = graphSession.findProcessDefinition("auction", 2);
48     assertEquals(secondVersionProcessDefinitionId, processDefinition.getId());
49     assertEquals("auction", processDefinition.getName());
50     assertEquals(2, processDefinition.getVersion());
51   }
52
53   public void testFindLatestProcessDefinition() throws Exception JavaDoc {
54     // put 3 process definitions in the database with the same name, but different versions
55
ProcessDefinition processDefinition = new ProcessDefinition("auction");
56     processDefinition.setVersion(1);
57     graphSession.saveProcessDefinition( processDefinition );
58     
59     processDefinition = new ProcessDefinition("auction");
60     processDefinition.setVersion(2);
61     graphSession.saveProcessDefinition( processDefinition );
62
63     processDefinition = new ProcessDefinition("auction");
64     processDefinition.setVersion(3);
65     graphSession.saveProcessDefinition( processDefinition );
66     // get the assigned id of the last verions
67
long lastVersionProcessDefinitionId = processDefinition.getId();
68     
69     newTransaction();
70     
71     processDefinition = graphSession.findLatestProcessDefinition( "auction" );
72     assertEquals(lastVersionProcessDefinitionId, processDefinition.getId());
73     assertEquals("auction", processDefinition.getName());
74     assertEquals(3, processDefinition.getVersion());
75   }
76
77   public void testFindAllProcessDefinitions() throws Exception JavaDoc {
78     // put 3 process definitions in the database with the same name, but different versions
79
ProcessDefinition processDefinition = new ProcessDefinition("auction");
80     processDefinition.setVersion(1);
81     graphSession.saveProcessDefinition( processDefinition );
82     
83     processDefinition = new ProcessDefinition("auction");
84     processDefinition.setVersion(2);
85     graphSession.saveProcessDefinition( processDefinition );
86
87     processDefinition = new ProcessDefinition("auction");
88     processDefinition.setVersion(3);
89     graphSession.saveProcessDefinition( processDefinition );
90     
91     processDefinition = new ProcessDefinition("bake cake");
92     processDefinition.setVersion(1);
93     graphSession.saveProcessDefinition( processDefinition );
94     
95     processDefinition = new ProcessDefinition("bake cake");
96     processDefinition.setVersion(2);
97     graphSession.saveProcessDefinition( processDefinition );
98     
99     newTransaction();
100     
101     List JavaDoc allProcessDefinitions = graphSession.findAllProcessDefinitions();
102     assertEquals(5, allProcessDefinitions.size());
103     assertEquals(3, ((ProcessDefinition)allProcessDefinitions.get(0)).getVersion());
104     assertEquals("auction", ((ProcessDefinition)allProcessDefinitions.get(0)).getName());
105     assertEquals(2, ((ProcessDefinition)allProcessDefinitions.get(1)).getVersion());
106     assertEquals("auction", ((ProcessDefinition)allProcessDefinitions.get(1)).getName());
107     assertEquals(1, ((ProcessDefinition)allProcessDefinitions.get(2)).getVersion());
108     assertEquals("auction", ((ProcessDefinition)allProcessDefinitions.get(2)).getName());
109     assertEquals(2, ((ProcessDefinition)allProcessDefinitions.get(3)).getVersion());
110     assertEquals("bake cake", ((ProcessDefinition)allProcessDefinitions.get(3)).getName());
111     assertEquals(1, ((ProcessDefinition)allProcessDefinitions.get(4)).getVersion());
112     assertEquals("bake cake", ((ProcessDefinition)allProcessDefinitions.get(4)).getName());
113   }
114
115   public void testFindAllProcessDefinitionVersions() throws Exception JavaDoc {
116     // put 3 process definitions in the database with the same name, but different versions
117
ProcessDefinition processDefinition = new ProcessDefinition("auction");
118     processDefinition.setVersion(1);
119     graphSession.saveProcessDefinition( processDefinition );
120     
121     processDefinition = new ProcessDefinition("auction");
122     processDefinition.setVersion(2);
123     graphSession.saveProcessDefinition( processDefinition );
124
125     processDefinition = new ProcessDefinition("auction");
126     processDefinition.setVersion(3);
127     graphSession.saveProcessDefinition( processDefinition );
128     
129     processDefinition = new ProcessDefinition("bake cake");
130     processDefinition.setVersion(1);
131     graphSession.saveProcessDefinition( processDefinition );
132     
133     processDefinition = new ProcessDefinition("bake cake");
134     processDefinition.setVersion(2);
135     graphSession.saveProcessDefinition( processDefinition );
136     
137     newTransaction();
138     
139     List JavaDoc allProcessDefinitionVersions = graphSession.findAllProcessDefinitionVersions("auction");
140     assertEquals(3, allProcessDefinitionVersions.size());
141     assertEquals(3, ((ProcessDefinition)allProcessDefinitionVersions.get(0)).getVersion());
142     assertEquals("auction", ((ProcessDefinition)allProcessDefinitionVersions.get(0)).getName());
143     assertEquals(2, ((ProcessDefinition)allProcessDefinitionVersions.get(1)).getVersion());
144     assertEquals("auction", ((ProcessDefinition)allProcessDefinitionVersions.get(1)).getName());
145     assertEquals(1, ((ProcessDefinition)allProcessDefinitionVersions.get(2)).getVersion());
146     assertEquals("auction", ((ProcessDefinition)allProcessDefinitionVersions.get(2)).getName());
147
148     allProcessDefinitionVersions = graphSession.findAllProcessDefinitionVersions("bake cake");
149     assertEquals(2, allProcessDefinitionVersions.size());
150     assertEquals(2, ((ProcessDefinition)allProcessDefinitionVersions.get(0)).getVersion());
151     assertEquals("bake cake", ((ProcessDefinition)allProcessDefinitionVersions.get(0)).getName());
152     assertEquals(1, ((ProcessDefinition)allProcessDefinitionVersions.get(1)).getVersion());
153     assertEquals("bake cake", ((ProcessDefinition)allProcessDefinitionVersions.get(1)).getName());
154   }
155   
156   public void testSaveAndLoadProcessInstance() {
157     ProcessInstance processInstance = new ProcessInstance();
158     graphSession.saveProcessInstance(processInstance);
159     newTransaction();
160     assertNotNull(graphSession.loadProcessInstance(processInstance.getId()));
161   }
162   
163   public void testUpdateProcessInstance() {
164     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
165       "<process-definition>" +
166       " <start-state name='s' />" +
167       " <node name='n' />" +
168       "</process-definition>");
169
170     processDefinition = saveAndReload(processDefinition);
171
172     ProcessInstance processInstance = new ProcessInstance(processDefinition);
173
174     processInstance = saveAndReload(processInstance);
175     long pid = processInstance.getId();
176     
177     assertEquals("s", processInstance.getRootToken().getNode().getName());
178     processInstance.getRootToken().setNode(processInstance.getProcessDefinition().getNode("n"));
179     
180     processInstance = saveAndReload(processInstance);
181     assertEquals("n", processInstance.getRootToken().getNode().getName());
182     assertEquals(pid, processInstance.getId());
183   }
184   
185   public void testFindProcessInstancesByProcessDefinition() {
186     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
187       "<process-definition>" +
188       " <start-state name='s' />" +
189       " <node name='n' />" +
190       "</process-definition>");
191
192     graphSession.saveProcessDefinition(processDefinition);
193
194     graphSession.saveProcessInstance(new ProcessInstance(processDefinition));
195     graphSession.saveProcessInstance(new ProcessInstance(processDefinition));
196     graphSession.saveProcessInstance(new ProcessInstance(processDefinition));
197     
198     newTransaction();
199     
200     List JavaDoc processInstances = graphSession.findProcessInstances(processDefinition.getId());
201     assertEquals(3, processInstances.size());
202     
203     // process instances should be ordered from recent to old
204
long previousStart = System.currentTimeMillis();
205     Iterator JavaDoc iter = processInstances.iterator();
206     while (iter.hasNext()) {
207       ProcessInstance processInstance = (ProcessInstance) iter.next();
208       long processStart = processInstance.getStart().getTime();
209       assertTrue(previousStart >= processStart);
210       previousStart = processStart;
211     }
212   }
213   
214   public void testDeleteProcessInstance() {
215     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
216       "<process-definition>" +
217       " <start-state name='s' />" +
218       " <node name='n' />" +
219       "</process-definition>");
220     graphSession.saveProcessDefinition(processDefinition);
221     ProcessInstance processInstance = new ProcessInstance(processDefinition);
222     graphSession.saveProcessInstance(processInstance);
223     
224     newTransaction();
225     
226     graphSession.deleteProcessInstance(processInstance.getId());
227     
228     newTransaction();
229
230     assertEquals(0, graphSession.findProcessInstances(processDefinition.getId()).size());
231   }
232
233   public void testDeleteProcessDefinition() {
234     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
235       "<process-definition>" +
236       " <start-state name='s' />" +
237       " <node name='n' />" +
238       "</process-definition>");
239     graphSession.saveProcessDefinition(processDefinition);
240     graphSession.saveProcessInstance(new ProcessInstance(processDefinition));
241     graphSession.saveProcessInstance(new ProcessInstance(processDefinition));
242     graphSession.saveProcessInstance(new ProcessInstance(processDefinition));
243     graphSession.saveProcessInstance(new ProcessInstance(processDefinition));
244     
245     newTransaction();
246     
247     graphSession.deleteProcessDefinition(processDefinition.getId());
248     
249     newTransaction();
250
251     assertEquals(0, graphSession.findAllProcessDefinitions().size());
252     assertEquals(0, graphSession.findProcessInstances(processDefinition.getId()).size());
253   }
254
255   public void testLatestProcessDefinitions() {
256     commitAndCloseSession();
257     
258     ProcessDefinition websale = new ProcessDefinition("websale");
259     ProcessArchiveDeployer.deployProcessDefinition(websale, getJbpmSessionFactory());
260     ProcessArchiveDeployer.deployProcessDefinition(websale, getJbpmSessionFactory());
261     ProcessArchiveDeployer.deployProcessDefinition(websale, getJbpmSessionFactory());
262
263     ProcessDefinition changeNappy = new ProcessDefinition("change nappy");
264     ProcessArchiveDeployer.deployProcessDefinition(changeNappy, getJbpmSessionFactory());
265     ProcessArchiveDeployer.deployProcessDefinition(changeNappy, getJbpmSessionFactory());
266
267     beginSessionTransaction();
268     
269     List JavaDoc latestProcessDefinitions = graphSession.findLatestProcessDefinitions();
270     assertEquals(2, latestProcessDefinitions.size());
271     assertEquals(3, getVersionOfProcess("websale", latestProcessDefinitions));
272     assertEquals(2, getVersionOfProcess("change nappy", latestProcessDefinitions));
273   }
274
275   private int getVersionOfProcess(String JavaDoc name, List JavaDoc latestProcessDefinitions) {
276     Iterator JavaDoc iter = latestProcessDefinitions.iterator();
277     while (iter.hasNext()) {
278       ProcessDefinition processDefinition = (ProcessDefinition) iter.next();
279       if (name.equals(processDefinition.getName())) {
280         return processDefinition.getVersion();
281       }
282     }
283     return -2;
284   }
285 }
286
Popular Tags