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