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: JSTests.java,v 1.4 2006/11/19 21:53:47 mlipp Exp $
021: *
022: * $Log: JSTests.java,v $
023: * Revision 1.4 2006/11/19 21:53:47 mlipp
024: * Finished support for native Java types.
025: *
026: * Revision 1.3 2006/09/29 12:32:07 drmlipp
027: * Consistently using WfMOpen as projct name now.
028: *
029: * Revision 1.2 2006/03/08 14:46:42 drmlipp
030: * Synchronized with 1.3.3p5.
031: *
032: * Revision 1.1.1.1.6.1 2006/03/02 15:30:59 drmlipp
033: * Added E4X test.
034: *
035: * Revision 1.1.1.1 2004/08/18 15:18:47 drmlipp
036: * Update to 1.2
037: *
038: * Revision 1.1 2004/03/31 19:30:33 lipp
039: * Improved test.
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.ArrayList;
049: import java.util.Collection;
050: import java.util.Iterator;
051: import java.util.List;
052:
053: import javax.security.auth.login.LoginException;
054:
055: import org.w3c.dom.Node;
056:
057: import de.danet.an.util.junit.EJBClientTest;
058: import de.danet.an.util.sax.SAXContentBuffer;
059:
060: import de.danet.an.workflow.omgcore.ProcessData;
061: import de.danet.an.workflow.omgcore.WfActivity;
062: import de.danet.an.workflow.omgcore.WfProcess;
063: import de.danet.an.workflow.omgcore.WfRequester;
064:
065: import de.danet.an.workflow.api.Activity;
066: import de.danet.an.workflow.api.DefaultProcessData;
067: import de.danet.an.workflow.api.DefaultRequester;
068: import de.danet.an.workflow.api.FactoryConfigurationError;
069: import de.danet.an.workflow.api.ProcessDefinitionDirectory;
070: import de.danet.an.workflow.api.ProcessDirectory;
071: import de.danet.an.workflow.api.ProcessMgr;
072: import de.danet.an.workflow.api.SAXEventBuffer;
073: import de.danet.an.workflow.api.WorkflowService;
074: import de.danet.an.workflow.api.WorkflowServiceFactory;
075:
076: import common.UTLoginContext;
077: import junit.framework.Test;
078: import junit.framework.TestCase;
079: import junit.framework.TestSuite;
080:
081: /**
082: * Test JS
083: */
084: public class JSTests extends TestCase {
085: private static UTLoginContext plc = null;
086: static {
087: try {
088: plc = new UTLoginContext();
089: plc.login();
090: } catch (LoginException e) {
091: throw new IllegalStateException(e.getMessage());
092: }
093: }
094:
095: /**
096: * A process directory reference.
097: */
098: private ProcessDirectory pdd = null;
099:
100: /**
101: * Constructor of this TestCase
102: */
103: public JSTests(String name) {
104: super (name);
105: }
106:
107: /**
108: * Stellt diese TestSuite zusammen.
109: */
110: public static Test suite() {
111: TestSuite suite = new TestSuite();
112: suite.addTest(new JSTests("importProcessDefinitions"));
113: suite.addTest(new JSTests("testAbandon"));
114: suite.addTest(new JSTests("testReplacer"));
115: suite.addTest(new JSTests("testAUK"));
116: suite.addTest(new JSTests("testJavaArg1"));
117: suite.addTest(new JSTests("testJavaArg2"));
118: return new EJBClientTest(plc, suite);
119: }
120:
121: private WorkflowService workflowService = null;
122:
123: /**
124: * Initialisierung.
125: */
126: protected void setUp() throws Exception {
127: try {
128: WorkflowServiceFactory wfsf = WorkflowServiceFactory
129: .newInstance();
130: workflowService = wfsf.newWorkflowService();
131: } catch (FactoryConfigurationError e) {
132: throw new IllegalStateException(e.getMessage());
133: }
134: }
135:
136: protected void tearDown() throws Exception {
137: workflowService.release(workflowService);
138: workflowService = null;
139: }
140:
141: /**
142: * Import the process definitions from a XPDL file
143: * unsing the ProcessDefinitionDirectory bean.
144: */
145: public void importProcessDefinitions() throws Exception {
146: // Create process definition directory bean
147: ProcessDefinitionDirectory pdd = workflowService
148: .processDefinitionDirectory();
149:
150: InputStream is = getClass().getResourceAsStream(
151: "/tools/jstests.xml");
152: assertTrue(is != null);
153: BufferedReader br = new BufferedReader(new InputStreamReader(
154: is, "ISO-8859-1"));
155: StringBuffer sb = new StringBuffer();
156: String st;
157: while ((st = br.readLine()) != null) {
158: sb.append(st + "\n");
159: }
160: pdd.importProcessDefinitions(sb.toString());
161: Collection processDefinitions = pdd.processDefinitions();
162: assertTrue(processDefinitions.size() > 0);
163: }
164:
165: /**
166: * Test.
167: */
168: public void testAbandon() throws Exception {
169: ProcessDefinitionDirectory procDefDir = null;
170: ProcessDirectory procDir = null;
171: try {
172: procDefDir = workflowService.processDefinitionDirectory();
173: procDir = workflowService.processDirectory();
174: ProcessMgr pmgr = procDefDir.processMgr("jstests",
175: "testAbandon");
176: WfProcess process = pmgr
177: .createProcess(new DefaultRequester(workflowService));
178: process.start();
179: assertTrue(stateReached(process, "closed.completed"));
180: for (Iterator i = process.activitiesInState("closed")
181: .iterator(); i.hasNext();) {
182: WfActivity act = (Activity) i.next();
183: if (act.name().equals("Run JS")) {
184: assertTrue(act.state().equals(
185: "closed.completed.abandoned"));
186: } else if (act.name().equals("Handle Exception")) {
187: assertTrue(act.state().equals(
188: "closed.completed.normal"));
189: } else {
190: assertTrue(act.name(), false);
191: }
192: }
193: procDir.removeProcess(process);
194: } finally {
195: workflowService.release(procDefDir);
196: workflowService.release(procDir);
197: }
198: }
199:
200: /**
201: * Test.
202: */
203: public void testReplacer() throws Exception {
204: ProcessDefinitionDirectory procDefDir = null;
205: ProcessDirectory procDir = null;
206: try {
207: procDefDir = workflowService.processDefinitionDirectory();
208: procDir = workflowService.processDirectory();
209: ProcessMgr pmgr = procDefDir.processMgr("jstests",
210: "testReplacer");
211: WfProcess process = pmgr
212: .createProcess(new DefaultRequester(workflowService));
213: process.start();
214: assertTrue(stateReached(process, "closed.completed"));
215: SAXContentBuffer xmlRes = (SAXContentBuffer) process
216: .processContext().get("XMLResult");
217: Node resRoot = xmlRes.toW3cDom();
218: Node fc = resRoot.getFirstChild();
219: assertTrue(fc.toString(), fc.getLocalName().equals("Item"));
220: assertTrue(fc.getAttributes().getNamedItem("Id")
221: .getNodeValue().equals("Start"));
222: assertTrue(process.processContext().get("Result").equals(
223: "Test"));
224: procDir.removeProcess(process);
225: } finally {
226: workflowService.release(procDefDir);
227: workflowService.release(procDir);
228: }
229: }
230:
231: /**
232: * Test.
233: */
234: public void testAUK() throws Exception {
235: ProcessDefinitionDirectory procDefDir = null;
236: ProcessDirectory procDir = null;
237: try {
238: procDefDir = workflowService.processDefinitionDirectory();
239: procDir = workflowService.processDirectory();
240: ProcessMgr pmgr = procDefDir.processMgr("jstests",
241: "testAUK");
242: WfProcess process = pmgr
243: .createProcess(new DefaultRequester(workflowService));
244: process.start();
245: assertTrue(stateReached(process, "closed.completed"));
246: String s = (String) process.processContext().get("Result");
247: assertTrue(s.startsWith("jstests/testAUK "));
248: procDir.removeProcess(process);
249: } finally {
250: workflowService.release(procDefDir);
251: workflowService.release(procDir);
252: }
253: }
254:
255: /**
256: * Test.
257: */
258: public void testJavaArg1() throws Exception {
259: ProcessDefinitionDirectory procDefDir = null;
260: ProcessDirectory procDir = null;
261: try {
262: procDefDir = workflowService.processDefinitionDirectory();
263: procDir = workflowService.processDirectory();
264: ProcessMgr pmgr = procDefDir.processMgr("jstests",
265: "testJavaArg1");
266: WfProcess process = pmgr
267: .createProcess(new DefaultRequester(workflowService));
268: process.start();
269: assertTrue(stateReached(process, "closed.completed"));
270: List l = (List) process.processContext().get("Result");
271: assertTrue(l.size() == 1);
272: assertTrue(((String) l.get(0)).equals("Hallo"));
273: procDir.removeProcess(process);
274: } finally {
275: workflowService.release(procDefDir);
276: workflowService.release(procDir);
277: }
278: }
279:
280: /**
281: * Test.
282: */
283: public void testJavaArg2() throws Exception {
284: ProcessDefinitionDirectory procDefDir = null;
285: ProcessDirectory procDir = null;
286: try {
287: procDefDir = workflowService.processDefinitionDirectory();
288: procDir = workflowService.processDirectory();
289: ProcessMgr pmgr = procDefDir.processMgr("jstests",
290: "testJavaArg2");
291: WfProcess process = pmgr
292: .createProcess(new DefaultRequester(workflowService));
293: ProcessData data = new DefaultProcessData();
294: List l = new ArrayList();
295: data.put("Argument", l);
296: l.add("Text:");
297: process.setProcessContext(data);
298: process.start();
299: assertTrue(stateReached(process, "closed.completed"));
300: l = (List) process.processContext().get("Result");
301: assertTrue(l.size() == 2);
302: assertTrue(((String) l.get(1)).equals("Hallo"));
303: procDir.removeProcess(process);
304: } finally {
305: workflowService.release(procDefDir);
306: workflowService.release(procDir);
307: }
308: }
309:
310: private WfProcess createProcess(String pkgId, String prcId,
311: WfRequester req) throws Exception {
312: ProcessDefinitionDirectory procDir = null;
313: try {
314: procDir = workflowService.processDefinitionDirectory();
315: ProcessMgr pmgr = procDir.processMgr(pkgId, prcId);
316: return pmgr.createProcess(req);
317: } finally {
318: workflowService.release(procDir);
319: }
320: }
321:
322: private boolean stateReached(WfProcess proc, String procState)
323: throws Exception {
324: boolean test = true;
325: boolean stateReached = false;
326: int maxRetries = 100;
327: while (test) {
328: if (maxRetries-- > 0) {
329: if (proc.state().startsWith(procState)) {
330: stateReached = true;
331: test = false;
332: } else {
333: Thread.sleep(500);
334: }
335: } else {
336: test = false;
337: }
338: }
339: return stateReached;
340: }
341:
342: private boolean stateReached(WfActivity act, String actState)
343: throws Exception {
344: boolean test = true;
345: boolean stateReached = false;
346: int maxRetries = 100;
347: while (test) {
348: if (maxRetries-- > 0) {
349: if (act.state().startsWith(actState)) {
350: stateReached = true;
351: test = false;
352: } else {
353: Thread.sleep(500);
354: }
355: } else {
356: test = false;
357: }
358: }
359: return stateReached;
360: }
361: }
|