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: TestFileConsumer.java 2626 2006-10-27 02:14:20Z 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: import javax.jbi.messaging.ExchangeStatus;
032: import javax.jbi.messaging.RobustInOnly;
033: import javax.xml.namespace.QName;
034: import javax.xml.transform.stream.StreamSource;
035:
036: import junit.framework.TestCase;
037:
038: import org.apache.servicemix.client.DefaultServiceMixClient;
039: import org.apache.servicemix.components.file.FileWriter;
040: import org.apache.servicemix.jbi.container.ActivationSpec;
041: import org.apache.servicemix.jbi.container.JBIContainer;
042:
043: import com.bostechcorp.cbesb.common.util.FileUtil;
044: import com.bostechcorp.cbesb.runtime.file.FileOperator;
045:
046: public class TestFileConsumer extends TestCase {
047:
048: protected JBIContainer container = new JBIContainer();
049:
050: private static final String CONFIG = "target/test-data/config";
051: private static final String IN = "target/test-data/in";
052: private static final String OUT = "target/test-data/out";
053: private static final String RESULT = "target/test-data/result";
054:
055: protected void setUp() throws Exception {
056: container.setUseMBeanServer(false);
057: container.setCreateMBeanServer(false);
058: container.setEmbedded(true);
059: container.setCreateJmxConnector(false);
060: container.init();
061: deleteFile();
062: }
063:
064: protected void tearDown() throws Exception {
065: if (container != null) {
066: container.shutDown();
067: }
068: }
069:
070: /**
071: * The component flow for this test case is listed as follow:
072: *
073: * SMClient creates -> RobustInOnly -> route FileWriter component -> Write it into the inbox directory
074: * -> picked up by FileComponent's consumer side -> Create a InOnly message -> route to ReceiverComponent
075: *
076: */
077: public void testInOnlyCase1() throws Exception {
078: runTestCase("consumer/file.wsdl", "provider/file.wsdl",
079: "<hello>world</hello>", false);
080: }
081:
082: public void testInOnlyCase2() throws Exception {
083: runTestCase("consumer2/file.wsdl", "provider2/file.wsdl",
084: "<hello>world</hello>", false);
085: }
086:
087: protected long runTestCase(String consumer, String provider,
088: String msg, boolean streaming) throws Exception {
089: // File Component
090: FileComponent component = new FileComponent();
091: container.activateComponent(component, "FileComponent");
092:
093: // Add the FileWriter
094: FileWriter invoker = new FileWriter();
095: invoker.setDirectory(new File("target/test-data/in/inbox"));
096:
097: ActivationSpec asInvoker = new ActivationSpec("invoker",
098: invoker);
099: asInvoker.setService(new QName("urn:test", "invoker"));
100: container.activateComponent(asInvoker);
101:
102: // Start container
103: container.start();
104:
105: // Deploy SU
106: URL url = getClass().getClassLoader().getResource(consumer);
107: File path = new File(new URI(url.toString()));
108: path = path.getParentFile();
109: component.getServiceUnitManager().deploy("consumer",
110: path.getAbsolutePath());
111: component.getServiceUnitManager().start("consumer");
112:
113: // File provider
114: URL urlProvider = getClass().getClassLoader().getResource(
115: provider);
116: File wsdlPath = new File(new URI(urlProvider.toString()));
117: wsdlPath = wsdlPath.getParentFile();
118: component.getServiceUnitManager().deploy("provider",
119: wsdlPath.getAbsolutePath());
120: component.getServiceUnitManager().start("provider");
121:
122: // Call it
123: DefaultServiceMixClient client = new DefaultServiceMixClient(
124: container);
125: RobustInOnly in = client.createRobustInOnlyExchange();
126: in.setService(new QName("urn:test", "invoker"));
127: in.getInMessage().setContent(
128: new StreamSource(new ByteArrayInputStream(msg
129: .getBytes())));
130:
131: long t0 = System.currentTimeMillis();
132: client.sendSync(in);
133: long t1 = System.currentTimeMillis();
134: assertTrue(in.getStatus() == ExchangeStatus.DONE);
135:
136: try {
137: Thread.sleep(10 * 1000);
138: System.err.println("Sleep 10 s!");
139: } catch (Exception e) {
140: e.printStackTrace();
141: }
142:
143: File f = new File("message0");
144: if (!f.exists())
145: fail("Failed to create result file");
146: String result = FileUtil.readStringFromFile("message0");
147: String expected = result = FileUtil
148: .readStringFromFile("target/classes/message0");
149: assertTrue(result.equals(expected));
150: f.delete();
151: return t1 - t0;
152: }
153:
154: /**
155: * Clean output folder. Delete all of the test files and folders.
156: */
157: private void deleteFile() {
158: File[] config = FileOperator.scanDir(CONFIG);
159: File[] in = FileOperator.scanDir(IN);
160: File[] out = FileOperator.scanDir(OUT);
161: File[] result = FileOperator.scanDir(RESULT);
162: for (int i = 0; i < config.length; i++) {
163: if (!(config[i].delete())) {
164: File[] configs = FileOperator.scanDir(config[i]
165: .getPath());
166: for (int j = 0; j < configs.length; j++) {
167: configs[j].delete();
168: }
169: config[i].delete();
170: }
171: }
172: for (int i = 0; i < in.length; i++) {
173: if (!(in[i].delete())) {
174: File[] ins = FileOperator.scanDir(in[i].getPath());
175: for (int j = 0; j < ins.length; j++) {
176: ins[j].delete();
177: }
178: in[i].delete();
179: }
180: }
181: for (int i = 0; i < out.length; i++) {
182: if (!(out[i].delete())) {
183: File[] outs = FileOperator.scanDir(out[i].getPath());
184: for (int j = 0; j < outs.length; j++) {
185: outs[j].delete();
186: }
187: out[i].delete();
188: }
189: }
190: for (int i = 0; i < result.length; i++) {
191: if (!(result[i].delete())) {
192: File[] results = FileOperator.scanDir(result[i]
193: .getPath());
194: for (int j = 0; j < results.length; j++) {
195: results[j].delete();
196: }
197: result[i].delete();
198: }
199: }
200: }
201: }
|