/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2003 Danet GmbH (www.danet.de), GS-AN.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: WfMOpenTestCase.java 1607 2006-09-29 12:32:13Z drmlipp $
*
* $Log$
* Revision 1.1.1.3 2004/08/18 15:17:39 drmlipp
* Update to 1.2
*
* Revision 1.14 2004/02/21 12:50:29 lipp
* Made tests a bit more robust.
*
* Revision 1.13 2004/01/27 11:45:32 lipp
* Preserve newlines when reading process definitions.
*
* Revision 1.12 2003/10/21 21:00:45 lipp
* Moved EJBClientTest to new junit sub-package.
*
* Revision 1.11 2003/10/20 10:31:19 barzik
* - separated process creation prerequisites from proper process creation
* - execute prerequisites and first creation separately
*
* Revision 1.10 2003/10/08 12:39:40 huaiyang
* make test weblogic compatible.
*
* Revision 1.9 2003/06/27 09:44:03 lipp
* Fixed copyright/license information.
*
* Revision 1.8 2003/04/26 16:46:55 lipp
* Made unittests and systemtests coexist in eclipse.
*
* Revision 1.7 2003/04/16 19:58:49 lipp
* Adapted to jdk 1.4
*
* Revision 1.6 2003/03/21 14:21:43 montag
* New system test for wsif invocation.
*
* Revision 1.5 2003/02/25 17:08:18 lipp
* Reorganized requester implementation.
*
* Revision 1.4 2003/02/14 14:03:03 lipp
* Reactivated process removal.
*
* Revision 1.3 2003/02/12 16:14:43 lipp
* Delays now based on events.
*
* Revision 1.2 2002/11/25 17:27:19 weidauer
* completed documentation
*
* Revision 1.1 2002/11/25 17:22:42 weidauer
* refactored with WfMOpenTestCase as base test case and cleaned up classes
*
*/
package process;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.rmi.RemoteException;
import java.util.Collection;
import junit.framework.TestCase;
import common.STProjectLoginContext;
import javax.security.auth.login.LoginException;
import de.danet.an.util.junit.EJBClientTest;
import de.danet.an.workflow.api.DefaultRequester;
import de.danet.an.workflow.api.FactoryConfigurationError;
import de.danet.an.workflow.api.Process;
import de.danet.an.workflow.api.ProcessDefinitionDirectory;
import de.danet.an.workflow.api.ProcessDirectory;
import de.danet.an.workflow.api.ProcessMgr;
import de.danet.an.workflow.api.WorkflowService;
import de.danet.an.workflow.api.WorkflowServiceFactory;
import de.danet.an.workflow.omgcore.WfActivity;
import de.danet.an.workflow.omgcore.WfProcess;
import de.danet.an.workflow.omgcore.WfRequester;
/**
* Base test case for WfMOpen test cases.
* @author <a href="mailto:weidauer@danet.de">Christian Weidauer</a>
* @version 1.0
*/
public class WfMOpenTestCase extends TestCase {
protected static STProjectLoginContext plc = null;
static {
try {
plc = new STProjectLoginContext();
plc.login();
} catch (LoginException e) {
throw new IllegalStateException (e.getMessage ());
}
}
/**
* Describe variable <code>xpdlFile</code> here.
*
*/
protected String xpdlFile;
/**
* <code>setStateDirectly</code> defines how a state transition is caused.
*
*/
protected boolean setStateDirectly = false;
/**
* Describe <code>setSetStateDirectly</code> method here.
*
* @param setStateDirectly a <code>boolean</code> value
*/
protected void setSetStateDirectly(boolean setStateDirectly) {
this.setStateDirectly = setStateDirectly;
}
/**
* Constructor of this TestCase
* @param name a <code>String</code> value
*/
public WfMOpenTestCase(String name) {
super(name);
}
/**
* Describe constant <code>RUNNING</code> here.
*
*/
protected static final String RUNNING = "open.running";
/**
* Describe constant <code>SUSPENDED</code> here.
*
*/
protected static final String SUSPENDED = "open.not_running.suspended";
/**
* Describe constant <code>NOT_STARTED</code> here.
*
*/
protected static final String NOT_STARTED = "open.not_running.not_started";
/**
* Describe constant <code>COMPLETED</code> here.
*
*/
protected static final String COMPLETED = "closed.completed";
/**
* Describe constant <code>TERMINATED</code> here.
*
*/
protected static final String TERMINATED = "closed.terminated";
/**
* Describe constant <code>ABORTED</code> here.
*
*/
protected static final String ABORTED = "closed.aborted";
private static WorkflowService wfsCache = null;
protected WorkflowService workflowService() {
if (wfsCache == null) {
try {
WorkflowServiceFactory wfsf
= WorkflowServiceFactory.newInstance ();
wfsCache = wfsf.newWorkflowService();
} catch (FactoryConfigurationError e) {
throw new IllegalStateException (e.getMessage());
}
}
return wfsCache;
}
private static WfRequester defReqCache = null;
protected WfRequester defaultRequester ()
throws RemoteException {
if (defReqCache == null) {
defReqCache = new DefaultRequester (workflowService());
}
return defReqCache;
}
/**
* Import the process definitions from a XPDL file
* using the ProcessDefinitionDirectory bean.
* @exception Exception if an error occurs
*/
public void importProcessDefinitions() throws Exception {
ProcessDefinitionDirectory pdd
= workflowService().processDefinitionDirectory();
InputStream is = getClass().getResourceAsStream(xpdlFile);
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);
}
/**
* remove the process definition
* using the ProcessDefinitionDirectory bean.
* @param packageId Id attribute of the process package.
* @param processId Id attribute of the process.
* @exception Exception if an error occurs
*/
public void removeProcessDefinition(String packageId, String processId)
throws Exception {
ProcessDefinitionDirectory pdd
= workflowService().processDefinitionDirectory();
pdd.removeProcessDefinition(packageId, processId);
}
/**
* Describe <code>createProcess</code> method here.
*
* @param pkgId a <code>String</code> value
* @param prcId a <code>String</code> value
* @param req a <code>WfRequester</code> value
* @return a <code>SmartWfProcess</code> value
* @exception Exception if an error occurs
*/
protected SmartWfProcess createProcess (String pkgId, String prcId)
throws Exception {
ProcessDefinitionDirectory pdd
= workflowService().processDefinitionDirectory();
ProcessMgr pmgr = pdd.processMgr(pkgId, prcId);
return smart(pmgr.createProcess (defaultRequester()));
}
/**
*
*/
private static ProcessMgr processManager = null;
/**
* Prepares the creation of a process.
*
* @param pkgId a <code>String</code> value
* @param prcId a <code>String</code> value
* @exception Exception if an error occurs
*/
protected void prepareCreateProcess (String pkgId, String prcId){
try {
ProcessDefinitionDirectory pdd
= workflowService().processDefinitionDirectory();
processManager = pdd.processMgr(pkgId, prcId);
defReqCache = defaultRequester();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Creates a smart process using its process manager and default
* requester.
*
* @return a <code>SmartWfProcess</code> value
* @exception Exception if an error occurs
*/
protected SmartWfProcess createProcess()
throws Exception {
return smart(processManager.createProcess(defReqCache));
}
/**
* Describe <code>removeProcess</code> method here.
*
* @param process a <code>SmartWfProcess</code> value
* @exception Exception if an error occurs
*/
protected void removeProcess(SmartWfProcess process) throws Exception {
ProcessDirectory pd = workflowService().processDirectory();
pd.removeProcess((Process)process.getWfProcess());
}
/**
* Describe <code>smart</code> method here.
*
* @param activity a <code>WfActivity</code> value
* @return a <code>SmartWfActivity</code> value
*/
protected SmartWfActivity smart(WfActivity activity) throws Exception {
return new SmartWfActivity(activity, setStateDirectly);
}
/**
* Describe <code>smart</code> method here.
*
* @param process a <code>WfProcess</code> value
* @return a <code>SmartWfProcess</code> value
*/
protected SmartWfProcess smart(WfProcess process)
throws Exception {
return new SmartWfProcess(process, setStateDirectly);
}
protected void assertState (SmartWfExecutionObject obj, String state) {
for (int i = 0; i < 15; i++) {
if (obj.hasState (state)) {
return;
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
assertTrue (obj.toString() + " should have state "
+ state + ", has " + obj.state(), obj.hasState (state));
}
}