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: *
024: */
025: /*
026: * Copyright 2005-2006 The Apache Software Foundation.
027: *
028: * Licensed under the Apache License, Version 2.0 (the "License");
029: * you may not use this file except in compliance with the License.
030: * You may obtain a copy of the License at
031: *
032: * http://www.apache.org/licenses/LICENSE-2.0
033: *
034: * Unless required by applicable law or agreed to in writing, software
035: * distributed under the License is distributed on an "AS IS" BASIS,
036: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
037: * See the License for the specific language governing permissions and
038: * limitations under the License.
039: */
040: package com.bostechcorp.cbesb.runtime.component.transformer;
041:
042: import java.io.File;
043: import java.io.FileInputStream;
044: import java.net.URI;
045: import java.net.URL;
046:
047: import javax.jbi.messaging.ExchangeStatus;
048: import javax.jbi.messaging.InOut;
049: import javax.jbi.messaging.NormalizedMessage;
050: import javax.xml.namespace.QName;
051: import javax.xml.transform.Source;
052: import javax.xml.transform.dom.DOMSource;
053:
054: import junit.framework.TestCase;
055:
056: import org.apache.servicemix.client.DefaultServiceMixClient;
057: import org.apache.servicemix.jbi.container.JBIContainer;
058: import org.apache.servicemix.jbi.jaxp.SourceTransformer;
059: import org.apache.servicemix.jbi.messaging.InOutImpl;
060: import org.w3c.dom.Document;
061:
062: import com.bostechcorp.cbesb.common.util.Dom;
063: import com.bostechcorp.cbesb.common.util.FileUtil;
064: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.NormalizedMessageHandler;
065: import com.bostechcorp.cbesb.runtime.ccsl.nmhandler.StringSource;
066:
067: public class TransformerProviderTest extends TestCase {
068:
069: protected JBIContainer container;
070:
071: protected void setUp() throws Exception {
072: container = new JBIContainer();
073: container.setUseMBeanServer(false);
074: container.setCreateMBeanServer(false);
075: container.setEmbedded(true);
076: container.setCreateJmxConnector(false);
077: container.init();
078: }
079:
080: protected void tearDown() throws Exception {
081: if (container != null) {
082: container.shutDown();
083: }
084: }
085:
086: /*
087: * The component flow for this testing are as follow:
088: *
089: * SMClient creates -> InOnly -> route FileComponent's Provider side -> Write into the outbox directory
090: * -> picked up by FilePoller -> route to ReceiverComponent
091: *
092: */
093:
094: protected long testInOut(File inputFile, boolean streaming,
095: boolean isXml) throws Exception {
096:
097: // ParserConfig Component
098: TransformerComponent component = new TransformerComponent();
099: container.activateComponent(component,
100: "TransformerProviderTest");
101:
102: // Start container
103: container.start();
104:
105: // Deploy SU
106: URL url = getClass().getClassLoader().getResource(
107: "provider/transformer.wsdl");
108: File path = new File(new URI(url.toString()));
109: path = path.getParentFile();
110: component.getServiceUnitManager().deploy("provider",
111: path.getAbsolutePath());
112: component.getServiceUnitManager().start("provider");
113:
114: // Call it
115: DefaultServiceMixClient client = new DefaultServiceMixClient(
116: container);
117: InOut inout = new InOutImpl("exchangeId");
118:
119: inout
120: .setInterfaceName(new QName(
121: "http://parser.bostechcorp.com/Test",
122: "XformInterface"));
123: NormalizedMessage nmsg = inout.createMessage();
124:
125: //Create an instance of DataMessageUtil to wrap the NormalizedMessage
126: NormalizedMessageHandler dataMessage1 = new NormalizedMessageHandler(
127: nmsg);
128: //Add the Source as a record
129: if (isXml) {
130: Document input = Dom.getDomTree(inputFile, null);
131: input.normalizeDocument();
132: dataMessage1.addRecord(new DOMSource(input));
133: } else {
134: dataMessage1.addRecord(new StringSource(FileUtil
135: .readString(new FileInputStream(inputFile))));
136: }
137:
138: dataMessage1.debugContents();
139:
140: nmsg = dataMessage1.generateMessageContent();
141: inout.setMessage(nmsg, "In");
142:
143: long t0 = System.currentTimeMillis();
144:
145: boolean result = client.sendSync(inout);
146: assertTrue(result);
147: assertTrue(inout.getStatus() == ExchangeStatus.ACTIVE);
148: NormalizedMessage out = inout.getOutMessage();
149: assertNotNull(out);
150: Source src = out.getContent();
151: assertNotNull(src);
152:
153: String resultXML = "";
154: System.err.println(resultXML = new SourceTransformer()
155: .toString(src));
156: assertTrue(
157: "ParserConfig result fails to match: ",
158: resultXML
159: .equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><DataEnvelope><XMLRecord><Message xmlns=\"http://www.bostechcorp.com/variable\"><ID>Record 1</ID><Data>This is the data for record 1</Data></Message></XMLRecord><XMLRecord><Message xmlns=\"http://www.bostechcorp.com/variable\"><ID>Record 2</ID><Data>This is the data for record 2</Data></Message></XMLRecord><XMLRecord><Message xmlns=\"http://www.bostechcorp.com/variable\"><ID>Record 3</ID><Data>This is the data for record 3</Data></Message></XMLRecord><XMLRecord><Message xmlns=\"http://www.bostechcorp.com/variable\"><ID>Record 4</ID><Data>This is the data for record 4</Data></Message></XMLRecord><XMLRecord><Message xmlns=\"http://www.bostechcorp.com/variable\"><ID>Record 5</ID><Data>This is the data for record 5</Data></Message></XMLRecord></DataEnvelope>"));
160:
161: inout.setStatus(ExchangeStatus.DONE);
162: client.send(inout);
163: long t1 = System.currentTimeMillis();
164:
165: // Let us sleep for a while and give other side a chance to receive exchange
166: try {
167: Thread.sleep(5 * 1000);
168: } catch (InterruptedException e) {
169: // TODO Auto-generated catch block
170: e.printStackTrace();
171: }
172:
173: component.getServiceUnitManager().stop("provider");
174: component.getServiceUnitManager().shutDown("provider");
175: component.getServiceUnitManager().undeploy("provider",
176: path.getAbsolutePath());
177:
178: return t1 - t0;
179: }
180:
181: public void testInOut() {
182: try {
183: // testInOut(new File("target/test-data/in/sendsuppressIn.xml"), false, true);
184: // testInOut(new File("target/test-data/in/contacts.txt"), false, false);
185: testInOut(new File("target/test-data/in/variabletag.txt"),
186: false, false);
187: } catch (Exception e) {
188: // TODO Auto-generated catch block
189: e.printStackTrace();
190: fail();
191: }
192: }
193:
194: }
|