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