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: TestFtpProvider.java 3655 2006-12-12 07:52:59Z jzhang $
024: */
025: package com.bostechcorp.cbesb.runtime.component.ftp;
026:
027: import java.io.BufferedReader;
028: import java.io.ByteArrayInputStream;
029: import java.io.File;
030: import java.io.FileInputStream;
031: import java.io.InputStream;
032: import java.io.InputStreamReader;
033: import java.net.URI;
034: import java.net.URL;
035:
036: import javax.jbi.messaging.ExchangeStatus;
037: import javax.jbi.messaging.InOnly;
038: import javax.xml.namespace.QName;
039: import javax.xml.transform.stream.StreamSource;
040:
041: import junit.framework.TestCase;
042:
043: import org.apache.servicemix.client.DefaultServiceMixClient;
044: import org.apache.servicemix.jbi.container.ActivationSpec;
045: import org.apache.servicemix.jbi.container.JBIContainer;
046: import org.apache.servicemix.tck.Receiver;
047: import org.apache.servicemix.tck.ReceiverComponent;
048:
049: import com.bostechcorp.cbesb.common.util.FileUtil;
050:
051: public class TestFtpProvider extends TestCase {
052: protected JBIContainer container = new JBIContainer();
053:
054: protected void setUp() throws Exception {
055: container.setUseMBeanServer(false);
056: container.setCreateMBeanServer(false);
057: container.setEmbedded(true);
058: container.setCreateJmxConnector(false);
059: container.init();
060: }
061:
062: protected void tearDown() throws Exception {
063: if (container != null) {
064: container.shutDown();
065: }
066: }
067:
068: public void testInOnlyCase1() throws Exception {
069: runTestCase("provider/ftpprovider.wsdl");
070: }
071:
072: private long runTestCase(String provider) throws Exception {
073: // FTP Component
074: FtpComponent component = new FtpComponent();
075: container.activateComponent(component, "FtpComponent");
076:
077: // Add a receiver component
078: Receiver receiver = new ReceiverComponent();
079: ActivationSpec asReceiver = new ActivationSpec("receiver",
080: receiver);
081: asReceiver.setService(new QName(
082: "http://bostechcorp.com/SU/Ftp", "Ftp_Interface"));
083: container.activateComponent(asReceiver);
084:
085: // Add the FilePoller
086: /*FilePoller connector = new FilePoller();
087: connector.setFile(new File("target/test-data/out/outbox"));
088: ActivationSpec asConnector = new ActivationSpec("connector", connector);
089: asConnector.setDestinationService(new QName("http://file.bostechcorp.com/Test",
090: "FileInterface"));
091: container.activateComponent(asConnector);*/
092:
093: // Start container
094: container.start();
095:
096: // Deploy SU
097: URL url = getClass().getClassLoader().getResource(provider);
098: File path = new File(new URI(url.toString()));
099: path = path.getParentFile();
100: component.getServiceUnitManager().deploy("provider",
101: path.getAbsolutePath());
102: component.getServiceUnitManager().start("provider");
103:
104: // Call it
105: DefaultServiceMixClient client = new DefaultServiceMixClient(
106: container);
107: InOnly in = client.createInOnlyExchange();
108: in.setInterfaceName(new QName("http://bostechcorp.com/SU/Ftp",
109: "Ftp_Interface"));
110: String script = getStr(new FileInputStream(
111: "target/in/scripts.xml"));
112: String msg = "<DataEnvelope><XMLRecord>" + script
113: + "</XMLRecord></DataEnvelope>";
114: in.getInMessage().setContent(
115: new StreamSource(new ByteArrayInputStream(msg
116: .getBytes())));
117:
118: long t0 = System.currentTimeMillis();
119: client.sendSync(in);
120: long t1 = System.currentTimeMillis();
121: assertTrue(in.getStatus() == ExchangeStatus.DONE);
122:
123: // let us give FilePoller some time to pick up files
124: try {
125: Thread.sleep(10 * 1000);
126: } catch (InterruptedException e) {
127: e.printStackTrace();
128: }
129:
130: // Check we received the message
131: // receiver.getMessageList().assertMessagesReceived(1);
132: File f = new File("message0");
133: if (!f.exists())
134: fail("Failed to create result file");
135: String result = FileUtil.readStringFromFile("message0");
136: String expected = result = FileUtil
137: .readStringFromFile("target/classes/message0");
138: assertTrue(result.equals(expected));
139: f.delete();
140: // return t1 - t0;
141:
142: component.getServiceUnitManager().stop("provider");
143: component.getServiceUnitManager().shutDown("provider");
144: component.getServiceUnitManager().undeploy("provider",
145: path.getAbsolutePath());
146:
147: return t1 - t0;
148: }
149:
150: private String getStr(InputStream in) {
151: try {
152: BufferedReader buffer = new BufferedReader(
153: new InputStreamReader(in));
154: String tempstr = "";
155: String strsum = "";
156: while ((tempstr = buffer.readLine()) != null) {
157: strsum = strsum + tempstr;
158: }
159: buffer.close();
160: // in.close();
161: return strsum;
162: } catch (Exception e) {
163: e.printStackTrace();
164: }
165: return "";
166: }
167: }
|