KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > tutorial > db > HelloWorldDbTest


1 package org.jbpm.tutorial.db;
2
3 import java.util.List JavaDoc;
4
5 import junit.framework.TestCase;
6
7 import org.jbpm.db.GraphSession;
8 import org.jbpm.db.JbpmSession;
9 import org.jbpm.db.JbpmSessionFactory;
10 import org.jbpm.graph.def.ProcessDefinition;
11 import org.jbpm.graph.exe.ProcessInstance;
12 import org.jbpm.graph.exe.Token;
13
14 public class HelloWorldDbTest extends TestCase {
15
16   // We need one JbpmSessionFactory per application. So we put it in
17
// a static variable. The JbpmSessionFactory will be used in the
18
// test methods to create JbpmSession's.
19
static JbpmSessionFactory jbpmSessionFactory =
20       JbpmSessionFactory.buildJbpmSessionFactory();
21   
22   static {
23     // Since the hypersonic in-memory database is a new, fresh database,
24
// we need to create the schema at runtime before we start the tests.
25
// The next line creates the database tables and foreign key
26
// constraints.
27
jbpmSessionFactory.getJbpmSchema().createSchema();
28   }
29
30   public void testSimplePersistence() {
31     // Between the 3 method calls below, all data is passed via the
32
// database. Here, in this unit test, these 3 methods are executed
33
// right after each other because we want to test a complete process
34
// scenario. But in reality, these methods represent different
35
// requests to a server.
36

37     // Since we start with a clean, empty in-memory database, we have to
38
// deploy the process first. In reality, this is done once by the
39
// process developer.
40
deployProcessDefinition();
41
42     // Suppose we want to start a process instance (=process execution)
43
// when a user submits a form in a web application...
44
processInstanceIsCreatedWhenUserSubmitsWebappForm();
45
46     // Then, the arrival of an asynchronous message will continue
47
// execution.
48
theProcessInstanceContinuesWhenAnAsyncMessageIsReceived();
49   }
50
51   public void deployProcessDefinition() {
52     // This test shows a process definition and one execution
53
// of the process definition. The process definition has
54
// 3 nodes: an unnamed start-state, a state 's' and an
55
// end-state named 'end'.
56
ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
57       "<process-definition name='hello world'>" +
58       " <start-state name='start'>" +
59       " <transition to='s' />" +
60       " </start-state>" +
61       " <state name='s'>" +
62       " <transition to='end' />" +
63       " </state>" +
64       " <end-state name='end' />" +
65       "</process-definition>"
66     );
67     
68     // Let's open a new persistence session
69
JbpmSession jbpmSession = jbpmSessionFactory.openJbpmSession();
70     // ... and begin a transaction on the persistence session
71
jbpmSession.beginTransaction();
72     
73     // Save the process definition in the database
74
jbpmSession
75         .getGraphSession()
76         .saveProcessDefinition(processDefinition);
77     
78     // Commit the transaction
79
jbpmSession.commitTransaction();
80     // And close the jbpmSession.
81
jbpmSession.close();
82   }
83
84   public void processInstanceIsCreatedWhenUserSubmitsWebappForm() {
85     // The code in this method could be inside a struts-action
86
// or a JSF managed bean.
87

88     // Let's open a new persistence session
89
JbpmSession jbpmSession = jbpmSessionFactory.openJbpmSession();
90     // ... and begin a transaction on the persistence session.
91
jbpmSession.beginTransaction();
92     
93     // Now we can query the database for the process definition that we
94
// deployed above.
95
ProcessDefinition processDefinition =
96         jbpmSession
97           .getGraphSession()
98           .findLatestProcessDefinition("hello world");
99     
100     // With the processDefinition that we retrieved from the database, we
101
// can create an execution of the process definition just like in the
102
// hello world example (which was without persistence).
103
ProcessInstance processInstance =
104         new ProcessInstance(processDefinition);
105     
106     Token token = processInstance.getRootToken();
107     assertEquals("start", token.getNode().getName());
108     // Let's start the process execution
109
token.signal();
110     // Now the process is in the state 's'.
111
assertEquals("s", token.getNode().getName());
112     
113     // Now the processInstance is saved in the database. So the
114
// current state of the execution of the process is stored in the
115
// database.
116
jbpmSession
117         .getGraphSession()
118         .saveProcessInstance(processInstance);
119     // The method below will get the process instance back out
120
// of the database and resume execution by providing another
121
// external signal.
122

123     // At the end of the webapp action, the transaction is committed.
124
jbpmSession.commitTransaction();
125     // And close the jbpmSession.
126
jbpmSession.close();
127   }
128
129   public void theProcessInstanceContinuesWhenAnAsyncMessageIsReceived() {
130     // The code in this method could be the content of a message driven bean.
131

132     // Let's open a new persistence session.
133
JbpmSession jbpmSession = jbpmSessionFactory.openJbpmSession();
134     // ... and begin a transaction on the persistence session
135
// note that it's also possible to use a jdbc connection from a
136
// DataSource in your application server.
137
jbpmSession.beginTransaction();
138     GraphSession graphSession = jbpmSession.getGraphSession();
139
140     // First, we need to get the process instance back out of the database.
141
// There are several options to know what process instance we are dealing
142
// with here. The easiest in this simple test case is just to look for
143
// the full list of process instances. That should give us only one
144
// result. So let's look up the process definition.
145
ProcessDefinition processDefinition =
146         graphSession.findLatestProcessDefinition("hello world");
147
148     // Now, we search for all process instances of this process definition.
149
List JavaDoc processInstances =
150         graphSession.findProcessInstances(processDefinition.getId());
151     
152     // Because we know that in the context of this unit test, there is
153
// only one execution. In real life, the processInstanceId can be
154
// extracted from the content of the message that arrived or from
155
// the user making a choice.
156
ProcessInstance processInstance =
157         (ProcessInstance) processInstances.get(0);
158     
159     // Now we can continue the execution. Note that the processInstance
160
// delegates signals to the main path of execution (=the root token).
161
processInstance.signal();
162
163     // After this signal, we know the process execution should have
164
// arrived in the end-state.
165
assertTrue(processInstance.hasEnded());
166     
167     // Now we can update the state of the execution in the database
168
graphSession.saveProcessInstance(processInstance);
169
170     // At the end of the MDB, the transaction is committed.
171
jbpmSession.commitTransaction();
172     // And the jbpmSession is closed.
173
jbpmSession.close();
174   }
175 }
176
Popular Tags