/*
* Danet GmbH
* Beratung und Software-Entwicklung
* Gesch�ftstelle AN
*
* $Id: SubFlow.java 2326 2007-03-27 21:59:44Z mlipp $
*
* $Log$
* Revision 1.3 2006/10/13 13:58:32 drmlipp
* Adapted to new environment.
*
* Revision 1.2 2006/10/07 20:41:34 mlipp
* Merged J2EE 1.4 adaptions from test branch.
*
* Revision 1.1.1.1 2004/08/18 15:18:47 drmlipp
* Update to 1.2
*
* Revision 1.1 2004/07/18 17:07:53 lipp
* Added test.
*
*/
package process;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collection;
import java.util.Iterator;
import javax.naming.InitialContext;
import javax.rmi.PortableRemoteObject;
import javax.security.auth.login.LoginException;
import de.danet.an.util.EJBUtil;
import de.danet.an.util.junit.EJBClientTest;
import de.danet.an.workflow.omgcore.WfActivity;
import de.danet.an.workflow.omgcore.WfExecutionObject;
import de.danet.an.workflow.omgcore.WfExecutionObject.State;
import de.danet.an.workflow.omgcore.WfProcess;
import de.danet.an.workflow.omgcore.WfProcessMgr;
import de.danet.an.workflow.omgcore.WfRequester;
import de.danet.an.workflow.api.DefaultRequester;
import de.danet.an.workflow.api.FactoryConfigurationError;
import de.danet.an.workflow.api.FormalParameter;
import de.danet.an.workflow.api.Process;
import de.danet.an.workflow.api.ProcessDefinition;
import de.danet.an.workflow.api.ProcessDefinitionDirectory;
import de.danet.an.workflow.api.ProcessDirectory;
import de.danet.an.workflow.api.WorkflowService;
import de.danet.an.workflow.api.WorkflowServiceFactory;
import de.danet.an.workflow.ejbs.admin.ProcessDefinitionDirectoryHome;
import de.danet.an.workflow.ejbs.core.WfActivityHome;
import common.UTLoginContext;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Zusammenstellung aller TimerObjectTests.
*/
public class SubFlow extends TestCase {
private static UTLoginContext plc = null;
static {
try {
plc = new UTLoginContext();
plc.login();
} catch (LoginException e) {
throw new IllegalStateException (e.getMessage ());
}
}
/**
* Konstruktor zum Erzeugen eines TestCase
*/
public SubFlow(String name) {
super (name);
}
/**
* Stellt diese TestSuite zusammen.
*/
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(new SubFlow("importProcessDefinitions"));
suite.addTest(new SubFlow("checkFormalParams"));
return new EJBClientTest (plc, suite);
}
private static WorkflowService wfsCache = null;
private WorkflowService workflowService() {
if (wfsCache == null) {
try {
WorkflowServiceFactory wfsf
= WorkflowServiceFactory.newInstance ();
wfsCache = wfsf.newWorkflowService();
} catch (FactoryConfigurationError e) {
throw new IllegalStateException (e.getMessage());
}
}
return wfsCache;
}
/**
* Import the process definitions from a XPDL file
* unsing the ProcessDefinitionDirectory bean.
*/
public void importProcessDefinitions() throws Exception {
// Create process definition directory bean
ProcessDefinitionDirectory pdd
= workflowService().processDefinitionDirectory();
InputStream is = getClass().getResourceAsStream("/process/subflow.xml");
assertTrue (is != null);
BufferedReader br = new BufferedReader
(new InputStreamReader(is, "ISO-8859-1"));
StringBuffer sb = new StringBuffer();
String st;
while ((st = br.readLine()) != null) {
sb.append(st + "\n");
}
pdd.importProcessDefinitions(sb.toString());
Collection processDefinitions = pdd.processDefinitions();
assertTrue (processDefinitions.size() > 0);
}
private WfProcess createProcess
(String pkgId, String prcId, WfRequester req)
throws Exception {
ProcessDefinitionDirectory pdd = null;
try {
pdd = workflowService().processDefinitionDirectory ();
WfProcessMgr pmgr = pdd.processMgr(pkgId, prcId);
return pmgr.createProcess (req);
} finally {
workflowService().release (pdd);
}
}
/**
* Create a new process for the given type.
*/
public void checkFormalParams() throws Exception {
WfRequester cont = new DefaultRequester(workflowService());
Process process = (Process)createProcess
("subflowtest", "called1", cont);
FormalParameter[] fps = process.processDefinition().formalParameters();
assertTrue(fps.length == 2);
assertTrue(fps[0].id().equals ("testData"));
assertTrue(fps[1].id().equals ("status"));
}
/**
* Create a new process for the given type and start it.
*/
public void createAndStartProcess1() throws Exception {
WfRequester cont = new DefaultRequester(workflowService());
WfProcess process = createProcess("ut-process", "jut1", cont);
assertTrue(process.state().startsWith("open.not_running.not_started"));
process.start();
assertTrue(process.state().startsWith("open.running"));
// get the activities
Collection c = process.steps();
Iterator it = c.iterator();
while (it.hasNext()) {
WfActivity a = (WfActivity)it.next();
assertTrue(a.state() != null);
}
}
/**
* Create a new process for the given type and start it.
*/
public void createAndStartProcess2() throws Exception {
WfRequester cont = new DefaultRequester(workflowService());
WfProcess process = createProcess("ut-process", "jut2", cont);
assertTrue(process.state().startsWith("open.not_running.not_started"));
process.start();
assertTrue(process.state().startsWith("open.running"));
// get the activities
Collection c = process.steps();
Iterator it = c.iterator();
while (it.hasNext()) {
WfActivity a = (WfActivity)it.next();
assertTrue(a.state() != null);
}
}
/**
* Create a new process for the given type and start it.
*/
public void createAndStartProcess3() throws Exception {
WfRequester cont = new DefaultRequester(workflowService());
WfProcess process = createProcess("ut-process", "jut3", cont);
assertTrue(process.state().startsWith("open.not_running.not_started"));
process.start();
Thread.sleep(2500);
assertTrue(process + " should be completed, is " + process.state(),
process.state().startsWith("closed.completed"));
// get the activities
Collection c = process.steps();
Iterator it = c.iterator();
int cnt = 0;
while (it.hasNext()) {
WfActivity a = (WfActivity)it.next();
if (a.state().startsWith
(WfExecutionObject.ClosedState.COMPLETED.toString())) {
cnt += 1;
}
}
assertTrue (cnt == 5);
}
/**
* Create a new process for the given type.
*/
public void createProcess2() throws Exception {
WfRequester cont = new DefaultRequester(workflowService());
WfProcess process = createProcess("ut-process", "jut2", cont);
assertTrue(process.state().startsWith("open.not_running.not_started"));
process.start();
Thread.sleep (1000);
assertTrue(process.state().startsWith("open.running"));
int i = 9;
int resCnt = 0;
while (i-- > 0) {
boolean resd = false;
for (Iterator it = process.steps().iterator(); it.hasNext();) {
WfActivity a = (WfActivity)it.next();
if (a.state().startsWith ("open.not_running.suspended")) {
a.resume ();
resd = true;
resCnt += 1;
Thread.sleep (500);
}
}
if (!resd) {
break;
}
}
assertTrue (i >= 0);
assertTrue (resCnt == 8);
}
/**
* Create a new process for the given type.
*/
public void createProcess3() throws Exception {
WfRequester cont = new DefaultRequester(workflowService());
WfProcess process = createProcess("ut-process", "jut3", cont);
assertTrue(process.state().startsWith("open.not_running.not_started"));
process.start();
Thread.sleep (5000);
assertTrue("Process state should be closed.completed.normal, is "
+ process.state(),
process.state().startsWith("closed.completed.normal"));
assertTrue("Process workflow state should be closed, is "
+ process.workflowState(),
process.workflowState().equals (State.CLOSED));
}
/**
* Remove a given process from the database.
*/
public void removeProcess3() throws Exception {
WfRequester cont = new DefaultRequester(workflowService());
WfProcess process = createProcess("ut-process", "jut999", cont);
ProcessDirectory pd = null;
try {
pd = workflowService().processDirectory ();
pd.removeProcess ((Process)process);
} finally {
workflowService().release (pd);
}
}
/**
* Returns a list of all activities assigned to a given process
* from the ActivityBean.
*/
public void activityFindByProcess() throws Exception {
InitialContext ic = new InitialContext();
Object objref = ic.lookup("ActivityBean");
WfActivityHome home
= (WfActivityHome)PortableRemoteObject.narrow
(objref, WfActivityHome.class);
assertTrue (home != null);
Collection c = home.findByProcess(new Long(1));
assertTrue (c != null);
}
/**
* Create a new process and then remove its process definition.
*/
public void createProcess1AndRemoveProcessDef() throws Exception {
ProcessDefinitionDirectory pdd = null;
try {
WfRequester cont = new DefaultRequester(workflowService());
Process process = (Process)createProcess("ut-process", "jut1", cont);
assertTrue(process.state().startsWith("open.not_running.not_started"));
process.start();
assertTrue(process.state().startsWith("open.running"));
// remove its process definition
pdd = workflowService().processDefinitionDirectory ();
ProcessDefinition pd
= pdd.lookupProcessDefinition("ut-process", "jut1");
assertTrue(pd!=null);
pdd.removeProcessDefinition("ut-process", "jut1");
boolean gotEx = false;
try {
pd = pdd.lookupProcessDefinition("ut-process", "jut1");
} catch (Exception ex) {
gotEx = true;
}
assertTrue(gotEx);
// check the process definition of the process
ProcessDefinition procDef = process.processDefinition();
assertTrue(procDef.packageId().equals("ut-process"));
assertTrue(procDef.processId().equals("jut1"));
// import the process definition again.
importProcessDefinitions();
pd = pdd.lookupProcessDefinition("ut-process", "jut1");
assertTrue(pd!=null);
} finally {
workflowService().release (pdd);
}
}
public static void importProcessDefinitions(String filename)
throws Exception {
// Create process definition directory bean
ProcessDefinitionDirectoryHome pddh
= (ProcessDefinitionDirectoryHome)EJBUtil.lookupEJBHome
(ProcessDefinitionDirectoryHome.class,
"ejb/de.danet.an.wfdemo.ProcessDefinitionDirectory");
ProcessDefinitionDirectory pdd = pddh.create();
InputStream is = SubFlow.class.getResourceAsStream(filename);
assertTrue (is != null);
BufferedReader br = new BufferedReader
(new InputStreamReader(is, "ISO-8859-1"));
StringBuffer sb = new StringBuffer();
String st;
while ((st = br.readLine()) != null) {
sb.append(st + "\n");
}
pdd.importProcessDefinitions(sb.toString());
Collection processDefinitions = pdd.processDefinitions();
assertTrue (processDefinitions.size() > 0);
}
}