001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2005 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: DeferredChoice.java,v 1.5 2007/03/27 21:59:43 mlipp Exp $
021: *
022: * $Log: DeferredChoice.java,v $
023: * Revision 1.5 2007/03/27 21:59:43 mlipp
024: * Fixed lots of checkstyle warnings.
025: *
026: * Revision 1.4 2006/09/29 12:32:08 drmlipp
027: * Consistently using WfMOpen as projct name now.
028: *
029: * Revision 1.3 2005/02/23 15:43:20 drmlipp
030: * Synchronized with 1.3.
031: *
032: * Revision 1.1.2.4 2005/02/04 15:54:38 drmlipp
033: * Fixed race condition.
034: *
035: * Revision 1.1.2.3 2005/02/04 15:28:39 drmlipp
036: * Fixed import.
037: *
038: * Revision 1.2 2005/02/04 14:25:27 drmlipp
039: * Synchronized with 1.3rc2.
040: *
041: * Revision 1.1.2.2 2005/02/04 10:43:42 drmlipp
042: * Added more tests.
043: *
044: * Revision 1.1.2.1 2005/02/03 20:55:54 drmlipp
045: * Added test.
046: *
047: */
048: package process;
049:
050: import java.io.BufferedReader;
051: import java.io.InputStream;
052: import java.io.InputStreamReader;
053: import java.rmi.RemoteException;
054: import java.util.Iterator;
055:
056: import junit.framework.Test;
057: import junit.framework.TestSuite;
058: import de.danet.an.util.junit.EJBClientTest;
059: import de.danet.an.workflow.api.Activity;
060: import de.danet.an.workflow.api.DefaultRequester;
061: import de.danet.an.workflow.api.ImportException;
062: import de.danet.an.workflow.api.PrioritizedMessage;
063: import de.danet.an.workflow.api.ProcessDefinitionDirectory;
064: import de.danet.an.workflow.api.ProcessDirectory;
065: import de.danet.an.workflow.api.ProcessMgr;
066: import de.danet.an.workflow.api.WorkflowService;
067: import de.danet.an.workflow.api.WorkflowServiceFactory;
068: import de.danet.an.workflow.omgcore.WfProcess;
069: import de.danet.an.workflow.omgcore.WfRequester;
070: import de.danet.an.workflow.omgcore.ProcessData;
071:
072: /**
073: * Test of DeferredChoices within workflow processes.
074: * After completion, all previously created processes and process definitions
075: * are removed.
076: * @version 1.0
077: */
078: public class DeferredChoice extends WfMOpenTestCase {
079: /**
080: * Access to process definition directory (singleton)
081: */
082: private ProcessDefinitionDirectory defDir = null;
083:
084: /**
085: * Access to process directory (singleton)
086: */
087: private ProcessDirectory procDir = null;
088:
089: /**
090: * Access to default requester (singleton)
091: */
092: private WfRequester requester = null;
093:
094: /**
095: * Constructor of this TestCase
096: * @param name a <code>String</code> value
097: */
098: public DeferredChoice(String name) {
099: super (name);
100: }
101:
102: /**
103: * Construct this test suite.
104: * @return a <code>Test</code> value
105: */
106: public static Test suite() {
107: TestSuite suite = new TestSuite();
108: suite.addTest(new DeferredChoice("loopTest"));
109: suite.addTest(new DeferredChoice("terminateProcessTest"));
110: suite.addTest(new DeferredChoice("terminateActivityTest"));
111: return new EJBClientTest(plc, suite);
112: }
113:
114: /**
115: * Test.
116: * @exception Exception if an error occurs
117: */
118: public void loopTest() throws Exception {
119: ProcessMgr mgr = defDir.processMgr("deferredChoiceTests",
120: "loopingDeferredChoiceTest");
121: WfProcess proc = mgr.createProcess(requester);
122: String procKey = proc.key();
123: proc.start();
124: assertTrue(stateReached(proc, "closed.completed"));
125: ProcessData data = proc.processContext();
126: String path = (String) data.get("Path");
127: assertTrue(path, path
128: .equals("Path:ACT0:ACT1:ACT2:ACT5:ACT1:ACT2:ACT5"
129: + ":ACT1:ACT3:ACT5:ACT1:ACT3:ACT5"));
130: procDir.removeProcess(proc);
131: }
132:
133: /**
134: * Test.
135: * @exception Exception if an error occurs
136: */
137: public void terminateProcessTest() throws Exception {
138: ProcessMgr mgr = defDir.processMgr("deferredChoiceTests",
139: "terminateTest");
140: WfProcess proc = mgr.createProcess(requester);
141: String procKey = proc.key();
142: proc.start();
143: assertTrue(stateReached(proc, "open.running"));
144: proc.terminate();
145: assertTrue(stateReached(proc, "closed.terminated"));
146: procDir.removeProcess(proc);
147: }
148:
149: /**
150: * Test.
151: * @exception Exception if an error occurs
152: */
153: public void terminateActivityTest() throws Exception {
154: ProcessMgr mgr = defDir.processMgr("deferredChoiceTests",
155: "terminateTest");
156: WfProcess proc = mgr.createProcess(requester);
157: String procKey = proc.key();
158: proc.start();
159: Activity act2 = null;
160: Activity act3 = null;
161: assertTrue(stateReached(proc, "open.running"));
162: for (Iterator i = proc.steps().iterator(); i.hasNext();) {
163: Activity a = (Activity) i.next();
164: if (a.name().equals("ACT2")) {
165: act2 = a;
166: } else if (a.name().equals("ACT3")) {
167: act3 = a;
168: }
169: }
170: assertTrue(act2 != null);
171: assertTrue(act3 != null);
172: assertTrue(stateReached(act2, "open.running"));
173: assertTrue(stateReached(act3, "open.running"));
174: act2.terminate();
175: assertTrue(stateReached(act2, "closed.terminated"));
176: assertTrue(stateReached(act3, "open.not_running.not_started"));
177: assertTrue(stateReached(proc, "closed.terminated"));
178: procDir.removeProcess(proc);
179: }
180:
181: /**
182: * Initialisation.
183: * The <code>setUp</code> method defines the way a state change is
184: * realized. Override this method to change this way.
185: * @exception Exception if an error occurs
186: */
187: protected void setUp() throws Exception {
188: super .setUp();
189: WorkflowService wfs = WorkflowServiceFactory.newInstance()
190: .newWorkflowService();
191: try {
192: defDir = wfs.processDefinitionDirectory();
193: } catch (RemoteException exc) {
194: System.err
195: .println("Process definition directory not accessible: "
196: + exc.getMessage());
197: System.exit(-1);
198: }
199:
200: procDir = wfs.processDirectory();
201: requester = new DefaultRequester(wfs);
202: importProcessDefinition("/process/deferredChoice.xml");
203: }
204:
205: private void importProcessDefinition(String name) throws Exception {
206: StringBuffer processDefinition = new StringBuffer();
207: InputStream is = getClass().getResourceAsStream(name);
208: BufferedReader in = new BufferedReader(new InputStreamReader(
209: is, "ISO-8859-1"));
210: String line = null;
211: while ((line = in.readLine()) != null) {
212: processDefinition.append(line + "\n");
213: }
214: try {
215: defDir.importProcessDefinitions(processDefinition
216: .toString());
217: } catch (ImportException exc) {
218: Iterator msg = exc.messages().iterator();
219: while (msg.hasNext()) {
220: System.out.println(((PrioritizedMessage) msg.next())
221: .message());
222: }
223: }
224: }
225:
226: private boolean stateReached(WfProcess proc, String procState)
227: throws Exception {
228: boolean test = true;
229: boolean stateReached = false;
230: int maxRetries = 100;
231: while (test) {
232: if (maxRetries-- > 0) {
233: if (proc.state().startsWith(procState)) {
234: stateReached = true;
235: test = false;
236: } else {
237: Thread.sleep(500);
238: }
239: } else {
240: test = false;
241: }
242: }
243: return stateReached;
244: }
245:
246: private boolean stateReached(Activity act, String actState)
247: throws Exception {
248: boolean test = true;
249: boolean stateReached = false;
250: int maxRetries = 100;
251: while (test) {
252: if (maxRetries-- > 0) {
253: if (act.state().startsWith(actState)) {
254: stateReached = true;
255: test = false;
256: } else {
257: Thread.sleep(500);
258: }
259: } else {
260: test = false;
261: }
262: }
263: return stateReached;
264: }
265: }
|