KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > scheduler > exe > TimerDbTest


1 package org.jbpm.scheduler.exe;
2
3 import java.text.DateFormat JavaDoc;
4 import java.text.SimpleDateFormat JavaDoc;
5 import java.util.Calendar JavaDoc;
6 import java.util.Date JavaDoc;
7 import java.util.GregorianCalendar JavaDoc;
8 import java.util.Iterator JavaDoc;
9
10 import org.jbpm.db.AbstractDbTestCase;
11 import org.jbpm.db.SchedulerSession;
12 import org.jbpm.graph.def.Action;
13 import org.jbpm.graph.def.ProcessDefinition;
14 import org.jbpm.graph.exe.ProcessInstance;
15
16
17 public class TimerDbTest extends AbstractDbTestCase {
18   
19
20   SchedulerSession schedulerSession = null;
21   
22   protected void beginSessionTransaction() {
23     super.beginSessionTransaction();
24     schedulerSession = new SchedulerSession(jbpmSession);
25   }
26
27   public void testSaveTimer() {
28     Date JavaDoc now = new Date JavaDoc();
29     
30     Timer timer = new Timer();
31     timer.setName("timer-name");
32     timer.setDueDate(now);
33     timer.setTransitionName("transition-name");
34     timer.setRepeat("repeat-duration");
35     
36     timer = saveAndReloadTimer(timer);
37     
38     assertEquals("timer-name", timer.getName());
39
40     // we test for the same date in a simple format
41
DateFormat JavaDoc df = SimpleDateFormat.getDateInstance();
42     assertEquals(df.format(now), df.format(timer.getDueDate()));
43
44     // we test for each part of the date to see where we fail per database
45
// to help with debugging.
46
Calendar JavaDoc ncal = new GregorianCalendar JavaDoc();
47     ncal.setTime(now);
48     Calendar JavaDoc tcal = new GregorianCalendar JavaDoc();
49     tcal.setTime(timer.getDueDate());
50     assertEquals(ncal.get(Calendar.YEAR), tcal.get(Calendar.YEAR));
51     assertEquals(ncal.get(Calendar.MONTH), tcal.get(Calendar.MONTH));
52     assertEquals(ncal.get(Calendar.DAY_OF_MONTH), tcal.get(Calendar.DAY_OF_MONTH));
53     assertEquals(ncal.get(Calendar.HOUR_OF_DAY), tcal.get(Calendar.HOUR_OF_DAY));
54     assertEquals(ncal.get(Calendar.MINUTE), tcal.get(Calendar.MINUTE));
55     assertEquals(ncal.get(Calendar.SECOND), tcal.get(Calendar.SECOND));
56     assertEquals(ncal.get(Calendar.MILLISECOND), tcal.get(Calendar.MILLISECOND));
57     
58     // we test for the actual date
59
assertEquals(now, timer.getDueDate());
60     assertEquals("transition-name", timer.getTransitionName());
61     assertEquals("repeat-duration", timer.getRepeat());
62   }
63   
64   public void testTimerCreation() {
65     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
66       "<process-definition>" +
67       " <start-state>" +
68       " <transition to='catch crooks' />" +
69       " </start-state>" +
70       " <state name='catch crooks'>" +
71       " <timer name='reminder' duedate='5 seconds' />" +
72       " <transition to='end'/>" +
73       " </state>" +
74       " <end-state name='end'/>" +
75       "</process-definition>"
76     );
77
78     graphSession.saveProcessDefinition(processDefinition);
79     ProcessInstance processInstance = new ProcessInstance(processDefinition);
80     graphSession.saveProcessInstance(processInstance);
81     long before = System.currentTimeMillis();
82     processInstance.signal();
83     long after = System.currentTimeMillis();
84     graphSession.saveProcessInstance(processInstance);
85     
86     newTransaction();
87     
88     Timer timer = (Timer) schedulerSession.findTimersByDueDate().next();
89     assertEquals("reminder", timer.getName());
90     assertTrue( (before+5000) <= timer.getDueDate().getTime() );
91     assertTrue( timer.getDueDate().getTime() <= (after+5000) );
92     assertEquals("catch crooks", timer.getGraphElement().getName());
93   }
94
95   public void testTimerCancellation() {
96     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
97       "<process-definition>" +
98       " <start-state>" +
99       " <transition to='catch crooks' />" +
100       " </start-state>" +
101       " <state name='catch crooks'>" +
102       " <timer name='reminder' duedate='5 seconds' />" +
103       " <transition to='end'/>" +
104       " </state>" +
105       " <end-state name='end'/>" +
106       "</process-definition>"
107     );
108
109     graphSession.saveProcessDefinition(processDefinition);
110     ProcessInstance processInstance = new ProcessInstance(processDefinition);
111     graphSession.saveProcessInstance(processInstance);
112     processInstance.signal();
113     processInstance.signal();
114     graphSession.saveProcessInstance(processInstance);
115     
116     newTransaction();
117     
118     assertFalse(schedulerSession.findTimersByDueDate().hasNext());
119   }
120
121   public void testTimerAction() {
122     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
123       "<process-definition name='process'>" +
124       " <start-state>" +
125       " <transition to='sometask' />" +
126       " </start-state>" +
127       " <task-node name='sometask'>" +
128       " <timer name='reminder'" +
129       " duedate='1 business minutes'" +
130       " repeat='1 business minutes'" +
131       " transition='time-out-transition' >" +
132       " <action class='my-action-handler-class-name' />" +
133       " </timer>" +
134       " <task name='do something'/>" +
135       " <transition name='time-out-transition' to='sometask' />" +
136       " </task-node>" +
137       "</process-definition>"
138     );
139     jbpmSession.getGraphSession().saveProcessDefinition(processDefinition);
140     ProcessInstance processInstance = new ProcessInstance(processDefinition);
141
142     jbpmSession.getGraphSession().saveProcessInstance(processInstance);
143     newTransaction();
144     
145     processInstance = jbpmSession.getGraphSession().loadProcessInstance(processInstance.getId());
146     processInstance.signal();
147     
148     processDefinition = processInstance.getProcessDefinition();
149     Iterator JavaDoc iter = processDefinition.getNode("sometask").getEvent("node-enter").getActions().iterator();
150     while (iter.hasNext()) {
151       Action action = (Action) iter.next();
152       action.getId();
153     }
154     
155     jbpmSession.getGraphSession().saveProcessInstance(processInstance);
156     newTransaction();
157
158     SchedulerSession schedulerSession = new SchedulerSession(jbpmSession);
159     Timer timer = (Timer) schedulerSession.findTimersByDueDate().next();
160     assertNotNull(timer);
161   }
162
163   public Timer saveAndReloadTimer(Timer timer) {
164     schedulerSession.saveTimer(timer);
165     newTransaction();
166     timer = (Timer) jbpmSession.getSession().load(Timer.class, new Long JavaDoc(timer.getId()));
167     return timer;
168   }
169   
170   public void testTaskTimerExecution() {
171     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
172       "<process-definition>" +
173       " <start-state>" +
174       " <transition to='timed task' />" +
175       " </start-state>" +
176       " <task-node name='timed task'>" +
177       " <task>" +
178       " <timer duedate='23 business seconds'>" +
179       " <action class='geftem-eu-shuppe-oender-ze-konte'/>" +
180       " </timer>" +
181       " </task>" +
182       " </task-node>" +
183       "</process-definition>"
184     );
185     processDefinition = saveAndReload(processDefinition);
186     
187     ProcessInstance processInstance = new ProcessInstance(processDefinition);
188     processInstance.signal();
189     
190     processInstance = saveAndReload(processInstance);
191
192     assertTrue(schedulerSession.findTimersByDueDate().hasNext());
193   }
194   
195   public void testTimerCancellationAtProcessEnd() {
196     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
197       "<process-definition>" +
198       " <start-state>" +
199       " <transition to='s' />" +
200       " </start-state>" +
201       " <state name='s'>" +
202       " <event type='node-enter'>" +
203       " <create-timer duedate='26 business seconds'>" +
204       " <action class='claim.you.are.Innocent' />" +
205       " </create-timer>" +
206       " </event>" +
207       " <transition to='end' />" +
208       " </state>" +
209       " <end-state name='end' />" +
210       "</process-definition>"
211     );
212     processDefinition = saveAndReload(processDefinition);
213     
214     ProcessInstance processInstance = new ProcessInstance(processDefinition);
215     processInstance.signal();
216     
217     processInstance = saveAndReload(processInstance);
218
219     assertTrue(schedulerSession.findTimersByDueDate().hasNext());
220     processInstance.signal();
221     
222     processInstance = saveAndReload(processInstance);
223
224     assertFalse(schedulerSession.findTimersByDueDate().hasNext());
225   }
226 }
227
Popular Tags