001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2004 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: ExceptionTests.java,v 1.6 2006/10/26 13:24:54 drmlipp Exp $
021: *
022: * $Log: ExceptionTests.java,v $
023: * Revision 1.6 2006/10/26 13:24:54 drmlipp
024: * Extended exception handling.
025: *
026: * Revision 1.5 2006/10/19 12:20:53 drmlipp
027: * Fixed test.
028: *
029: * Revision 1.4 2006/10/19 11:47:52 drmlipp
030: * Added suspended test.
031: *
032: * Revision 1.3 2006/09/29 12:32:07 drmlipp
033: * Consistently using WfMOpen as projct name now.
034: *
035: * Revision 1.2 2005/08/25 13:24:22 drmlipp
036: * Synchronized with 1.3.1p6.
037: *
038: * Revision 1.1.2.1 2005/08/24 20:41:08 drmlipp
039: * Added exception mapping tests.
040: *
041: * Revision 1.1 2004/12/22 19:28:11 mlipp
042: * Added jelly tool.
043: *
044: */
045: package tools;
046:
047: import java.io.BufferedReader;
048: import java.io.InputStream;
049: import java.io.InputStreamReader;
050:
051: import java.util.Collection;
052: import java.util.Iterator;
053:
054: import javax.security.auth.login.LoginException;
055:
056: import de.danet.an.util.junit.EJBClientTest;
057:
058: import de.danet.an.workflow.omgcore.WfActivity;
059: import de.danet.an.workflow.omgcore.WfProcess;
060: import de.danet.an.workflow.omgcore.WfRequester;
061:
062: import de.danet.an.workflow.api.Activity;
063: import de.danet.an.workflow.api.DefaultRequester;
064: import de.danet.an.workflow.api.FactoryConfigurationError;
065: import de.danet.an.workflow.api.Process;
066: import de.danet.an.workflow.api.ProcessDefinitionDirectory;
067: import de.danet.an.workflow.api.ProcessDirectory;
068: import de.danet.an.workflow.api.ProcessMgr;
069: import de.danet.an.workflow.api.WorkflowService;
070: import de.danet.an.workflow.api.WorkflowServiceFactory;
071:
072: import common.UTLoginContext;
073: import junit.framework.Test;
074: import junit.framework.TestCase;
075: import junit.framework.TestSuite;
076:
077: /**
078: * Test exception mapping
079: */
080: public class ExceptionTests extends TestCase {
081: private static UTLoginContext plc = null;
082: static {
083: try {
084: plc = new UTLoginContext();
085: plc.login();
086: } catch (LoginException e) {
087: throw new IllegalStateException(e.getMessage());
088: }
089: }
090:
091: /**
092: * A process directory reference.
093: */
094: private ProcessDirectory pdd = null;
095:
096: /**
097: * Constructor of this TestCase
098: */
099: public ExceptionTests(String name) {
100: super (name);
101: }
102:
103: /**
104: * Stellt diese TestSuite zusammen.
105: */
106: public static Test suite() {
107: TestSuite suite = new TestSuite();
108: suite.addTest(new ExceptionTests("importProcessDefinitions"));
109: suite.addTest(new ExceptionTests("defaultMapping"));
110: suite.addTest(new ExceptionTests("overrideMapping"));
111: suite.addTest(new ExceptionTests("redefineMapping"));
112: suite.addTest(new ExceptionTests("suspendMapping"));
113: suite.addTest(new ExceptionTests("suspendMapping2"));
114: return new EJBClientTest(plc, suite);
115: }
116:
117: private WorkflowService workflowService = null;
118:
119: /**
120: * Initialisierung.
121: */
122: protected void setUp() throws Exception {
123: try {
124: WorkflowServiceFactory wfsf = WorkflowServiceFactory
125: .newInstance();
126: workflowService = wfsf.newWorkflowService();
127: } catch (FactoryConfigurationError e) {
128: throw new IllegalStateException(e.getMessage());
129: }
130: }
131:
132: protected void tearDown() throws Exception {
133: workflowService.release(workflowService);
134: workflowService = null;
135: }
136:
137: /**
138: * Import the process definitions from a XPDL file
139: * unsing the ProcessDefinitionDirectory bean.
140: */
141: public void importProcessDefinitions() throws Exception {
142: // Create process definition directory bean
143: ProcessDefinitionDirectory pdd = workflowService
144: .processDefinitionDirectory();
145:
146: InputStream is = getClass().getResourceAsStream(
147: "/tools/exceptiontest.xml");
148: assertTrue(is != null);
149: BufferedReader br = new BufferedReader(new InputStreamReader(
150: is, "ISO-8859-1"));
151: StringBuffer sb = new StringBuffer();
152: String st;
153: while ((st = br.readLine()) != null) {
154: sb.append(st + "\n");
155: }
156: pdd.importProcessDefinitions(sb.toString());
157: Collection processDefinitions = pdd.processDefinitions();
158: assertTrue(processDefinitions.size() > 0);
159: }
160:
161: /**
162: * Test.
163: */
164: public void defaultMapping() throws Exception {
165: ProcessDefinitionDirectory procDefDir = null;
166: ProcessDirectory procDir = null;
167: try {
168: procDefDir = workflowService.processDefinitionDirectory();
169: procDir = workflowService.processDirectory();
170: ProcessMgr pmgr = procDefDir.processMgr("ExceptionTests",
171: "defaultMapping");
172: WfProcess process = pmgr
173: .createProcess(new DefaultRequester(workflowService));
174: process.start();
175: assertTrue(stateReached(process, "closed.completed"));
176: boolean found = false;
177: for (Iterator i = process.steps().iterator(); i.hasNext();) {
178: WfActivity act = (WfActivity) i.next();
179: if (act.name().equals("Generic")) {
180: found = true;
181: assertTrue(act.state().startsWith(
182: "closed.completed"));
183: }
184: }
185: assertTrue(found);
186: procDir.removeProcess(process);
187: } finally {
188: workflowService.release(procDefDir);
189: workflowService.release(procDir);
190: }
191: }
192:
193: /**
194: * Test.
195: */
196: public void overrideMapping() throws Exception {
197: ProcessDefinitionDirectory procDefDir = null;
198: ProcessDirectory procDir = null;
199: try {
200: procDefDir = workflowService.processDefinitionDirectory();
201: procDir = workflowService.processDirectory();
202: ProcessMgr pmgr = procDefDir.processMgr("ExceptionTests",
203: "overrideMapping");
204: WfProcess process = pmgr
205: .createProcess(new DefaultRequester(workflowService));
206: process.start();
207: assertTrue(stateReached(process, "closed.terminated"));
208: boolean found = false;
209: for (Iterator i = process.steps().iterator(); i.hasNext();) {
210: WfActivity act = (WfActivity) i.next();
211: if (act.name().equals("Get stock quote")) {
212: found = true;
213: assertTrue(act.state().startsWith(
214: "closed.terminated"));
215: }
216: }
217: assertTrue(found);
218: procDir.removeProcess(process);
219: } finally {
220: workflowService.release(procDefDir);
221: workflowService.release(procDir);
222: }
223: }
224:
225: /**
226: * Test.
227: */
228: public void redefineMapping() throws Exception {
229: ProcessDefinitionDirectory procDefDir = null;
230: ProcessDirectory procDir = null;
231: try {
232: procDefDir = workflowService.processDefinitionDirectory();
233: procDir = workflowService.processDirectory();
234: ProcessMgr pmgr = procDefDir.processMgr("ExceptionTests",
235: "redefineMapping");
236: WfProcess process = pmgr
237: .createProcess(new DefaultRequester(workflowService));
238: process.start();
239: assertTrue(stateReached(process, "closed.completed"));
240: boolean found = false;
241: for (Iterator i = process.steps().iterator(); i.hasNext();) {
242: WfActivity act = (WfActivity) i.next();
243: if (act.name().equals("Generic")) {
244: found = true;
245: assertTrue(act.state().startsWith(
246: "closed.completed"));
247: }
248: }
249: assertTrue(found);
250: procDir.removeProcess(process);
251: } finally {
252: workflowService.release(procDefDir);
253: workflowService.release(procDir);
254: }
255: }
256:
257: /**
258: * Test.
259: */
260: public void suspendMapping() throws Exception {
261: ProcessDefinitionDirectory procDefDir = null;
262: ProcessDirectory procDir = null;
263: try {
264: procDefDir = workflowService.processDefinitionDirectory();
265: procDir = workflowService.processDirectory();
266: ProcessMgr pmgr = procDefDir.processMgr("ExceptionTests",
267: "suspendMapping");
268: WfProcess process = pmgr
269: .createProcess(new DefaultRequester(workflowService));
270: process.start();
271: Activity toolAct = null;
272: for (Iterator i = process.steps().iterator(); i.hasNext();) {
273: Activity act = (Activity) i.next();
274: if (act.name().equals("Get stock quote")) {
275: toolAct = act;
276: break;
277: }
278: }
279: assertTrue(toolAct != null);
280: stateReached(toolAct,
281: "open.not_running.suspended.abandoning");
282: toolAct.resume();
283: assertTrue(stateReached(process, "closed.completed"));
284: boolean found = false;
285: for (Iterator i = process.steps().iterator(); i.hasNext();) {
286: WfActivity act = (WfActivity) i.next();
287: if (act.name().equals("Generic")) {
288: found = true;
289: assertTrue(act.state().startsWith(
290: "closed.completed"));
291: }
292: }
293: assertTrue(found);
294: procDir.removeProcess(process);
295: } finally {
296: workflowService.release(procDefDir);
297: workflowService.release(procDir);
298: }
299: }
300:
301: /**
302: * Test.
303: */
304: public void suspendMapping2() throws Exception {
305: ProcessDefinitionDirectory procDefDir = null;
306: ProcessDirectory procDir = null;
307: try {
308: procDefDir = workflowService.processDefinitionDirectory();
309: procDir = workflowService.processDirectory();
310: ProcessMgr pmgr = procDefDir.processMgr("ExceptionTests",
311: "suspendMapping");
312: WfProcess process = pmgr
313: .createProcess(new DefaultRequester(workflowService));
314: process.start();
315: Activity toolAct = null;
316: for (Iterator i = process.steps().iterator(); i.hasNext();) {
317: Activity act = (Activity) i.next();
318: if (act.name().equals("Get stock quote")) {
319: toolAct = act;
320: break;
321: }
322: }
323: assertTrue(toolAct != null);
324: stateReached(toolAct,
325: "open.not_running.suspended.abandoning");
326: toolAct
327: .changeState("open.not_running.suspended.clearing_exception");
328: toolAct.resume();
329: assertTrue(stateReached(process, "closed.completed"));
330: boolean found = false;
331: for (Iterator i = process.steps().iterator(); i.hasNext();) {
332: WfActivity act = (WfActivity) i.next();
333: if (act.name().equals("Generic")) {
334: found = true;
335: assertTrue(act.state().startsWith(
336: "open.not_running.not_started"));
337: }
338: }
339: assertTrue(found);
340: found = false;
341: for (Iterator i = process.steps().iterator(); i.hasNext();) {
342: WfActivity act = (WfActivity) i.next();
343: if (act.name().equals("Normal")) {
344: found = true;
345: assertTrue(act.state().startsWith(
346: "closed.completed"));
347: }
348: }
349: assertTrue(found);
350: procDir.removeProcess(process);
351: } finally {
352: workflowService.release(procDefDir);
353: workflowService.release(procDir);
354: }
355: }
356:
357: private WfProcess createProcess(String pkgId, String prcId,
358: WfRequester req) throws Exception {
359: ProcessDefinitionDirectory procDir = null;
360: try {
361: procDir = workflowService.processDefinitionDirectory();
362: ProcessMgr pmgr = procDir.processMgr(pkgId, prcId);
363: return pmgr.createProcess(req);
364: } finally {
365: workflowService.release(procDir);
366: }
367: }
368:
369: private boolean stateReached(WfProcess proc, String procState)
370: throws Exception {
371: boolean test = true;
372: boolean stateReached = false;
373: int maxRetries = 100;
374: while (test) {
375: if (maxRetries-- > 0) {
376: if (proc.state().startsWith(procState)) {
377: stateReached = true;
378: test = false;
379: } else {
380: Thread.sleep(500);
381: }
382: } else {
383: test = false;
384: }
385: }
386: return stateReached;
387: }
388:
389: private boolean stateReached(WfActivity act, String actState)
390: throws Exception {
391: boolean test = true;
392: boolean stateReached = false;
393: int maxRetries = 100;
394: while (test) {
395: if (maxRetries-- > 0) {
396: if (act.state().startsWith(actState)) {
397: stateReached = true;
398: test = false;
399: } else {
400: Thread.sleep(500);
401: }
402: } else {
403: test = false;
404: }
405: }
406: return stateReached;
407: }
408: }
|