KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > tutorial > action > ActionTest


1 package org.jbpm.tutorial.action;
2
3 import junit.framework.TestCase;
4
5 import org.jbpm.graph.def.ProcessDefinition;
6 import org.jbpm.graph.exe.ProcessInstance;
7
8 public class ActionTest extends TestCase {
9
10   // Each test will start with setting the static isExecuted
11
// member of MyActionHandler to false.
12
public void setUp() {
13     MyActionHandler.isExecuted = false;
14   }
15
16   public void testTransitionAction() {
17     // The next process is a variant of the hello world process.
18
// We have added an action on the transition from state s
19
// to the end-state. The purpose of this test is to show
20
// how easy it is to integrate java code in a jBPM process.
21
ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
22       "<process-definition>" +
23       " <start-state>" +
24       " <transition to='s' />" +
25       " </start-state>" +
26       " <state name='s'>" +
27       " <transition to='end'>" +
28       " <action class='org.jbpm.tutorial.action.MyActionHandler' />" +
29       " </transition>" +
30       " </state>" +
31       " <end-state name='end' />" +
32       "</process-definition>"
33     );
34     
35     // Let's start a new execution for the process definition.
36
ProcessInstance processInstance =
37       new ProcessInstance(processDefinition);
38     
39     // The next signal will cause the execution to leave the start
40
// state and enter the state 's'
41
processInstance.signal();
42
43     // Here we show that MyActionHandler was not yet executed.
44
assertFalse(MyActionHandler.isExecuted);
45     // ... and that the the main path of execution is positioned in
46
// the state 's'
47
assertSame(processDefinition.getNode("s"),
48                processInstance.getRootToken().getNode());
49     
50     // The next signal will trigger the execution of the root
51
// token. The token will take the transition with the
52
// action and the action will be executed during the
53
// call to the signal method.
54
processInstance.signal();
55     
56     // Here we can see that MyActionHandler was executed during
57
// the call to the signal method.
58
assertTrue(MyActionHandler.isExecuted);
59   }
60
61   public void testNodeActions() {
62     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
63       "<process-definition>" +
64       " <start-state>" +
65       " <transition to='s' />" +
66       " </start-state>" +
67       " <state name='s'>" +
68       " <event type='node-enter'>" +
69       " <action class='org.jbpm.tutorial.action.MyActionHandler' />" +
70       " </event>" +
71       " <event type='node-leave'>" +
72       " <action class='org.jbpm.tutorial.action.MyActionHandler' />" +
73       " </event>" +
74       " <transition to='end'/>" +
75       " </state>" +
76       " <end-state name='end' />" +
77       "</process-definition>"
78     );
79     
80     ProcessInstance processInstance =
81       new ProcessInstance(processDefinition);
82     
83     assertFalse(MyActionHandler.isExecuted);
84     // The next signal will cause the execution to leave the start
85
// state and enter the state 's'. So the state 's' is entered
86
// and hence the action is executed.
87
processInstance.signal();
88     assertTrue(MyActionHandler.isExecuted);
89     
90     // Let's reset the MyActionHandler.isExecuted
91
MyActionHandler.isExecuted = false;
92   
93     // The next signal will trigger execution to leave the
94
// state 's'. So the action will be executed again.
95
processInstance.signal();
96     // Voila.
97
assertTrue(MyActionHandler.isExecuted);
98   }
99 }
100
Popular Tags