001: /*
002: * Danet GmbH
003: * Beratung und Software-Entwicklung
004: * Geschäftstelle AN
005: *
006: * $Id: SubFlow.java,v 1.4 2007/03/27 21:59:42 mlipp Exp $
007: *
008: * $Log: SubFlow.java,v $
009: * Revision 1.4 2007/03/27 21:59:42 mlipp
010: * Fixed lots of checkstyle warnings.
011: *
012: * Revision 1.3 2006/10/13 13:58:32 drmlipp
013: * Adapted to new environment.
014: *
015: * Revision 1.2 2006/10/07 20:41:34 mlipp
016: * Merged J2EE 1.4 adaptions from test branch.
017: *
018: * Revision 1.1.1.1 2004/08/18 15:18:47 drmlipp
019: * Update to 1.2
020: *
021: * Revision 1.1 2004/07/18 17:07:53 lipp
022: * Added test.
023: *
024: */
025: package process;
026:
027: import java.io.BufferedReader;
028: import java.io.InputStream;
029: import java.io.InputStreamReader;
030:
031: import java.util.Collection;
032: import java.util.Iterator;
033:
034: import javax.naming.InitialContext;
035: import javax.rmi.PortableRemoteObject;
036: import javax.security.auth.login.LoginException;
037:
038: import de.danet.an.util.EJBUtil;
039: import de.danet.an.util.junit.EJBClientTest;
040:
041: import de.danet.an.workflow.omgcore.WfActivity;
042: import de.danet.an.workflow.omgcore.WfExecutionObject;
043: import de.danet.an.workflow.omgcore.WfExecutionObject.State;
044: import de.danet.an.workflow.omgcore.WfProcess;
045: import de.danet.an.workflow.omgcore.WfProcessMgr;
046: import de.danet.an.workflow.omgcore.WfRequester;
047:
048: import de.danet.an.workflow.api.DefaultRequester;
049: import de.danet.an.workflow.api.FactoryConfigurationError;
050: import de.danet.an.workflow.api.FormalParameter;
051: import de.danet.an.workflow.api.Process;
052: import de.danet.an.workflow.api.ProcessDefinition;
053: import de.danet.an.workflow.api.ProcessDefinitionDirectory;
054: import de.danet.an.workflow.api.ProcessDirectory;
055: import de.danet.an.workflow.api.WorkflowService;
056: import de.danet.an.workflow.api.WorkflowServiceFactory;
057:
058: import de.danet.an.workflow.ejbs.admin.ProcessDefinitionDirectoryHome;
059: import de.danet.an.workflow.ejbs.core.WfActivityHome;
060:
061: import common.UTLoginContext;
062: import junit.framework.Test;
063: import junit.framework.TestCase;
064: import junit.framework.TestSuite;
065:
066: /**
067: * Zusammenstellung aller TimerObjectTests.
068: */
069: public class SubFlow extends TestCase {
070:
071: private static UTLoginContext plc = null;
072: static {
073: try {
074: plc = new UTLoginContext();
075: plc.login();
076: } catch (LoginException e) {
077: throw new IllegalStateException(e.getMessage());
078: }
079: }
080:
081: /**
082: * Konstruktor zum Erzeugen eines TestCase
083: */
084: public SubFlow(String name) {
085: super (name);
086: }
087:
088: /**
089: * Stellt diese TestSuite zusammen.
090: */
091: public static Test suite() {
092: TestSuite suite = new TestSuite();
093: suite.addTest(new SubFlow("importProcessDefinitions"));
094: suite.addTest(new SubFlow("checkFormalParams"));
095: return new EJBClientTest(plc, suite);
096: }
097:
098: private static WorkflowService wfsCache = null;
099:
100: private WorkflowService workflowService() {
101: if (wfsCache == null) {
102: try {
103: WorkflowServiceFactory wfsf = WorkflowServiceFactory
104: .newInstance();
105: wfsCache = wfsf.newWorkflowService();
106: } catch (FactoryConfigurationError e) {
107: throw new IllegalStateException(e.getMessage());
108: }
109: }
110: return wfsCache;
111: }
112:
113: /**
114: * Import the process definitions from a XPDL file
115: * unsing the ProcessDefinitionDirectory bean.
116: */
117: public void importProcessDefinitions() throws Exception {
118: // Create process definition directory bean
119: ProcessDefinitionDirectory pdd = workflowService()
120: .processDefinitionDirectory();
121:
122: InputStream is = getClass().getResourceAsStream(
123: "/process/subflow.xml");
124: assertTrue(is != null);
125: BufferedReader br = new BufferedReader(new InputStreamReader(
126: is, "ISO-8859-1"));
127: StringBuffer sb = new StringBuffer();
128: String st;
129: while ((st = br.readLine()) != null) {
130: sb.append(st + "\n");
131: }
132: pdd.importProcessDefinitions(sb.toString());
133: Collection processDefinitions = pdd.processDefinitions();
134: assertTrue(processDefinitions.size() > 0);
135:
136: }
137:
138: private WfProcess createProcess(String pkgId, String prcId,
139: WfRequester req) throws Exception {
140: ProcessDefinitionDirectory pdd = null;
141: try {
142: pdd = workflowService().processDefinitionDirectory();
143: WfProcessMgr pmgr = pdd.processMgr(pkgId, prcId);
144: return pmgr.createProcess(req);
145: } finally {
146: workflowService().release(pdd);
147: }
148: }
149:
150: /**
151: * Create a new process for the given type.
152: */
153: public void checkFormalParams() throws Exception {
154: WfRequester cont = new DefaultRequester(workflowService());
155: Process process = (Process) createProcess("subflowtest",
156: "called1", cont);
157: FormalParameter[] fps = process.processDefinition()
158: .formalParameters();
159: assertTrue(fps.length == 2);
160: assertTrue(fps[0].id().equals("testData"));
161: assertTrue(fps[1].id().equals("status"));
162: }
163:
164: /**
165: * Create a new process for the given type and start it.
166: */
167: public void createAndStartProcess1() throws Exception {
168: WfRequester cont = new DefaultRequester(workflowService());
169: WfProcess process = createProcess("ut-process", "jut1", cont);
170: assertTrue(process.state().startsWith(
171: "open.not_running.not_started"));
172: process.start();
173: assertTrue(process.state().startsWith("open.running"));
174: // get the activities
175: Collection c = process.steps();
176: Iterator it = c.iterator();
177: while (it.hasNext()) {
178: WfActivity a = (WfActivity) it.next();
179: assertTrue(a.state() != null);
180: }
181: }
182:
183: /**
184: * Create a new process for the given type and start it.
185: */
186: public void createAndStartProcess2() throws Exception {
187: WfRequester cont = new DefaultRequester(workflowService());
188: WfProcess process = createProcess("ut-process", "jut2", cont);
189: assertTrue(process.state().startsWith(
190: "open.not_running.not_started"));
191: process.start();
192: assertTrue(process.state().startsWith("open.running"));
193: // get the activities
194: Collection c = process.steps();
195: Iterator it = c.iterator();
196: while (it.hasNext()) {
197: WfActivity a = (WfActivity) it.next();
198: assertTrue(a.state() != null);
199: }
200: }
201:
202: /**
203: * Create a new process for the given type and start it.
204: */
205: public void createAndStartProcess3() throws Exception {
206: WfRequester cont = new DefaultRequester(workflowService());
207: WfProcess process = createProcess("ut-process", "jut3", cont);
208: assertTrue(process.state().startsWith(
209: "open.not_running.not_started"));
210: process.start();
211: Thread.sleep(2500);
212: assertTrue(process + " should be completed, is "
213: + process.state(), process.state().startsWith(
214: "closed.completed"));
215: // get the activities
216: Collection c = process.steps();
217: Iterator it = c.iterator();
218: int cnt = 0;
219: while (it.hasNext()) {
220: WfActivity a = (WfActivity) it.next();
221: if (a.state().startsWith(
222: WfExecutionObject.ClosedState.COMPLETED.toString())) {
223: cnt += 1;
224: }
225: }
226: assertTrue(cnt == 5);
227: }
228:
229: /**
230: * Create a new process for the given type.
231: */
232: public void createProcess2() throws Exception {
233: WfRequester cont = new DefaultRequester(workflowService());
234: WfProcess process = createProcess("ut-process", "jut2", cont);
235: assertTrue(process.state().startsWith(
236: "open.not_running.not_started"));
237: process.start();
238: Thread.sleep(1000);
239: assertTrue(process.state().startsWith("open.running"));
240: int i = 9;
241: int resCnt = 0;
242: while (i-- > 0) {
243: boolean resd = false;
244: for (Iterator it = process.steps().iterator(); it.hasNext();) {
245: WfActivity a = (WfActivity) it.next();
246: if (a.state().startsWith("open.not_running.suspended")) {
247: a.resume();
248: resd = true;
249: resCnt += 1;
250: Thread.sleep(500);
251: }
252: }
253: if (!resd) {
254: break;
255: }
256: }
257: assertTrue(i >= 0);
258: assertTrue(resCnt == 8);
259: }
260:
261: /**
262: * Create a new process for the given type.
263: */
264: public void createProcess3() throws Exception {
265: WfRequester cont = new DefaultRequester(workflowService());
266: WfProcess process = createProcess("ut-process", "jut3", cont);
267: assertTrue(process.state().startsWith(
268: "open.not_running.not_started"));
269: process.start();
270: Thread.sleep(5000);
271: assertTrue(
272: "Process state should be closed.completed.normal, is "
273: + process.state(), process.state().startsWith(
274: "closed.completed.normal"));
275: assertTrue("Process workflow state should be closed, is "
276: + process.workflowState(), process.workflowState()
277: .equals(State.CLOSED));
278: }
279:
280: /**
281: * Remove a given process from the database.
282: */
283: public void removeProcess3() throws Exception {
284: WfRequester cont = new DefaultRequester(workflowService());
285: WfProcess process = createProcess("ut-process", "jut999", cont);
286: ProcessDirectory pd = null;
287: try {
288: pd = workflowService().processDirectory();
289: pd.removeProcess((Process) process);
290: } finally {
291: workflowService().release(pd);
292: }
293: }
294:
295: /**
296: * Returns a list of all activities assigned to a given process
297: * from the ActivityBean.
298: */
299: public void activityFindByProcess() throws Exception {
300: InitialContext ic = new InitialContext();
301: Object objref = ic.lookup("ActivityBean");
302: WfActivityHome home = (WfActivityHome) PortableRemoteObject
303: .narrow(objref, WfActivityHome.class);
304: assertTrue(home != null);
305: Collection c = home.findByProcess(new Long(1));
306: assertTrue(c != null);
307: }
308:
309: /**
310: * Create a new process and then remove its process definition.
311: */
312: public void createProcess1AndRemoveProcessDef() throws Exception {
313: ProcessDefinitionDirectory pdd = null;
314: try {
315: WfRequester cont = new DefaultRequester(workflowService());
316: Process process = (Process) createProcess("ut-process",
317: "jut1", cont);
318: assertTrue(process.state().startsWith(
319: "open.not_running.not_started"));
320: process.start();
321: assertTrue(process.state().startsWith("open.running"));
322: // remove its process definition
323: pdd = workflowService().processDefinitionDirectory();
324: ProcessDefinition pd = pdd.lookupProcessDefinition(
325: "ut-process", "jut1");
326: assertTrue(pd != null);
327: pdd.removeProcessDefinition("ut-process", "jut1");
328: boolean gotEx = false;
329: try {
330: pd = pdd.lookupProcessDefinition("ut-process", "jut1");
331: } catch (Exception ex) {
332: gotEx = true;
333: }
334: assertTrue(gotEx);
335: // check the process definition of the process
336: ProcessDefinition procDef = process.processDefinition();
337: assertTrue(procDef.packageId().equals("ut-process"));
338: assertTrue(procDef.processId().equals("jut1"));
339: // import the process definition again.
340: importProcessDefinitions();
341: pd = pdd.lookupProcessDefinition("ut-process", "jut1");
342: assertTrue(pd != null);
343: } finally {
344: workflowService().release(pdd);
345: }
346: }
347:
348: public static void importProcessDefinitions(String filename)
349: throws Exception {
350: // Create process definition directory bean
351: ProcessDefinitionDirectoryHome pddh = (ProcessDefinitionDirectoryHome) EJBUtil
352: .lookupEJBHome(ProcessDefinitionDirectoryHome.class,
353: "ejb/de.danet.an.wfdemo.ProcessDefinitionDirectory");
354: ProcessDefinitionDirectory pdd = pddh.create();
355: InputStream is = SubFlow.class.getResourceAsStream(filename);
356: assertTrue(is != null);
357: BufferedReader br = new BufferedReader(new InputStreamReader(
358: is, "ISO-8859-1"));
359: StringBuffer sb = new StringBuffer();
360: String st;
361: while ((st = br.readLine()) != null) {
362: sb.append(st + "\n");
363: }
364: pdd.importProcessDefinitions(sb.toString());
365: Collection processDefinitions = pdd.processDefinitions();
366: assertTrue(processDefinitions.size() > 0);
367: }
368: }
|