/*
* Danet GmbH
* Beratung und Software-Entwicklung
* Gesch�ftstelle AN
*
* $Id: JSTest2.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.2 2004/06/14 19:37:20 lipp
* Fixed assignment functions and cleaned up assignment related
* interfaces.
*
* Revision 1.1 2004/01/28 11:32:55 montag
* additional testcase added.
*
* Revision 1.3 2004/01/27 11:45:33 lipp
* Preserve newlines when reading process definitions.
*
* Revision 1.2 2004/01/26 16:46:48 montag
* jstest now processing input of type SchemaType.
*
* Revision 1.1 2004/01/26 15:11:19 montag
* Tool JSExecutor now returns SchemaType objects.
*
*
*/
package process;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collection;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.dom.DOMResult;
import javax.security.auth.login.LoginException;
import org.w3c.dom.Element;
import org.w3c.dom.Document;
import org.jaxen.XPath;
import org.jaxen.dom.DOMXPath;
import org.jaxen.jdom.JDOMXPath;
import de.danet.an.util.EJBUtil;
import de.danet.an.util.junit.EJBClientTest;
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.omgcore.ProcessData;
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.WorkflowService;
import de.danet.an.workflow.api.WorkflowServiceFactory;
import de.danet.an.workflow.ejbs.admin.ProcessDefinitionDirectoryHome;
import de.danet.an.workflow.spis.ras.ResourceAssignmentService;
import de.danet.an.workflow.util.SAXEventBufferImpl;
import common.UTLoginContext;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Zusammenstellung aller TimerObjectTests.
*/
public class JSTest2 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 JSTest2(String name) {
super (name);
}
/**
* Stellt diese TestSuite zusammen.
*/
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(new JSTest2("importProcessDefinitions"));
suite.addTest(new JSTest2("createProcessAndRemoveProcessDef"));
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
= workflowService().processDefinitionDirectory();
InputStream is = getClass().getResourceAsStream("/process/jstest2.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 and then remove its process definition.
*/
public void createProcessAndRemoveProcessDef() throws Exception {
ProcessDefinitionDirectory pdd = null;
try {
WfRequester cont = new DefaultRequester(workflowService());
Process process = (Process)createProcess("N1", "N2", cont);
process.start();
Thread.sleep(5000);
// get processdata
ProcessData data = process.processContext();
Object value = data.get("result");
if (value != null) {
SAXEventBufferImpl myBuffer
= (SAXEventBufferImpl)value;
TransformerFactory tf
= TransformerFactory.newInstance();
SAXTransformerFactory saxTransFact = null;
if (tf.getFeature(SAXTransformerFactory.FEATURE)) {
saxTransFact = (SAXTransformerFactory)tf;
}
TransformerHandler transHand = null;
transHand = saxTransFact.newTransformerHandler();
StreamResult streamResult = new StreamResult
(new java.io.ByteArrayOutputStream());
transHand.setResult(streamResult);
myBuffer.emit(transHand);
System.out.println(streamResult.getOutputStream().toString());
DOMResult domResult = new DOMResult();
transHand = saxTransFact.newTransformerHandler();
transHand.setResult(domResult);
myBuffer.emit(transHand);
Element returnResult = ((Document)domResult.getNode())
.getDocumentElement();
XPath xpath = new DOMXPath("/BLUB/root/element1");
Element element1
= (Element)xpath.selectSingleNode(returnResult);
String value1 = element1.getAttribute("attr1");
// check testXSLT.xml for the expected result
assertTrue(value1.equals("42"));
xpath = new DOMXPath("/BLUB/root/element2");
Element element2
= (Element)xpath.selectSingleNode(returnResult);
String value2 = element2.getAttribute("attr2");
// check testXSLT.xml for the expected result
assertTrue(value2.equals("66"));
}
// remove its process definition
pdd = workflowService().processDefinitionDirectory ();
ProcessDefinition pd
= pdd.lookupProcessDefinition("N1", "N2");
assertTrue(pd!=null);
pdd.removeProcessDefinition("N1", "N2");
boolean gotEx = false;
try {
pd = pdd.lookupProcessDefinition("N1", "N2");
} catch (Exception ex) {
gotEx = true;
}
assertTrue(gotEx);
// check the process definition of the process
ProcessDefinition procDef = process.processDefinition();
assertTrue(procDef.packageId().equals("N1"));
assertTrue(procDef.processId().equals("N2"));
// import the process definition again.
importProcessDefinitions();
pd = pdd.lookupProcessDefinition("N1", "N2");
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 = JSTest2.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);
}
}