1 17 package org.apache.servicemix.jms; 18 19 import java.io.File ; 20 import java.net.URI ; 21 import java.net.URL ; 22 23 import javax.jbi.messaging.InOnly; 24 import javax.jbi.messaging.InOut; 25 import javax.jbi.messaging.NormalizedMessage; 26 import javax.naming.Context ; 27 import javax.naming.InitialContext ; 28 import javax.xml.namespace.QName ; 29 import javax.xml.transform.Source ; 30 31 import junit.framework.TestCase; 32 33 import org.apache.activemq.ActiveMQConnectionFactory; 34 import org.apache.activemq.broker.BrokerService; 35 import org.apache.activemq.jndi.ActiveMQInitialContextFactory; 36 import org.apache.activemq.pool.PooledConnectionFactory; 37 import org.apache.activemq.xbean.BrokerFactoryBean; 38 import org.apache.servicemix.client.DefaultServiceMixClient; 39 import org.apache.servicemix.components.jms.JmsReceiverComponent; 40 import org.apache.servicemix.components.jms.JmsServiceComponent; 41 import org.apache.servicemix.components.util.EchoComponent; 42 import org.apache.servicemix.jbi.container.ActivationSpec; 43 import org.apache.servicemix.jbi.container.JBIContainer; 44 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 45 import org.apache.servicemix.jbi.jaxp.StringSource; 46 import org.apache.servicemix.tck.Receiver; 47 import org.apache.servicemix.tck.ReceiverComponent; 48 import org.springframework.core.io.ClassPathResource; 49 import org.springframework.jms.core.JmsTemplate; 50 51 public class JMSComponentTest extends TestCase { 52 53 protected JBIContainer container; 54 protected BrokerService broker; 55 protected ActiveMQConnectionFactory connectionFactory; 56 57 protected void setUp() throws Exception { 58 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, ActiveMQInitialContextFactory.class.getName()); 59 System.setProperty(Context.PROVIDER_URL, "tcp://localhost:61216"); 60 61 62 BrokerFactoryBean bfb = new BrokerFactoryBean(new ClassPathResource("org/apache/servicemix/jms/activemq.xml")); 63 bfb.afterPropertiesSet(); 64 broker = bfb.getBroker(); 65 broker.start(); 66 67 container = new JBIContainer(); 68 container.setUseMBeanServer(true); 69 container.setCreateMBeanServer(true); 70 container.setMonitorInstallationDirectory(false); 71 container.setNamingContext(new InitialContext ()); 72 container.init(); 73 74 connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61216"); 75 } 76 77 protected void tearDown() throws Exception { 78 if (container != null) { 79 container.shutDown(); 80 } 81 if (broker != null) { 82 broker.stop(); 83 } 84 } 85 86 public void testProviderInOnly() throws Exception { 87 JmsComponent component = new JmsComponent(); 89 container.activateComponent(component, "JMSComponent"); 90 91 JmsReceiverComponent jmsReceiver = new JmsReceiverComponent(); 93 JmsTemplate template = new JmsTemplate(connectionFactory); 94 template.setDefaultDestinationName("queue/A"); 95 jmsReceiver.setTemplate(template); 96 jmsReceiver.afterPropertiesSet(); 97 ActivationSpec asJmsReceiver = new ActivationSpec("jmsReceiver", jmsReceiver); 98 asJmsReceiver.setDestinationService(new QName ("test", "receiver")); 99 container.activateComponent(asJmsReceiver); 100 101 Receiver receiver = new ReceiverComponent(); 103 ActivationSpec asReceiver = new ActivationSpec("receiver", receiver); 104 asReceiver.setService(new QName ("test", "receiver")); 105 container.activateComponent(asReceiver); 106 107 container.start(); 109 110 URL url = getClass().getClassLoader().getResource("provider/jms.wsdl"); 112 File path = new File (new URI (url.toString())); 113 path = path.getParentFile(); 114 component.getServiceUnitManager().deploy("provider", path.getAbsolutePath()); 115 component.getServiceUnitManager().start("provider"); 116 117 DefaultServiceMixClient client = new DefaultServiceMixClient(container); 119 InOnly in = client.createInOnlyExchange(); 120 in.setInterfaceName(new QName ("http://jms.servicemix.org/Test", "ProviderInterface")); 121 in.getInMessage().setContent(new StringSource("<hello>world</hello>")); 122 client.send(in); 123 124 receiver.getMessageList().assertMessagesReceived(1); 126 } 127 128 public void testProviderInOut() throws Exception { 129 JmsComponent component = new JmsComponent(); 131 container.activateComponent(component, "JMSComponent"); 132 133 JmsServiceComponent jmsReceiver = new JmsServiceComponent(); 135 JmsTemplate template = new JmsTemplate(new PooledConnectionFactory(connectionFactory)); 136 template.setDefaultDestinationName("queue/A"); 137 jmsReceiver.setTemplate(template); 138 jmsReceiver.afterPropertiesSet(); 139 ActivationSpec asJmsReceiver = new ActivationSpec("jmsReceiver", jmsReceiver); 140 asJmsReceiver.setDestinationService(new QName ("test", "receiver")); 141 container.activateComponent(asJmsReceiver); 142 143 EchoComponent echo = new EchoComponent(); 145 ActivationSpec asEcho = new ActivationSpec("receiver", echo); 146 asEcho.setService(new QName ("test", "receiver")); 147 container.activateComponent(asEcho); 148 149 container.start(); 151 152 URL url = getClass().getClassLoader().getResource("provider/jms.wsdl"); 154 File path = new File (new URI (url.toString())); 155 path = path.getParentFile(); 156 component.getServiceUnitManager().deploy("provider", path.getAbsolutePath()); 157 component.getServiceUnitManager().start("provider"); 158 159 DefaultServiceMixClient client = new DefaultServiceMixClient(container); 161 InOut inout = client.createInOutExchange(); 162 inout.setInterfaceName(new QName ("http://jms.servicemix.org/Test", "ProviderInterface")); 163 inout.getInMessage().setContent(new StringSource("<hello>world</hello>")); 164 boolean result = client.sendSync(inout); 165 assertTrue(result); 166 NormalizedMessage out = inout.getOutMessage(); 167 assertNotNull(out); 168 Source src = out.getContent(); 169 assertNotNull(src); 170 System.err.println(new SourceTransformer().toString(src)); 171 } 172 173 205 206 public void testProviderConsumerInOut() throws Exception { 207 JmsComponent component = new JmsComponent(); 209 container.activateComponent(component, "JMSComponent"); 210 211 EchoComponent echo = new EchoComponent(); 213 ActivationSpec asEcho = new ActivationSpec("receiver", echo); 214 asEcho.setService(new QName ("http://jms.servicemix.org/Test", "Echo")); 215 container.activateComponent(asEcho); 216 217 container.start(); 219 220 { 222 URL url = getClass().getClassLoader().getResource("provider/jms.wsdl"); 223 File path = new File (new URI (url.toString())); 224 path = path.getParentFile(); 225 component.getServiceUnitManager().deploy("provider", path.getAbsolutePath()); 226 component.getServiceUnitManager().start("provider"); 227 } 228 229 { 231 URL url = getClass().getClassLoader().getResource("consumer/jms.wsdl"); 232 File path = new File (new URI (url.toString())); 233 path = path.getParentFile(); 234 component.getServiceUnitManager().deploy("consumer", path.getAbsolutePath()); 235 component.getServiceUnitManager().start("consumer"); 236 } 237 238 DefaultServiceMixClient client = new DefaultServiceMixClient(container); 240 InOut inout = client.createInOutExchange(); 241 inout.setInterfaceName(new QName ("http://jms.servicemix.org/Test", "ProviderInterface")); 242 inout.getInMessage().setContent(new StringSource("<hello>world</hello>")); 243 boolean result = client.sendSync(inout); 244 assertTrue(result); 245 NormalizedMessage out = inout.getOutMessage(); 246 assertNotNull(out); 247 Source src = out.getContent(); 248 assertNotNull(src); 249 System.err.println(new SourceTransformer().toString(src)); 250 251 } 252 253 } 254 | Popular Tags |