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: *
023: * $Id: XsltProviderTest.java 3844 2006-12-14 21:12:38Z elu $
024: */
025: package com.bostechcorp.cbesb.runtime.component.xslt;
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.stream.StreamSource;
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:
045: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.NormalizedMessageHandler;
046:
047: /**
048: * @author j.zhang
049: * @version 1.0.0
050: */
051: public class XsltProviderTest extends TestCase {
052:
053: protected JBIContainer container;
054:
055: protected void setUp() throws Exception {
056: container = new JBIContainer();
057: container.setUseMBeanServer(false);
058: container.setCreateMBeanServer(false);
059: container.setEmbedded(true);
060: container.setCreateJmxConnector(false);
061: container.init();
062: }
063:
064: protected void tearDown() throws Exception {
065: if (container != null) {
066: container.shutDown();
067: }
068: }
069:
070: public void testInOut() {
071: try {
072: testInOut("target/test-data/in/john.xml", false);
073: } catch (Exception e) {
074: // TODO Auto-generated catch block
075: e.printStackTrace();
076: fail();
077: }
078: }
079:
080: protected long testInOut(String inputFile, boolean streaming)
081: throws Exception {
082: // ParserConfig Component
083: File xmlFile = new File(inputFile);
084: // File xsltFile = new File(outputFile); // JAXP reads data using the Source interface
085: Source xmlSource = new StreamSource(xmlFile);
086:
087: XsltComponent component = new XsltComponent();
088:
089: container.activateComponent(component, "XsltProviderTest");
090:
091: // Start container
092: container.start();
093:
094: // Deploy SU
095: URL url = getClass().getClassLoader().getResource(
096: "provider/xslt.wsdl");
097: File path = new File(new URI(url.toString()));
098: path = path.getParentFile();
099: component.getServiceUnitManager().deploy("provider",
100: path.getAbsolutePath());
101: component.getServiceUnitManager().start("provider");
102:
103: // Call it
104: DefaultServiceMixClient client = new DefaultServiceMixClient(
105: container);
106:
107: InOut inout = new InOutImpl("exchangeId");
108:
109: inout.setInterfaceName(new QName(
110: "http://parser.bostechcorp.com/Test", "XsltInterface"));
111:
112: NormalizedMessage nmsg = inout.createMessage();
113:
114: //Create an instance of DataMessageUtil to wrap the NormalizedMessage
115: NormalizedMessageHandler dataMessage1 = new NormalizedMessageHandler(
116: nmsg);
117: //Add the Source as a record
118: dataMessage1.addRecord(xmlSource);
119: dataMessage1.debugContents();
120:
121: nmsg = dataMessage1.generateMessageContent();
122: inout.setMessage(nmsg, "In");
123:
124: long t0 = System.currentTimeMillis();
125:
126: boolean result = client.sendSync(inout);
127: assertTrue(result);
128: assertTrue(inout.getStatus() == ExchangeStatus.ACTIVE);
129: NormalizedMessage out = inout.getOutMessage();
130: assertNotNull(out);
131: Source src = out.getContent();
132: assertNotNull(src);
133:
134: String resultXML = "";
135: System.err.println(resultXML = new SourceTransformer()
136: .toString(src));
137: assertTrue(
138: "ParserConfig result fails to match: ",
139: resultXML
140: .equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><DataEnvelope><XMLRecord><person>\r\n<name>John Fitzgerald Kennedy</name>\r\n<birthDate>5/29/1917</birthDate>\r\n</person></XMLRecord></DataEnvelope>"));
141:
142: inout.setStatus(ExchangeStatus.DONE);
143: client.send(inout);
144: // assertTrue(inout.getStatus() == ExchangeStatus.DONE);
145: long t1 = System.currentTimeMillis();
146:
147: // Let us sleep for a while and give other side a chance to receive exchange
148: try {
149: Thread.sleep(5 * 1000);
150: } catch (InterruptedException e) {
151: // TODO Auto-generated catch block
152: e.printStackTrace();
153: }
154:
155: component.getServiceUnitManager().stop("provider");
156: component.getServiceUnitManager().shutDown("provider");
157: component.getServiceUnitManager().undeploy("provider",
158: path.getAbsolutePath());
159:
160: return t1 - t0;
161: }
162: }
|