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: ActParamTests.java,v 1.4 2007/03/27 21:59:42 mlipp Exp $
021: *
022: * $Log: ActParamTests.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:10 drmlipp
027: * Consistently using WfMOpen as projct name now.
028: *
029: * Revision 1.2 2006/03/08 14:46:43 drmlipp
030: * Synchronized with 1.3.3p5.
031: *
032: * Revision 1.1.2.1 2006/03/03 15:49:44 drmlipp
033: * Renamed and tests for E4X added.
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/06/30 13:24:17 lipp
039: * Added jelly test.
040: *
041: */
042: package process;
043:
044: import java.io.BufferedReader;
045: import java.io.InputStream;
046: import java.io.InputStreamReader;
047:
048: import java.util.Iterator;
049:
050: import javax.security.auth.login.LoginException;
051:
052: import org.jaxen.XPath;
053: import org.jaxen.jdom.JDOMXPath;
054: import org.jdom.Document;
055: import org.jdom.input.SAXHandler;
056: import org.w3c.dom.Node;
057: import org.w3c.dom.CharacterData;
058:
059: import de.danet.an.util.junit.EJBClientTest;
060: import de.danet.an.util.sax.SAXContentBuffer;
061: import de.danet.an.util.sax.SAXEventLogger;
062:
063: import de.danet.an.workflow.omgcore.WfProcess;
064: import de.danet.an.workflow.omgcore.WfProcessMgr;
065: import de.danet.an.workflow.omgcore.WfRequester;
066:
067: import de.danet.an.workflow.api.DefaultRequester;
068: import de.danet.an.workflow.api.FactoryConfigurationError;
069: import de.danet.an.workflow.api.Process;
070: import de.danet.an.workflow.api.ProcessDefinition;
071: import de.danet.an.workflow.api.ProcessDefinitionDirectory;
072: import de.danet.an.workflow.api.ProcessDirectory;
073: import de.danet.an.workflow.api.SAXEventBuffer;
074: import de.danet.an.workflow.api.WorkflowService;
075: import de.danet.an.workflow.api.WorkflowServiceFactory;
076:
077: import common.UTLoginContext;
078: import junit.framework.Test;
079: import junit.framework.TestCase;
080: import junit.framework.TestSuite;
081:
082: /**
083: * This class provides ...
084: *
085: * @author <a href="mailto:lipp@danet.de">Michael Lipp</a>
086: * @version $Revision: 1.4 $
087: */
088:
089: public class ActParamTests extends TestCase {
090:
091: private static UTLoginContext plc = null;
092: static {
093: try {
094: plc = new UTLoginContext();
095: plc.login();
096: } catch (LoginException e) {
097: throw new IllegalStateException(e.getMessage());
098: }
099: }
100:
101: /**
102: * Creates an instance of <code>JellyTests</code>
103: * with all attributes initialized to default values.
104: */
105: public ActParamTests(String name) {
106: super (name);
107: }
108:
109: public static Test suite() {
110: TestSuite suite = new TestSuite();
111: suite.addTest(new ActParamTests("importProcessDefinitions"));
112: suite.addTest(new ActParamTests("jellyArg"));
113: suite.addTest(new ActParamTests("e4xArg"));
114: suite.addTest(new ActParamTests("removeProcessDefinitions"));
115: return new EJBClientTest(plc, suite);
116: }
117:
118: private static WorkflowService wfsCache = null;
119:
120: private WorkflowService workflowService() {
121: if (wfsCache == null) {
122: try {
123: WorkflowServiceFactory wfsf = WorkflowServiceFactory
124: .newInstance();
125: wfsCache = wfsf.newWorkflowService();
126: } catch (FactoryConfigurationError e) {
127: throw new IllegalStateException(e.getMessage());
128: }
129: }
130: return wfsCache;
131: }
132:
133: /**
134: * Import the process definitions from a XPDL file
135: * unsing the ProcessDefinitionDirectory bean.
136: */
137: public void importProcessDefinitions() throws Exception {
138: ProcessDefinitionDirectory pdd = null;
139: try {
140: pdd = workflowService().processDefinitionDirectory();
141:
142: InputStream is = getClass().getResourceAsStream(
143: "/process/actParamTests.xml");
144: assertTrue(is != null);
145: BufferedReader br = new BufferedReader(
146: new InputStreamReader(is, "ISO-8859-1"));
147: StringBuffer sb = new StringBuffer();
148: String st;
149: while ((st = br.readLine()) != null) {
150: sb.append(st + "\n");
151: }
152: pdd.importProcessDefinitions(sb.toString());
153: } finally {
154: workflowService().release(pdd);
155: }
156: }
157:
158: private WfProcess createProcess(String pkgId, String prcId,
159: WfRequester req) throws Exception {
160: ProcessDefinitionDirectory pdd = null;
161: try {
162: pdd = workflowService().processDefinitionDirectory();
163: WfProcessMgr pmgr = pdd.processMgr(pkgId, prcId);
164: return pmgr.createProcess(req);
165: } finally {
166: workflowService().release(pdd);
167: }
168: }
169:
170: /**
171: * Run test.
172: */
173: public void jellyArg() throws Exception {
174: ProcessDirectory pd = null;
175: try {
176: pd = workflowService().processDirectory();
177: WfRequester cont = new DefaultRequester(workflowService());
178: Process process = (Process) createProcess("actParamTests",
179: "jelly-test1", cont);
180: process.start();
181: assertTrue(stateReached(process, "closed.completed"));
182: SAXEventBuffer res = (SAXEventBuffer) process
183: .processContext().get("result");
184: SAXHandler sh = new SAXHandler();
185: res.emit(sh);
186: Document resDoc = sh.getDocument();
187: XPath xpath = new JDOMXPath(
188: "/result/inserted/root/element1/@attr1");
189: String val = xpath.stringValueOf(resDoc);
190: assertTrue("Value is: " + val, val.equals("42"));
191: xpath = new JDOMXPath("/result/test");
192: val = xpath.stringValueOf(resDoc);
193: assertTrue("Value is: " + val, val.equals("42"));
194: xpath = new JDOMXPath("count(/result/test)");
195: val = xpath.stringValueOf(resDoc);
196: assertTrue("Value is: " + val, val.equals("5"));
197: pd.removeProcess(process);
198: } finally {
199: workflowService().release(pd);
200: }
201: }
202:
203: /**
204: * Run test.
205: */
206: public void e4xArg() throws Exception {
207: ProcessDirectory pd = null;
208: try {
209: pd = workflowService().processDirectory();
210: WfRequester cont = new DefaultRequester(workflowService());
211: Process process = (Process) createProcess("actParamTests",
212: "testE4xActParam", cont);
213: process.start();
214: assertTrue(stateReached(process, "closed.completed"));
215: SAXContentBuffer res = (SAXContentBuffer) process
216: .processContext().get("result");
217: Node resRoot = res.toW3cDom();
218: Node fc = resRoot.getFirstChild();
219: assertTrue(fc.toString(), fc.getLocalName().equals("Hello"));
220: Node child = fc.getFirstChild();
221: String data = "";
222: do {
223: if (child instanceof CharacterData) {
224: data = data + ((CharacterData) child).getData();
225: }
226: child = child.getNextSibling();
227: } while (child != null);
228: assertTrue(data.equals("World"));
229: pd.removeProcess(process);
230: } finally {
231: workflowService().release(pd);
232: }
233: }
234:
235: /**
236: * Remove its process definition.
237: */
238: public void removeProcessDefinitions() throws Exception {
239: ProcessDefinitionDirectory pdd = null;
240: try {
241: pdd = workflowService().processDefinitionDirectory();
242: for (Iterator i = pdd.processDefinitions().iterator(); i
243: .hasNext();) {
244: ProcessDefinition pd = (ProcessDefinition) i.next();
245: if (pd.packageId().equals("jelly-test")) {
246: pdd.removeProcessDefinition(pd.packageName(), pd
247: .processName());
248: }
249: }
250: } finally {
251: workflowService().release(pdd);
252: }
253: }
254:
255: private boolean stateReached(WfProcess proc, String procState)
256: throws Exception {
257: int maxRetries = 30;
258: while (true) {
259: if (proc.state().startsWith(procState)) {
260: return true;
261: }
262: if (maxRetries-- <= 0) {
263: return false;
264: }
265: Thread.sleep(1000);
266: }
267: }
268:
269: }
|