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: JellyTests.java,v 1.2 2006/09/29 12:32:07 drmlipp Exp $
021: *
022: * $Log: JellyTests.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 2004/12/22 19:28:11 mlipp
027: * Added jelly tool.
028: *
029: */
030: package tools;
031:
032: import java.io.BufferedReader;
033: import java.io.InputStream;
034: import java.io.InputStreamReader;
035:
036: import java.util.Collection;
037:
038: import javax.security.auth.login.LoginException;
039:
040: import de.danet.an.util.junit.EJBClientTest;
041:
042: import de.danet.an.workflow.omgcore.WfActivity;
043: import de.danet.an.workflow.omgcore.WfProcess;
044: import de.danet.an.workflow.omgcore.WfRequester;
045:
046: import de.danet.an.workflow.api.DefaultRequester;
047: import de.danet.an.workflow.api.FactoryConfigurationError;
048: import de.danet.an.workflow.api.ProcessDefinitionDirectory;
049: import de.danet.an.workflow.api.ProcessDirectory;
050: import de.danet.an.workflow.api.ProcessMgr;
051: import de.danet.an.workflow.api.WorkflowService;
052: import de.danet.an.workflow.api.WorkflowServiceFactory;
053:
054: import common.UTLoginContext;
055: import junit.framework.Test;
056: import junit.framework.TestCase;
057: import junit.framework.TestSuite;
058:
059: /**
060: * Test Jelly
061: */
062: public class JellyTests extends TestCase {
063: private static UTLoginContext plc = null;
064: static {
065: try {
066: plc = new UTLoginContext();
067: plc.login();
068: } catch (LoginException e) {
069: throw new IllegalStateException(e.getMessage());
070: }
071: }
072:
073: /**
074: * A process directory reference.
075: */
076: private ProcessDirectory pdd = null;
077:
078: /**
079: * Constructor of this TestCase
080: */
081: public JellyTests(String name) {
082: super (name);
083: }
084:
085: /**
086: * Stellt diese TestSuite zusammen.
087: */
088: public static Test suite() {
089: TestSuite suite = new TestSuite();
090: suite.addTest(new JellyTests("importProcessDefinitions"));
091: suite.addTest(new JellyTests("basicTest"));
092: suite.addTest(new JellyTests("insertTest"));
093: suite.addTest(new JellyTests("resultsTest"));
094: suite.addTest(new JellyTests("dbTest"));
095: return new EJBClientTest(plc, suite);
096: }
097:
098: private WorkflowService workflowService = null;
099:
100: /**
101: * Initialisierung.
102: */
103: protected void setUp() throws Exception {
104: try {
105: WorkflowServiceFactory wfsf = WorkflowServiceFactory
106: .newInstance();
107: workflowService = wfsf.newWorkflowService();
108: } catch (FactoryConfigurationError e) {
109: throw new IllegalStateException(e.getMessage());
110: }
111: }
112:
113: protected void tearDown() throws Exception {
114: workflowService.release(workflowService);
115: workflowService = null;
116: }
117:
118: /**
119: * Import the process definitions from a XPDL file
120: * unsing the ProcessDefinitionDirectory bean.
121: */
122: public void importProcessDefinitions() throws Exception {
123: // Create process definition directory bean
124: ProcessDefinitionDirectory pdd = workflowService
125: .processDefinitionDirectory();
126:
127: InputStream is = getClass().getResourceAsStream(
128: "/tools/jellytests.xml");
129: assertTrue(is != null);
130: BufferedReader br = new BufferedReader(new InputStreamReader(
131: is, "ISO-8859-1"));
132: StringBuffer sb = new StringBuffer();
133: String st;
134: while ((st = br.readLine()) != null) {
135: sb.append(st + "\n");
136: }
137: pdd.importProcessDefinitions(sb.toString());
138: Collection processDefinitions = pdd.processDefinitions();
139: assertTrue(processDefinitions.size() > 0);
140: }
141:
142: /**
143: * Test.
144: */
145: public void basicTest() throws Exception {
146: ProcessDefinitionDirectory procDefDir = null;
147: ProcessDirectory procDir = null;
148: try {
149: procDefDir = workflowService.processDefinitionDirectory();
150: procDir = workflowService.processDirectory();
151: ProcessMgr pmgr = procDefDir.processMgr("jellytests",
152: "basicTest");
153: WfProcess process = pmgr
154: .createProcess(new DefaultRequester(workflowService));
155: process.start();
156: assertTrue(stateReached(process, "closed.completed"));
157: procDir.removeProcess(process);
158: } finally {
159: workflowService.release(procDefDir);
160: workflowService.release(procDir);
161: }
162: }
163:
164: /**
165: * Test.
166: */
167: public void insertTest() throws Exception {
168: ProcessDefinitionDirectory procDefDir = null;
169: ProcessDirectory procDir = null;
170: try {
171: procDefDir = workflowService.processDefinitionDirectory();
172: procDir = workflowService.processDirectory();
173: ProcessMgr pmgr = procDefDir.processMgr("jellytests",
174: "insertTest");
175: WfProcess process = pmgr
176: .createProcess(new DefaultRequester(workflowService));
177: process.start();
178: assertTrue(stateReached(process, "closed.completed"));
179: procDir.removeProcess(process);
180: } finally {
181: workflowService.release(procDefDir);
182: workflowService.release(procDir);
183: }
184: }
185:
186: /**
187: * Test.
188: */
189: public void resultsTest() throws Exception {
190: ProcessDefinitionDirectory procDefDir = null;
191: ProcessDirectory procDir = null;
192: try {
193: procDefDir = workflowService.processDefinitionDirectory();
194: procDir = workflowService.processDirectory();
195: ProcessMgr pmgr = procDefDir.processMgr("jellytests",
196: "resultsTest");
197: WfProcess process = pmgr
198: .createProcess(new DefaultRequester(workflowService));
199: process.start();
200: assertTrue(stateReached(process, "closed.completed"));
201: procDir.removeProcess(process);
202: } finally {
203: workflowService.release(procDefDir);
204: workflowService.release(procDir);
205: }
206: }
207:
208: /**
209: * Test.
210: */
211: public void dbTest() throws Exception {
212: ProcessDefinitionDirectory procDefDir = null;
213: ProcessDirectory procDir = null;
214: try {
215: procDefDir = workflowService.processDefinitionDirectory();
216: procDir = workflowService.processDirectory();
217: ProcessMgr pmgr = procDefDir.processMgr("jellytests",
218: "dbTest");
219: WfProcess process = pmgr
220: .createProcess(new DefaultRequester(workflowService));
221: process.start();
222: assertTrue(stateReached(process, "closed.completed"));
223: procDir.removeProcess(process);
224: } finally {
225: workflowService.release(procDefDir);
226: workflowService.release(procDir);
227: }
228: }
229:
230: private WfProcess createProcess(String pkgId, String prcId,
231: WfRequester req) throws Exception {
232: ProcessDefinitionDirectory procDir = null;
233: try {
234: procDir = workflowService.processDefinitionDirectory();
235: ProcessMgr pmgr = procDir.processMgr(pkgId, prcId);
236: return pmgr.createProcess(req);
237: } finally {
238: workflowService.release(procDir);
239: }
240: }
241:
242: private boolean stateReached(WfProcess proc, String procState)
243: throws Exception {
244: boolean test = true;
245: boolean stateReached = false;
246: int maxRetries = 100;
247: while (test) {
248: if (maxRetries-- > 0) {
249: if (proc.state().startsWith(procState)) {
250: stateReached = true;
251: test = false;
252: } else {
253: Thread.sleep(500);
254: }
255: } else {
256: test = false;
257: }
258: }
259: return stateReached;
260: }
261:
262: private boolean stateReached(WfActivity act, String actState)
263: throws Exception {
264: boolean test = true;
265: boolean stateReached = false;
266: int maxRetries = 100;
267: while (test) {
268: if (maxRetries-- > 0) {
269: if (act.state().startsWith(actState)) {
270: stateReached = true;
271: test = false;
272: } else {
273: Thread.sleep(500);
274: }
275: } else {
276: test = false;
277: }
278: }
279: return stateReached;
280: }
281: }
|