/*
* 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: Transition.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.21 2003/10/21 21:00:45 lipp
* Moved EJBClientTest to new junit sub-package.
*
* Revision 1.20 2003/10/08 12:39:40 huaiyang
* make test weblogic compatible.
*
* Revision 1.19 2003/09/19 15:01:34 schlue
* Initial deadline test added (not working yet).
*
* Revision 1.18 2003/09/19 13:12:29 lipp
* Adapted to closed.completed having a substate.
*
* Revision 1.17 2003/06/29 18:41:19 lipp
* Minor fixes.
*
* Revision 1.16 2003/06/27 09:44:03 lipp
* Fixed copyright/license information.
*
* Revision 1.15 2003/06/03 14:50:12 schlue
* Test case for AND split with OTHERWISE condition added.
*
* Revision 1.14 2003/06/03 11:58:27 schlue
* PR56 fixed.
*
* Revision 1.13 2003/06/02 13:27:16 lipp
* Fixed PR35 (user error).
*
* Revision 1.12 2003/05/31 18:30:32 lipp
* Fixed test case.
*
* Revision 1.11 2003/05/16 14:13:12 schlue
* Cascade completed (still not running as intended)
*
* Revision 1.10 2003/05/16 10:45:55 schlue
* Loop test updated (still not running correctly)
*
* Revision 1.9 2003/05/15 11:23:42 schlue
* Added initial cascade test (still incomplete).
*
* Revision 1.8 2003/05/15 07:36:01 schlue
* Added first loop test.
*
* Revision 1.7 2003/05/14 12:02:26 schlue
* Import of process descriptions changed due to modified import behaviour.
*
* Revision 1.6 2003/05/08 10:50:28 schlue
* Initial subflow tests added.
*
* Revision 1.5 2003/05/05 14:39:50 lipp
* Moved code for removing process automatically to event handling.
*
* Revision 1.4 2003/04/17 14:13:28 schlue
* More complex transition test added.
*
* Revision 1.3 2003/04/16 19:58:49 lipp
* Adapted to jdk 1.4
*
* Revision 1.2 2003/04/16 14:06:50 schlue
* Transition path test added.
*
* Revision 1.1 2003/04/16 09:43:32 schlue
* Test cases for transition added.
*
*
*/
package process;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.List;
import java.rmi.RemoteException;
import de.danet.an.util.junit.EJBClientTest;
import de.danet.an.workflow.omgcore.ProcessData;
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.DefaultRequester;
import de.danet.an.workflow.api.ImportException;
import de.danet.an.workflow.api.InvalidKeyException;
import de.danet.an.workflow.api.PrioritizedMessage;
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 junit.framework.Test;
import junit.framework.TestResult;
import junit.framework.TestSuite;
/**
* Test subject area of "transition" of processes.
* This includes conditions, complex split and join transition conditions,
* return values, etc.
* 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 Transition extends WfMOpenTestCase {
/**
* Access to process definition directory (singleton)
*/
private ProcessDefinitionDirectory 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 Transition(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 Transition("testRun"));
suite.addTest(new Transition("testPath"));
suite.addTest(new Transition("testLoop"));
suite.addTest(new Transition("testCascade"));
suite.addTest(new Transition("testScrabble"));
suite.addTest(new Transition("testAndSplitOtherwise"));
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 () {
Transition.super.run (testResult);
}
}, plc, testResult);
}
/**
* Test creating a process, passing through and (automatically) removing.
* @exception Exception if an error occurs
*/
public void testRun() throws Exception {
List msgList = importProcessDefinition("/process/minimal.xml");
ProcessMgr mgr = defDir.processMgr("SystemTest", "stopOnStart");
WfProcess proc = mgr.createProcess(requester);
String procKey = proc.key();
proc.start();
assertTrue(stateReached(proc, "open.running.running",
"open.not_running.suspended.suspended"));
Activity act = (Activity)proc.steps().toArray()[0];
act.resume();
assertTrue(stateReached(proc, "closed.completed",
"closed.completed"));
// Assure that process has to be removed manually
assertTrue(mgr.processByKey(procKey) != null);
procDir.removeProcess(proc);
boolean processRemoved = false;
try {
mgr.processByKey(procKey);
} catch (InvalidKeyException exc) {
processRemoved = true;
}
assertTrue(processRemoved);
mgr = defDir.processMgr("SystemTest", "stopOnFinish");
proc = mgr.createProcess(requester);
procKey = proc.key();
proc.start();
assertTrue(stateReached(proc, "open.running.running",
"open.not_running.suspended.suspended"));
act = (Activity)proc.steps().toArray()[0];
act.resume();
assertTrue(stateReached(proc, "closed.completed",
"closed.completed"));
// Assure that process has to be removed manually
assertTrue(mgr.processByKey(procKey) != null);
procDir.removeProcess(proc);
processRemoved = false;
try {
mgr.processByKey(procKey);
} catch (InvalidKeyException exc) {
processRemoved = true;
}
assertTrue(processRemoved);
mgr = defDir.processMgr("SystemTest", "completeNoRemoval");
proc = mgr.createProcess(requester);
procKey = proc.key();
proc.start();
assertTrue(stateReached(proc, "closed.completed",
"closed.completed"));
// Assure that process has to be removed manually
assertTrue(mgr.processByKey(procKey) != null);
procDir.removeProcess(proc);
processRemoved = false;
try {
mgr.processByKey(procKey);
} catch (InvalidKeyException exc) {
processRemoved = true;
}
assertTrue(processRemoved);
mgr = defDir.processMgr("SystemTest", "completeRemoval");
proc = mgr.createProcess(requester);
procKey = proc.key();
proc.start();
// Assure that process has been removed automatically
processRemoved = false;
int maxRetries = 5;
boolean test = true;
while (test){
if (maxRetries-- > 0) {
try {
mgr.processByKey(procKey);
Thread.sleep(1000);
} catch (InvalidKeyException exc) {
processRemoved = true;
test = false;
}
} else {
test = false;
}
}
assertTrue(processRemoved);
// Imported process definitions will be removed automatically by import
// within next test case!
}
/**
* Test the transition path when executing processes.
* @exception Exception if an error occurs
*/
public void testPath() throws Exception {
List msgList = importProcessDefinition("/process/transition1.xml");
ProcessMgr mgr = defDir.processMgr("SystemTest", "simplePath");
WfProcess proc = mgr.createProcess(requester);
proc.start();
// Wait for completion
assertTrue(stateReached(proc, "closed.completed",
"closed.completed"));
ProcessData data = proc.processContext();
assertTrue(((String)data.get("TransitionPath"))
.equals("PATH:act1:act2:act3"));
procDir.removeProcess(proc);
mgr = defDir.processMgr("SystemTest", "block_2_in_2_out");
proc = mgr.createProcess(requester);
proc.start();
// Wait for completion
assertTrue("Process not completed in time",
stateReached(proc, "closed.completed",
"closed.completed"));
data = proc.processContext();
assertTrue(((String)data.get("ConfirmA3")).equals("OK"));
assertTrue(((String)data.get("ConfirmA4")).equals("OK"));
procDir.removeProcess(proc);
defDir.removeProcessDefinition("SystemTest", "simplePath");
defDir.removeProcessDefinition("SystemTest", "block_2_in_2_out");
}
/**
* Test conditional looping within a process.
* @exception Exception if an error occurs
*/
public void testLoop() throws Exception {
StringBuffer processDefinition = new StringBuffer();
// Read correct process definition
InputStream is = getClass()
.getResourceAsStream("/process/loopNoStart.xml");
BufferedReader in = new BufferedReader
(new InputStreamReader(is, "ISO-8859-1"));
String line = null;
while ((line = in.readLine())!= null) {
processDefinition.append(line+"\n");
}
try {
List msg = defDir.importProcessDefinitions(processDefinition.toString());
assertTrue(msg.size() == 1);
assertTrue(((PrioritizedMessage)msg.get(0)).priority()
.equals(PrioritizedMessage.Priority.WARN));
assertTrue(((PrioritizedMessage)msg.get(0)).unmappedMessage()
.equals("ImportMessages#procdef.transition.noentry"));
} catch (ImportException exc) {
Iterator msg = exc.messages().iterator();
while (msg.hasNext()) {
System.out.println(((PrioritizedMessage)msg.next()).unmappedMessage());
}
}
List msgList = importProcessDefinition("/process/loop.xml");
assertTrue(msgList.size() == 0);
ProcessMgr mgr = defDir.processMgr("SystemTest", "loop_3_times");
WfProcess proc = mgr.createProcess(requester);
proc.start();
// Wait for completion
assertTrue(stateReached(proc, "closed.completed"));
ProcessData data = proc.processContext();
assertTrue(((Long)data.get("Counter")).intValue() == 3);
procDir.removeProcess(proc);
defDir.removeProcessDefinition("SystemTest", "loop_3_times");
}
/**
* Test conditions by use of a cascading process.
* @exception Exception if an error occurs
*/
public void testCascade() throws Exception {
// Uses XPDL imported by testLoop
ProcessMgr mgr = defDir.processMgr("SystemTest", "cascade");
WfProcess proc = mgr.createProcess(requester);
proc.start();
// Wait for completion
assertTrue(stateReached(proc, "closed.completed"));
ProcessData data = proc.processContext();
assertTrue(((Boolean)data.get("completed"))
.equals(Boolean.TRUE));
procDir.removeProcess(proc);
defDir.removeProcessDefinition("SystemTest", "cascade");
}
/**
* Test conditions by use of a cascading process.
* @exception Exception if an error occurs
*/
public void testScrabble() throws Exception {
// Uses XPDL imported by testLoop
ProcessMgr mgr = defDir.processMgr("SystemTest", "scrabble");
WfProcess proc = mgr.createProcess(requester);
proc.start();
// Wait for completion
assertTrue(stateReached(proc, "closed.completed"));
ProcessData data = proc.processContext();
assertTrue(((String)data.get("compound")).equals("DANET"));
procDir.removeProcess(proc);
defDir.removeProcessDefinition("SystemTest", "cascade");
}
/**
* Test correct handling of OTHERWISE case within an AND split.
* @exception Exception if an error occurs
*/
public void testAndSplitOtherwise() throws Exception {
// Uses XPDL imported by testLoop
ProcessMgr mgr = defDir.processMgr("SystemTest", "andOtherwiseTest");
WfProcess proc = mgr.createProcess(requester);
proc.start();
// Wait for completion
assertTrue(stateReached(proc, "closed.completed"));
ProcessData data = proc.processContext();
assertTrue(((Boolean)data.get("Success")).equals(Boolean.TRUE));
assertTrue(((Boolean)data.get("Error")).equals(Boolean.FALSE));
procDir.removeProcess(proc);
defDir.removeProcessDefinition("SystemTest", "andOtherwiseTest");
}
/**
* 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 = 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 List importProcessDefinition(String name) throws Exception {
StringBuffer processDefinition = new StringBuffer();
List msgList = null;
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 {msgList = defDir.importProcessDefinitions(processDefinition.toString());
} catch (ImportException exc) {
msgList = exc.messages();
Iterator msg = msgList.iterator();
while (msg.hasNext()) {
System.out.println(((PrioritizedMessage)msg.next())
.message());
}
}
return msgList;
}
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(1000);
}
} else {
test = false;
}
}
return stateReached;
}
private boolean stateReached(WfProcess proc,
String procState,
String actState)
throws Exception {
Activity act = (Activity)proc.steps().toArray()[0];
boolean test = true;
boolean stateReached = false;
int maxRetries = 10;
while (test){
if (maxRetries-- > 0) {
if (proc.state().startsWith(procState)
&& act.state().startsWith(actState)) {
stateReached = true;
test = false;
} else {
Thread.sleep(1000);
}
} else {
test = false;
}
}
return stateReached;
}
}