001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2003 Danet GmbH (www.danet.de), GS-AN.
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: GenericTest.java,v 1.2 2006/09/29 12:32:07 drmlipp Exp $
021: *
022: * $Log: GenericTest.java,v $
023: * Revision 1.2 2006/09/29 12:32:07 drmlipp
024: * Consistently using WfMOpen as projct name now.
025: *
026: * Revision 1.1.1.2 2004/08/18 15:17:39 drmlipp
027: * Update to 1.2
028: *
029: * Revision 1.12 2004/01/27 11:45:32 lipp
030: * Preserve newlines when reading process definitions.
031: *
032: * Revision 1.11 2003/11/20 17:25:17 weidauer
033: * made cleaning operations static
034: *
035: * Revision 1.10 2003/11/04 16:49:33 weidauer
036: * added initializing of process relevant data
037: *
038: * Revision 1.9 2003/10/30 16:22:51 weidauer
039: * added cleaning suite
040: *
041: * Revision 1.8 2003/10/30 10:58:28 weidauer
042: * fixed removeAllProcesses
043: *
044: * Revision 1.7 2003/10/28 07:54:05 barzik
045: * *** empty log message ***
046: *
047: * Revision 1.6 2003/10/22 16:10:06 weidauer
048: * initial version
049: *
050: */
051: package load;
052:
053: import junit.framework.Test;
054: import junit.framework.TestCase;
055: import junit.framework.TestSuite;
056:
057: import java.util.Iterator;
058: import java.util.Collection;
059: import java.io.BufferedReader;
060: import java.io.InputStream;
061: import java.io.InputStreamReader;
062: import java.rmi.RemoteException;
063: import java.rmi.NoSuchObjectException;
064:
065: import javax.security.auth.login.LoginException;
066:
067: import common.STProjectLoginContext;
068:
069: import de.danet.an.util.junit.EJBClientTest;
070:
071: import de.danet.an.workflow.api.DefaultRequester;
072: import de.danet.an.workflow.api.FactoryConfigurationError;
073: import de.danet.an.workflow.api.Process;
074: import de.danet.an.workflow.api.ProcessDefinitionDirectory;
075: import de.danet.an.workflow.api.ProcessDirectory;
076: import de.danet.an.workflow.api.ProcessMgr;
077: import de.danet.an.workflow.api.WorkflowService;
078: import de.danet.an.workflow.api.WorkflowServiceFactory;
079: import de.danet.an.workflow.omgcore.WfRequester;
080: import de.danet.an.workflow.omgcore.ProcessData;
081:
082: /**
083: * This class can be used in two ways: <br>
084: * It may define a test suite based on process defintion of process P0 in XPDL
085: * file "DefaultForGenericTest.xml" (simple JUnit test) OR <br>
086: * It can be used in a generic way by defining a particulary XPDL file
087: * containing the process P0 process defintion (e. g. used for load test) using
088: * the appropriate constructor.
089: *
090: * @author <a href="mailto:weidauer@danet.de">Christian Weidauer</a>
091: * @version 1.0
092: */
093: public class GenericTest extends TestCase {
094:
095: /**
096: * The package id of the process definition of process P0
097: */
098: private String packageID = null;
099:
100: /**
101: * The file that contains the process definition of process P0
102: */
103: private String xpdlFile = null;
104:
105: /**
106: * The intial process data which is supposed to be set.
107: */
108: private ProcessData initialProcessData = null;
109:
110: private static WorkflowService wfsCache = null;
111:
112: protected static WorkflowService workflowService() {
113: if (wfsCache == null) {
114: try {
115: WorkflowServiceFactory wfsf = WorkflowServiceFactory
116: .newInstance();
117: wfsCache = wfsf.newWorkflowService();
118: } catch (FactoryConfigurationError e) {
119: throw new IllegalStateException(e.getMessage());
120: }
121: }
122: return wfsCache;
123: }
124:
125: protected static STProjectLoginContext plc = null;
126: static {
127: try {
128: plc = new STProjectLoginContext();
129: plc.login();
130: } catch (LoginException e) {
131: throw new IllegalStateException(e.getMessage());
132: }
133: }
134:
135: /**
136: * process manager
137: */
138: private static ProcessMgr processManager = null;
139:
140: /**
141: * Constructor of this TestCase
142: * @param name a <code>String</code> value
143: */
144: public GenericTest(String name) {
145: super (name);
146: packageID = "DefaultForGenericTest";
147: xpdlFile = "/load/" + packageID + ".xml";
148: }
149:
150: /**
151: * Constructor of this TestCase
152: * @param name test case
153: * @param packageID ID of package of process P0
154: */
155: public GenericTest(String name, String packageID) {
156: super (name);
157: this .packageID = packageID;
158: xpdlFile = "/load/" + packageID + ".xml";
159: }
160:
161: /**
162: * Constructor of this TestCase
163: * @param name test case
164: * @param packageID ID of package of process P0
165: * @param initialProcessData process data which is supposed to be set
166: */
167: public GenericTest(String name, String packageID,
168: ProcessData initialProcessData) {
169: super (name);
170: this .packageID = packageID;
171: xpdlFile = "/load/" + packageID + ".xml";
172: this .initialProcessData = initialProcessData;
173: }
174:
175: private static TestSuite cleaningSuite = new TestSuite(
176: "cleaningSuite");
177: static {
178: cleaningSuite.addTest(new GenericTest(
179: "closeAllCloseableProcesses"));
180: cleaningSuite.addTest(new GenericTest(
181: "waitForNoRunningProcesses"));
182: cleaningSuite.addTest(new GenericTest("removeAllProcesses"));
183: }
184:
185: /**
186: * Deliver a suite for cleaning the processes (including closing, waiting
187: * for not running and removing).
188: * @return a <code>Test</code> value
189: */
190: public static TestSuite cleaningSuite() {
191: return cleaningSuite;
192: }
193:
194: /**
195: * Construct this test suite.
196: * @return a <code>Test</code> value
197: */
198: public static Test suite() {
199: TestSuite suite = new TestSuite();
200: suite.addTest(new GenericTest("prepareForCreateProcess"));
201: suite.addTest(new GenericTest("createProcess"));
202: suite.addTest(new GenericTest("createAndStartProcess"));
203: suite.addTest(new GenericTest("removeAllProcesses"));
204: return new EJBClientTest(plc, suite);
205: }
206:
207: /**
208: * Prepare for creating a process.
209: * @exception Exception if an error occurs
210: */
211: public void prepareForCreateProcess() throws Exception {
212:
213: // import the process definitions from the XPDL file
214: ProcessDefinitionDirectory pdd = workflowService()
215: .processDefinitionDirectory();
216: InputStream is = getClass().getResourceAsStream(xpdlFile);
217: assertTrue(is != null);
218: BufferedReader br = new BufferedReader(new InputStreamReader(
219: is, "ISO-8859-1"));
220: StringBuffer sb = new StringBuffer();
221: String st;
222: while ((st = br.readLine()) != null) {
223: sb.append(st + "\n");
224: }
225: pdd.importProcessDefinitions(sb.toString());
226: Collection processDefinitions = pdd.processDefinitions();
227: assertTrue(processDefinitions.size() > 0);
228:
229: // prepare the creation of process packageID/"P0"
230: try {
231: processManager = pdd.processMgr(packageID, "P0");
232: defReqCache = defaultRequester();
233: } catch (Exception e) {
234: e.printStackTrace();
235: }
236: }
237:
238: /**
239: * Create the process of id="P0".
240: * @exception Exception if an error occurs
241: */
242: public void createProcess() throws Exception {
243: Process process = createProcess(packageID, "P0");
244: }
245:
246: /**
247: * Create and start the process of id="P0".
248: * @exception Exception if an error occurs
249: */
250: public void createAndStartProcess() throws Exception {
251: Process process = createProcess(packageID, "P0");
252: process.start();
253: }
254:
255: /**
256: * Create and start the process of id="P0".
257: * @exception Exception if an error occurs
258: */
259: public void createStartAndInitializeProcess() throws Exception {
260: Process process = createProcess(packageID, "P0");
261: process.setProcessContext(initialProcessData);
262: process.start();
263: }
264:
265: /**
266: * Removes all processes created.
267: * @exception Exception if an error occurs
268: */
269: public static void removeAllProcesses() throws Exception {
270: // loop for all processes created
271: ProcessDirectory pd = workflowService().processDirectory();
272: Collection processes = pd.processes();
273: for (Iterator it = processes.iterator(); it.hasNext();) {
274: try {
275: Process process = (Process) (it.next());
276: pd.removeProcess(process);
277: } catch (Exception e) {
278: // process not in database anymore, so it is not running anymore
279: }
280: }
281: }
282:
283: /**
284: * Close (terminate, abort) all processes.
285: * @exception Exception if an error occurs
286: */
287: public static void closeAllCloseableProcesses() throws Exception {
288: // loop for all processes created
289: ProcessDirectory pd = workflowService().processDirectory();
290: Collection processes = pd.processes();
291: for (Iterator it = processes.iterator(); it.hasNext();) {
292: Process process = (Process) (it.next());
293: try {
294: process.abort();
295: } catch (Exception e) {
296: try {
297: process.terminate();
298: } catch (Exception ex) {
299: }
300: }
301: }
302: }
303:
304: /**
305: * Wait until no processes are running anymore.
306: * @exception Exception if an error occurs
307: */
308: public static void waitForNoRunningProcesses() throws Exception {
309: // loop for all processes created
310: ProcessDirectory pd = workflowService().processDirectory();
311: boolean runningProcesses = false;
312: do {
313: runningProcesses = false;
314: Collection processes = pd.processes();
315: for (Iterator it = processes.iterator(); it.hasNext();) {
316: try {
317: Process process = (Process) (it.next());
318: if (process.state().startsWith("open.running")) {
319: runningProcesses = true;
320: }
321: } catch (NoSuchObjectException e) {
322: // object not database anymore, so it is not running anymore
323: }
324: }
325: Thread.sleep(100);
326: } while (runningProcesses);
327: }
328:
329: private static WfRequester defReqCache = null;
330:
331: protected WfRequester defaultRequester() throws RemoteException {
332: if (defReqCache == null) {
333: defReqCache = new DefaultRequester(workflowService());
334: }
335: return defReqCache;
336: }
337:
338: /**
339: * Describe <code>createProcess</code> method here.
340: *
341: * @param pkgId a <code>String</code> value
342: * @param prcId a <code>String</code> value
343: * @return a <code>Process</code> value
344: * @exception Exception if an error occurs
345: */
346: protected Process createProcess(String pkgId, String prcId)
347: throws Exception {
348: ProcessDefinitionDirectory pdd = workflowService()
349: .processDefinitionDirectory();
350: ProcessMgr pmgr = pdd.processMgr(pkgId, prcId);
351: Process process = (Process) pmgr
352: .createProcess(defaultRequester());
353: return process;
354: }
355: }
|