001: /*
002: * Danet GmbH
003: * Beratung und Software-Entwicklung
004: * Geschäftstelle AN
005: *
006: * $Id: Basic.java,v 1.5 2007/03/27 21:59:42 mlipp Exp $
007: *
008: * $Log: Basic.java,v $
009: * Revision 1.5 2007/03/27 21:59:42 mlipp
010: * Fixed lots of checkstyle warnings.
011: *
012: * Revision 1.4 2006/10/13 13:58:32 drmlipp
013: * Adapted to new environment.
014: *
015: * Revision 1.3 2006/10/07 20:41:34 mlipp
016: * Merged J2EE 1.4 adaptions from test branch.
017: *
018: * Revision 1.2 2005/04/22 15:11:07 drmlipp
019: * Merged changes from 1.3 branch up to 1.3p15.
020: *
021: * Revision 1.1.1.3.6.1 2005/04/08 12:32:29 drmlipp
022: * Added test.
023: *
024: * Revision 1.1.1.3 2004/08/18 15:18:47 drmlipp
025: * Update to 1.2
026: *
027: * Revision 1.30 2004/07/18 17:07:32 lipp
028: * Added check.
029: *
030: * Revision 1.29 2004/06/14 19:37:20 lipp
031: * Fixed assignment functions and cleaned up assignment related
032: * interfaces.
033: *
034: * Revision 1.28 2004/01/27 11:45:33 lipp
035: * Preserve newlines when reading process definitions.
036: *
037: * Revision 1.27 2003/11/18 20:59:12 lipp
038: * Added ras access.
039: *
040: * Revision 1.26 2003/10/30 21:03:49 lipp
041: * Removed interface EJBObject from ActivityProxy, WebLogic cannot handle
042: * it.
043: *
044: * Revision 1.25 2003/10/21 21:00:45 lipp
045: * Moved EJBClientTest to new junit sub-package.
046: *
047: * Revision 1.24 2003/10/08 11:15:45 huaiyang
048: * correct compile error.
049: *
050: * Revision 1.23 2003/10/08 07:42:34 lipp
051: * Made tests weblogic compatible.
052: *
053: * Revision 1.22 2003/09/19 13:12:29 lipp
054: * Adapted to closed.completed having a substate.
055: *
056: * Revision 1.21 2003/05/14 08:30:07 lipp
057: * Package import behaviour changed.
058: *
059: * Revision 1.20 2003/05/02 14:55:58 lipp
060: * Resolved some more package dependencies.
061: *
062: * Revision 1.19 2003/04/26 16:46:55 lipp
063: * Made unittests and systemtests coexist in eclipse.
064: *
065: * Revision 1.18 2003/04/16 19:25:04 lipp
066: * Adapted to jdk 1.4
067: *
068: * Revision 1.17 2003/02/27 15:14:40 lipp
069: * Some fixes.
070: *
071: * Revision 1.16 2003/02/25 17:08:27 lipp
072: * Reorganized requester implementation.
073: *
074: * Revision 1.15 2003/02/05 15:57:06 lipp
075: * Replaced DummyRequester with DefaultRequester.
076: *
077: * Revision 1.14 2002/11/19 15:14:52 lipp
078: * New transition manager.
079: *
080: * Revision 1.13 2002/11/11 12:20:45 lipp
081: * Some fixes.
082: *
083: * Revision 1.12 2002/11/06 07:54:56 huaiyang
084: * Add the test of createProcess1AndRemoveProcessDef.
085: *
086: * Revision 1.11 2002/10/23 11:36:47 lipp
087: * Got them running again.
088: *
089: * Revision 1.10 2002/10/23 07:29:15 lipp
090: * Adapted to state handling changes.
091: *
092: * Revision 1.9 2002/10/22 17:19:01 lipp
093: * Still adapting to state handling changes.
094: *
095: * Revision 1.8 2002/09/08 18:49:18 lipp
096: * Proper use of packageId and processId.
097: *
098: * Revision 1.7 2002/09/04 20:50:44 lipp
099: * Adapted to new process manager name schema.
100: *
101: * Revision 1.6 2002/08/30 13:37:05 lipp
102: * Using Workflow engine facade now.
103: *
104: * Revision 1.5 2002/08/29 12:49:18 lipp
105: * Removed no longer needed imports.
106: *
107: * Revision 1.4 2002/08/26 20:23:14 lipp
108: * Lots of method renames.
109: *
110: * Revision 1.3 2002/08/21 22:06:48 lipp
111: * Finished transition to ProcessMgrStub.
112: *
113: * Revision 1.2 2002/02/04 16:18:11 huaiyang
114: * Add the test of activityByKey.
115: *
116: * Revision 1.1 2002/02/03 21:41:42 lipp
117: * Cleaned up unittests.
118: *
119: */
120: package process;
121:
122: import java.io.BufferedReader;
123: import java.io.InputStream;
124: import java.io.InputStreamReader;
125:
126: import java.util.Collection;
127: import java.util.Iterator;
128:
129: import javax.naming.InitialContext;
130: import javax.rmi.PortableRemoteObject;
131: import javax.security.auth.login.LoginException;
132:
133: import de.danet.an.util.EJBUtil;
134: import de.danet.an.util.junit.EJBClientTest;
135:
136: import de.danet.an.workflow.omgcore.WfActivity;
137: import de.danet.an.workflow.omgcore.WfExecutionObject;
138: import de.danet.an.workflow.omgcore.WfExecutionObject.State;
139: import de.danet.an.workflow.omgcore.WfProcess;
140: import de.danet.an.workflow.omgcore.WfProcessMgr;
141: import de.danet.an.workflow.omgcore.WfRequester;
142:
143: import de.danet.an.workflow.api.DefaultRequester;
144: import de.danet.an.workflow.api.FactoryConfigurationError;
145: import de.danet.an.workflow.api.MethodInvocationBatch;
146: import de.danet.an.workflow.api.Process;
147: import de.danet.an.workflow.api.ProcessDefinition;
148: import de.danet.an.workflow.api.ProcessDefinitionDirectory;
149: import de.danet.an.workflow.api.ProcessDirectory;
150: import de.danet.an.workflow.api.WorkflowService;
151: import de.danet.an.workflow.api.WorkflowServiceFactory;
152:
153: import de.danet.an.workflow.ejbs.admin.ProcessDefinitionDirectoryHome;
154: import de.danet.an.workflow.ejbs.core.WfActivityHome;
155:
156: import common.UTLoginContext;
157: import junit.framework.Test;
158: import junit.framework.TestCase;
159: import junit.framework.TestSuite;
160:
161: /**
162: * Zusammenstellung aller TimerObjectTests.
163: */
164: public class Basic extends TestCase {
165:
166: private static UTLoginContext plc = null;
167: static {
168: try {
169: plc = new UTLoginContext();
170: plc.login();
171: } catch (LoginException e) {
172: throw new IllegalStateException(e.getMessage());
173: }
174: }
175:
176: /**
177: * Konstruktor zum Erzeugen eines TestCase
178: */
179: public Basic(String name) {
180: super (name);
181: }
182:
183: /**
184: * Stellt diese TestSuite zusammen.
185: */
186: public static Test suite() {
187: TestSuite suite = new TestSuite();
188: suite.addTest(new Basic("importProcessDefinitions"));
189: suite.addTest(new Basic("createProcess2"));
190: suite.addTest(new Basic("createProcess3"));
191: suite.addTest(new Basic("createAndStartProcess1"));
192: suite.addTest(new Basic("createAndStartProcess2"));
193: suite.addTest(new Basic("createAndStartProcess3"));
194: suite.addTest(new Basic("createAndStartProcessBatched"));
195: suite.addTest(new Basic("createProcess1AndRemoveProcessDef"));
196: return new EJBClientTest(plc, suite);
197: }
198:
199: private static WorkflowService wfsCache = null;
200:
201: private WorkflowService workflowService() {
202: if (wfsCache == null) {
203: try {
204: WorkflowServiceFactory wfsf = WorkflowServiceFactory
205: .newInstance();
206: wfsCache = wfsf.newWorkflowService();
207: } catch (FactoryConfigurationError e) {
208: throw new IllegalStateException(e.getMessage());
209: }
210: }
211: return wfsCache;
212: }
213:
214: /**
215: * Import the process definitions from a XPDL file
216: * unsing the ProcessDefinitionDirectory bean.
217: */
218: public void importProcessDefinitions() throws Exception {
219: // Create process definition directory bean
220: ProcessDefinitionDirectory pdd = workflowService()
221: .processDefinitionDirectory();
222:
223: InputStream is = getClass().getResourceAsStream(
224: "/process/testXPDL.xml");
225: assertTrue(is != null);
226: BufferedReader br = new BufferedReader(new InputStreamReader(
227: is, "ISO-8859-1"));
228: StringBuffer sb = new StringBuffer();
229: String st;
230: while ((st = br.readLine()) != null) {
231: sb.append(st + "\n");
232: }
233: pdd.importProcessDefinitions(sb.toString());
234: Collection processDefinitions = pdd.processDefinitions();
235: assertTrue(processDefinitions.size() > 0);
236:
237: }
238:
239: private WfProcess createProcess(String pkgId, String prcId,
240: WfRequester req) throws Exception {
241: ProcessDefinitionDirectory pdd = null;
242: try {
243: pdd = workflowService().processDefinitionDirectory();
244: WfProcessMgr pmgr = pdd.processMgr(pkgId, prcId);
245: return pmgr.createProcess(req);
246: } finally {
247: workflowService().release(pdd);
248: }
249: }
250:
251: /**
252: * Create a new process for the given type and start it.
253: */
254: public void createAndStartProcess1() throws Exception {
255: WfRequester cont = new DefaultRequester(workflowService());
256: WfProcess process = createProcess("ut-process", "jut1", cont);
257: assertTrue(process.state().startsWith(
258: "open.not_running.not_started"));
259: process.start();
260: assertTrue(process.state().startsWith("open.running"));
261: // get the activities
262: Collection c = process.steps();
263: Iterator it = c.iterator();
264: while (it.hasNext()) {
265: WfActivity a = (WfActivity) it.next();
266: assertTrue(a.state() != null);
267: }
268: }
269:
270: /**
271: * Create a new process for the given type and start it.
272: */
273: public void createAndStartProcess2() throws Exception {
274: WfRequester cont = new DefaultRequester(workflowService());
275: WfProcess process = createProcess("ut-process", "jut2", cont);
276: assertTrue(process.state().startsWith(
277: "open.not_running.not_started"));
278: process.start();
279: assertTrue(process.state().startsWith("open.running"));
280: // get the activities
281: Collection c = process.steps();
282: Iterator it = c.iterator();
283: while (it.hasNext()) {
284: WfActivity a = (WfActivity) it.next();
285: assertTrue(a.state() != null);
286: }
287: }
288:
289: /**
290: * Create a new process for the given type and start it.
291: */
292: public void createAndStartProcess3() throws Exception {
293: WfRequester cont = new DefaultRequester(workflowService());
294: WfProcess process = createProcess("ut-process", "jut3", cont);
295: assertTrue(process.state().startsWith(
296: "open.not_running.not_started"));
297: process.start();
298: Thread.sleep(2500);
299: assertTrue(process + " should be completed, is "
300: + process.state(), process.state().startsWith(
301: "closed.completed"));
302: // get the activities
303: Collection c = process.steps();
304: Iterator it = c.iterator();
305: int cnt = 0;
306: while (it.hasNext()) {
307: WfActivity a = (WfActivity) it.next();
308: if (a.state().startsWith(
309: WfExecutionObject.ClosedState.COMPLETED.toString())) {
310: cnt += 1;
311: }
312: }
313: assertTrue(cnt == 5);
314: }
315:
316: /**
317: * Create a new process for the given type and start it.
318: */
319: public void createAndStartProcessBatched() throws Exception {
320: WfRequester cont = new DefaultRequester(workflowService());
321:
322: ProcessDefinitionDirectory pdd = null;
323: WfProcessMgr pmgr = null;
324: try {
325: pdd = workflowService().processDefinitionDirectory();
326: pmgr = pdd.processMgr("ut-process", "jut1");
327: } finally {
328: workflowService().release(pdd);
329: }
330: MethodInvocationBatch mib = new MethodInvocationBatch();
331: mib
332: .addInvocation(
333: pmgr,
334: "createProcess",
335: new String[] { "de.danet.an.workflow.omgcore.WfRequester" },
336: new Object[] { new DefaultRequester(
337: workflowService()) });
338: mib.addInvocation(-1, "key", null, null, false);
339: mib.addInvocation(-2, "start", null, null, false);
340: MethodInvocationBatch.Result mir = (MethodInvocationBatch.Result) workflowService()
341: .executeBatch(mib);
342: if (mir.hasExceptions()) {
343: Exception e = mir.firstException();
344: assertTrue("Problem executing batch: " + e.getMessage(),
345: false);
346: }
347: Object[] results = mir.results();
348: WfProcess proc = (WfProcess) results[0];
349: assertTrue(proc.key().equals(results[1]));
350: }
351:
352: /**
353: * Create a new process for the given type.
354: */
355: public void createProcess2() throws Exception {
356: WfRequester cont = new DefaultRequester(workflowService());
357: WfProcess process = createProcess("ut-process", "jut2", cont);
358: assertTrue(process.state().startsWith(
359: "open.not_running.not_started"));
360: process.start();
361: Thread.sleep(1000);
362: assertTrue(process.state().startsWith("open.running"));
363: int i = 9;
364: int resCnt = 0;
365: while (i-- > 0) {
366: boolean resd = false;
367: for (Iterator it = process.steps().iterator(); it.hasNext();) {
368: WfActivity a = (WfActivity) it.next();
369: if (a.state().startsWith("open.not_running.suspended")) {
370: a.resume();
371: resd = true;
372: resCnt += 1;
373: Thread.sleep(500);
374: }
375: }
376: if (!resd) {
377: break;
378: }
379: }
380: assertTrue(i >= 0);
381: assertTrue(resCnt == 8);
382: }
383:
384: /**
385: * Create a new process for the given type.
386: */
387: public void createProcess3() throws Exception {
388: WfRequester cont = new DefaultRequester(workflowService());
389: WfProcess process = createProcess("ut-process", "jut3", cont);
390: assertTrue(process.state().startsWith(
391: "open.not_running.not_started"));
392: process.start();
393: Thread.sleep(5000);
394: assertTrue(
395: "Process state should be closed.completed.normal, is "
396: + process.state(), process.state().startsWith(
397: "closed.completed.normal"));
398: assertTrue("Process workflow state should be closed, is "
399: + process.workflowState(), process.workflowState()
400: .equals(State.CLOSED));
401: }
402:
403: /**
404: * Remove a given process from the database.
405: */
406: public void removeProcess3() throws Exception {
407: WfRequester cont = new DefaultRequester(workflowService());
408: WfProcess process = createProcess("ut-process", "jut999", cont);
409: ProcessDirectory pd = null;
410: try {
411: pd = workflowService().processDirectory();
412: pd.removeProcess((Process) process);
413: } finally {
414: workflowService().release(pd);
415: }
416: }
417:
418: /**
419: * Create a new process and then remove its process definition.
420: */
421: public void createProcess1AndRemoveProcessDef() throws Exception {
422: ProcessDefinitionDirectory pdd = null;
423: try {
424: WfRequester cont = new DefaultRequester(workflowService());
425: Process process = (Process) createProcess("ut-process",
426: "jut1", cont);
427: assertTrue(process.state().startsWith(
428: "open.not_running.not_started"));
429: process.start();
430: assertTrue(process.state().startsWith("open.running"));
431: // remove its process definition
432: pdd = workflowService().processDefinitionDirectory();
433: ProcessDefinition pd = pdd.lookupProcessDefinition(
434: "ut-process", "jut1");
435: assertTrue(pd != null);
436: pdd.removeProcessDefinition("ut-process", "jut1");
437: boolean gotEx = false;
438: try {
439: pd = pdd.lookupProcessDefinition("ut-process", "jut1");
440: } catch (Exception ex) {
441: gotEx = true;
442: }
443: assertTrue(gotEx);
444: // check the process definition of the process
445: ProcessDefinition procDef = process.processDefinition();
446: assertTrue(procDef.packageId().equals("ut-process"));
447: assertTrue(procDef.processId().equals("jut1"));
448: // import the process definition again.
449: importProcessDefinitions();
450: pd = pdd.lookupProcessDefinition("ut-process", "jut1");
451: assertTrue(pd != null);
452: } finally {
453: workflowService().release(pdd);
454: }
455: }
456:
457: public static void importProcessDefinitions(String filename)
458: throws Exception {
459: ProcessDefinitionDirectory pdd = WorkflowServiceFactory
460: .newInstance().newWorkflowService()
461: .processDefinitionDirectory();
462: InputStream is = Basic.class.getResourceAsStream(filename);
463: assertTrue(is != null);
464: BufferedReader br = new BufferedReader(new InputStreamReader(
465: is, "ISO-8859-1"));
466: StringBuffer sb = new StringBuffer();
467: String st;
468: while ((st = br.readLine()) != null) {
469: sb.append(st + "\n");
470: }
471: pdd.importProcessDefinitions(sb.toString());
472: Collection processDefinitions = pdd.processDefinitions();
473: assertTrue(processDefinitions.size() > 0);
474: }
475: }
|