/*
* This file is part of the WfMOpen project.
* Copyright (C) 2005 Danet GmbH (www.danet.de), BU TEL.
* 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: Cleanup.java 2550 2007-10-22 14:00:54Z $
*
* $Log$
* Revision 1.2 2005/04/22 15:11:07 drmlipp
* Merged changes from 1.3 branch up to 1.3p15.
*
* Revision 1.1.2.1 2005/04/13 16:13:24 drmlipp
* Added.
*
*/
package process;
import java.util.Iterator;
import java.rmi.RemoteException;
import javax.security.auth.login.LoginException;
import de.danet.an.util.junit.EJBClientTest;
import de.danet.an.workflow.omgcore.WfActivity;
import de.danet.an.workflow.omgcore.WfExecutionObject;
import de.danet.an.workflow.omgcore.WfExecutionObject.State;
import de.danet.an.workflow.api.FactoryConfigurationError;
import de.danet.an.workflow.api.Process;
import de.danet.an.workflow.api.ProcessDirectory;
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;
/**
* Testing audit event handling. This test suite does not make any senseful
* things, rather than forcing the creation of several audit events in order
* to test that they've been created and written the appropriate database
* tables.
*/
public class Cleanup extends TestCase {
private static UTLoginContext plc = null;
static {
try {
plc = new UTLoginContext();
plc.login();
} catch (LoginException e) {
throw new IllegalStateException (e.getMessage ());
}
}
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;
}
/* ********************************************************************* */
/* Test suite */
/* ********************************************************************* */
/**
* Constructor of this TestCase
* @param name the name of the test case
*/
public Cleanup(String name) {
super (name);
}
/**
* The definition of the test suite. There's one test case per event type.
* However, not all test cases are currently doing something.
* @return the test suite
*/
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest (new Cleanup("cleanup"));
return new EJBClientTest (plc, suite);
}
/* ********************************************************************* */
/* Test cases */
/* ********************************************************************* */
/**
* Test case
* @throws Exception ...
*/
public void cleanup () throws Exception {
ProcessDirectory pd = workflowService().processDirectory ();
for (Iterator i = pd.processes().iterator (); i.hasNext ();) {
Process proc = (Process)i.next ();
boolean done = false;
for (int count = 5; count > 0; count--) {
try {
handleProcess (pd, proc);
done = true;
break;
} catch (RemoteException e) {
}
}
if (!done) {
System.out.println ("Failed to remove " + proc);
} else {
System.out.println ("Removed " + proc);
}
}
}
private void handleProcess (ProcessDirectory pd, Process proc)
throws Exception {
if (proc.workflowState() == State.OPEN) {
for (Iterator j = proc.validStates().iterator (); j.hasNext ();) {
if (((String)j.next()).startsWith("closed.terminated")) {
Iterator sa = proc.activitiesInState
("open.not_running.suspended").iterator();
if (!sa.hasNext ()) {
proc.terminate();
} else {
WfActivity act = (WfActivity)sa.next ();
act.abort();
}
if (!waitForState (proc, "closed")) {
System.out.println ("Cannot terminate " + proc);
return;
}
}
break;
}
if (proc.workflowState() == State.OPEN) {
proc.suspend();
if (!waitForState (proc, "open.not_running.suspended")) {
System.out.println ("Cannot suspend " + proc);
return;
}
proc.abort();
if (!waitForState (proc, "closed")) {
System.out.println ("Cannot abort " + proc);
return;
}
}
}
pd.removeProcess(proc);
}
/* ********************************************************************* */
/* HELPER methods */
/* ********************************************************************* */
private boolean waitForState (WfExecutionObject exObj, String state)
throws RemoteException {
for (int count = 10; count > 0; count--) {
try {
Thread.sleep (500);
} catch (InterruptedException e) {
}
if (exObj.state().startsWith (state)) {
return true;
}
}
return false;
}
}