/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2004 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: JSTests.java 1934 2006-11-19 21:53:47Z mlipp $
*
* $Log$
* Revision 1.3 2006/09/29 12:32:07 drmlipp
* Consistently using WfMOpen as projct name now.
*
* Revision 1.2 2006/03/08 14:46:42 drmlipp
* Synchronized with 1.3.3p5.
*
* Revision 1.1.1.1.6.1 2006/03/02 15:30:59 drmlipp
* Added E4X test.
*
* Revision 1.1.1.1 2004/08/18 15:18:47 drmlipp
* Update to 1.2
*
* Revision 1.1 2004/03/31 19:30:33 lipp
* Improved test.
*
*/
package tools;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import javax.security.auth.login.LoginException;
import org.w3c.dom.Node;
import de.danet.an.util.junit.EJBClientTest;
import de.danet.an.util.sax.SAXContentBuffer;
import de.danet.an.workflow.omgcore.ProcessData;
import de.danet.an.workflow.omgcore.WfActivity;
import de.danet.an.workflow.omgcore.WfProcess;
import de.danet.an.workflow.omgcore.WfRequester;
import de.danet.an.workflow.api.Activity;
import de.danet.an.workflow.api.DefaultProcessData;
import de.danet.an.workflow.api.DefaultRequester;
import de.danet.an.workflow.api.FactoryConfigurationError;
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.SAXEventBuffer;
import de.danet.an.workflow.api.WorkflowService;
import de.danet.an.workflow.api.WorkflowServiceFactory;
import common.UTLoginContext;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Test JS
*/
public class JSTests extends TestCase {
private static UTLoginContext plc = null;
static {
try {
plc = new UTLoginContext();
plc.login();
} catch (LoginException e) {
throw new IllegalStateException (e.getMessage ());
}
}
/**
* A process directory reference.
*/
private ProcessDirectory pdd = null;
/**
* Constructor of this TestCase
*/
public JSTests(String name) {
super (name);
}
/**
* Stellt diese TestSuite zusammen.
*/
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(new JSTests("importProcessDefinitions"));
suite.addTest(new JSTests("testAbandon"));
suite.addTest(new JSTests("testReplacer"));
suite.addTest(new JSTests("testAUK"));
suite.addTest(new JSTests("testJavaArg1"));
suite.addTest(new JSTests("testJavaArg2"));
return new EJBClientTest (plc, suite);
}
private WorkflowService workflowService = null;
/**
* Initialisierung.
*/
protected void setUp() throws Exception {
try {
WorkflowServiceFactory wfsf
= WorkflowServiceFactory.newInstance ();
workflowService = wfsf.newWorkflowService();
} catch (FactoryConfigurationError e) {
throw new IllegalStateException (e.getMessage());
}
}
protected void tearDown() throws Exception {
workflowService.release (workflowService);
workflowService = null;
}
/**
* 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("/tools/jstests.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);
}
/**
* Test.
*/
public void testAbandon() throws Exception {
ProcessDefinitionDirectory procDefDir = null;
ProcessDirectory procDir = null;
try {
procDefDir = workflowService.processDefinitionDirectory();
procDir = workflowService.processDirectory();
ProcessMgr pmgr = procDefDir.processMgr ("jstests", "testAbandon");
WfProcess process
= pmgr.createProcess(new DefaultRequester (workflowService));
process.start();
assertTrue (stateReached (process, "closed.completed"));
for (Iterator i = process.activitiesInState("closed").iterator ();
i.hasNext ();) {
WfActivity act = (Activity)i.next ();
if (act.name().equals ("Run JS")) {
assertTrue (act.state().equals
("closed.completed.abandoned"));
} else if (act.name().equals ("Handle Exception")) {
assertTrue (act.state().equals
("closed.completed.normal"));
} else {
assertTrue (act.name (), false);
}
}
procDir.removeProcess(process);
} finally {
workflowService.release (procDefDir);
workflowService.release (procDir);
}
}
/**
* Test.
*/
public void testReplacer() throws Exception {
ProcessDefinitionDirectory procDefDir = null;
ProcessDirectory procDir = null;
try {
procDefDir = workflowService.processDefinitionDirectory();
procDir = workflowService.processDirectory();
ProcessMgr pmgr = procDefDir.processMgr ("jstests", "testReplacer");
WfProcess process
= pmgr.createProcess(new DefaultRequester (workflowService));
process.start();
assertTrue (stateReached (process, "closed.completed"));
SAXContentBuffer xmlRes = (SAXContentBuffer)
process.processContext().get("XMLResult");
Node resRoot = xmlRes.toW3cDom();
Node fc = resRoot.getFirstChild();
assertTrue (fc.toString(), fc.getLocalName().equals("Item"));
assertTrue (fc.getAttributes().getNamedItem("Id")
.getNodeValue().equals("Start"));
assertTrue (process.processContext().get("Result").equals("Test"));
procDir.removeProcess(process);
} finally {
workflowService.release (procDefDir);
workflowService.release (procDir);
}
}
/**
* Test.
*/
public void testAUK() throws Exception {
ProcessDefinitionDirectory procDefDir = null;
ProcessDirectory procDir = null;
try {
procDefDir = workflowService.processDefinitionDirectory();
procDir = workflowService.processDirectory();
ProcessMgr pmgr = procDefDir.processMgr ("jstests", "testAUK");
WfProcess process
= pmgr.createProcess(new DefaultRequester (workflowService));
process.start();
assertTrue (stateReached (process, "closed.completed"));
String s = (String)process.processContext().get("Result");
assertTrue (s.startsWith("jstests/testAUK "));
procDir.removeProcess(process);
} finally {
workflowService.release (procDefDir);
workflowService.release (procDir);
}
}
/**
* Test.
*/
public void testJavaArg1() throws Exception {
ProcessDefinitionDirectory procDefDir = null;
ProcessDirectory procDir = null;
try {
procDefDir = workflowService.processDefinitionDirectory();
procDir = workflowService.processDirectory();
ProcessMgr pmgr = procDefDir.processMgr ("jstests", "testJavaArg1");
WfProcess process
= pmgr.createProcess(new DefaultRequester (workflowService));
process.start();
assertTrue (stateReached (process, "closed.completed"));
List l = (List)process.processContext().get("Result");
assertTrue (l.size() == 1);
assertTrue (((String)l.get(0)).equals("Hallo"));
procDir.removeProcess(process);
} finally {
workflowService.release (procDefDir);
workflowService.release (procDir);
}
}
/**
* Test.
*/
public void testJavaArg2() throws Exception {
ProcessDefinitionDirectory procDefDir = null;
ProcessDirectory procDir = null;
try {
procDefDir = workflowService.processDefinitionDirectory();
procDir = workflowService.processDirectory();
ProcessMgr pmgr = procDefDir.processMgr ("jstests", "testJavaArg2");
WfProcess process
= pmgr.createProcess(new DefaultRequester (workflowService));
ProcessData data = new DefaultProcessData ();
List l = new ArrayList();
data.put("Argument", l);
l.add ("Text:");
process.setProcessContext(data);
process.start();
assertTrue (stateReached (process, "closed.completed"));
l = (List)process.processContext().get("Result");
assertTrue (l.size() == 2);
assertTrue (((String)l.get(1)).equals("Hallo"));
procDir.removeProcess(process);
} finally {
workflowService.release (procDefDir);
workflowService.release (procDir);
}
}
private WfProcess createProcess
(String pkgId, String prcId, WfRequester req)
throws Exception {
ProcessDefinitionDirectory procDir = null;
try {
procDir = workflowService.processDefinitionDirectory();
ProcessMgr pmgr = procDir.processMgr(pkgId, prcId);
return pmgr.createProcess (req);
} finally {
workflowService.release (procDir);
}
}
private boolean stateReached(WfProcess proc, String procState)
throws Exception {
boolean test = true;
boolean stateReached = false;
int maxRetries = 100;
while (test){
if (maxRetries-- > 0) {
if (proc.state().startsWith(procState)) {
stateReached = true;
test = false;
} else {
Thread.sleep(500);
}
} else {
test = false;
}
}
return stateReached;
}
private boolean stateReached(WfActivity act, String actState)
throws Exception {
boolean test = true;
boolean stateReached = false;
int maxRetries = 100;
while (test){
if (maxRetries-- > 0) {
if (act.state().startsWith(actState)) {
stateReached = true;
test = false;
} else {
Thread.sleep(500);
}
} else {
test = false;
}
}
return stateReached;
}
}