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 1599 2006-10-10 06:57:30Z jzhang $
024: */
025: package com.bostechcorp.cbesb.runtime.component.jms;
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.jbi.messaging.NormalizedMessage;
035: import javax.xml.namespace.QName;
036: import javax.xml.transform.Source;
037: import javax.xml.transform.stream.StreamSource;
038:
039: import junit.framework.TestCase;
040:
041: import org.apache.servicemix.client.DefaultServiceMixClient;
042: import org.apache.servicemix.components.util.EchoComponent;
043: import org.apache.servicemix.jbi.container.ActivationSpec;
044: import org.apache.servicemix.jbi.container.JBIContainer;
045: import org.apache.servicemix.jbi.jaxp.SourceTransformer;
046:
047: public class TestJMSConsumer extends TestCase {
048:
049: protected JBIContainer container = new JBIContainer();
050:
051: protected void setUp() throws Exception {
052: container.setUseMBeanServer(false);
053: container.setCreateMBeanServer(false);
054: container.setEmbedded(true);
055: container.setCreateJmxConnector(false);
056: container.init();
057: }
058:
059: protected void tearDown() throws Exception {
060: if (container != null) {
061: container.shutDown();
062: }
063: }
064:
065: protected long testInOnOnly(String msg, boolean streaming)
066: throws Exception {
067:
068: // JMS Component
069: JMSComponent component = new JMSComponent();
070: container.activateComponent(component, "JMSComponent");
071:
072: EchoComponent echo = new EchoComponent();
073: ActivationSpec asEcho = new ActivationSpec("receiver", echo);
074: asEcho.setService(new QName("http://jms.bostechcorp.com/Test",
075: "Echo"));
076: container.activateComponent(asEcho);
077:
078: // Start container
079: container.start();
080:
081: // Deploy SU
082: URL url = getClass().getClassLoader().getResource(
083: "consumer/jms.wsdl");
084: File path = new File(new URI(url.toString()));
085: path = path.getParentFile();
086: component.getServiceUnitManager().deploy("consumer",
087: path.getAbsolutePath());
088: component.getServiceUnitManager().start("consumer");
089:
090: // let us give JMS Receiver some time to receive message
091: try {
092: Thread.sleep(10 * 1000);
093: } catch (InterruptedException e) {
094: e.printStackTrace();
095: }
096:
097: // Call it
098: DefaultServiceMixClient client = new DefaultServiceMixClient(
099: container);
100: InOnly inonly = client.createInOnlyExchange();
101: inonly.setInterfaceName(new QName(
102: "http://jms.bostechcorp.com/Test", "JMSInterface"));
103: inonly.getInMessage().setContent(
104: new StreamSource(new ByteArrayInputStream(msg
105: .getBytes())));
106:
107: boolean result = client.sendSync(inonly);
108: assertTrue(result);
109:
110: NormalizedMessage in = inonly.getInMessage();
111: assertNotNull(in);
112:
113: Source src = in.getContent();
114: assertNotNull(src);
115:
116: System.err.println(new SourceTransformer().toString(src));
117:
118: long t0 = System.currentTimeMillis();
119: client.sendSync(inonly);
120: long t1 = System.currentTimeMillis();
121: assertTrue(inonly.getStatus() == ExchangeStatus.DONE);
122:
123: long t2 = System.currentTimeMillis();
124:
125: return t2 - t0;
126:
127: }
128:
129: protected long testInOnOut(String msg, boolean streaming)
130: throws Exception {
131:
132: // JMS Component
133: JMSComponent component = new JMSComponent();
134: container.activateComponent(component, "JMSComponent");
135:
136: EchoComponent echo = new EchoComponent();
137: ActivationSpec asEcho = new ActivationSpec("receiver", echo);
138: asEcho.setService(new QName("http://jms.bostechcorp.com/Test",
139: "Echo"));
140: container.activateComponent(asEcho);
141:
142: // Start container
143: container.start();
144:
145: // Deploy SU
146: URL url = getClass().getClassLoader().getResource(
147: "consumer/jms.wsdl");
148: File path = new File(new URI(url.toString()));
149: path = path.getParentFile();
150: component.getServiceUnitManager().deploy("consumer",
151: path.getAbsolutePath());
152: component.getServiceUnitManager().start("consumer");
153:
154: // let us give JMS Receiver some time to receive message
155: try {
156: Thread.sleep(10 * 1000);
157: } catch (InterruptedException e) {
158: e.printStackTrace();
159: }
160:
161: System.err.println("I am in the consumer");
162:
163: return 0;
164: }
165:
166: /**
167: * The component flow for this test case is listed as follow:
168: *
169: * SMClient creates -> RobustInOnly -> route FileWriter component -> Write it into the inbox directory
170: * -> picked up by FileComponent's consumer side -> Create a InOnly message -> route to ReceiverComponent
171: *
172: */
173: public void testInOnOut() throws Exception {
174: testInOnOut("<hello>world</hello>", false);
175: }
176: }
|