package
com.sample;
?
import
junit.framework.TestCase;
?
import
org.jbpm.JbpmConfiguration;
import
org.jbpm.JbpmContext;
import
org.jbpm.context.exe.ContextInstance;
import
org.jbpm.graph.def.ProcessDefinition;
import
org.jbpm.graph.exe.ProcessInstance;
?
public
class
SimpleProcessTest extends TestCase {
?
???
private
JbpmConfiguration config = JbpmConfiguration.getInstance();
???
private
JbpmContext ctx = config.createJbpmContext();
???
// helloworld
對應(yīng)于
jbpm_processdefinition
表的
name
字段值,也即
processdefinition.xml
的
name
???
//
這個值得取比較耗時,實際項目里最好和“數(shù)據(jù)庫的
JDBC
連接”一樣,讓它共享,不要頻繁打開關(guān)閉。
???
private
ProcessDefinition processDefinition = ctx.getGraphSession().findLatestProcessDefinition("helloworld");
?
???
public
void
testNewRequest() {
???????
long
id = newRequest();
??????? System.out.println("id=" + id);
??????? checkNewRequest(id);
??????? confirmRequest(id);
??????? checkconfirmRequest(id);
???????
ctx.close();//
關(guān)閉
jbpm
容器
??? }
?
???
/**
???
?
*
創(chuàng)建一個請假單
???
?
*
???
?
*
@return
???
?
*/
???
private
long
newRequest() {
???????
//
創(chuàng)建一個新流程
??????? ProcessInstance pi = processDefinition.createProcessInstance();
???????
//
取得流程的數(shù)據(jù)環(huán)境
??????? ContextInstance ci = pi.getContextInstance();
???????
//
創(chuàng)建一張請假單
??????? ci.setVariable("name", "
陳剛
www.chengang.com.cn"
);
??????? ci.setVariable("day", 2);
???????
assertEquals(null, ci.getVariable("note"));
???????
//
請假申請結(jié)束,轉(zhuǎn)到下一個流程結(jié)點
??????? pi.signal();
???????
return
pi.getId();
??? }
?
???
/**
???
?
*
檢查請假單的數(shù)據(jù)
???
?
*
???
?
*
@param
id
???
?
*/
???
private
void
checkNewRequest(long id) {
???????
//
從數(shù)據(jù)庫提取原流程
??????? ProcessInstance pi = ctx.loadProcessInstance(id);
???????
//
取得流程的數(shù)據(jù)環(huán)境
??????? ContextInstance ci = pi.getContextInstance();
???????
//
創(chuàng)建一張請假單
???????
assertEquals("
陳剛
www.chengang.com.cn"
, ci.getVariable("name"));
???????
assertEquals(Integer.valueOf(2), ci.getVariable("day"));
???????
assertEquals("
我要請假
"
, ci.getVariable("note"));
?
???????
//
當(dāng)前是結(jié)點為
confirm
???????
assertEquals(pi.getRootToken().getNode().getName(), "confirm");
???????
//
流程還沒結(jié)束
???????
assertFalse(pi.hasEnded());
??? }
?
???
/**
???
?
*
審批陳剛的請假申請
???
?
*
???
?
*
@param
id
???
?
*/
???
private
void
confirmRequest(long id) {
??????? ProcessInstance pi = ctx.loadProcessInstance(id);
??????? ContextInstance ci = pi.getContextInstance();
???????
//
不通過
??????? ci.setVariable("note", "
不準(zhǔn)請假,繼續(xù)加班
"
);
???????
//
審批結(jié)束,到下一個流程結(jié)點
??????? pi.signal();
??? }
?
???
private
void
checkConfirmRequest(long id) {
??????? ProcessInstance pi = ctx.loadProcessInstance(id);
??????? ContextInstance ci = pi.getContextInstance();
???????
// ConfirmAction
類在
signal
后執(zhí)行,所以覆蓋了經(jīng)理的審批意見
???????
assertEquals("
準(zhǔn)假
"
, ci.getVariable("note"));
?
???????
//
當(dāng)前是結(jié)點為
end
???????
assertEquals(pi.getRootToken().getNode().getName(), "end");
???????
//
流程結(jié)束了
???????
assertTrue(pi.hasEnded());
??? }
?
}