KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > taskmgmt > exe > TaskTimerExecutionDbTest


1 package org.jbpm.taskmgmt.exe;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.List JavaDoc;
5
6 import org.jbpm.db.AbstractDbTestCase;
7 import org.jbpm.graph.def.ActionHandler;
8 import org.jbpm.graph.def.ProcessDefinition;
9 import org.jbpm.graph.exe.ExecutionContext;
10 import org.jbpm.graph.exe.ProcessInstance;
11 import org.jbpm.scheduler.exe.Timer;
12 import org.jbpm.scheduler.impl.SchedulerThread;
13
14 public class TaskTimerExecutionDbTest extends AbstractDbTestCase {
15
16   static int counter = 0;
17   
18   public static class PlusPlus implements ActionHandler {
19     private static final long serialVersionUID = 1L;
20     public void execute(ExecutionContext executionContext) throws Exception JavaDoc {
21       counter++;
22     }
23   }
24   
25   public void setUp() {
26     super.setUp();
27     counter = 0;
28   }
29
30   public void testTimerCreation() {
31     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
32       "<process-definition>" +
33       " <start-state>" +
34       " <transition to='a' />" +
35       " </start-state>" +
36       " <task-node name='a'>" +
37       " <task name='clean ceiling'>" +
38       " <timer name='ceiling-timer' duedate='0 seconds'>" +
39       " <action class='org.jbpm.taskmgmt.exe.TaskTimerExecutionDbTest$PlusPlus' />" +
40       " </timer>" +
41       " </task>" +
42       " </task-node>" +
43       "</process-definition>"
44     );
45
46     processDefinition = saveAndReload(processDefinition);
47     
48     ProcessInstance processInstance = new ProcessInstance(processDefinition);
49     processInstance.signal();
50     
51     processInstance = saveAndReload(processInstance);
52
53     Iterator JavaDoc timersIter = jbpmSession.getSchedulerSession().findTimersByDueDate();
54     assertTrue(timersIter.hasNext());
55     Timer timer = (Timer) timersIter.next();
56     assertEquals("ceiling-timer", timer.getName());
57   }
58
59   public void testTimerDeletion() {
60     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
61       "<process-definition>" +
62       " <start-state>" +
63       " <transition to='a' />" +
64       " </start-state>" +
65       " <task-node name='a'>" +
66       " <task name='clean ceiling'>" +
67       " <timer name='ceiling-timer' duedate='0 seconds'>" +
68       " <action class='org.jbpm.taskmgmt.exe.TaskTimerExecutionDbTest$PlusPlus' />" +
69       " </timer>" +
70       " </task>" +
71       " <transition to='b' />" +
72       " </task-node>" +
73       " <state name='b' />" +
74       "</process-definition>"
75     );
76
77     processDefinition = saveAndReload(processDefinition);
78     
79     ProcessInstance processInstance = new ProcessInstance(processDefinition);
80     processInstance.signal();
81     
82     jbpmSession.getGraphSession().saveProcessInstance(processInstance);
83     newTransaction();
84     
85     List JavaDoc taskInstances = jbpmSession.getTaskMgmtSession().findTaskInstancesByToken(processInstance.getRootToken().getId());
86     assertEquals(1, taskInstances.size() );
87     
88     TaskInstance taskInstance = (TaskInstance) taskInstances.get(0);
89     taskInstance.end();
90     
91     jbpmSession.getGraphSession().saveProcessInstance(taskInstance.getTaskMgmtInstance().getProcessInstance());
92     newTransaction();
93     
94     Iterator JavaDoc timersIter = jbpmSession.getSchedulerSession().findTimersByDueDate();
95     assertFalse(timersIter.hasNext());
96   }
97
98   public void testTimerExecution() {
99     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
100       "<process-definition>" +
101       " <start-state>" +
102       " <transition to='a' />" +
103       " </start-state>" +
104       " <task-node name='a'>" +
105       " <task name='clean ceiling'>" +
106       " <timer name='ceiling-timer' duedate='0 seconds'>" +
107       " <action class='org.jbpm.taskmgmt.exe.TaskTimerExecutionDbTest$PlusPlus' />" +
108       " </timer>" +
109       " </task>" +
110       " </task-node>" +
111       "</process-definition>"
112     );
113
114     processDefinition = saveAndReload(processDefinition);
115     
116     ProcessInstance processInstance = new ProcessInstance(processDefinition);
117     processInstance.signal();
118     
119     jbpmSession.getGraphSession().saveProcessInstance(processInstance);
120     commitAndCloseSession();
121
122     // the timer executor creates its own JbpmSession.
123
assertEquals(0, counter);
124     new SchedulerThread().executeTimers();
125     assertEquals(1, counter);
126
127     // because the tearDown wants to close it :-)
128
beginSessionTransaction();
129   }
130
131   public void testTimerExecutionRepeat() {
132     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
133       "<process-definition>" +
134       " <start-state>" +
135       " <transition to='a' />" +
136       " </start-state>" +
137       " <task-node name='a'>" +
138       " <task name='clean ceiling'>" +
139       " <timer name='ceiling-timer' duedate='0 seconds' repeat='1 second'>" +
140       " <action class='org.jbpm.taskmgmt.exe.TaskTimerExecutionDbTest$PlusPlus' />" +
141       " </timer>" +
142       " </task>" +
143       " </task-node>" +
144       "</process-definition>"
145     );
146
147     processDefinition = saveAndReload(processDefinition);
148     
149     ProcessInstance processInstance = new ProcessInstance(processDefinition);
150     processInstance.signal();
151     
152     jbpmSession.getGraphSession().saveProcessInstance(processInstance);
153     newTransaction();
154     
155     // fetch the original duedate
156
Iterator JavaDoc timersIter = jbpmSession.getSchedulerSession().findTimersByDueDate();
157     assertTrue(timersIter.hasNext());
158     Timer timer = (Timer) timersIter.next();
159     long originalDueDate = timer.getDueDate().getTime();
160     
161     commitAndCloseSession();
162
163     // the timer executor creates its own JbpmSession.
164
assertEquals(0, counter);
165     new SchedulerThread().executeTimers();
166     assertEquals(1, counter);
167
168     // because the tearDown wants to close it :-)
169
beginSessionTransaction();
170
171     // check if the timer has be re-scheduled because of the repeat.
172
timersIter = jbpmSession.getSchedulerSession().findTimersByDueDate();
173     assertTrue(timersIter.hasNext());
174     timer = (Timer) timersIter.next();
175     // check that the timer was rescheduled with a duedate 1 second after the original duedate.
176
assertEquals(originalDueDate+1000, timer.getDueDate().getTime());
177   }
178 }
179
Popular Tags