001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2005 Danet GmbH (www.danet.de), BU TEL.
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: InvokeProcess.java,v 1.4 2007/03/27 21:59:42 mlipp Exp $
021: *
022: * $Log: InvokeProcess.java,v $
023: * Revision 1.4 2007/03/27 21:59:42 mlipp
024: * Fixed lots of checkstyle warnings.
025: *
026: * Revision 1.3 2006/09/29 12:32:13 drmlipp
027: * Consistently using WfMOpen as projct name now.
028: *
029: * Revision 1.2 2005/04/22 15:11:06 drmlipp
030: * Merged changes from 1.3 branch up to 1.3p15.
031: *
032: * Revision 1.1.2.1 2005/04/18 13:06:09 drmlipp
033: * Invoke a single process.
034: *
035: */
036: package load;
037:
038: import java.io.BufferedReader;
039: import java.io.InputStream;
040: import java.io.InputStreamReader;
041:
042: import javax.security.auth.login.LoginException;
043:
044: import de.danet.an.util.junit.EJBClientTest;
045:
046: import de.danet.an.workflow.omgcore.WfProcess;
047: import de.danet.an.workflow.omgcore.WfRequester;
048:
049: import de.danet.an.workflow.api.DefaultRequester;
050: import de.danet.an.workflow.api.FactoryConfigurationError;
051: import de.danet.an.workflow.api.ProcessDefinitionDirectory;
052: import de.danet.an.workflow.api.ProcessMgr;
053: import de.danet.an.workflow.api.WorkflowService;
054: import de.danet.an.workflow.api.WorkflowServiceFactory;
055:
056: import common.UTLoginContext;
057: import junit.framework.Test;
058: import junit.framework.TestCase;
059: import junit.framework.TestSuite;
060:
061: /**
062: * This class provides ...
063: *
064: * @author <a href="mailto:mnl@mnl.de">Michael N. Lipp</a>
065: * @version $Revision: 1.4 $
066: */
067:
068: public class InvokeProcess extends TestCase {
069:
070: private static UTLoginContext plc = null;
071: static {
072: try {
073: plc = new UTLoginContext();
074: plc.login();
075: } catch (LoginException e) {
076: throw new IllegalStateException(e.getMessage());
077: }
078: }
079:
080: private static WorkflowService wfsCache = null;
081:
082: /**
083: * Creates an instance of <code>InvokeProcess</code>
084: * with all attributes initialized to default values.
085: */
086: public InvokeProcess(String name) {
087: super (name);
088: }
089:
090: /**
091: * Make this test suite.
092: */
093: public static Test suite() {
094: TestSuite rootSuite = new TestSuite();
095: // rootSuite.addTest(new InvokeProcess("importProcessDefinitions"));
096: rootSuite.addTest(new InvokeProcess("runTest"));
097: return new EJBClientTest(plc, rootSuite);
098: }
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: WorkflowServiceFactory wsf = WorkflowServiceFactory
119: .newInstance();
120: WorkflowService ws = workflowService();
121: ProcessDefinitionDirectory pdd = ws
122: .processDefinitionDirectory();
123: InputStream is = getClass().getResourceAsStream(
124: "/load/testByAudit2.xml");
125: assertTrue(is != null);
126: BufferedReader br = new BufferedReader(new InputStreamReader(
127: is, "ISO-8859-1"));
128: StringBuffer sb = new StringBuffer();
129: String st;
130: while ((st = br.readLine()) != null) {
131: sb.append(st + "\n");
132: }
133: pdd.importProcessDefinitions(sb.toString());
134: }
135:
136: public void runTest() throws Exception {
137: WfRequester req = new DefaultRequester(workflowService());
138: WfProcess process = createProcess("testByAudit2",
139: "testByAudit_emptySeq_na", req);
140: assertTrue(process != null);
141: process.start();
142: assertTrue(stateReached(process, "closed"));
143: }
144:
145: /**
146: * Creates the process
147: * using the ProcessDefinitionDirectory bean.
148: */
149: private WfProcess createProcess(String pkgId, String prcId,
150: WfRequester req) throws Exception {
151: ProcessDefinitionDirectory procDir = null;
152: try {
153: procDir = workflowService().processDefinitionDirectory();
154: ProcessMgr pmgr = procDir.processMgr(pkgId, prcId);
155: return pmgr.createProcess(req);
156: } finally {
157: workflowService().release(procDir);
158: }
159: }
160:
161: private boolean stateReached(WfProcess proc, String procState)
162: throws Exception {
163: int maxRetries = 30;
164: while (true) {
165: if (proc.state().startsWith(procState)) {
166: return true;
167: }
168: if (maxRetries-- <= 0) {
169: return false;
170: }
171: Thread.sleep(1000);
172: }
173: }
174:
175: }
|