📄 timerdbtest.java
字号:
/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jbpm.scheduler.exe;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import org.jbpm.context.exe.ContextInstance;
import org.jbpm.db.AbstractDbTestCase;
import org.jbpm.graph.def.Action;
import org.jbpm.graph.def.ActionHandler;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ExecutionContext;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.scheduler.impl.SchedulerThread;
import org.jbpm.taskmgmt.exe.TaskInstance;
public class TimerDbTest extends AbstractDbTestCase {
public void testSaveTimer() {
Date now = new Date();
Timer timer = new Timer();
timer.setName("timer-name");
timer.setDueDate(now);
timer.setTransitionName("transition-name");
timer.setRepeat("repeat-duration");
timer = saveAndReloadTimer(timer);
assertEquals("timer-name", timer.getName());
// we test for the same date in a simple format
DateFormat df = SimpleDateFormat.getDateInstance();
assertEquals(df.format(now), df.format(timer.getDueDate()));
// we test for each part of the date to see where we fail per database
// to help with debugging.
Calendar ncal = new GregorianCalendar();
ncal.setTime(now);
Calendar tcal = new GregorianCalendar();
tcal.setTime(timer.getDueDate());
assertEquals(ncal.get(Calendar.YEAR), tcal.get(Calendar.YEAR));
assertEquals(ncal.get(Calendar.MONTH), tcal.get(Calendar.MONTH));
assertEquals(ncal.get(Calendar.DAY_OF_MONTH), tcal.get(Calendar.DAY_OF_MONTH));
assertEquals(ncal.get(Calendar.HOUR_OF_DAY), tcal.get(Calendar.HOUR_OF_DAY));
assertEquals(ncal.get(Calendar.MINUTE), tcal.get(Calendar.MINUTE));
assertEquals(ncal.get(Calendar.SECOND), tcal.get(Calendar.SECOND));
assertEquals(ncal.get(Calendar.MILLISECOND), tcal.get(Calendar.MILLISECOND));
// we test for the actual date
assertEquals(now, timer.getDueDate());
assertEquals("transition-name", timer.getTransitionName());
assertEquals("repeat-duration", timer.getRepeat());
}
public void testTimerCreation() {
ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
"<process-definition>" +
" <start-state>" +
" <transition to='catch crooks' />" +
" </start-state>" +
" <state name='catch crooks'>" +
" <timer name='reminder' duedate='5 seconds' />" +
" <transition to='end'/>" +
" </state>" +
" <end-state name='end'/>" +
"</process-definition>"
);
graphSession.saveProcessDefinition(processDefinition);
ProcessInstance processInstance = new ProcessInstance(processDefinition);
jbpmContext.save(processInstance);
long before = System.currentTimeMillis();
processInstance.signal();
long after = System.currentTimeMillis();
jbpmContext.save(processInstance);
newTransaction();
Timer timer = (Timer) schedulerSession.findTimersByDueDate(5).next();
assertEquals("reminder", timer.getName());
assertTrue( (before+5000) <= timer.getDueDate().getTime() );
assertTrue( timer.getDueDate().getTime() <= (after+5000) );
assertEquals("catch crooks", timer.getGraphElement().getName());
}
public void testTimerCancellation() {
ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
"<process-definition>" +
" <start-state>" +
" <transition to='catch crooks' />" +
" </start-state>" +
" <state name='catch crooks'>" +
" <timer name='reminder' duedate='5 seconds' />" +
" <transition to='end'/>" +
" </state>" +
" <end-state name='end'/>" +
"</process-definition>"
);
graphSession.saveProcessDefinition(processDefinition);
ProcessInstance processInstance = new ProcessInstance(processDefinition);
jbpmContext.save(processInstance);
processInstance.signal();
processInstance.signal();
jbpmContext.save(processInstance);
newTransaction();
assertFalse(schedulerSession.findTimersByDueDate(5).hasNext());
}
public void testTimerAction() {
ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
"<process-definition name='process'>" +
" <start-state>" +
" <transition to='sometask' />" +
" </start-state>" +
" <task-node name='sometask'>" +
" <timer name='reminder'" +
" duedate='1 business minutes'" +
" repeat='1 business minutes'" +
" transition='time-out-transition' >" +
" <action class='my-action-handler-class-name' />" +
" </timer>" +
" <task name='do something'/>" +
" <transition name='time-out-transition' to='sometask' />" +
" </task-node>" +
"</process-definition>"
);
graphSession.saveProcessDefinition(processDefinition);
ProcessInstance processInstance = new ProcessInstance(processDefinition);
jbpmContext.save(processInstance);
newTransaction();
processInstance = graphSession.loadProcessInstance(processInstance.getId());
processInstance.signal();
processDefinition = processInstance.getProcessDefinition();
Iterator iter = processDefinition.getNode("sometask").getEvent("node-enter").getActions().iterator();
while (iter.hasNext()) {
Action action = (Action) iter.next();
action.getId();
}
jbpmContext.save(processInstance);
newTransaction();
Timer timer = (Timer) schedulerSession.findTimersByDueDate(5).next();
assertNotNull(timer);
}
public Timer saveAndReloadTimer(Timer timer) {
schedulerSession.saveTimer(timer);
newTransaction();
timer = (Timer) session.load(Timer.class, new Long(timer.getId()));
return timer;
}
public void testTaskTimerExecution() {
ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
"<process-definition>" +
" <start-state>" +
" <transition to='timed task' />" +
" </start-state>" +
" <task-node name='timed task'>" +
" <task>" +
" <timer duedate='23 business seconds'>" +
" <action class='geftem-eu-shuppe-oender-ze-konte'/>" +
" </timer>" +
" </task>" +
" </task-node>" +
"</process-definition>"
);
processDefinition = saveAndReload(processDefinition);
ProcessInstance processInstance = new ProcessInstance(processDefinition);
processInstance.signal();
processInstance = saveAndReload(processInstance);
assertTrue(schedulerSession.findTimersByDueDate(5).hasNext());
}
public void testTimerCancellationAtProcessEnd() {
ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
"<process-definition>" +
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -