/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2005 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: DeferredChoice.java 2326 2007-03-27 21:59:44Z mlipp $
*
* $Log$
* Revision 1.4 2006/09/29 12:32:08 drmlipp
* Consistently using WfMOpen as projct name now.
*
* Revision 1.3 2005/02/23 15:43:20 drmlipp
* Synchronized with 1.3.
*
* Revision 1.1.2.4 2005/02/04 15:54:38 drmlipp
* Fixed race condition.
*
* Revision 1.1.2.3 2005/02/04 15:28:39 drmlipp
* Fixed import.
*
* Revision 1.2 2005/02/04 14:25:27 drmlipp
* Synchronized with 1.3rc2.
*
* Revision 1.1.2.2 2005/02/04 10:43:42 drmlipp
* Added more tests.
*
* Revision 1.1.2.1 2005/02/03 20:55:54 drmlipp
* Added test.
*
*/
package process;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.rmi.RemoteException;
import java.util.Iterator;
import junit.framework.Test;
import junit.framework.TestSuite;
import de.danet.an.util.junit.EJBClientTest;
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.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 de.danet.an.workflow.omgcore.WfProcess;
import de.danet.an.workflow.omgcore.WfRequester;
import de.danet.an.workflow.omgcore.ProcessData;
/**
* Test of DeferredChoices within workflow processes.
* After completion, all previously created processes and process definitions
* are removed.
* @version 1.0
*/
public class DeferredChoice 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 DeferredChoice(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 DeferredChoice("loopTest"));
suite.addTest(new DeferredChoice("terminateProcessTest"));
suite.addTest(new DeferredChoice("terminateActivityTest"));
return new EJBClientTest (plc, suite);
}
/**
* Test.
* @exception Exception if an error occurs
*/
public void loopTest() throws Exception {
ProcessMgr mgr = defDir.processMgr
("deferredChoiceTests", "loopingDeferredChoiceTest");
WfProcess proc = mgr.createProcess(requester);
String procKey = proc.key();
proc.start();
assertTrue(stateReached(proc, "closed.completed"));
ProcessData data = proc.processContext();
String path = (String)data.get("Path");
assertTrue(path, path.equals("Path:ACT0:ACT1:ACT2:ACT5:ACT1:ACT2:ACT5"
+ ":ACT1:ACT3:ACT5:ACT1:ACT3:ACT5"));
procDir.removeProcess(proc);
}
/**
* Test.
* @exception Exception if an error occurs
*/
public void terminateProcessTest() throws Exception {
ProcessMgr mgr = defDir.processMgr
("deferredChoiceTests", "terminateTest");
WfProcess proc = mgr.createProcess(requester);
String procKey = proc.key();
proc.start();
assertTrue(stateReached(proc, "open.running"));
proc.terminate ();
assertTrue(stateReached(proc, "closed.terminated"));
procDir.removeProcess(proc);
}
/**
* Test.
* @exception Exception if an error occurs
*/
public void terminateActivityTest() throws Exception {
ProcessMgr mgr = defDir.processMgr
("deferredChoiceTests", "terminateTest");
WfProcess proc = mgr.createProcess(requester);
String procKey = proc.key();
proc.start();
Activity act2 = null;
Activity act3 = null;
assertTrue(stateReached(proc, "open.running"));
for (Iterator i = proc.steps().iterator (); i.hasNext ();) {
Activity a = (Activity)i.next ();
if (a.name().equals ("ACT2")) {
act2 = a;
} else if (a.name().equals ("ACT3")) {
act3 = a;
}
}
assertTrue (act2 != null);
assertTrue (act3 != null);
assertTrue(stateReached(act2, "open.running"));
assertTrue(stateReached(act3, "open.running"));
act2.terminate ();
assertTrue(stateReached(act2, "closed.terminated"));
assertTrue(stateReached(act3, "open.not_running.not_started"));
assertTrue(stateReached(proc, "closed.terminated"));
procDir.removeProcess(proc);
}
/**
* 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);
importProcessDefinition("/process/deferredChoice.xml");
}
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(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(Activity 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;
}
}