现在的位置: 首页 > 综合 > 正文

OSWorkFlow深入浅出(3)—让工作流起来

2013年10月21日 ⁄ 综合 ⁄ 共 2237字 ⁄ 字号 评论关闭

之前承诺过要逐一讲解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的各个配置细节,如果您还保持着比较浓烈的好奇心,请等待这下一篇的新鲜出炉。

抱歉!评论已关闭.