KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > example > AsyncProcessTest


1 package org.example;
2
3 import org.jbpm.db.JbpmSession;
4 import org.jbpm.db.GraphSession;
5 import org.jbpm.db.JbpmSessionFactory;
6 import org.jbpm.graph.def.ProcessDefinition;
7 import org.jbpm.graph.exe.ProcessInstance;
8 import org.jbpm.graph.exe.Token;
9
10 import junit.framework.TestCase;
11
12 /**
13  * @author Alejandro Guízar
14  * @version $Revision: 1.2 $ $Date: 2005/06/21 20:15:30 $
15  */

16 public class AsyncProcessTest extends TestCase {
17   
18   private static JbpmSessionFactory jbpmSessionFactory =
19     JbpmSessionFactory.buildJbpmSessionFactory();
20   
21   public AsyncProcessTest(String JavaDoc name) {
22     super(name);
23   }
24   
25   public void testAsyncScenario() throws Exception JavaDoc {
26     // Let's open a new persistence session
27
JbpmSession jbpmSession = jbpmSessionFactory.openJbpmSession();
28     // ... and begin a transaction on the persistence session.
29
jbpmSession.beginTransaction();
30     
31     // query the database for the process definition that we
32
// deployed above.
33
GraphSession graphSession = jbpmSession.getGraphSession();
34     ProcessDefinition processDefinition =
35         graphSession.findLatestProcessDefinition("asyncProcess");
36
37     ProcessInstance processInstance = new ProcessInstance(processDefinition);
38     //save the newly created process instance to the database
39
graphSession.saveProcessInstance(processInstance);
40     
41     //commit the changes, so concurrent tokens are able to lock on the process instance
42
jbpmSession.commitTransaction();
43     
44     //start a new transaction
45
jbpmSession.beginTransaction();
46     
47     //lock on the process instance
48
graphSession.lockProcessInstance(processInstance);
49     Token token = processInstance.getRootToken();
50     assertEquals("start", token.getNode().getName());
51     
52     //start the process execution
53
token.signal();
54     // Now the process is in node A
55
assertEquals("A", token.getNode().getName());
56     token.signal();
57     
58     //validate that 3 tokens were forked and that they are located at
59
//their corresponding states
60
assertEquals("fork", token.getNode().getName());
61     Token b = token.getChild("branch-b");
62     Token c = token.getChild("branch-c");
63     Token d = token.getChild("branch-d");
64     assertEquals("B", b.getNode().getName());
65     assertEquals("C", c.getNode().getName());
66     assertEquals("D", d.getNode().getName());
67     
68     // save process instance to the database. So the
69
// current state of the execution of the process is stored in the
70
// database.
71
graphSession.saveProcessInstance(processInstance);
72
73     //commit transaction
74
jbpmSession.commitTransaction();
75     // And close the jbpmSession.
76
jbpmSession.close();
77     
78     Thread.currentThread().sleep(10000);
79     
80     //validate that the instance is completed
81
assertInstanceCompleted(processInstance.getId());
82   }
83   
84   private void assertInstanceCompleted(long processInstanceId) {
85     // Let's open a new persistence session
86
JbpmSession jbpmSession = jbpmSessionFactory.openJbpmSession();
87     // ... and begin a transaction on the persistence session.
88
jbpmSession.beginTransaction();
89     GraphSession graphSession = jbpmSession.getGraphSession();
90     
91     // Now we can query the database to validate that the process instance is completed
92
ProcessInstance pi = graphSession.loadProcessInstance( processInstanceId );
93     graphSession.lockProcessInstance(pi);
94     assertNotNull( pi.getEnd() );
95     
96     jbpmSession.commitTransaction();
97     //and close the jbpmSession.
98
jbpmSession.close();
99   }
100 }
101
Popular Tags