KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > tutorial > helloworld > HelloWorldTest


1 package org.jbpm.tutorial.helloworld;
2
3 import junit.framework.TestCase;
4
5 import org.jbpm.graph.def.ProcessDefinition;
6 import org.jbpm.graph.exe.ProcessInstance;
7 import org.jbpm.graph.exe.Token;
8
9 public class HelloWorldTest extends TestCase {
10   
11   public void testHelloWorldProcess() {
12     // This method shows a process definition and one execution
13
// of the process definition. The process definition has
14
// 3 nodes: an unnamed start-state, a state 's' and an
15
// end-state named 'end'.
16
// The next line parses a piece of xml text into a
17
// ProcessDefinition. A ProcessDefinition is the formal
18
// description of a process represented as a java object.
19
ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
20       "<process-definition>" +
21       " <start-state>" +
22       " <transition to='s' />" +
23       " </start-state>" +
24       " <state name='s'>" +
25       " <transition to='end' />" +
26       " </state>" +
27       " <end-state name='end' />" +
28       "</process-definition>"
29     );
30     
31     // The next line creates one execution of the process definition.
32
// After construction, the process execution has one main path
33
// of execution (=the root token) that is positioned in the
34
// start-state.
35
ProcessInstance processInstance =
36         new ProcessInstance(processDefinition);
37     
38     // After construction, the process execution has one main path
39
// of execution (=the root token).
40
Token token = processInstance.getRootToken();
41     
42     // Also after construction, the main path of execution is positioned
43
// in the start-state of the process definition.
44
assertSame(processDefinition.getStartState(), token.getNode());
45     
46     // Let's start the process execution, leaving the start-state
47
// over its default transition.
48
token.signal();
49     // The signal method will block until the process execution
50
// enters a wait state.
51

52     // The process execution will have entered the first wait state
53
// in state 's'. So the main path of execution is not
54
// positioned in state 's'
55
assertSame(processDefinition.getNode("s"), token.getNode());
56
57     // Let's send another signal. This will resume execution by
58
// leaving the state 's' over its default transition.
59
token.signal();
60     // Now the signal method returned because the process instance
61
// has arrived in the end-state.
62

63     assertSame(processDefinition.getNode("end"), token.getNode());
64   }
65
66   
67   public void testThreeStateProcess() {
68     // This test shows a process similar to the simplest process
69
// above, but now there are 3 states instead of one, which will
70
// result in 2 extra signals to be given by the test.
71
ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
72       "<process-definition>" +
73       " <start-state>" +
74       " <transition to='phase one' />" +
75       " </start-state>" +
76       " <state name='phase one'>" +
77       " <transition to='phase two' />" +
78       " </state>" +
79       " <state name='phase two'>" +
80       " <transition to='phase three' />" +
81       " </state>" +
82       " <state name='phase three'>" +
83       " <transition to='end' />" +
84       " </state>" +
85       " <end-state name='end' />" +
86       "</process-definition>"
87     );
88     
89     ProcessInstance processInstance =
90         new ProcessInstance(processDefinition);
91     Token token = processInstance.getRootToken();
92     assertSame(processDefinition.getStartState(), token.getNode());
93
94     token.signal();
95     assertSame(processDefinition.getNode("phase one"), token.getNode());
96
97     token.signal();
98     assertSame(processDefinition.getNode("phase two"), token.getNode());
99
100     token.signal();
101     assertSame(processDefinition.getNode("phase three"), token.getNode());
102
103     token.signal();
104     assertSame(processDefinition.getNode("end"), token.getNode());
105   }
106 }
107
Popular Tags