/*
* 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: JellyTests.java 2326 2007-03-27 21:59:44Z mlipp $
*
* $Log$
* Revision 1.2 2006/09/29 12:32:10 drmlipp
* Consistently using WfMOpen as projct name now.
*
* Revision 1.1.1.1 2004/08/18 15:18:47 drmlipp
* Update to 1.2
*
* Revision 1.1 2004/06/30 13:24:17 lipp
* Added jelly test.
*
*/
package process;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import javax.security.auth.login.LoginException;
import org.jaxen.XPath;
import org.jaxen.jdom.JDOMXPath;
import org.jdom.Document;
import org.jdom.input.SAXHandler;
import de.danet.an.util.junit.EJBClientTest;
import de.danet.an.util.sax.SAXEventLogger;
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.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.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;
/**
* This class provides ...
*
* @author <a href="mailto:lipp@danet.de">Michael Lipp</a>
* @version $Revision: 2326 $
*/
public class JellyTests extends TestCase {
private static UTLoginContext plc = null;
static {
try {
plc = new UTLoginContext();
plc.login();
} catch (LoginException e) {
throw new IllegalStateException (e.getMessage ());
}
}
/**
* Creates an instance of <code>JellyTests</code>
* with all attributes initialized to default values.
*/
public JellyTests(String name) {
super (name);
}
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(new JellyTests("importProcessDefinitions"));
suite.addTest(new JellyTests("test1"));
suite.addTest(new JellyTests("removeProcessDefinitions"));
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 {
ProcessDefinitionDirectory pdd = null;
try {
pdd = workflowService().processDefinitionDirectory ();
InputStream is = getClass()
.getResourceAsStream("/process/jellytest.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());
} finally {
workflowService().release (pdd);
}
}
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);
}
}
/**
* Run test.
*/
public void test1() throws Exception {
ProcessDirectory pd = null;
try {
pd = workflowService().processDirectory ();
WfRequester cont = new DefaultRequester(workflowService());
Process process = (Process)createProcess
("jelly-test", "jelly-test1", cont);
process.start();
assertTrue (stateReached (process, "closed.completed"));
SAXEventBuffer res = (SAXEventBuffer)
process.processContext().get ("result");
SAXHandler sh = new SAXHandler ();
res.emit (sh);
Document resDoc = sh.getDocument();
XPath xpath = new JDOMXPath("/result/inserted/root/element1/@attr1");
String val = xpath.stringValueOf(resDoc);
assertTrue ("Value is: " + val, val.equals ("42"));
xpath = new JDOMXPath("/result/test");
val = xpath.stringValueOf(resDoc);
assertTrue ("Value is: " + val, val.equals ("42"));
xpath = new JDOMXPath("count(/result/test)");
val = xpath.stringValueOf(resDoc);
assertTrue ("Value is: " + val, val.equals ("5"));
pd.removeProcess(process);
} finally {
workflowService().release (pd);
}
}
/**
* Remove its process definition.
*/
public void removeProcessDefinitions() throws Exception {
ProcessDefinitionDirectory pdd = null;
try {
pdd = workflowService().processDefinitionDirectory ();
for (Iterator i = pdd.processDefinitions().iterator ();
i.hasNext ();) {
ProcessDefinition pd = (ProcessDefinition)i.next ();
if (pd.packageId().equals ("jelly-test")) {
pdd.removeProcessDefinition
(pd.packageName(), pd.processName());
}
}
} finally {
workflowService().release (pdd);
}
}
private boolean stateReached(WfProcess proc, String procState)
throws Exception {
int maxRetries = 30;
while (true){
if (proc.state().startsWith(procState)) {
return true;
}
if (maxRetries-- <= 0) {
return false;
}
Thread.sleep(1000);
}
}
}