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: TestFileProvider.java 2277 2006-10-21 11:53:50Z jzhang $
024: */
025: package com.bostechcorp.cbesb.runtime.component.file;
026:
027: import java.io.ByteArrayInputStream;
028: import java.io.File;
029: import java.net.URI;
030: import java.net.URL;
031:
032: import javax.jbi.messaging.ExchangeStatus;
033: import javax.jbi.messaging.InOnly;
034: import javax.xml.namespace.QName;
035: import javax.xml.transform.stream.StreamSource;
036:
037: import junit.framework.TestCase;
038:
039: import org.apache.servicemix.client.DefaultServiceMixClient;
040: import org.apache.servicemix.components.file.FilePoller;
041: import org.apache.servicemix.jbi.container.ActivationSpec;
042: import org.apache.servicemix.jbi.container.JBIContainer;
043: import org.apache.servicemix.tck.Receiver;
044: import org.apache.servicemix.tck.ReceiverComponent;
045:
046: import com.bostechcorp.cbesb.common.util.FileUtil;
047: import com.bostechcorp.cbesb.runtime.file.FileOperator;
048:
049: /**
050: * @author j.zhang
051: * @version 1.0.0
052: */
053: public class TestFileProvider extends TestCase {
054:
055: protected JBIContainer container;
056:
057: private static final String CONFIG = "target/test-data/config";
058: private static final String IN = "target/test-data/in";
059: private static final String OUT = "target/test-data/out";
060: private static final String RESULT = "target/test-data/result";
061:
062: protected void setUp() throws Exception {
063: container = new JBIContainer();
064: container.setUseMBeanServer(false);
065: container.setCreateMBeanServer(false);
066: container.setEmbedded(true);
067: container.setCreateJmxConnector(false);
068: container.init();
069: deleteFile();
070: }
071:
072: protected void tearDown() throws Exception {
073: if (container != null) {
074: container.shutDown();
075: }
076: }
077:
078: /*
079: * The component flow for this testing are as follow:
080: *
081: * SMClient creates -> InOnly -> route FileComponent's Provider side -> Write into the
082: * outbox directory -> picked up by FilePoller -> route to ReceiverComponent
083: *
084: */
085:
086: protected long testInOnly(String msg, boolean streaming)
087: throws Exception {
088: // HTTP Component
089: FileComponent component = new FileComponent();
090: // ((FileLifeCycle)
091: // component.getLifeCycle()).getConfiguration().setStreamingEnabled(streaming);
092: container.activateComponent(component, "FileProviderTest");
093:
094: // Add a receiver component
095: Receiver receiver = new ReceiverComponent();
096: ActivationSpec asReceiver = new ActivationSpec("receiver",
097: receiver);
098: asReceiver.setService(new QName(
099: "http://file.bostechcorp.com/Test", "FileInterface"));
100: container.activateComponent(asReceiver);
101:
102: // Add the FilePoller
103: FilePoller connector = new FilePoller();
104: connector.setFile(new File("target/test-data/out/outbox"));
105: ActivationSpec asConnector = new ActivationSpec("connector",
106: connector);
107: asConnector.setDestinationService(new QName(
108: "http://file.bostechcorp.com/Test", "FileInterface"));
109: container.activateComponent(asConnector);
110:
111: // Start container
112: container.start();
113:
114: // Deploy SU
115: URL url = getClass().getClassLoader().getResource(
116: "provider/file.wsdl");
117: File path = new File(new URI(url.toString()));
118: path = path.getParentFile();
119: component.getServiceUnitManager().deploy("provider",
120: path.getAbsolutePath());
121: component.getServiceUnitManager().start("provider");
122:
123: // Call it
124: DefaultServiceMixClient client = new DefaultServiceMixClient(
125: container);
126: InOnly in = client.createInOnlyExchange();
127: in.setInterfaceName(new QName(
128: "http://file.bostechcorp.com/Test", "FileInterface"));
129: in.getInMessage().setContent(
130: new StreamSource(new ByteArrayInputStream(msg
131: .getBytes())));
132:
133: long t0 = System.currentTimeMillis();
134: client.sendSync(in);
135: long t1 = System.currentTimeMillis();
136: assertTrue(in.getStatus() == ExchangeStatus.DONE);
137:
138: // let us give FilePoller some time to pick up files
139: try {
140: Thread.sleep(10 * 1000);
141: } catch (InterruptedException e) {
142: e.printStackTrace();
143: }
144:
145: // Check we received the message
146: // receiver.getMessageList().assertMessagesReceived(1);
147: File f = new File("message0");
148: if (!f.exists())
149: fail("Failed to create result file");
150: String result = FileUtil.readStringFromFile("message0");
151: String expected = result = FileUtil
152: .readStringFromFile("target/classes/message0");
153: assertTrue(result.equals(expected));
154: f.delete();
155: // return t1 - t0;
156:
157: component.getServiceUnitManager().stop("provider");
158: component.getServiceUnitManager().shutDown("provider");
159: component.getServiceUnitManager().undeploy("provider",
160: path.getAbsolutePath());
161:
162: return t1 - t0;
163: }
164:
165: public void testInOnly() throws Exception {
166: testInOnly(
167: "<DataEnvelope><XMLRecord><hello>world</hello></XMLRecord></DataEnvelope>",
168: false);
169: }
170:
171: /**
172: * Clean output folder. Delete all of the test files and folders.
173: */
174: private void deleteFile() {
175: File[] config = FileOperator.scanDir(CONFIG);
176: File[] in = FileOperator.scanDir(IN);
177: File[] out = FileOperator.scanDir(OUT);
178: File[] result = FileOperator.scanDir(RESULT);
179: for (int i = 0; i < config.length; i++) {
180: if (!(config[i].delete())) {
181: File[] configs = FileOperator.scanDir(config[i]
182: .getPath());
183: for (int j = 0; j < configs.length; j++) {
184: configs[j].delete();
185: }
186: config[i].delete();
187: }
188: }
189: for (int i = 0; i < in.length; i++) {
190: if (!(in[i].delete())) {
191: File[] ins = FileOperator.scanDir(in[i].getPath());
192: for (int j = 0; j < ins.length; j++) {
193: ins[j].delete();
194: }
195: in[i].delete();
196: }
197: }
198: for (int i = 0; i < out.length; i++) {
199: if (!(out[i].delete())) {
200: File[] outs = FileOperator.scanDir(out[i].getPath());
201: for (int j = 0; j < outs.length; j++) {
202: outs[j].delete();
203: }
204: out[i].delete();
205: }
206: }
207: for (int i = 0; i < result.length; i++) {
208: if (!(result[i].delete())) {
209: File[] results = FileOperator.scanDir(result[i]
210: .getPath());
211: for (int j = 0; j < results.length; j++) {
212: results[j].delete();
213: }
214: result[i].delete();
215: }
216: }
217: }
218: }
|