001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2005 Danet GmbH (www.danet.de), BU TEL.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * $Id: Cleanup.java,v 1.3 2006/09/29 12:32:10 drmlipp Exp $
021: *
022: * $Log: Cleanup.java,v $
023: * Revision 1.3 2006/09/29 12:32:10 drmlipp
024: * Consistently using WfMOpen as projct name now.
025: *
026: * Revision 1.2 2005/04/22 15:11:07 drmlipp
027: * Merged changes from 1.3 branch up to 1.3p15.
028: *
029: * Revision 1.1.2.1 2005/04/13 16:13:24 drmlipp
030: * Added.
031: *
032: */
033: package process;
034:
035: import java.util.Iterator;
036:
037: import java.rmi.RemoteException;
038:
039: import javax.security.auth.login.LoginException;
040:
041: import de.danet.an.util.junit.EJBClientTest;
042:
043: import de.danet.an.workflow.omgcore.WfActivity;
044: import de.danet.an.workflow.omgcore.WfExecutionObject;
045: import de.danet.an.workflow.omgcore.WfExecutionObject.State;
046:
047: import de.danet.an.workflow.api.FactoryConfigurationError;
048: import de.danet.an.workflow.api.Process;
049: import de.danet.an.workflow.api.ProcessDirectory;
050: import de.danet.an.workflow.api.WorkflowService;
051: import de.danet.an.workflow.api.WorkflowServiceFactory;
052:
053: import common.UTLoginContext;
054: import junit.framework.Test;
055: import junit.framework.TestCase;
056: import junit.framework.TestSuite;
057:
058: /**
059: * Testing audit event handling. This test suite does not make any senseful
060: * things, rather than forcing the creation of several audit events in order
061: * to test that they've been created and written the appropriate database
062: * tables.
063: */
064: public class Cleanup extends TestCase {
065: private static UTLoginContext plc = null;
066: static {
067: try {
068: plc = new UTLoginContext();
069: plc.login();
070: } catch (LoginException e) {
071: throw new IllegalStateException(e.getMessage());
072: }
073: }
074:
075: private static WorkflowService wfsCache = null;
076:
077: private WorkflowService workflowService() {
078: if (wfsCache == null) {
079: try {
080: WorkflowServiceFactory wfsf = WorkflowServiceFactory
081: .newInstance();
082: wfsCache = wfsf.newWorkflowService();
083: } catch (FactoryConfigurationError e) {
084: throw new IllegalStateException(e.getMessage());
085: }
086: }
087: return wfsCache;
088: }
089:
090: /* ********************************************************************* */
091: /* Test suite */
092: /* ********************************************************************* */
093:
094: /**
095: * Constructor of this TestCase
096: * @param name the name of the test case
097: */
098: public Cleanup(String name) {
099: super (name);
100: }
101:
102: /**
103: * The definition of the test suite. There's one test case per event type.
104: * However, not all test cases are currently doing something.
105: * @return the test suite
106: */
107: public static Test suite() {
108: TestSuite suite = new TestSuite();
109: suite.addTest(new Cleanup("cleanup"));
110: return new EJBClientTest(plc, suite);
111: }
112:
113: /* ********************************************************************* */
114: /* Test cases */
115: /* ********************************************************************* */
116:
117: /**
118: * Test case
119: * @throws Exception ...
120: */
121: public void cleanup() throws Exception {
122:
123: ProcessDirectory pd = workflowService().processDirectory();
124: for (Iterator i = pd.processes().iterator(); i.hasNext();) {
125: Process proc = (Process) i.next();
126: boolean done = false;
127: for (int count = 5; count > 0; count--) {
128: try {
129: handleProcess(pd, proc);
130: done = true;
131: break;
132: } catch (RemoteException e) {
133: }
134: }
135: if (!done) {
136: System.out.println("Failed to remove " + proc);
137: } else {
138: System.out.println("Removed " + proc);
139: }
140: }
141: }
142:
143: private void handleProcess(ProcessDirectory pd, Process proc)
144: throws Exception {
145: if (proc.workflowState() == State.OPEN) {
146: for (Iterator j = proc.validStates().iterator(); j
147: .hasNext();) {
148: if (((String) j.next()).startsWith("closed.terminated")) {
149: Iterator sa = proc.activitiesInState(
150: "open.not_running.suspended").iterator();
151: if (!sa.hasNext()) {
152: proc.terminate();
153: } else {
154: WfActivity act = (WfActivity) sa.next();
155: act.abort();
156: }
157: if (!waitForState(proc, "closed")) {
158: System.out.println("Cannot terminate " + proc);
159: return;
160: }
161: }
162: break;
163: }
164: if (proc.workflowState() == State.OPEN) {
165: proc.suspend();
166: if (!waitForState(proc, "open.not_running.suspended")) {
167: System.out.println("Cannot suspend " + proc);
168: return;
169: }
170: proc.abort();
171: if (!waitForState(proc, "closed")) {
172: System.out.println("Cannot abort " + proc);
173: return;
174: }
175: }
176: }
177: pd.removeProcess(proc);
178: }
179:
180: /* ********************************************************************* */
181: /* HELPER methods */
182: /* ********************************************************************* */
183:
184: private boolean waitForState(WfExecutionObject exObj, String state)
185: throws RemoteException {
186: for (int count = 10; count > 0; count--) {
187: try {
188: Thread.sleep(500);
189: } catch (InterruptedException e) {
190: }
191: if (exObj.state().startsWith(state)) {
192: return true;
193: }
194: }
195: return false;
196: }
197:
198: }
|