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: Timing.java,v 1.2 2006/09/29 12:32:07 drmlipp Exp $
021: *
022: * $Log: Timing.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.1 2004/08/18 15:18:47 drmlipp
027: * Update to 1.2
028: *
029: * Revision 1.2 2004/02/23 13:59:31 lipp
030: * Added simple waittool invocation.
031: *
032: * Revision 1.1 2004/02/20 18:56:35 lipp
033: * Renamed package waittool to timing (much better ;-)).
034: *
035: * Revision 1.2 2004/02/20 16:24:42 lipp
036: * Added waiting tests.
037: *
038: * Revision 1.1 2004/02/19 17:55:55 lipp
039: * Initial version of waittool.
040: *
041: */
042: package tools;
043:
044: import java.io.BufferedReader;
045: import java.io.InputStream;
046: import java.io.InputStreamReader;
047:
048: import java.util.Collection;
049: import java.util.HashMap;
050: import java.util.Iterator;
051: import java.util.Map;
052:
053: import javax.security.auth.login.LoginException;
054:
055: import de.danet.an.util.junit.EJBClientTest;
056:
057: import de.danet.an.workflow.omgcore.ProcessData;
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.Channel;
063: import de.danet.an.workflow.api.DefaultRequester;
064: import de.danet.an.workflow.api.FactoryConfigurationError;
065: import de.danet.an.workflow.api.ProcessDefinitionDirectory;
066: import de.danet.an.workflow.api.ProcessDirectory;
067: import de.danet.an.workflow.api.ProcessMgr;
068: import de.danet.an.workflow.api.WorkflowService;
069: import de.danet.an.workflow.api.WorkflowServiceFactory;
070:
071: import common.UTLoginContext;
072: import junit.framework.Test;
073: import junit.framework.TestCase;
074: import junit.framework.TestSuite;
075:
076: /**
077: * Test Cahbacc
078: */
079: public class Timing extends TestCase {
080: private static UTLoginContext plc = null;
081: static {
082: try {
083: plc = new UTLoginContext();
084: plc.login();
085: } catch (LoginException e) {
086: throw new IllegalStateException(e.getMessage());
087: }
088: }
089:
090: /**
091: * A process directory reference.
092: */
093: private ProcessDirectory pdd = null;
094:
095: /**
096: * Constructor of this TestCase
097: */
098: public Timing(String name) {
099: super (name);
100: }
101:
102: /**
103: * Stellt diese TestSuite zusammen.
104: */
105: public static Test suite() {
106: TestSuite suite = new TestSuite();
107: suite.addTest(new Timing("importProcessDefinitions"));
108: suite.addTest(new Timing("waitTest1"));
109: suite.addTest(new Timing("waitTest2"));
110: suite.addTest(new Timing("simpleTest"));
111: suite.addTest(new Timing("expiredTest"));
112: suite.addTest(new Timing("cancelTest"));
113: return new EJBClientTest(plc, suite);
114: }
115:
116: private WorkflowService workflowService = null;
117:
118: /**
119: * Initialisierung.
120: */
121: protected void setUp() throws Exception {
122: try {
123: WorkflowServiceFactory wfsf = WorkflowServiceFactory
124: .newInstance();
125: workflowService = wfsf.newWorkflowService();
126: } catch (FactoryConfigurationError e) {
127: throw new IllegalStateException(e.getMessage());
128: }
129: }
130:
131: protected void tearDown() throws Exception {
132: workflowService.release(workflowService);
133: workflowService = null;
134: }
135:
136: /**
137: * Import the process definitions from a XPDL file
138: * unsing the ProcessDefinitionDirectory bean.
139: */
140: public void importProcessDefinitions() throws Exception {
141: // Create process definition directory bean
142: ProcessDefinitionDirectory pdd = workflowService
143: .processDefinitionDirectory();
144:
145: InputStream is = getClass().getResourceAsStream(
146: "/tools/timing.xml");
147: assertTrue(is != null);
148: BufferedReader br = new BufferedReader(new InputStreamReader(
149: is, "ISO-8859-1"));
150: StringBuffer sb = new StringBuffer();
151: String st;
152: while ((st = br.readLine()) != null) {
153: sb.append(st + "\n");
154: }
155: pdd.importProcessDefinitions(sb.toString());
156: Collection processDefinitions = pdd.processDefinitions();
157: assertTrue(processDefinitions.size() > 0);
158: }
159:
160: /**
161: * Test.
162: */
163: public void waitTest1() throws Exception {
164: ProcessDefinitionDirectory procDefDir = null;
165: ProcessDirectory procDir = null;
166: try {
167: procDefDir = workflowService.processDefinitionDirectory();
168: procDir = workflowService.processDirectory();
169: ProcessMgr pmgr = procDefDir.processMgr("timing",
170: "wait_test1");
171: WfProcess process = pmgr
172: .createProcess(new DefaultRequester(workflowService));
173: process.start();
174: assertTrue(stateReached(process, "closed.completed"));
175: procDir.removeProcess(process);
176: } finally {
177: workflowService.release(procDefDir);
178: workflowService.release(procDir);
179: }
180: }
181:
182: /**
183: * Test.
184: */
185: public void waitTest2() throws Exception {
186: ProcessDefinitionDirectory procDefDir = null;
187: ProcessDirectory procDir = null;
188: try {
189: procDefDir = workflowService.processDefinitionDirectory();
190: procDir = workflowService.processDirectory();
191: ProcessMgr pmgr = procDefDir.processMgr("timing",
192: "wait_test2");
193: WfProcess process = pmgr
194: .createProcess(new DefaultRequester(workflowService));
195: process.start();
196: assertTrue(stateReached(process, "closed.completed"));
197: procDir.removeProcess(process);
198: } finally {
199: workflowService.release(procDefDir);
200: workflowService.release(procDir);
201: }
202: }
203:
204: /**
205: * Test.
206: */
207: public void simpleTest() throws Exception {
208: ProcessDefinitionDirectory procDefDir = null;
209: ProcessDirectory procDir = null;
210: try {
211: procDefDir = workflowService.processDefinitionDirectory();
212: procDir = workflowService.processDirectory();
213: ProcessMgr pmgr = procDefDir.processMgr("timing",
214: "simple_test");
215: WfProcess process = pmgr
216: .createProcess(new DefaultRequester(workflowService));
217: process.start();
218: assertTrue(stateReached(process, "closed.completed"));
219: ProcessData pd = process.processContext();
220: assertTrue("EXPIRED".equals(pd.get("waitResult")));
221: procDir.removeProcess(process);
222: } finally {
223: workflowService.release(procDefDir);
224: workflowService.release(procDir);
225: }
226: }
227:
228: /**
229: * Test.
230: */
231: public void expiredTest() throws Exception {
232: ProcessDefinitionDirectory procDefDir = null;
233: ProcessDirectory procDir = null;
234: try {
235: procDefDir = workflowService.processDefinitionDirectory();
236: procDir = workflowService.processDirectory();
237: ProcessMgr pmgr = procDefDir.processMgr("timing",
238: "expired_test");
239: WfProcess process = pmgr
240: .createProcess(new DefaultRequester(workflowService));
241: process.start();
242: assertTrue(stateReached(process, "closed.completed"));
243: ProcessData pd = process.processContext();
244: assertTrue("EXPIRED".equals(pd.get("waitResult")));
245: procDir.removeProcess(process);
246: } finally {
247: workflowService.release(procDefDir);
248: workflowService.release(procDir);
249: }
250: }
251:
252: /**
253: * Test.
254: */
255: public void cancelTest() throws Exception {
256: ProcessDefinitionDirectory procDefDir = null;
257: ProcessDirectory procDir = null;
258: try {
259: procDefDir = workflowService.processDefinitionDirectory();
260: procDir = workflowService.processDirectory();
261: ProcessMgr pmgr = procDefDir.processMgr("timing",
262: "cancel_test");
263: WfProcess process = pmgr
264: .createProcess(new DefaultRequester(workflowService));
265: process.start();
266: assertTrue(stateReached(process, "closed.completed"));
267: ProcessData pd = process.processContext();
268: assertTrue("EXPIRED".equals(pd.get("wait2Result")));
269: assertTrue("CANCELED".equals(pd.get("wait1Result")));
270: procDir.removeProcess(process);
271: } finally {
272: workflowService.release(procDefDir);
273: workflowService.release(procDir);
274: }
275: }
276:
277: private WfProcess createProcess(String pkgId, String prcId,
278: WfRequester req) throws Exception {
279: ProcessDefinitionDirectory procDir = null;
280: try {
281: procDir = workflowService.processDefinitionDirectory();
282: ProcessMgr pmgr = procDir.processMgr(pkgId, prcId);
283: return pmgr.createProcess(req);
284: } finally {
285: workflowService.release(procDir);
286: }
287: }
288:
289: private boolean stateReached(WfProcess proc, String procState)
290: throws Exception {
291: boolean test = true;
292: boolean stateReached = false;
293: int maxRetries = 100;
294: while (test) {
295: if (maxRetries-- > 0) {
296: if (proc.state().startsWith(procState)) {
297: stateReached = true;
298: test = false;
299: } else {
300: Thread.sleep(500);
301: }
302: } else {
303: test = false;
304: }
305: }
306: return stateReached;
307: }
308:
309: private boolean stateReached(WfActivity act, String actState)
310: throws Exception {
311: boolean test = true;
312: boolean stateReached = false;
313: int maxRetries = 100;
314: while (test) {
315: if (maxRetries-- > 0) {
316: if (act.state().startsWith(actState)) {
317: stateReached = true;
318: test = false;
319: } else {
320: Thread.sleep(500);
321: }
322: } else {
323: test = false;
324: }
325: }
326: return stateReached;
327: }
328: }
|