001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2003 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: XSLT.java,v 1.4 2007/03/27 21:59:42 mlipp Exp $
021: *
022: * $Log: XSLT.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:08 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.1.3.6.2 2005/12/14 09:57:12 drmlipp
033: * Added support in XSLTTool for all basic out types.
034: *
035: * Revision 1.1.1.3.6.1 2005/12/13 17:51:54 drmlipp
036: * Added support for datetime parameters fo XSLTTool.
037: *
038: * Revision 1.1.1.3 2004/08/18 15:17:39 drmlipp
039: * Update to 1.2
040: *
041: * Revision 1.24 2004/02/19 13:10:32 lipp
042: * Clarified start-/endDocument usage in SAXEventBuffers.
043: *
044: * Revision 1.23 2004/02/17 17:12:16 montag
045: * new testcase for returning a not wellformed element.
046: *
047: * Revision 1.22 2004/02/17 16:25:36 montag
048: * testcase 12 corrected.
049: *
050: * Revision 1.21 2004/02/17 15:29:07 lipp
051: * Several fixes.
052: *
053: * Revision 1.20 2004/02/17 08:07:41 montag
054: * test12 for List return
055: *
056: * Revision 1.19 2004/01/28 15:21:44 montag
057: * test asserts corrected.
058: *
059: * Revision 1.18 2004/01/28 09:25:49 montag
060: * additional testcase added.
061: *
062: * Revision 1.17 2004/01/28 08:34:33 montag
063: * new functionality for merging multiple xml documents in one.
064: *
065: * Revision 1.16 2004/01/27 17:44:47 montag
066: * stateReached() instead of Thread.sleep() used.
067: *
068: * Revision 1.15 2004/01/27 17:26:05 montag
069: * test cases adapted. stylesheet as property
070: * of the prcess description.
071: *
072: * Revision 1.14 2004/01/27 13:08:07 lipp
073: * Added tests.
074: *
075: * Revision 1.13 2004/01/27 12:11:08 montag
076: * XSLTTool now implements ResultProvider interface.
077: *
078: * Revision 1.12 2003/10/21 21:00:45 lipp
079: * Moved EJBClientTest to new junit sub-package.
080: *
081: * Revision 1.11 2003/10/08 12:39:40 huaiyang
082: * make test weblogic compatible.
083: *
084: * Revision 1.10 2003/07/14 09:01:19 montag
085: * error corrected setting the result type
086: * if there is no output mapping defined.
087: *
088: * Revision 1.9 2003/07/11 14:07:09 montag
089: * return SAXEventBufferImpl if no output
090: * mapping is defined.
091: *
092: * Revision 1.8 2003/07/10 14:42:13 montag
093: * xlst transformation als SAXEventBuffer.
094: *
095: * Revision 1.7 2003/07/04 09:09:09 montag
096: * debug logging inserted.
097: *
098: * Revision 1.6 2003/07/03 12:53:50 montag
099: * url handling corrected.
100: *
101: * Revision 1.5 2003/07/02 13:50:09 montag
102: * Property XSLT no longer mandatory.
103: *
104: * Revision 1.4 2003/07/02 11:53:13 montag
105: * Support for additional IN parameters.
106: *
107: * Revision 1.3 2003/07/02 09:30:38 montag
108: * handling of complex out parameters
109: * with no defined mapping fixed.
110: *
111: * Revision 1.2 2003/07/02 07:33:50 montag
112: * first workng version of the XSLTTool.
113: * Next steps:
114: * - handling of not well-formed transformation results.
115: * - support for additional parameters.
116: * - adapt logging messages.
117: *
118: * Revision 1.1 2003/07/01 14:28:43 montag
119: * initial of XSLTTool.
120: *
121: *
122: */
123: package tools;
124:
125: import java.util.Iterator;
126: import java.util.Map;
127: import java.util.Date;
128:
129: import javax.xml.transform.TransformerFactory;
130: import javax.xml.transform.sax.SAXTransformerFactory;
131: import javax.xml.transform.sax.TransformerHandler;
132: import javax.xml.transform.dom.DOMResult;
133:
134: import org.xml.sax.Attributes;
135: import org.xml.sax.ContentHandler;
136: import org.xml.sax.SAXException;
137: import org.xml.sax.ext.LexicalHandler;
138: import org.xml.sax.helpers.AttributesImpl;
139:
140: import org.w3c.dom.Element;
141: import org.w3c.dom.Document;
142:
143: import org.jaxen.XPath;
144: import org.jaxen.dom.DOMXPath;
145: import org.jaxen.jdom.JDOMXPath;
146:
147: import org.jdom.input.SAXHandler;
148: import org.jdom.output.XMLOutputter;
149:
150: import junit.framework.Test;
151: import junit.framework.TestSuite;
152:
153: import process.WfMOpenTestCase;
154:
155: import de.danet.an.util.junit.EJBClientTest;
156: import de.danet.an.util.sax.BodyFilter;
157: import de.danet.an.util.XMLUtil;
158:
159: import de.danet.an.workflow.util.SAXEventBufferImpl;
160: import de.danet.an.workflow.api.ProcessDefinitionDirectory;
161: import de.danet.an.workflow.api.ProcessMgr;
162: import de.danet.an.workflow.omgcore.WfProcess;
163:
164: /**
165: * Test the tool of XSLTDynamicInvocationTool.
166: * @author <a href="mailto:weidauer@danet.de">Christian Weidauer</a>
167: * @version 1.0
168: */
169: public class XSLT extends WfMOpenTestCase {
170: private ProcessDefinitionDirectory pdd = null;
171:
172: /**
173: * Constructor of this TestCase
174: * @param name a <code>String</code> value
175: */
176: public XSLT(String name) {
177: super (name);
178: }
179:
180: /**
181: * Construct this test suite.
182: * @return a <code>Test</code> value
183: */
184: public static Test suite() {
185: TestSuite suite = new TestSuite();
186: suite.addTest(new XSLT("importProcessDefinitions"));
187: suite.addTest(new XSLT("invokeTest1"));
188: suite.addTest(new XSLT("invokeTest2"));
189: suite.addTest(new XSLT("invokeTest3"));
190: suite.addTest(new XSLT("invokeTest4"));
191: suite.addTest(new XSLT("invokeTest5"));
192: suite.addTest(new XSLT("invokeTest6"));
193: suite.addTest(new XSLT("invokeTest7"));
194: suite.addTest(new XSLT("invokeTest8"));
195: suite.addTest(new XSLT("invokeTest9"));
196: suite.addTest(new XSLT("invokeTest10"));
197: suite.addTest(new XSLT("invokeTest11"));
198: suite.addTest(new XSLT("invokeTest12"));
199: suite.addTest(new XSLT("invokeTest13"));
200: suite.addTest(new XSLT("invokeTest15"));
201: suite.addTest(new XSLT("removeProcessDefinition"));
202: return new EJBClientTest(plc, suite);
203: }
204:
205: /**
206: * Initialisation.
207: * The <code>setUp</code> method defines the way a state change is
208: * realized. Override this method to change this way.
209: * @exception Exception if an error occurs
210: */
211: protected void setUp() throws Exception {
212: super .setUp();
213: xpdlFile = "/tools/testXSLT.xml";
214: pdd = workflowService().processDefinitionDirectory();
215: }
216:
217: /**
218: * Test a xslt tool invocation which returns a complex data type of DOM
219: * element.
220: * @exception Exception if an error occurs
221: */
222: public void invokeTest1() throws Exception {
223: ProcessMgr pmgr = pdd.processMgr("st-testXSLT", "testXSLT1");
224: WfProcess process = pmgr.createProcess(defaultRequester());
225: process.start();
226: assertTrue(stateReached(process, "closed.completed"));
227: Map map = (Map) process.result();
228: for (Iterator it = map.keySet().iterator(); it.hasNext();) {
229: String name = (String) it.next();
230: if (name.equals("result")) {
231: String returnResult = (String) map.get(name);
232: // check testXSLT.xml for the expected result
233: assertTrue(returnResult.equals("Matisse, Henri"));
234: }
235: }
236: }
237:
238: /**
239: * Test a xslt tool invocation which returns a complex data type of DOM
240: * element.
241: * @exception Exception if an error occurs
242: */
243: public void invokeTest2() throws Exception {
244: ProcessMgr pmgr = pdd.processMgr("st-testXSLT", "testXSLT2");
245: WfProcess process = pmgr.createProcess(defaultRequester());
246: process.start();
247: assertTrue(stateReached(process, "closed.completed"));
248: Map map = (Map) process.result();
249: for (Iterator it = map.keySet().iterator(); it.hasNext();) {
250: String name = (String) it.next();
251: if (name.equals("result")) {
252: String returnResult = (String) map.get(name);
253: // check testXSLT.xml for the expected result
254: assertTrue(returnResult.equals("Matisse, Henri"));
255: }
256: }
257: }
258:
259: /**
260: * Test a xslt tool invocation which returns a complex data type of DOM
261: * element.
262: * @exception Exception if an error occurs
263: */
264: public void invokeTest3() throws Exception {
265: ProcessMgr pmgr = pdd.processMgr("st-testXSLT", "testXSLT3");
266: WfProcess process = pmgr.createProcess(defaultRequester());
267: process.start();
268: assertTrue(stateReached(process, "closed.completed"));
269: Map map = (Map) process.result();
270: for (Iterator it = map.keySet().iterator(); it.hasNext();) {
271: String name = (String) it.next();
272: if (name.equals("result")) {
273: String returnResult = (String) map.get(name);
274: // check testXSLT.xml for the expected result
275: assertTrue(returnResult.equals("Matisse, Henri"));
276: }
277: if (name.equals("complexResult")) {
278: SAXEventBufferImpl myBuffer = (SAXEventBufferImpl) map
279: .get(name);
280: TransformerFactory tf = TransformerFactory
281: .newInstance();
282: SAXTransformerFactory saxTransFact = null;
283: if (tf.getFeature(SAXTransformerFactory.FEATURE)) {
284: saxTransFact = (SAXTransformerFactory) tf;
285: }
286: TransformerHandler transHand = null;
287: transHand = saxTransFact.newTransformerHandler();
288: DOMResult domResult = new DOMResult();
289: transHand.setResult(domResult);
290: myBuffer.emit(transHand);
291: Element returnResult = ((Document) domResult.getNode())
292: .getDocumentElement();
293:
294: // org.w3c.dom.Element returnResult
295: // = (org.w3c.dom.Element)map.get(name);
296:
297: // javax.xml.transform.stream.StreamResult streamResult
298: // = new javax.xml.transform.stream.StreamResult(System.out);
299: // javax.xml.transform.TransformerFactory tf
300: // = javax.xml.transform.TransformerFactory.newInstance();
301: // javax.xml.transform.Transformer serializer
302: // = tf.newTransformer();
303: // serializer.setOutputProperty
304: // (javax.xml.transform.OutputKeys.ENCODING,"ISO-8859-1");
305: // serializer.setOutputProperty
306: // (javax.xml.transform.OutputKeys.DOCTYPE_SYSTEM,
307: // "users.dtd");
308: // serializer.setOutputProperty
309: // (javax.xml.transform.OutputKeys.INDENT,"yes");
310: // serializer.transform
311: // (new javax.xml.transform.dom.DOMSource(returnResult),
312: // streamResult);
313:
314: XPath xpath = new DOMXPath("/table/record[1]/painter");
315: String value = xpath.stringValueOf(returnResult);
316: // check testXSLT.xml for the expected result
317: assertTrue(value.equals("Matisse, Henri"));
318: }
319: }
320: }
321:
322: /**
323: * Test a xslt tool invocation which returns a complex data type of DOM
324: * element.
325: * @exception Exception if an error occurs
326: */
327: public void invokeTest4() throws Exception {
328: ProcessMgr pmgr = pdd.processMgr("st-testXSLT", "testXSLT4");
329: WfProcess process = pmgr.createProcess(defaultRequester());
330: process.start();
331: assertTrue(stateReached(process, "closed.completed"));
332: Map map = (Map) process.result();
333: for (Iterator it = map.keySet().iterator(); it.hasNext();) {
334: String name = (String) it.next();
335: if (name.equals("result")) {
336: String returnResult = (String) map.get(name);
337: // check testXSLT.xml for the expected result
338: assertTrue(returnResult.equals("Matisse, Henri"));
339: }
340: if (name.equals("complexResult")) {
341: SAXEventBufferImpl myBuffer = (SAXEventBufferImpl) map
342: .get(name);
343: TransformerFactory tf = TransformerFactory
344: .newInstance();
345: SAXTransformerFactory saxTransFact = null;
346: if (tf.getFeature(SAXTransformerFactory.FEATURE)) {
347: saxTransFact = (SAXTransformerFactory) tf;
348: }
349: TransformerHandler transHand = null;
350: transHand = saxTransFact.newTransformerHandler();
351: DOMResult domResult = new DOMResult();
352: transHand.setResult(domResult);
353: myBuffer.emit(transHand);
354: Element returnResult = ((Document) domResult.getNode())
355: .getDocumentElement();
356:
357: // javax.xml.transform.stream.StreamResult streamResult
358: // = new javax.xml.transform.stream.StreamResult(System.out);
359: // javax.xml.transform.TransformerFactory tf
360: // = javax.xml.transform.TransformerFactory.newInstance();
361: // javax.xml.transform.Transformer serializer
362: // = tf.newTransformer();
363: // serializer.setOutputProperty
364: // (javax.xml.transform.OutputKeys.ENCODING,"ISO-8859-1");
365: // serializer.setOutputProperty
366: // (javax.xml.transform.OutputKeys.DOCTYPE_SYSTEM,
367: // "users.dtd");
368: // serializer.setOutputProperty
369: // (javax.xml.transform.OutputKeys.INDENT,"yes");
370: // serializer.transform
371: // (new javax.xml.transform.dom.DOMSource(returnResult),
372: // streamResult);
373:
374: XPath xpath = new DOMXPath("/table/record[1]/city");
375: String value = xpath.stringValueOf(returnResult);
376: // check testXSLT.xml for the expected result
377: assertTrue(value.equals("Weiterstadt"));
378: }
379: }
380: }
381:
382: /**
383: * Test a xslt tool invocation which returns a complex data type of DOM
384: * element.
385: * @exception Exception if an error occurs
386: */
387: public void invokeTest5() throws Exception {
388: ProcessMgr pmgr = pdd.processMgr("st-testXSLT", "testXSLT5");
389: WfProcess process = pmgr.createProcess(defaultRequester());
390: process.start();
391: assertTrue(stateReached(process, "closed.completed"));
392: Map map = (Map) process.result();
393: for (Iterator it = map.keySet().iterator(); it.hasNext();) {
394: String name = (String) it.next();
395: if (name.equals("result")) {
396: String returnResult = (String) map.get(name);
397: // check testXSLT.xml for the expected result
398: assertTrue(returnResult.equals("Matisse, Henri"));
399: }
400: if (name.equals("complexResult")) {
401: SAXEventBufferImpl myBuffer = (SAXEventBufferImpl) map
402: .get(name);
403: TransformerFactory tf = TransformerFactory
404: .newInstance();
405: SAXTransformerFactory saxTransFact = null;
406: if (tf.getFeature(SAXTransformerFactory.FEATURE)) {
407: saxTransFact = (SAXTransformerFactory) tf;
408: }
409: TransformerHandler transHand = null;
410: transHand = saxTransFact.newTransformerHandler();
411: DOMResult domResult = new DOMResult();
412: transHand.setResult(domResult);
413: myBuffer.emit(transHand);
414: Element returnResult = ((Document) domResult.getNode())
415: .getDocumentElement();
416:
417: // javax.xml.transform.stream.StreamResult streamResult
418: // = new javax.xml.transform.stream.StreamResult(System.out);
419: // javax.xml.transform.TransformerFactory tf =
420: // javax.xml.transform.TransformerFactory.newInstance();
421: // javax.xml.transform.Transformer serializer
422: // = tf.newTransformer();
423: // serializer.setOutputProperty
424: // (javax.xml.transform.OutputKeys.ENCODING,"ISO-8859-1");
425: // serializer.setOutputProperty
426: // (javax.xml.transform.OutputKeys.DOCTYPE_SYSTEM,
427: // "users.dtd");
428: // serializer.setOutputProperty
429: // (javax.xml.transform.OutputKeys.INDENT,"yes");
430: // serializer.transform
431: // (new javax.xml.transform.dom.DOMSource(returnResult),
432: // streamResult);
433:
434: XPath xpath = new DOMXPath("/table/row[1]/@painter");
435: String value = xpath.stringValueOf(returnResult);
436: // check testXSLT.xml for the expected result
437: assertTrue(value.equals("Matisse, Henri"));
438: }
439: }
440: }
441:
442: /**
443: * Test a xslt tool invocation which returns a complex data type of DOM
444: * element.
445: * @exception Exception if an error occurs
446: */
447: public void invokeTest6() throws Exception {
448: ProcessMgr pmgr = pdd.processMgr("st-testXSLT", "testXSLT6");
449: WfProcess process = pmgr.createProcess(defaultRequester());
450: process.start();
451: // There should be a server error, because the result
452: // of the transformation is not wellformed.
453: assertTrue(stateReached(process, "closed.completed"));
454: }
455:
456: /**
457: * Test a xslt tool invocation which returns a complex data type of DOM
458: * element.
459: * @exception Exception if an error occurs
460: */
461: public void invokeTest7() throws Exception {
462: ProcessMgr pmgr = pdd.processMgr("st-testXSLT", "testXSLT7");
463: WfProcess process = pmgr.createProcess(defaultRequester());
464: process.start();
465: assertTrue(stateReached(process, "closed.completed"));
466: Map map = (Map) process.result();
467: for (Iterator it = map.keySet().iterator(); it.hasNext();) {
468: String name = (String) it.next();
469: if (name.equals("result")) {
470: String returnResult = (String) map.get(name);
471: // check testXSLT.xml for the expected result
472: assertTrue(returnResult.equals("Hallo"));
473: }
474: }
475: }
476:
477: /**
478: * Test a xslt tool invocation which returns a complex data type of DOM
479: * element.
480: * @exception Exception if an error occurs
481: */
482: public void invokeTest8() throws Exception {
483: ProcessMgr pmgr = pdd.processMgr("st-testXSLT", "testXSLT8");
484: WfProcess process = pmgr.createProcess(defaultRequester());
485: process.start();
486: // There should be a server error, because
487: // the transformation produces no xml.
488: assertTrue(stateReached(process, "closed.terminated"));
489: }
490:
491: /**
492: * Test a xslt tool invocation which returns a complex data type of DOM
493: * element.
494: * @exception Exception if an error occurs
495: */
496: public void invokeTest9() throws Exception {
497: ProcessMgr pmgr = pdd.processMgr("st-testXSLT", "testXSLT9");
498: WfProcess process = pmgr.createProcess(defaultRequester());
499: process.start();
500: assertTrue(stateReached(process, "closed.completed"));
501: Map map = (Map) process.result();
502: for (Iterator it = map.keySet().iterator(); it.hasNext();) {
503: String name = (String) it.next();
504: if (name.equals("result")) {
505: String returnResult = (String) map.get(name);
506: // check testXSLT.xml for the expected result
507: assertTrue(returnResult.equals("Matisse, Henri"));
508: }
509: if (name.equals("complexResult")) {
510: SAXEventBufferImpl myBuffer = (SAXEventBufferImpl) map
511: .get(name);
512: TransformerFactory tf = TransformerFactory
513: .newInstance();
514: SAXTransformerFactory saxTransFact = null;
515: if (tf.getFeature(SAXTransformerFactory.FEATURE)) {
516: saxTransFact = (SAXTransformerFactory) tf;
517: }
518: TransformerHandler transHand = null;
519: transHand = saxTransFact.newTransformerHandler();
520: DOMResult domResult = new DOMResult();
521: transHand.setResult(domResult);
522: myBuffer.emit(transHand);
523: Element returnResult = ((Document) domResult.getNode())
524: .getDocumentElement();
525:
526: // javax.xml.transform.stream.StreamResult streamResult
527: // = new javax.xml.transform.stream.StreamResult(System.out);
528: // javax.xml.transform.TransformerFactory tf
529: // = javax.xml.transform.TransformerFactory.newInstance();
530: // javax.xml.transform.Transformer serializer
531: // = tf.newTransformer();
532: // serializer.setOutputProperty
533: // (javax.xml.transform.OutputKeys.ENCODING,"ISO-8859-1");
534: // serializer.setOutputProperty
535: // (javax.xml.transform.OutputKeys.DOCTYPE_SYSTEM,
536: // "users.dtd");
537: // serializer.setOutputProperty
538: // (javax.xml.transform.OutputKeys.INDENT,"yes");
539: // serializer.transform
540: // (new javax.xml.transform.dom.DOMSource(returnResult),
541: // streamResult);
542:
543: XPath xpath = new DOMXPath("/table/record[1]/city");
544: String value = xpath.stringValueOf(returnResult);
545: // check testXSLT.xml for the expected result
546: assertTrue(value.equals("Darmstadt"));
547: }
548: }
549: }
550:
551: /**
552: * Test a xslt tool invocation which returns a complex data type of DOM
553: * element.
554: * @exception Exception if an error occurs
555: */
556: public void invokeTest10() throws Exception {
557: ProcessMgr pmgr = pdd.processMgr("st-testXSLT", "testXSLT10");
558: WfProcess process = pmgr.createProcess(defaultRequester());
559: process.start();
560: assertTrue(stateReached(process, "closed.completed"));
561: Map map = (Map) process.result();
562: for (Iterator it = map.keySet().iterator(); it.hasNext();) {
563: String name = (String) it.next();
564: if (name.equals("result")) {
565: String returnResult = (String) map.get(name);
566: // check testXSLT.xml for the expected result
567: assertTrue(returnResult.equals("Matisse, Henri"));
568: }
569: if (name.equals("complexResult")) {
570: SAXEventBufferImpl myBuffer = (SAXEventBufferImpl) map
571: .get(name);
572: TransformerFactory tf = TransformerFactory
573: .newInstance();
574: SAXTransformerFactory saxTransFact = null;
575: if (tf.getFeature(SAXTransformerFactory.FEATURE)) {
576: saxTransFact = (SAXTransformerFactory) tf;
577: }
578: TransformerHandler transHand = null;
579: transHand = saxTransFact.newTransformerHandler();
580: DOMResult domResult = new DOMResult();
581: transHand.setResult(domResult);
582: myBuffer.emit(transHand);
583: Element returnResult = ((Document) domResult.getNode())
584: .getDocumentElement();
585:
586: XPath xpath1 = new DOMXPath("/painters/table[1]/row[1]");
587: Element painter1 = (Element) xpath1
588: .selectSingleNode(returnResult);
589: String value1 = painter1.getAttribute("painter");
590: // check testXSLT.xml for the expected result
591: assertTrue(value1.equals("Matisse, Henri"));
592:
593: XPath xpath2 = new DOMXPath("/painters/table[2]/row[2]");
594: Element painter2 = (Element) xpath2
595: .selectSingleNode(returnResult);
596: String value2 = painter2.getAttribute("painter");
597: // check testXSLT.xml for the expected result
598: assertTrue(value2.equals("Monet, Claude"));
599: }
600: }
601: }
602:
603: /**
604: * Test a xslt tool invocation which returns a complex data type of DOM
605: * element.
606: * @exception Exception if an error occurs
607: */
608: public void invokeTest11() throws Exception {
609: ProcessMgr pmgr = pdd.processMgr("st-testXSLT", "testXSLT11");
610: WfProcess process = pmgr.createProcess(defaultRequester());
611: process.start();
612: assertTrue(stateReached(process, "closed.completed"));
613: Map map = (Map) process.result();
614: for (Iterator it = map.keySet().iterator(); it.hasNext();) {
615: String name = (String) it.next();
616: if (name.equals("result")) {
617: String returnResult = (String) map.get(name);
618: // check testXSLT.xml for the expected result
619: assertTrue(returnResult.equals("Matisse, Henri"));
620: }
621: if (name.equals("complexResult")) {
622: SAXEventBufferImpl myBuffer = (SAXEventBufferImpl) map
623: .get(name);
624: TransformerFactory tf = TransformerFactory
625: .newInstance();
626: SAXTransformerFactory saxTransFact = null;
627: if (tf.getFeature(SAXTransformerFactory.FEATURE)) {
628: saxTransFact = (SAXTransformerFactory) tf;
629: }
630: TransformerHandler transHand = null;
631: transHand = saxTransFact.newTransformerHandler();
632: DOMResult domResult = new DOMResult();
633: transHand.setResult(domResult);
634: myBuffer.emit(transHand);
635: Element returnResult = ((Document) domResult.getNode())
636: .getDocumentElement();
637:
638: XPath xpath = new DOMXPath("/painters/table[3]/row[3]");
639: Element painter = (Element) xpath
640: .selectSingleNode(returnResult);
641: String value = painter.getAttribute("title");
642: // check testXSLT.xml for the expected result
643: assertTrue(value
644: .equals("Poplars along the River Epte, Autumn"));
645: }
646: }
647: }
648:
649: /**
650: * Test a xslt tool invocation which returns a complex data type of DOM
651: * element.
652: * @exception Exception if an error occurs
653: */
654: public void invokeTest12() throws Exception {
655: ProcessMgr pmgr = pdd.processMgr("st-testXSLT", "testXSLT12");
656: WfProcess process = pmgr.createProcess(defaultRequester());
657: process.start();
658: assertTrue(stateReached(process, "closed.completed"));
659: Map map = (Map) process.result();
660: for (Iterator it = map.keySet().iterator(); it.hasNext();) {
661: String name = (String) it.next();
662: if (name.equals("complexResult")) {
663: SAXEventBufferImpl myBuffer = (SAXEventBufferImpl) map
664: .get(name);
665: TransformerFactory tf = TransformerFactory
666: .newInstance();
667: SAXTransformerFactory saxTransFact = null;
668: if (tf.getFeature(SAXTransformerFactory.FEATURE)) {
669: saxTransFact = (SAXTransformerFactory) tf;
670: }
671: TransformerHandler transHand = null;
672: transHand = saxTransFact.newTransformerHandler();
673: DOMResult domResult = new DOMResult();
674: transHand.setResult(domResult);
675: myBuffer.emit(transHand);
676: Element returnResult = ((Document) domResult.getNode())
677: .getDocumentElement();
678:
679: // javax.xml.transform.stream.StreamResult streamResult
680: // = new javax.xml.transform.stream.StreamResult(System.out);
681: // tf
682: // = javax.xml.transform.TransformerFactory.newInstance();
683: // javax.xml.transform.Transformer serializer
684: // = tf.newTransformer();
685: // serializer.setOutputProperty
686: // (javax.xml.transform.OutputKeys.ENCODING,"ISO-8859-1");
687: // serializer.setOutputProperty
688: // (javax.xml.transform.OutputKeys.DOCTYPE_SYSTEM,
689: // "users.dtd");
690: // serializer.setOutputProperty
691: // (javax.xml.transform.OutputKeys.INDENT,"yes");
692: // serializer.transform
693: // (new javax.xml.transform.dom.DOMSource(returnResult),
694: // streamResult);
695:
696: XPath xpath = new DOMXPath("/row");
697: Element painter = (Element) xpath
698: .selectSingleNode(returnResult);
699: String value = painter.getAttribute("title");
700: // check testXSLT.xml for the expected result
701: assertTrue(value.equals("La Musique"));
702: }
703: }
704: }
705:
706: /**
707: * Test a xslt tool invocation which returns a complex data type of DOM
708: * element.
709: * @exception Exception if an error occurs
710: */
711: public void invokeTest13() throws Exception {
712: ProcessMgr pmgr = pdd.processMgr("st-testXSLT", "testXSLT13");
713: WfProcess process = pmgr.createProcess(defaultRequester());
714: process.start();
715: assertTrue(stateReached(process, "closed.completed"));
716: Map map = (Map) process.result();
717: for (Iterator it = map.keySet().iterator(); it.hasNext();) {
718: String name = (String) it.next();
719: if (name.equals("complexResult")) {
720: SAXEventBufferImpl myBuffer = (SAXEventBufferImpl) map
721: .get(name);
722: SAXHandler sh = new SAXHandler();
723: sh.startDocument();
724: sh.startElement("", "temporary-root", "temporary-root",
725: new AttributesImpl());
726: myBuffer.emit(sh, (LexicalHandler) sh);
727: sh.endElement("", "temporary-root", "temporary-root");
728: sh.endDocument();
729: org.jdom.Element temporaryRoot = sh.getDocument()
730: .getRootElement();
731: XPath xpath = new JDOMXPath("/temporary-root/row[2]");
732: org.jdom.Element painter = (org.jdom.Element) xpath
733: .selectSingleNode(temporaryRoot);
734: String value = painter.getAttribute("title").getValue();
735: // check testXSLT.xml for the expected result
736: assertTrue(value
737: .equals("The Artist's Garden at Vetheuil"));
738: }
739: }
740: }
741:
742: /**
743: * @exception Exception if an error occurs
744: */
745: public void invokeTest15() throws Exception {
746: ProcessMgr pmgr = pdd.processMgr("st-testXSLT", "testXSLT15");
747: WfProcess process = pmgr.createProcess(defaultRequester());
748: process.start();
749: assertTrue(stateReached(process, "closed.completed"));
750: Map map = (Map) process.result();
751: Date dt = (Date) map.get("datetime");
752: assertTrue(dt.equals(XMLUtil
753: .parseXsdDateTime("2005-12-14T14:15:16")));
754: Double fv = (Double) map.get("floatval");
755: assertTrue(fv.doubleValue() == 2005);
756: Long iv = (Long) map.get("intval");
757: assertTrue(iv.intValue() == 41);
758: Boolean bv = (Boolean) map.get("boolval");
759: assertTrue(bv.booleanValue());
760: }
761:
762: /**
763: * Test a simple process definition with a simple soap tool invocation.
764: * @exception Exception if an error occurs
765: */
766: public void removeProcessDefinition() throws Exception {
767: removeProcessDefinition("st-testXSLT", "testXSLT1");
768: removeProcessDefinition("st-testXSLT", "testXSLT2");
769: removeProcessDefinition("st-testXSLT", "testXSLT3");
770: removeProcessDefinition("st-testXSLT", "testXSLT4");
771: removeProcessDefinition("st-testXSLT", "testXSLT5");
772: removeProcessDefinition("st-testXSLT", "testXSLT6");
773: removeProcessDefinition("st-testXSLT", "testXSLT7");
774: removeProcessDefinition("st-testXSLT", "testXSLT8");
775: removeProcessDefinition("st-testXSLT", "testXSLT9");
776: removeProcessDefinition("st-testXSLT", "testXSLT10");
777: removeProcessDefinition("st-testXSLT", "testXSLT11");
778: removeProcessDefinition("st-testXSLT", "testXSLT12");
779: removeProcessDefinition("st-testXSLT", "testXSLT13");
780: removeProcessDefinition("st-testXSLT", "testXSLT15");
781: }
782:
783: public static class BodyFiller extends
784: org.xml.sax.helpers.XMLFilterImpl {
785:
786: private boolean hasStartDocument = false;
787: private boolean hasEndDocument = false;
788:
789: /**
790: * Create a new BodyFiller.
791: * @param contentHandler the given content handler.
792: */
793: public BodyFiller(ContentHandler contentHandler) {
794: this .setContentHandler(contentHandler);
795: }
796:
797: /**
798: * @throws SAXException if any SAX processing error ocurred.
799: */
800: public void startDocument() throws SAXException {
801: this .getContentHandler().startDocument();
802: hasStartDocument = true;
803: }
804:
805: /**
806: * @throws SAXException if any SAX processing error ocurred.
807: */
808: public void endDocument() throws SAXException {
809: if (!hasEndDocument) {
810: this .getContentHandler().endDocument();
811: hasEndDocument = true;
812: }
813: }
814:
815: /**
816: * @throws SAXException if any SAX processing error ocurred.
817: */
818: public void startElement(String uri, String localName,
819: String qName, Attributes atts) throws SAXException {
820: if (!hasStartDocument) {
821: this .getContentHandler().startDocument();
822: hasStartDocument = true;
823: }
824: this .getContentHandler().startElement(uri, localName,
825: qName, atts);
826: }
827:
828: /**
829: * @throws SAXException if any SAX processing error ocurred.
830: */
831: public void endPrefixMapping(String prefix) throws SAXException {
832: if (!hasEndDocument) {
833: this .getContentHandler().endDocument();
834: hasEndDocument = true;
835: }
836: this .getContentHandler().endPrefixMapping(prefix);
837: }
838: }
839:
840: public static class MyContentHandler implements
841: org.xml.sax.ContentHandler {
842: private static final org.apache.commons.logging.Log logger = org.apache.commons.logging.LogFactory
843: .getLog(MyContentHandler.class);
844:
845: public void characters(char[] ch, int start, int length) {
846: logger.info("characters(" + ch + ", " + start + ", "
847: + length + ")");
848: }
849:
850: public void endDocument() {
851: logger.info("endDocument()");
852: }
853:
854: public void endElement(String namespaceURI, String localName,
855: String qName) {
856: logger.info("endElement(" + namespaceURI + ", " + localName
857: + ", " + qName + ")");
858: }
859:
860: public void endPrefixMapping(String prefix) {
861: logger.info("endPrefixMapping(" + prefix + ")");
862: }
863:
864: public void ignorableWhitespace(char[] ch, int start, int length) {
865: logger.info("ignorableWhitespace(" + ch + ", " + start
866: + ", " + length + ")");
867: }
868:
869: public void processingInstruction(String target, String data) {
870: logger.info("processingInstruction(" + target + ", " + data
871: + ")");
872: }
873:
874: public void setDocumentLocator(org.xml.sax.Locator locator) {
875: logger.info("setDocumentLocator(" + locator + ")");
876: }
877:
878: public void skippedEntity(String name) {
879: logger.info("skippedEntity(" + name + ")");
880: }
881:
882: public void startDocument() {
883: logger.info("startDocument()");
884: }
885:
886: public void startElement(String namespaceURI, String localName,
887: String qName, org.xml.sax.Attributes atts) {
888: logger.info("startElement(" + namespaceURI + ", "
889: + localName + ", " + qName + ")");
890: }
891:
892: public void startPrefixMapping(String prefix, String uri) {
893: logger.info("startPrefixMapping(" + prefix + ", " + uri
894: + ")");
895: }
896: }
897:
898: private boolean stateReached(WfProcess proc, String procState)
899: throws Exception {
900: boolean test = true;
901: boolean stateReached = false;
902: int maxRetries = 100;
903: while (test) {
904: if (maxRetries-- > 0) {
905: if (proc.state().startsWith(procState)) {
906: stateReached = true;
907: test = false;
908: } else {
909: Thread.sleep(500);
910: }
911: } else {
912: test = false;
913: }
914: }
915: return stateReached;
916: }
917:
918: }
|