001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify
008: * it under the terms of the GNU General Public License as published by
009: * the Free Software Foundation; either version 2 of the License, or
010: * (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc.,59 Temple Place, Suite 330, Boston, MA 02111-1307
020: * USA
021: *
022: * $Id: TestJdbcProvider.java 4575 2007-01-18 11:49:23Z jzhang $
023: *
024: */
025: package com.bostechcorp.cbesb.runtime.component.jdbc;
026:
027: import java.io.File;
028: import java.net.URI;
029: import java.net.URL;
030:
031: import javax.jbi.messaging.ExchangeStatus;
032: import javax.jbi.messaging.InOut;
033: import javax.jbi.messaging.NormalizedMessage;
034: import javax.xml.namespace.QName;
035: import javax.xml.transform.Source;
036: import javax.xml.transform.dom.DOMSource;
037:
038: import junit.framework.TestCase;
039:
040: import org.apache.servicemix.client.DefaultServiceMixClient;
041: import org.apache.servicemix.jbi.container.JBIContainer;
042: import org.apache.servicemix.jbi.jaxp.SourceTransformer;
043: import org.apache.servicemix.jbi.messaging.InOutImpl;
044: import org.w3c.dom.Document;
045: import org.w3c.dom.Element;
046: import org.w3c.dom.Node;
047:
048: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.StringSource;
049:
050: /**
051: * JDBC component TestCase.
052: */
053: public class TestJdbcProvider extends TestCase {
054:
055: protected JBIContainer container;
056:
057: protected void setUp() throws Exception {
058: container = new JBIContainer();
059: container.setUseMBeanServer(false);
060: container.setCreateMBeanServer(false);
061: container.setEmbedded(true);
062: container.setCreateJmxConnector(false);
063: container.init();
064: }
065:
066: protected void tearDown() throws Exception {
067: if (container != null) {
068: container.shutDown();
069: }
070: }
071:
072: /**
073: * Testing the "execute" format of Request Message that contain the "pageSize"
074: * attribute, "keepOpen" attribute, question mark placeholder and "var" elements.
075: *
076: * @throws Throwable
077: */
078: public void testUseCase1() throws Throwable {
079: JdbcComponent jdbcComponent = new JdbcComponent();
080: container.activateComponent(jdbcComponent, "jdbcService");
081:
082: // Start container
083: container.start();
084:
085: // Deploy SU
086: URL url = getClass().getClassLoader().getResource(
087: "useCase1/jdbcUseCase1.wsdl");
088: File path = new File(new URI(url.toString()));
089: path = path.getParentFile();
090: jdbcComponent.getServiceUnitManager().deploy("jdbc",
091: path.getAbsolutePath());
092: jdbcComponent.getServiceUnitManager().start("jdbc");
093:
094: // Call it
095: DefaultServiceMixClient client = new DefaultServiceMixClient(
096: container);
097: InOut inout = new InOutImpl("exchangeId");
098: inout.setInterfaceName(new QName(
099: "http://jdbc.bostechcorp.com/Test", "jdbcInterface"));
100:
101: //create message content
102: String content = "<DataEnvelope><XMLRecord>"
103: + "<jdbc_request xmlns='http://cbesb.bostechcorp.com/jdbc/1.0'>"
104: + "<execute>"
105: + "<statement pageSize=\"-1\" keepOpen=\"false\">"
106: + "select stor_name, stor_address, city from stores where state=?"
107: + "</statement>"
108: + "<vars><var mode=\"IN\" datatype=\"VARCHAR\">CA</var></vars>"
109: + "</execute>" + "</jdbc_request>"
110: + "</XMLRecord></DataEnvelope>";
111:
112: StringSource inSrc = new StringSource(content);
113: NormalizedMessage nmsg = inout.createMessage();
114: nmsg.setContent(inSrc);
115: inout.setMessage(nmsg, "In");
116: /*
117:
118: // InOut inout = (InOut) me;
119: StringSource inSrc = new StringSource(content);//
120: NormalizedMessage nmsg = inout.createMessage();//
121: inout.setInMessage(nmsg);//
122: nmh = new NormalizedMessageHandler(nmsg);
123: nmh.addRecord(inSrc);
124: nmsg = nmh.generateMessageContent();
125: nmh.debugContents();
126: */
127: boolean result = client.sendSync(inout);
128: assertTrue(result);
129: assertTrue(inout.getStatus() == ExchangeStatus.ACTIVE);
130: NormalizedMessage out = inout.getOutMessage();
131: assertNotNull(out);
132: Source outSrc = out.getContent();
133: assertNotNull(outSrc);
134:
135: String resultXML = "";
136: resultXML = new SourceTransformer().toString(outSrc);
137: System.err.println(resultXML);
138:
139: inout.setStatus(ExchangeStatus.DONE);
140: client.send(inout);
141:
142: // Let us sleep for a while and give other side a chance to receive exchange
143: try {
144: Thread.sleep(5 * 1000);
145: } catch (InterruptedException e) {
146: e.printStackTrace();
147: }
148:
149: jdbcComponent.getServiceUnitManager().stop("jdbc");
150: jdbcComponent.getServiceUnitManager().shutDown("jdbc");
151: jdbcComponent.getServiceUnitManager().undeploy("jdbc",
152: path.getAbsolutePath());
153: }
154:
155: /**
156: * Testing the "get_page" format of Request Message that contain the absolute
157: * page number, the value "NEXT" and "PREVIOUS".
158: *
159: * @throws Throwable
160: */
161: public void testUseCase2() throws Throwable {
162: JdbcComponent jdbcComponent = new JdbcComponent();
163: container.activateComponent(jdbcComponent, "jdbcService");
164:
165: // Start container
166: container.start();
167:
168: // Deploy SU
169: URL url = getClass().getClassLoader().getResource(
170: "useCase1/jdbcUseCase1.wsdl");
171: File path = new File(new URI(url.toString()));
172: path = path.getParentFile();
173: jdbcComponent.getServiceUnitManager().deploy("jdbc",
174: path.getAbsolutePath());
175: jdbcComponent.getServiceUnitManager().start("jdbc");
176:
177: // Call it
178: DefaultServiceMixClient client = new DefaultServiceMixClient(
179: container);
180: InOut inout = new InOutImpl("exchangeId");
181: inout.setInterfaceName(new QName(
182: "http://jdbc.bostechcorp.com/Test", "jdbcInterface"));
183:
184: //create message content
185: String content = "<DataEnvelope><XMLRecord>"
186: + "<jdbc_request xmlns='http://cbesb.bostechcorp.com/jdbc/1.0'>"
187: + "<execute>"
188: + "<statement pageSize=\"2\" keepOpen=\"true\">"
189: + "select stor_id, stor_name, stor_address, city, state, zip from stores"
190: + "</statement>" + "</execute>" + "</jdbc_request>"
191: + "</XMLRecord></DataEnvelope>";
192: StringSource inSrc = new StringSource(content);
193: NormalizedMessage nmsg = inout.createMessage();
194: nmsg.setContent(inSrc);
195: inout.setMessage(nmsg, "In");
196:
197: boolean result = client.sendSync(inout);
198: assertTrue(result);
199: assertTrue(inout.getStatus() == ExchangeStatus.ACTIVE);
200: NormalizedMessage out = inout.getOutMessage();
201: assertNotNull(out);
202: Source outSrc = out.getContent();
203: assertNotNull(outSrc);
204:
205: String resultXML = "";
206: resultXML = new SourceTransformer().toString(outSrc);
207: System.err.println(resultXML);
208:
209: inout.setStatus(ExchangeStatus.DONE);
210: client.send(inout);
211:
212: // Let us sleep for a while and give other side a chance to receive exchange
213: try {
214: Thread.sleep(8 * 1000);
215: } catch (InterruptedException e) {
216: e.printStackTrace();
217: }
218:
219: InOut inout2 = new InOutImpl("exchangeId");
220: inout2.setInterfaceName(new QName(
221: "http://jdbc.bostechcorp.com/Test", "jdbcInterface"));
222:
223: String sessionId = getSessionID(out);
224: //create message content
225: String content2 = "<DataEnvelope><XMLRecord>"
226: + "<jdbc_request xmlns='http://cbesb.bostechcorp.com/jdbc/1.0' "
227: + "sessionId=\"" + sessionId + "\">"
228: + "<get_page>NEXT</get_page>" + "</jdbc_request>"
229: + "</XMLRecord></DataEnvelope>";
230: StringSource inSrc2 = new StringSource(content2);
231: NormalizedMessage nmsg2 = inout2.createMessage();
232: nmsg2.setContent(inSrc2);
233: inout2.setMessage(nmsg2, "In");
234:
235: boolean result2 = client.sendSync(inout2);
236: assertTrue(result2);
237: assertTrue(inout2.getStatus() == ExchangeStatus.ACTIVE);
238: NormalizedMessage out2 = inout2.getOutMessage();
239: assertNotNull(out2);
240: Source outSrc2 = out2.getContent();
241: assertNotNull(outSrc2);
242:
243: String resultXML2 = "";
244: resultXML2 = new SourceTransformer().toString(outSrc2);
245: System.err.println(resultXML2);
246:
247: inout2.setStatus(ExchangeStatus.DONE);
248: client.send(inout2);
249:
250: // Let us sleep for a while and give other side a chance to receive exchange
251: try {
252: Thread.sleep(8 * 1000);
253: } catch (InterruptedException e) {
254: e.printStackTrace();
255: }
256:
257: InOut inout3 = new InOutImpl("exchangeId");
258: inout3.setInterfaceName(new QName(
259: "http://jdbc.bostechcorp.com/Test", "jdbcInterface"));
260:
261: sessionId = getSessionID(out2);
262: //create message content
263: String content3 = "<DataEnvelope><XMLRecord>"
264: + "<jdbc_request xmlns='http://cbesb.bostechcorp.com/jdbc/1.0' "
265: + "sessionId=\"" + sessionId + "\">"
266: + "<get_page>PREVIOUS</get_page>" + "</jdbc_request>"
267: + "</XMLRecord></DataEnvelope>";
268: StringSource inSrc3 = new StringSource(content3);
269: NormalizedMessage nmsg3 = inout3.createMessage();
270: nmsg3.setContent(inSrc3);
271: inout3.setMessage(nmsg3, "In");
272:
273: boolean result3 = client.sendSync(inout3);
274: assertTrue(result3);
275: assertTrue(inout3.getStatus() == ExchangeStatus.ACTIVE);
276: NormalizedMessage out3 = inout3.getOutMessage();
277: assertNotNull(out3);
278: Source outSrc3 = out3.getContent();
279: assertNotNull(outSrc3);
280:
281: String resultXML3 = "";
282: resultXML3 = new SourceTransformer().toString(outSrc3);
283: System.err.println(resultXML3);
284:
285: inout3.setStatus(ExchangeStatus.DONE);
286: client.send(inout3);
287:
288: // Let us sleep for a while and give other side a chance to receive exchange
289: try {
290: Thread.sleep(8 * 1000);
291: } catch (InterruptedException e) {
292: e.printStackTrace();
293: }
294:
295: InOut inout4 = new InOutImpl("exchangeId");
296: inout4.setInterfaceName(new QName(
297: "http://jdbc.bostechcorp.com/Test", "jdbcInterface"));
298:
299: sessionId = getSessionID(out3);
300: //create message content
301: String content4 = "<DataEnvelope><XMLRecord>"
302: + "<jdbc_request xmlns='http://cbesb.bostechcorp.com/jdbc/1.0' "
303: + "sessionId=\"" + sessionId + "\">"
304: + "<get_page>3</get_page>" + "</jdbc_request>"
305: + "</XMLRecord></DataEnvelope>";
306: StringSource inSrc4 = new StringSource(content4);
307: NormalizedMessage nmsg4 = inout4.createMessage();
308: nmsg4.setContent(inSrc4);
309: inout4.setMessage(nmsg4, "In");
310:
311: boolean result4 = client.sendSync(inout4);
312: assertTrue(result4);
313: assertTrue(inout4.getStatus() == ExchangeStatus.ACTIVE);
314: NormalizedMessage out4 = inout4.getOutMessage();
315: assertNotNull(out4);
316: Source outSrc4 = out4.getContent();
317: assertNotNull(outSrc4);
318:
319: String resultXML4 = "";
320: resultXML4 = new SourceTransformer().toString(outSrc4);
321: System.err.println(resultXML4);
322:
323: inout4.setStatus(ExchangeStatus.DONE);
324: client.send(inout4);
325:
326: // Let us sleep for a while and give other side a chance to receive exchange
327: try {
328: Thread.sleep(8 * 1000);
329: } catch (InterruptedException e) {
330: e.printStackTrace();
331: }
332:
333: jdbcComponent.getServiceUnitManager().stop("jdbc");
334: jdbcComponent.getServiceUnitManager().shutDown("jdbc");
335: jdbcComponent.getServiceUnitManager().undeploy("jdbc",
336: path.getAbsolutePath());
337: }
338:
339: /**
340: * Testing Request Message - transaction: COMMIT, ROLLBACK, BEGIN and END.
341: * transaction BEGIN -> insert -> COMMIT -> END.
342: *
343: * @throws Exception
344: */
345: public void testUseCase3() throws Exception {
346: JdbcComponent jdbcComponent = new JdbcComponent();
347: container.activateComponent(jdbcComponent, "jdbcService");
348:
349: // Start container
350: container.start();
351:
352: // Deploy SU
353: URL url = getClass().getClassLoader().getResource(
354: "useCase3/jdbcUseCase3.wsdl");
355: File path = new File(new URI(url.toString()));
356: path = path.getParentFile();
357: jdbcComponent.getServiceUnitManager().deploy("jdbc",
358: path.getAbsolutePath());
359: jdbcComponent.getServiceUnitManager().start("jdbc");
360:
361: // Call it
362: DefaultServiceMixClient client = new DefaultServiceMixClient(
363: container);
364: InOut inout = new InOutImpl("exchangeId");
365: inout.setInterfaceName(new QName(
366: "http://jdbc.bostechcorp.com/Test", "jdbcInterface"));
367:
368: //create message content
369: String content = "<DataEnvelope><XMLRecord>"
370: + "<jdbc_request xmlns='http://cbesb.bostechcorp.com/jdbc/1.0'>"
371: + "<transaction>BEGIN</transaction>"
372: + "</jdbc_request>" + "</XMLRecord></DataEnvelope>";
373: StringSource inSrc = new StringSource(content);
374: NormalizedMessage nmsg = inout.createMessage();
375: nmsg.setContent(inSrc);
376: inout.setMessage(nmsg, "In");
377:
378: boolean result = client.sendSync(inout);
379: assertTrue(result);
380: assertTrue(inout.getStatus() == ExchangeStatus.ACTIVE);
381: NormalizedMessage out = inout.getOutMessage();
382: assertNotNull(out);
383: Source outSrc = out.getContent();
384: assertNotNull(outSrc);
385:
386: String resultXML = "";
387: resultXML = new SourceTransformer().toString(outSrc);
388: System.err.println(resultXML);
389:
390: inout.setStatus(ExchangeStatus.DONE);
391: client.send(inout);
392:
393: // Let us sleep for a while and give other side a chance to receive exchange
394: try {
395: Thread.sleep(8 * 1000);
396: } catch (InterruptedException e) {
397: e.printStackTrace();
398: }
399:
400: InOut inout2 = new InOutImpl("exchangeId");
401: inout2.setInterfaceName(new QName(
402: "http://jdbc.bostechcorp.com/Test", "jdbcInterface"));
403:
404: String sessionId = getSessionID(out);
405: //create message content
406: String content2 = "<DataEnvelope><XMLRecord>"
407: + "<jdbc_request xmlns='http://cbesb.bostechcorp.com/jdbc/1.0' sessionId=\""
408: + sessionId
409: + "\">"
410: + "<execute>"
411: + "<statement>insert into stores (stor_id, stor_name, stor_address, city, state, zip) values ('1234', 'KFC', '246 Five St.', 'Beijing', 'BJ', '10021')</statement>"
412: + "</execute>" + "</jdbc_request>"
413: + "</XMLRecord></DataEnvelope>";
414: StringSource inSrc2 = new StringSource(content2);
415: NormalizedMessage nmsg2 = inout2.createMessage();
416: nmsg2.setContent(inSrc2);
417: inout2.setMessage(nmsg2, "In");
418:
419: boolean result2 = client.sendSync(inout2);
420: assertTrue(result2);
421: assertTrue(inout2.getStatus() == ExchangeStatus.ACTIVE);
422: NormalizedMessage out2 = inout2.getOutMessage();
423: assertNotNull(out2);
424: Source outSrc2 = out2.getContent();
425: assertNotNull(outSrc2);
426:
427: String resultXML2 = "";
428: resultXML2 = new SourceTransformer().toString(outSrc2);
429: System.err.println(resultXML2);
430:
431: inout2.setStatus(ExchangeStatus.DONE);
432: client.send(inout2);
433:
434: // Let us sleep for a while and give other side a chance to receive exchange
435: try {
436: Thread.sleep(8 * 1000);
437: } catch (InterruptedException e) {
438: e.printStackTrace();
439: }
440:
441: InOut inout3 = new InOutImpl("exchangeId");
442: inout3.setInterfaceName(new QName(
443: "http://jdbc.bostechcorp.com/Test", "jdbcInterface"));
444:
445: sessionId = getSessionID(out);
446: //create message content
447: String content3 = "<DataEnvelope><XMLRecord>"
448: + "<jdbc_request xmlns=\"http://cbesb.bostechcorp.com/jdbc/1.0\" sessionId=\""
449: + sessionId + "\">"
450: + "<transaction>COMMIT</transaction>"
451: + "</jdbc_request>" + "</XMLRecord></DataEnvelope>";
452: StringSource inSrc3 = new StringSource(content3);
453: NormalizedMessage nmsg3 = inout3.createMessage();
454: nmsg3.setContent(inSrc3);
455: inout3.setMessage(nmsg3, "In");
456:
457: boolean result3 = client.sendSync(inout3);
458: assertTrue(result3);
459: assertTrue(inout3.getStatus() == ExchangeStatus.ACTIVE);
460: NormalizedMessage out3 = inout3.getOutMessage();
461: assertNotNull(out3);
462: Source outSrc3 = out3.getContent();
463: assertNotNull(outSrc3);
464:
465: String resultXML3 = "";
466: resultXML3 = new SourceTransformer().toString(outSrc3);
467: System.err.println(resultXML3);
468:
469: inout3.setStatus(ExchangeStatus.DONE);
470: client.send(inout3);
471:
472: // Let us sleep for a while and give other side a chance to receive exchange
473: try {
474: Thread.sleep(8 * 1000);
475: } catch (InterruptedException e) {
476: e.printStackTrace();
477: }
478:
479: InOut inout4 = new InOutImpl("exchangeId");
480: inout4.setInterfaceName(new QName(
481: "http://jdbc.bostechcorp.com/Test", "jdbcInterface"));
482:
483: sessionId = getSessionID(out);
484: //create message content
485: String content4 = "<DataEnvelope><XMLRecord>"
486: + "<jdbc_request xmlns='http://cbesb.bostechcorp.com/jdbc/1.0' "
487: + "sessionId=\"" + sessionId + "\">"
488: + "<transaction>END</transaction>" + "</jdbc_request>"
489: + "</XMLRecord></DataEnvelope>";
490: StringSource inSrc4 = new StringSource(content4);
491: NormalizedMessage nmsg4 = inout4.createMessage();
492: nmsg4.setContent(inSrc4);
493: inout4.setMessage(nmsg4, "In");
494:
495: boolean result4 = client.sendSync(inout4);
496: assertTrue(result4);
497: assertTrue(inout4.getStatus() == ExchangeStatus.ACTIVE);
498: NormalizedMessage out4 = inout4.getOutMessage();
499: assertNotNull(out4);
500: Source outSrc4 = out4.getContent();
501: assertNotNull(outSrc4);
502:
503: String resultXML4 = "";
504: resultXML4 = new SourceTransformer().toString(outSrc4);
505: System.err.println(resultXML4);
506:
507: inout4.setStatus(ExchangeStatus.DONE);
508: client.send(inout4);
509:
510: // Let us sleep for a while and give other side a chance to receive exchange
511: try {
512: Thread.sleep(8 * 1000);
513: } catch (InterruptedException e) {
514: e.printStackTrace();
515: }
516:
517: jdbcComponent.getServiceUnitManager().stop("jdbc");
518: jdbcComponent.getServiceUnitManager().shutDown("jdbc");
519: jdbcComponent.getServiceUnitManager().undeploy("jdbc",
520: path.getAbsolutePath());
521: }
522:
523: /**
524: * Delete the record which is added by usecase 3.
525: *
526: * @throws Exception
527: */
528: public void testUseCase4() throws Exception {
529: JdbcComponent jdbcComponent = new JdbcComponent();
530: container.activateComponent(jdbcComponent, "jdbcService");
531:
532: // Start container
533: container.start();
534:
535: // Deploy SU
536: URL url = getClass().getClassLoader().getResource(
537: "useCase1/jdbcUseCase1.wsdl");
538: File path = new File(new URI(url.toString()));
539: path = path.getParentFile();
540: jdbcComponent.getServiceUnitManager().deploy("jdbc",
541: path.getAbsolutePath());
542: jdbcComponent.getServiceUnitManager().start("jdbc");
543:
544: // Call it
545: DefaultServiceMixClient client = new DefaultServiceMixClient(
546: container);
547: InOut inout = new InOutImpl("exchangeId");
548: inout.setInterfaceName(new QName(
549: "http://jdbc.bostechcorp.com/Test", "jdbcInterface"));
550:
551: //create message content
552: String content = "<DataEnvelope><XMLRecord>"
553: + "<jdbc_request xmlns='http://cbesb.bostechcorp.com/jdbc/1.0' >"
554: + "<execute>"
555: + "<statement>delete from stores where stor_id='1234'</statement>"
556: + "</execute>" + "</jdbc_request>"
557: + "</XMLRecord></DataEnvelope>";
558: StringSource inSrc = new StringSource(content);
559: NormalizedMessage nmsg = inout.createMessage();
560: nmsg.setContent(inSrc);
561: inout.setMessage(nmsg, "In");
562: /*
563:
564: // InOut inout = (InOut) me;
565: StringSource inSrc = new StringSource(content);//
566: NormalizedMessage nmsg = inout.createMessage();//
567: inout.setInMessage(nmsg);//
568: nmh = new NormalizedMessageHandler(nmsg);
569: nmh.addRecord(inSrc);
570: nmsg = nmh.generateMessageContent();
571: nmh.debugContents();
572: */
573: boolean result = client.sendSync(inout);
574: assertTrue(result);
575: assertTrue(inout.getStatus() == ExchangeStatus.ACTIVE);
576: NormalizedMessage out = inout.getOutMessage();
577: assertNotNull(out);
578: Source outSrc = out.getContent();
579: assertNotNull(outSrc);
580:
581: String resultXML = "";
582: resultXML = new SourceTransformer().toString(outSrc);
583: System.err.println(resultXML);
584:
585: inout.setStatus(ExchangeStatus.DONE);
586: client.send(inout);
587:
588: // Let us sleep for a while and give other side a chance to receive exchange
589: try {
590: Thread.sleep(5 * 1000);
591: } catch (InterruptedException e) {
592: e.printStackTrace();
593: }
594:
595: jdbcComponent.getServiceUnitManager().stop("jdbc");
596: jdbcComponent.getServiceUnitManager().shutDown("jdbc");
597: jdbcComponent.getServiceUnitManager().undeploy("jdbc",
598: path.getAbsolutePath());
599: }
600:
601: /**
602: * Testing Request Message - transaction: COMMIT, ROLLBACK, BEGIN and END.
603: * transaction BEGIN -> insert -> ROLLBACK -> END.
604: *
605: * @throws Exception
606: */
607: public void testUseCase5() throws Exception {
608: JdbcComponent jdbcComponent = new JdbcComponent();
609: container.activateComponent(jdbcComponent, "jdbcService");
610:
611: // Start container
612: container.start();
613:
614: // Deploy SU
615: URL url = getClass().getClassLoader().getResource(
616: "useCase3/jdbcUseCase3.wsdl");
617: File path = new File(new URI(url.toString()));
618: path = path.getParentFile();
619: jdbcComponent.getServiceUnitManager().deploy("jdbc",
620: path.getAbsolutePath());
621: jdbcComponent.getServiceUnitManager().start("jdbc");
622:
623: // Call it
624: DefaultServiceMixClient client = new DefaultServiceMixClient(
625: container);
626: InOut inout = new InOutImpl("exchangeId");
627: inout.setInterfaceName(new QName(
628: "http://jdbc.bostechcorp.com/Test", "jdbcInterface"));
629:
630: //create message content
631: String content = "<DataEnvelope><XMLRecord>"
632: + "<jdbc_request xmlns='http://cbesb.bostechcorp.com/jdbc/1.0'>"
633: + "<transaction>BEGIN</transaction>"
634: + "</jdbc_request>" + "</XMLRecord></DataEnvelope>";
635: StringSource inSrc = new StringSource(content);
636: NormalizedMessage nmsg = inout.createMessage();
637: nmsg.setContent(inSrc);
638: inout.setMessage(nmsg, "In");
639:
640: boolean result = client.sendSync(inout);
641: assertTrue(result);
642: assertTrue(inout.getStatus() == ExchangeStatus.ACTIVE);
643: NormalizedMessage out = inout.getOutMessage();
644: assertNotNull(out);
645: Source outSrc = out.getContent();
646: assertNotNull(outSrc);
647:
648: String resultXML = "";
649: resultXML = new SourceTransformer().toString(outSrc);
650: System.err.println(resultXML);
651:
652: inout.setStatus(ExchangeStatus.DONE);
653: client.send(inout);
654:
655: // Let us sleep for a while and give other side a chance to receive exchange
656: try {
657: Thread.sleep(8 * 1000);
658: } catch (InterruptedException e) {
659: e.printStackTrace();
660: }
661:
662: InOut inout2 = new InOutImpl("exchangeId");
663: inout2.setInterfaceName(new QName(
664: "http://jdbc.bostechcorp.com/Test", "jdbcInterface"));
665:
666: String sessionId = getSessionID(out);
667: //create message content
668: String content2 = "<DataEnvelope><XMLRecord>"
669: + "<jdbc_request xmlns='http://cbesb.bostechcorp.com/jdbc/1.0' "
670: + "sessionId=\""
671: + sessionId
672: + "\">"
673: + "<execute>"
674: + "<statement>insert into stores (stor_id, stor_name, stor_address, city, state, zip) values ('1234', 'KFC', '246 Five St.', 'Beijing', 'BJ', '10021')</statement>"
675: + "</execute>" + "</jdbc_request>"
676: + "</XMLRecord></DataEnvelope>";
677: StringSource inSrc2 = new StringSource(content2);
678: NormalizedMessage nmsg2 = inout2.createMessage();
679: nmsg2.setContent(inSrc2);
680: inout2.setMessage(nmsg2, "In");
681:
682: boolean result2 = client.sendSync(inout2);
683: assertTrue(result2);
684: assertTrue(inout2.getStatus() == ExchangeStatus.ACTIVE);
685: NormalizedMessage out2 = inout2.getOutMessage();
686: assertNotNull(out2);
687: Source outSrc2 = out2.getContent();
688: assertNotNull(outSrc2);
689:
690: String resultXML2 = "";
691: resultXML2 = new SourceTransformer().toString(outSrc2);
692: System.err.println(resultXML2);
693:
694: inout2.setStatus(ExchangeStatus.DONE);
695: client.send(inout2);
696:
697: // Let us sleep for a while and give other side a chance to receive exchange
698: try {
699: Thread.sleep(8 * 1000);
700: } catch (InterruptedException e) {
701: e.printStackTrace();
702: }
703:
704: InOut inout3 = new InOutImpl("exchangeId");
705: inout3.setInterfaceName(new QName(
706: "http://jdbc.bostechcorp.com/Test", "jdbcInterface"));
707:
708: sessionId = getSessionID(out);
709: //create message content
710: String content3 = "<DataEnvelope><XMLRecord>"
711: + "<jdbc_request xmlns=\"http://cbesb.bostechcorp.com/jdbc/1.0\" sessionId=\""
712: + sessionId + "\">"
713: + "<transaction>ROLLBACK</transaction>"
714: + "</jdbc_request>" + "</XMLRecord></DataEnvelope>";
715: StringSource inSrc3 = new StringSource(content3);
716: NormalizedMessage nmsg3 = inout3.createMessage();
717: nmsg3.setContent(inSrc3);
718: inout3.setMessage(nmsg3, "In");
719:
720: boolean result3 = client.sendSync(inout3);
721: assertTrue(result3);
722: assertTrue(inout3.getStatus() == ExchangeStatus.ACTIVE);
723: NormalizedMessage out3 = inout3.getOutMessage();
724: assertNotNull(out3);
725: Source outSrc3 = out3.getContent();
726: assertNotNull(outSrc3);
727:
728: String resultXML3 = "";
729: resultXML3 = new SourceTransformer().toString(outSrc3);
730: System.err.println(resultXML3);
731:
732: inout3.setStatus(ExchangeStatus.DONE);
733: client.send(inout3);
734:
735: // Let us sleep for a while and give other side a chance to receive exchange
736: try {
737: Thread.sleep(8 * 1000);
738: } catch (InterruptedException e) {
739: e.printStackTrace();
740: }
741:
742: InOut inout4 = new InOutImpl("exchangeId");
743: inout4.setInterfaceName(new QName(
744: "http://jdbc.bostechcorp.com/Test", "jdbcInterface"));
745:
746: sessionId = getSessionID(out);
747: //create message content
748: String content4 = "<DataEnvelope><XMLRecord>"
749: + "<jdbc_request xmlns='http://cbesb.bostechcorp.com/jdbc/1.0' "
750: + "sessionId=\"" + sessionId + "\">"
751: + "<transaction>END</transaction>" + "</jdbc_request>"
752: + "</XMLRecord></DataEnvelope>";
753: StringSource inSrc4 = new StringSource(content4);
754: NormalizedMessage nmsg4 = inout4.createMessage();
755: nmsg4.setContent(inSrc4);
756: inout4.setMessage(nmsg4, "In");
757:
758: boolean result4 = client.sendSync(inout4);
759: assertTrue(result4);
760: assertTrue(inout4.getStatus() == ExchangeStatus.ACTIVE);
761: NormalizedMessage out4 = inout4.getOutMessage();
762: assertNotNull(out4);
763: Source outSrc4 = out4.getContent();
764: assertNotNull(outSrc4);
765:
766: String resultXML4 = "";
767: resultXML4 = new SourceTransformer().toString(outSrc4);
768: System.err.println(resultXML4);
769:
770: inout4.setStatus(ExchangeStatus.DONE);
771: client.send(inout4);
772:
773: // Let us sleep for a while and give other side a chance to receive exchange
774: try {
775: Thread.sleep(8 * 1000);
776: } catch (InterruptedException e) {
777: e.printStackTrace();
778: }
779:
780: jdbcComponent.getServiceUnitManager().stop("jdbc");
781: jdbcComponent.getServiceUnitManager().shutDown("jdbc");
782: jdbcComponent.getServiceUnitManager().undeploy("jdbc",
783: path.getAbsolutePath());
784: }
785:
786: /**
787: * Get the sessionID from the out NormalizedMessage.
788: *
789: * @param inMsg NormalizedMessage.
790: * @return SessionID.
791: */
792: private String getSessionID(NormalizedMessage inMsg)
793: throws Exception {
794: String id = "";
795: Source src = inMsg.getContent();
796: if (src instanceof DOMSource) {
797: Document doc = (Document) ((DOMSource) src).getNode();
798: Element dataEnvelope = doc.getDocumentElement();
799: Node xmlRecord = dataEnvelope.getFirstChild();
800: Node jdbcResponse = xmlRecord.getFirstChild();
801: id = jdbcResponse.getAttributes().getNamedItem("sessionId")
802: .getNodeValue();
803: }
804: /* if (src != null && !(src instanceof DOMSource)) {
805: DOMResult dr = new DOMResult();
806: TransformerFactory tf = TransformerFactory.newInstance();
807: Transformer t = tf.newTransformer();
808: t.transform(src, dr);
809: src = new DOMSource(dr.getNode());
810: }
811: if (src instanceof DOMSource) {
812: Document doc = (Document) ((DOMSource)src).getNode();
813: Element root = doc.getDocumentElement();
814: Node dataEnvelope = root.getFirstChild();
815: dataEnvelope.getNodeValue();
816: id = root.getAttribute("sessionId");
817: }*/
818: return id;
819: }
820: }
|