之前承諾過要逐一講解helloworld.xml的配置文件細節,但是在講解這些節點之前,需要先讓我們的helloworld飛起來,在這裏,我們採用junit4進行測試,應此,讀者除了需要將osworkflow所需要的jar文件加入到您的classpath之外,還需要將junit加入進來,話已至此,先讓我們的osworkflow飛起來哈!
1、HelloWorld單元測試。
package com.wangwenjun.osworkflow;
import java.util.Collections;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.opensymphony.workflow.InvalidActionException;
import com.opensymphony.workflow.InvalidEntryStateException;
import com.opensymphony.workflow.InvalidInputException;
import com.opensymphony.workflow.InvalidRoleException;
import com.opensymphony.workflow.Workflow;
import com.opensymphony.workflow.WorkflowException;
import com.opensymphony.workflow.basic.BasicWorkflow;
import com.opensymphony.workflow.spi.WorkflowEntry;
/**
* @author Alex(QQ:532500648)
* Create first work flow and test basic method,read and execute this unit test source
* maybe you will know the basic use of work flow,if you have some questions don't discard,
* Insist on reading the back of the article or give me a message.
*/
public class HelloWorld {
private Workflow wf = null;
@Before
public void init()
{
wf = new BasicWorkflow("helloWorld");
}
@Test
public void helloWorld()
{
try {
long id = wf.initialize("first", 100, Collections.EMPTY_MAP);
/*test is or not work flow have 1 valid steps*/
Assert.assertEquals(1,wf.getCurrentSteps(id).size());
/* invoke do action method.*/
wf.doAction(id, 1, Collections.EMPTY_MAP);
Assert.assertEquals(1,wf.getCurrentSteps(id).size());
/*query history steps*/
Assert.assertEquals(1, wf.getHistorySteps(id).size());
/*invoke do action method again.*/
wf.doAction(id, 2, Collections.EMPTY_MAP);
/*query history steps*/
Assert.assertEquals(2, wf.getHistorySteps(id).size());
/*test wf is or not finished.*/
Assert.assertEquals(0,wf.getCurrentSteps(id).size());
/*also can use state to test.*/
Assert.assertEquals(WorkflowEntry.COMPLETED,wf.getEntryState(id));
} catch (InvalidActionException e) {
e.printStackTrace();
} catch (InvalidRoleException e) {
e.printStackTrace();
} catch (InvalidInputException e) {
e.printStackTrace();
} catch (InvalidEntryStateException e) {
e.printStackTrace();
} catch (WorkflowException e) {
e.printStackTrace();
}
}
}
其中Workflow是一個比較重量級的對象,他在創建的時候需要創建一系列對象,比如解析配置文件,初始化持久化對象等等,因此在實際的項目中儘量的緩存,這樣可以減少很多的開銷。
針對上一篇文章中的配置文件,我們的測試用例全部測試通過,那麼我們是不是應該對xml中的配置進行比較詳細的解釋,嗯,我看着是一個比較不錯的主意(有點自說自話的意思哈,見諒!)
我們下一篇文章見!詳細介紹helloworld的各個配置細節,如果您還保持着比較濃烈的好奇心,請等待這下一篇的新鮮出爐。