/*
* 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: Subflow.java 2326 2007-03-27 21:59:44Z mlipp $
*
* $Log$
* Revision 1.3 2006/09/29 12:32:07 drmlipp
* Consistently using WfMOpen as projct name now.
*
* Revision 1.2 2004/09/10 11:53:15 drmlipp
* Made tests undividually runnable.
*
* Revision 1.1.1.2 2003/12/19 13:01:43 drmlipp
* Updated to 1.1rc1
*
* Revision 1.10 2003/11/05 08:53:24 huaiyang
* use WrappedProcess.
*
* Revision 1.9 2003/10/21 21:00:45 lipp
* Moved EJBClientTest to new junit sub-package.
*
* Revision 1.8 2003/10/08 12:39:40 huaiyang
* make test weblogic compatible.
*
* Revision 1.7 2003/09/19 13:12:29 lipp
* Adapted to closed.completed having a substate.
*
* Revision 1.6 2003/06/27 09:44:03 lipp
* Fixed copyright/license information.
*
* Revision 1.5 2003/06/04 15:11:50 schlue
* Bug in stateReached fixed.
*
* Revision 1.4 2003/05/14 12:02:26 schlue
* Import of process descriptions changed due to modified import behaviour.
*
* Revision 1.3 2003/05/13 16:56:34 schlue
* Complex subflow completed.
*
* Revision 1.2 2003/05/12 09:00:04 schlue
* PR39 fixed.
* Initial version of complex subflow added.
*
* Revision 1.1 2003/05/08 10:50:28 schlue
* Initial subflow tests added.
*
*
*/
package process;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.rmi.RemoteException;
import de.danet.an.util.junit.EJBClientTest;
import de.danet.an.workflow.omgcore.ProcessData;
import de.danet.an.workflow.omgcore.WfRequester;
import de.danet.an.workflow.api.DefaultRequester;
import de.danet.an.workflow.api.ImportException;
import de.danet.an.workflow.api.PrioritizedMessage;
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 common.WrappedProcess;
import common.WrappedProcessDefinitionDirectory;
import junit.framework.Test;
import junit.framework.TestResult;
import junit.framework.TestSuite;
/**
* Test calling of subprocesses and receiving return values.
* After completion, all previously created processes and process definitions
* are removed.
* @author <a href="mailto:schlueter@danet.de">Holger Schlueter</a>
* @version 1.0
*/
public class Subflow extends WfMOpenTestCase {
/**
* Access to process definition directory (singleton)
*/
private WrappedProcessDefinitionDirectory defDir = null;
/**
* Access to process directory (singleton)
*/
private ProcessDirectory procDir = null;
/**
* Access to default requester (singleton)
*/
private WfRequester requester = null;
/**
* Constructor of this TestCase
* @param name a <code>String</code> value
*/
public Subflow(String name) {
super (name);
}
/**
* Construct this test suite.
* @return a <code>Test</code> value
*/
public static Test suite() {
TestSuite suite = new TestSuite();
/*
suite.addTest(new Subflow("illegalSubRef"));
suite.addTest(new Subflow("simpleSubflow"));
suite.addTest(new Subflow("complexSubflow"));
*/
suite.addTest(new Subflow("complexSubflow"));
return suite;
}
/**
* Runs the tests with an appropriate login context.
*
* @param testResult a <code>TestResult</code> value
*/
public void run(final TestResult testResult) {
EJBClientTest.run (new Runnable () {
public void run () {
Subflow.super.run (testResult);
}
}, plc, testResult);
}
/**
* Test creating a process with a simple subflow, passing through and
* (automatically) removing.
* @exception Exception if an error occurs
*/
public void illegalSubRef() throws Exception {
StringBuffer processDefinition = new StringBuffer();
InputStream is = getClass()
.getResourceAsStream("/process/illegalSubRef.xml");
BufferedReader in = new BufferedReader
(new InputStreamReader(is, "ISO-8859-1"));
String line = null;
while ((line = in.readLine())!= null) {
processDefinition.append(line+"\n");
}
boolean gotException = false;
try {defDir.importProcessDefinitions(processDefinition.toString());
} catch (ImportException exc) {
assertTrue(exc.messages().size() == 1);
assertTrue(((PrioritizedMessage)exc.messages().get(0)).priority()
.equals(PrioritizedMessage.Priority.ERROR));
assertTrue(((PrioritizedMessage)exc.messages().get(0)).unmappedMessage()
.equals("ImportMessages#procdef.activity.subflow.notfound"));
gotException = true;
}
assertTrue(gotException);
}
/**
* Test creating a process with a simple subflow, passing through and
* (automatically) removing.
* @exception Exception if an error occurs
*/
public void simpleSubflow() throws Exception {
importProcessDefinition("/process/subflow.xml");
ProcessMgr mgr = defDir.processMgr("SystemTest", "withSimpleSub");
//WfProcess proc = mgr.createProcess(requester);
WrappedProcess proc = new WrappedProcess(mgr.createProcess(requester));
proc.start();
// Wait for completion
assertTrue(stateReached(proc, "closed.completed"));
ProcessData data = proc.processContext();
assertTrue(((String)data.get("TransitionPath"))
.equals("PATH:act1:sub_act1:act3"));
procDir.removeProcess(proc.getWfProcess());
defDir.removeProcessDefinition("SystemTest", "withSimpleSub");
defDir.removeProcessDefinition("SystemTest", "simpleSub");
}
/**
* Test creating a process with a complex subflow, passing through and
* (automatically) removing.
* @exception Exception if an error occurs
*/
public void complexSubflow() throws Exception {
importProcessDefinition("/process/complexSubflow.xml");
ProcessMgr mgr = defDir.processMgr("SystemTest", "complexSub");
//WfProcess proc = mgr.createProcess(requester);
WrappedProcess proc = new WrappedProcess(mgr.createProcess(requester));
proc.start();
// Wait for completion
assertTrue(stateReached(proc, "closed.completed"));
ProcessData data = proc.processContext();
assertTrue(((String)data.get("sub1Completed"))
.equals("OK"));
assertTrue(((String)data.get("sub2Completed"))
.equals("OK"));
procDir.removeProcess(proc.getWfProcess());
defDir.removeProcessDefinition("SystemTest", "complexSub");
defDir.removeProcessDefinition("SystemTest", "sub1");
defDir.removeProcessDefinition("SystemTest", "sub2");
defDir.removeProcessDefinition("SystemTest", "subsub");
}
/**
* Initialisation.
* The <code>setUp</code> method defines the way a state change is
* realized. Override this method to change this way.
* @exception Exception if an error occurs
*/
protected void setUp() throws Exception {
super.setUp();
WorkflowService wfs = WorkflowServiceFactory.newInstance()
.newWorkflowService();
try {
defDir = new WrappedProcessDefinitionDirectory
(wfs.processDefinitionDirectory());
} catch(RemoteException exc) {
System.err.println("Process definition directory not accessible: "
+ exc.getMessage());
System.exit(-1);
}
procDir = wfs.processDirectory();
requester = new DefaultRequester(wfs);
}
private void importProcessDefinition(String name) throws Exception {
StringBuffer processDefinition = new StringBuffer();
InputStream is = getClass().getResourceAsStream(name);
BufferedReader in = new BufferedReader
(new InputStreamReader(is, "ISO-8859-1"));
String line = null;
while ((line = in.readLine())!= null) {
processDefinition.append(line+"\n");
}
try {defDir.importProcessDefinitions(processDefinition.toString());
} catch (ImportException exc) {
Iterator msg = exc.messages().iterator();
while (msg.hasNext()) {
System.out.println(((PrioritizedMessage)msg.next())
.message());
}
}
}
private boolean stateReached(WrappedProcess proc,
String procState)
throws Exception {
boolean test = true;
boolean stateReached = false;
int maxRetries = 100;
while (test){
if (maxRetries-- > 0) {
System.out.println(proc.state());
if (proc.state().startsWith(procState)) {
stateReached = true;
test = false;
} else {
Thread.sleep(500);
}
} else {
test = false;
}
}
return stateReached;
}
}