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: XMLRPCTests.java,v 1.2 2007/04/22 19:44:16 mlipp Exp $
021: *
022: * $Log: XMLRPCTests.java,v $
023: * Revision 1.2 2007/04/22 19:44:16 mlipp
024: * Added type handling.
025: *
026: * Revision 1.1 2007/04/22 16:15:13 mlipp
027: * New generic XMLRPC tool.
028: *
029: * Revision 1.4 2006/11/19 21:53:47 mlipp
030: * Finished support for native Java types.
031: *
032: * Revision 1.3 2006/09/29 12:32:07 drmlipp
033: * Consistently using WfMOpen as projct name now.
034: *
035: * Revision 1.2 2006/03/08 14:46:42 drmlipp
036: * Synchronized with 1.3.3p5.
037: *
038: * Revision 1.1.1.1.6.1 2006/03/02 15:30:59 drmlipp
039: * Added E4X test.
040: *
041: * Revision 1.1.1.1 2004/08/18 15:18:47 drmlipp
042: * Update to 1.2
043: *
044: * Revision 1.1 2004/03/31 19:30:33 lipp
045: * Improved test.
046: *
047: */
048: package tools;
049:
050: import java.io.BufferedReader;
051: import java.io.InputStream;
052: import java.io.InputStreamReader;
053:
054: import java.util.ArrayList;
055: import java.util.Collection;
056: import java.util.Iterator;
057: import java.util.List;
058:
059: import javax.security.auth.login.LoginException;
060:
061: import org.apache.xmlrpc.server.PropertyHandlerMapping;
062: import org.apache.xmlrpc.server.XmlRpcServer;
063: import org.apache.xmlrpc.webserver.WebServer;
064: import org.w3c.dom.Node;
065:
066: import de.danet.an.util.junit.EJBClientTest;
067: import de.danet.an.util.sax.SAXContentBuffer;
068:
069: import de.danet.an.workflow.omgcore.ProcessData;
070: import de.danet.an.workflow.omgcore.WfActivity;
071: import de.danet.an.workflow.omgcore.WfProcess;
072: import de.danet.an.workflow.omgcore.WfRequester;
073:
074: import de.danet.an.workflow.api.Activity;
075: import de.danet.an.workflow.api.DefaultProcessData;
076: import de.danet.an.workflow.api.DefaultRequester;
077: import de.danet.an.workflow.api.FactoryConfigurationError;
078: import de.danet.an.workflow.api.ProcessDefinitionDirectory;
079: import de.danet.an.workflow.api.ProcessDirectory;
080: import de.danet.an.workflow.api.ProcessMgr;
081: import de.danet.an.workflow.api.SAXEventBuffer;
082: import de.danet.an.workflow.api.WorkflowService;
083: import de.danet.an.workflow.api.WorkflowServiceFactory;
084:
085: import common.UTLoginContext;
086: import junit.framework.Test;
087: import junit.framework.TestCase;
088: import junit.framework.TestSuite;
089:
090: /**
091: * Test XMLRPC
092: */
093: public class XMLRPCTests extends TestCase {
094: private static UTLoginContext plc = null;
095: static {
096: try {
097: plc = new UTLoginContext();
098: plc.login();
099: } catch (LoginException e) {
100: throw new IllegalStateException(e.getMessage());
101: }
102: }
103:
104: private WebServer xmlrpcWebServer = null;
105:
106: /**
107: * Constructor of this TestCase
108: */
109: public XMLRPCTests(String name) {
110: super (name);
111: }
112:
113: /**
114: * Assemble test suite.
115: */
116: public static Test suite() {
117: TestSuite suite = new TestSuite();
118: suite.addTest(new XMLRPCTests("importProcessDefinitions"));
119: suite.addTest(new XMLRPCTests("testAdder"));
120: suite.addTest(new XMLRPCTests("testEcho"));
121: return new EJBClientTest(plc, suite);
122: }
123:
124: private WorkflowService workflowService = null;
125:
126: /**
127: * Initialisierung.
128: */
129: protected void setUp() throws Exception {
130: try {
131: WorkflowServiceFactory wfsf = WorkflowServiceFactory
132: .newInstance();
133: workflowService = wfsf.newWorkflowService();
134: xmlrpcWebServer = startXmlRpcWebServer();
135: } catch (FactoryConfigurationError e) {
136: throw new IllegalStateException(e.getMessage());
137: }
138: }
139:
140: protected void tearDown() throws Exception {
141: workflowService.release(workflowService);
142: workflowService = null;
143: if (xmlrpcWebServer != null) {
144: xmlrpcWebServer.shutdown();
145: }
146: }
147:
148: /**
149: * Import the process definitions from a XPDL file
150: * unsing the ProcessDefinitionDirectory bean.
151: */
152: public void importProcessDefinitions() throws Exception {
153: // Create process definition directory bean
154: ProcessDefinitionDirectory pdd = workflowService
155: .processDefinitionDirectory();
156:
157: InputStream is = getClass().getResourceAsStream(
158: "/tools/xmlrpctests.xml");
159: assertTrue(is != null);
160: BufferedReader br = new BufferedReader(new InputStreamReader(
161: is, "ISO-8859-1"));
162: StringBuffer sb = new StringBuffer();
163: String st;
164: while ((st = br.readLine()) != null) {
165: sb.append(st + "\n");
166: }
167: pdd.importProcessDefinitions(sb.toString());
168: Collection processDefinitions = pdd.processDefinitions();
169: assertTrue(processDefinitions.size() > 0);
170: }
171:
172: /**
173: * Adder test
174: *
175: * @throws Exception
176: */
177: public void testAdder() throws Exception {
178: ProcessDefinitionDirectory procDefDir = null;
179: ProcessDirectory procDir = null;
180:
181: try {
182: procDefDir = workflowService.processDefinitionDirectory();
183: procDir = workflowService.processDirectory();
184: ProcessMgr pmgr = procDefDir.processMgr("xmlrpctests",
185: "addNumbers");
186: WfProcess process = pmgr
187: .createProcess(new DefaultRequester(workflowService));
188: process.start();
189: assertTrue(stateReached(process, "closed.completed"));
190: procDir.removeProcess(process);
191: } finally {
192: workflowService.release(procDefDir);
193: workflowService.release(procDir);
194: }
195:
196: }
197:
198: /**
199: * Echo test
200: *
201: * @throws Exception
202: */
203: public void testEcho() throws Exception {
204: ProcessDefinitionDirectory procDefDir = null;
205: ProcessDirectory procDir = null;
206:
207: try {
208: procDefDir = workflowService.processDefinitionDirectory();
209: procDir = workflowService.processDirectory();
210: ProcessMgr pmgr = procDefDir.processMgr("xmlrpctests",
211: "echoXml");
212: WfProcess process = pmgr
213: .createProcess(new DefaultRequester(workflowService));
214: process.start();
215: assertTrue(stateReached(process, "closed.completed"));
216: procDir.removeProcess(process);
217: } finally {
218: workflowService.release(procDefDir);
219: workflowService.release(procDir);
220: }
221:
222: }
223:
224: private WebServer startXmlRpcWebServer() throws Exception {
225: WebServer webServer = new WebServer(10301);
226: XmlRpcServer xmlRpcServer = webServer.getXmlRpcServer();
227: PropertyHandlerMapping phm = new PropertyHandlerMapping();
228: phm.addHandler("Calculator", Calculator.class);
229: phm.addHandler("EchoService", EchoService.class);
230: xmlRpcServer.setHandlerMapping(phm);
231:
232: webServer.start();
233: return webServer;
234: }
235:
236: private boolean stateReached(WfProcess proc, String procState)
237: throws Exception {
238: boolean test = true;
239: boolean stateReached = false;
240: int maxRetries = 100;
241: while (test) {
242: if (maxRetries-- > 0) {
243: if (proc.state().startsWith(procState)) {
244: stateReached = true;
245: test = false;
246: } else {
247: Thread.sleep(500);
248: }
249: } else {
250: test = false;
251: }
252: }
253: return stateReached;
254: }
255:
256: private boolean stateReached(WfActivity act, String actState)
257: throws Exception {
258: boolean test = true;
259: boolean stateReached = false;
260: int maxRetries = 100;
261: while (test) {
262: if (maxRetries-- > 0) {
263: if (act.state().startsWith(actState)) {
264: stateReached = true;
265: test = false;
266: } else {
267: Thread.sleep(500);
268: }
269: } else {
270: test = false;
271: }
272: }
273: return stateReached;
274: }
275: }
|