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.parser;
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 X12ParserProviderTest 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 ->
090: * Write into the outbox directory -> picked up by FilePoller -> route to
091: * ReceiverComponent
092: *
093: */
094: protected long testInOut(File inputFile, boolean streaming,
095: boolean isXml) throws Exception {
096:
097: // ParserConfig Component
098: ParserComponent component = new ParserComponent();
099: container.activateComponent(component, "ParserProviderTest");
100:
101: // Start container
102: container.start();
103:
104: // Deploy SU
105: // URL url = getClass().getClassLoader().getResource(
106: // "provider/parser.wsdl");
107: URL url = getClass().getClassLoader().getResource(
108: "provider2/X12.wsdl");
109: File path = new File(new URI(url.toString()));
110: path = path.getParentFile();
111: component.getServiceUnitManager().deploy("provider",
112: path.getAbsolutePath());
113: component.getServiceUnitManager().start("provider");
114:
115: // Call it
116: DefaultServiceMixClient client = new DefaultServiceMixClient(
117: container);
118: InOut inout = new InOutImpl("exchangeId");
119: inout
120: .setInterfaceName(new QName(
121: "http://parser.bostechcorp.com/Test",
122: "ParserInterface"));
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: // dataMessage1.addRecord(new StringSource(msg));
130:
131: if (isXml) {
132: Document input = Dom.getDomTree(inputFile, null);
133: input.normalizeDocument();
134: dataMessage1.addRecord(new DOMSource(input));
135: } else {
136: dataMessage1.addRecord(new StringSource(FileUtil
137: .readString(new FileInputStream(inputFile))));
138: }
139:
140: dataMessage1.debugContents();
141:
142: nmsg = dataMessage1.generateMessageContent();
143: inout.setMessage(nmsg, "In");
144:
145: long t0 = System.currentTimeMillis();
146:
147: boolean result = client.sendSync(inout);
148: assertTrue(result);
149: assertTrue(inout.getStatus() == ExchangeStatus.ACTIVE);
150: NormalizedMessage out = inout.getOutMessage();
151: assertNotNull(out);
152: Source src = out.getContent();
153: assertNotNull(src);
154:
155: String resultXML = new SourceTransformer().toString(src);
156: System.err.println(resultXML);
157: /*assertTrue(
158: "ParserConfig result fails to match: ",
159: resultXML
160: .equals("<?xml version=\"1.0\" encoding=\"UTF-8\"?><DataEnvelope><XMLRecord><OrderRecord xmlns=\"http://www.bostechcorp.com/variable1\"><SKU>4267010</SKU><Manufacturer>Dell</Manufacturer><ItemDescription>Inspiron 5150 Notebook</ItemDescription><UnitPrice>1545.00</UnitPrice><Quantity>1</Quantity><Total>1545.00</Total></OrderRecord></XMLRecord></DataEnvelope>"));*/
161:
162: inout.setStatus(ExchangeStatus.DONE);
163: client.send(inout);
164: long t1 = System.currentTimeMillis();
165:
166: // Let us sleep for a while and give other side a chance to receive
167: // exchange
168: try {
169: Thread.sleep(5 * 1000);
170: } catch (InterruptedException e) {
171: // TODO Auto-generated catch block
172: e.printStackTrace();
173: }
174:
175: component.getServiceUnitManager().stop("provider");
176: component.getServiceUnitManager().shutDown("provider");
177: component.getServiceUnitManager().undeploy("provider",
178: path.getAbsolutePath());
179:
180: return t1 - t0;
181: }
182:
183: public void testInOut() {
184: try {
185: testInOut(new File("target/test-data/in/x12_2.txt"), false,
186: false);
187: } catch (Exception e) {
188: // TODO Auto-generated catch block
189: e.printStackTrace();
190: fail();
191: }
192: }
193: }
|