001: /*
002: * Danet GmbH
003: * Beratung und Software-Entwicklung
004: * Geschäftstelle AN
005: *
006: * $Id: JSTest.java,v 1.4 2007/03/27 21:59:42 mlipp Exp $
007: *
008: * $Log: JSTest.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.4 2004/06/14 19:37:20 lipp
022: * Fixed assignment functions and cleaned up assignment related
023: * interfaces.
024: *
025: * Revision 1.3 2004/01/27 11:45:33 lipp
026: * Preserve newlines when reading process definitions.
027: *
028: * Revision 1.2 2004/01/26 16:46:48 montag
029: * jstest now processing input of type SchemaType.
030: *
031: * Revision 1.1 2004/01/26 15:11:19 montag
032: * Tool JSExecutor now returns SchemaType objects.
033: *
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.Collection;
043:
044: import javax.xml.transform.TransformerFactory;
045: import javax.xml.transform.sax.SAXTransformerFactory;
046: import javax.xml.transform.sax.TransformerHandler;
047: import javax.xml.transform.stream.StreamResult;
048: import javax.xml.transform.dom.DOMResult;
049:
050: import javax.security.auth.login.LoginException;
051:
052: import org.w3c.dom.Element;
053: import org.w3c.dom.Document;
054:
055: import org.jaxen.XPath;
056: import org.jaxen.dom.DOMXPath;
057:
058: import de.danet.an.util.EJBUtil;
059: import de.danet.an.util.junit.EJBClientTest;
060:
061: import de.danet.an.workflow.omgcore.WfProcess;
062: import de.danet.an.workflow.omgcore.WfProcessMgr;
063: import de.danet.an.workflow.omgcore.WfRequester;
064: import de.danet.an.workflow.omgcore.ProcessData;
065:
066: import de.danet.an.workflow.api.DefaultRequester;
067: import de.danet.an.workflow.api.FactoryConfigurationError;
068: import de.danet.an.workflow.api.Process;
069: import de.danet.an.workflow.api.ProcessDefinition;
070: import de.danet.an.workflow.api.ProcessDefinitionDirectory;
071: import de.danet.an.workflow.api.WorkflowService;
072: import de.danet.an.workflow.api.WorkflowServiceFactory;
073:
074: import de.danet.an.workflow.ejbs.admin.ProcessDefinitionDirectoryHome;
075:
076: import de.danet.an.workflow.util.SAXEventBufferImpl;
077:
078: import common.UTLoginContext;
079: import junit.framework.Test;
080: import junit.framework.TestCase;
081: import junit.framework.TestSuite;
082:
083: /**
084: * Zusammenstellung aller TimerObjectTests.
085: */
086: public class JSTest extends TestCase {
087:
088: private static UTLoginContext plc = null;
089: static {
090: try {
091: plc = new UTLoginContext();
092: plc.login();
093: } catch (LoginException e) {
094: throw new IllegalStateException(e.getMessage());
095: }
096: }
097:
098: /**
099: * Konstruktor zum Erzeugen eines TestCase
100: */
101: public JSTest(String name) {
102: super (name);
103: }
104:
105: /**
106: * Stellt diese TestSuite zusammen.
107: */
108: public static Test suite() {
109: TestSuite suite = new TestSuite();
110: suite.addTest(new JSTest("importProcessDefinitions"));
111: suite.addTest(new JSTest("createProcessAndRemoveProcessDef"));
112: return new EJBClientTest(plc, suite);
113: }
114:
115: private static WorkflowService wfsCache = null;
116:
117: private WorkflowService workflowService() {
118: if (wfsCache == null) {
119: try {
120: WorkflowServiceFactory wfsf = WorkflowServiceFactory
121: .newInstance();
122: wfsCache = wfsf.newWorkflowService();
123: } catch (FactoryConfigurationError e) {
124: throw new IllegalStateException(e.getMessage());
125: }
126: }
127: return wfsCache;
128: }
129:
130: /**
131: * Import the process definitions from a XPDL file
132: * unsing the ProcessDefinitionDirectory bean.
133: */
134: public void importProcessDefinitions() throws Exception {
135: // Create process definition directory bean
136: ProcessDefinitionDirectory pdd = workflowService()
137: .processDefinitionDirectory();
138:
139: InputStream is = getClass().getResourceAsStream(
140: "/process/jstest.xml");
141: assertTrue(is != null);
142: BufferedReader br = new BufferedReader(new InputStreamReader(
143: is, "ISO-8859-1"));
144: StringBuffer sb = new StringBuffer();
145: String st;
146: while ((st = br.readLine()) != null) {
147: sb.append(st + "\n");
148: }
149: pdd.importProcessDefinitions(sb.toString());
150: Collection processDefinitions = pdd.processDefinitions();
151: assertTrue(processDefinitions.size() > 0);
152:
153: }
154:
155: private WfProcess createProcess(String pkgId, String prcId,
156: WfRequester req) throws Exception {
157: ProcessDefinitionDirectory pdd = null;
158: try {
159: pdd = workflowService().processDefinitionDirectory();
160: WfProcessMgr pmgr = pdd.processMgr(pkgId, prcId);
161: return pmgr.createProcess(req);
162: } finally {
163: workflowService().release(pdd);
164: }
165: }
166:
167: /**
168: * Create a new process and then remove its process definition.
169: */
170: public void createProcessAndRemoveProcessDef() throws Exception {
171: ProcessDefinitionDirectory pdd = null;
172: try {
173: WfRequester cont = new DefaultRequester(workflowService());
174: Process process = (Process) createProcess("N1", "N2", cont);
175: process.start();
176: Thread.sleep(5000);
177:
178: // get processdata
179: ProcessData data = process.processContext();
180: Object value = data.get("result");
181: if (value != null) {
182: SAXEventBufferImpl myBuffer = (SAXEventBufferImpl) value;
183: TransformerFactory tf = TransformerFactory
184: .newInstance();
185: SAXTransformerFactory saxTransFact = null;
186: if (tf.getFeature(SAXTransformerFactory.FEATURE)) {
187: saxTransFact = (SAXTransformerFactory) tf;
188: }
189: TransformerHandler transHand = null;
190: transHand = saxTransFact.newTransformerHandler();
191: StreamResult streamResult = new StreamResult(
192: new java.io.ByteArrayOutputStream());
193: transHand.setResult(streamResult);
194: myBuffer.emit(transHand);
195: System.out.println(streamResult.getOutputStream()
196: .toString());
197:
198: DOMResult domResult = new DOMResult();
199: transHand = saxTransFact.newTransformerHandler();
200: transHand.setResult(domResult);
201: myBuffer.emit(transHand);
202: Element returnResult = ((Document) domResult.getNode())
203: .getDocumentElement();
204: XPath xpath = new DOMXPath("/parent/firstChild");
205: String value2 = xpath.stringValueOf(returnResult);
206: assertTrue(value2.equals("I'm number one"));
207:
208: XPath xpath2 = new DOMXPath("/parent/thirdChild");
209: Element child = (Element) xpath2
210: .selectSingleNode(returnResult);
211: System.out.println("child = " + child.getClass());
212: org.jdom.Element child2 = new org.jdom.input.DOMBuilder()
213: .build(child);
214: org.jdom.output.XMLOutputter output = new org.jdom.output.XMLOutputter();
215: java.io.StringWriter sw = new java.io.StringWriter();
216: output.output(child2, sw);
217: System.out.println(sw.toString());
218: }
219:
220: // remove its process definition
221: pdd = workflowService().processDefinitionDirectory();
222: ProcessDefinition pd = pdd.lookupProcessDefinition("N1",
223: "N2");
224: assertTrue(pd != null);
225: pdd.removeProcessDefinition("N1", "N2");
226: boolean gotEx = false;
227: try {
228: pd = pdd.lookupProcessDefinition("N1", "N2");
229: } catch (Exception ex) {
230: gotEx = true;
231: }
232: assertTrue(gotEx);
233: // check the process definition of the process
234: ProcessDefinition procDef = process.processDefinition();
235: assertTrue(procDef.packageId().equals("N1"));
236: assertTrue(procDef.processId().equals("N2"));
237: // import the process definition again.
238: importProcessDefinitions();
239: pd = pdd.lookupProcessDefinition("N1", "N2");
240: assertTrue(pd != null);
241: } finally {
242: workflowService().release(pdd);
243: }
244: }
245:
246: public static void importProcessDefinitions(String filename)
247: throws Exception {
248: // Create process definition directory bean
249: ProcessDefinitionDirectoryHome pddh = (ProcessDefinitionDirectoryHome) EJBUtil
250: .lookupEJBHome(ProcessDefinitionDirectoryHome.class,
251: "ejb/de.danet.an.wfdemo.ProcessDefinitionDirectory");
252: ProcessDefinitionDirectory pdd = pddh.create();
253: InputStream is = JSTest.class.getResourceAsStream(filename);
254: assertTrue(is != null);
255: BufferedReader br = new BufferedReader(new InputStreamReader(
256: is, "ISO-8859-1"));
257: StringBuffer sb = new StringBuffer();
258: String st;
259: while ((st = br.readLine()) != null) {
260: sb.append(st + "\n");
261: }
262: pdd.importProcessDefinitions(sb.toString());
263: Collection processDefinitions = pdd.processDefinitions();
264: assertTrue(processDefinitions.size() > 0);
265: }
266: }
|