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.ExchangeStatus; 24 import javax.jbi.messaging.InOut; 25 import javax.jbi.servicedesc.ServiceEndpoint; 26 import javax.naming.Context ; 27 import javax.wsdl.Binding; 28 import javax.wsdl.Definition; 29 import javax.wsdl.Port; 30 import javax.wsdl.PortType; 31 import javax.wsdl.Service; 32 import javax.wsdl.factory.WSDLFactory; 33 import javax.xml.namespace.QName ; 34 35 import junit.framework.TestCase; 36 37 import org.apache.activemq.ActiveMQConnectionFactory; 38 import org.apache.activemq.broker.BrokerService; 39 import org.apache.activemq.jndi.ActiveMQInitialContextFactory; 40 import org.apache.activemq.xbean.BrokerFactoryBean; 41 import org.apache.commons.logging.Log; 42 import org.apache.commons.logging.LogFactory; 43 import org.apache.servicemix.client.DefaultServiceMixClient; 44 import org.apache.servicemix.components.util.EchoComponent; 45 import org.apache.servicemix.jbi.container.ActivationSpec; 46 import org.apache.servicemix.jbi.container.JBIContainer; 47 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 48 import org.apache.servicemix.jbi.jaxp.StringSource; 49 import org.springframework.core.io.ClassPathResource; 50 import org.w3c.dom.Document ; 51 52 public class JmsXBeanDeployerTest extends TestCase { 53 54 private static Log logger = LogFactory.getLog(JmsXBeanDeployerTest.class); 55 56 protected JBIContainer container; 57 protected BrokerService broker; 58 protected ActiveMQConnectionFactory connectionFactory; 59 60 protected void setUp() throws Exception { 61 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, ActiveMQInitialContextFactory.class.getName()); 62 System.setProperty(Context.PROVIDER_URL, "tcp://localhost:61216"); 63 64 BrokerFactoryBean bfb = new BrokerFactoryBean(new ClassPathResource("org/apache/servicemix/jms/activemq.xml")); 65 bfb.afterPropertiesSet(); 66 broker = bfb.getBroker(); 67 broker.start(); 68 69 container = new JBIContainer(); 70 container.setEmbedded(true); 73 container.init(); 74 75 connectionFactory = new ActiveMQConnectionFactory("tcp://localhost:61216"); 76 } 77 78 protected void tearDown() throws Exception { 79 if (container != null) { 80 container.shutDown(); 81 } 82 if (broker != null) { 83 broker.stop(); 84 } 85 } 86 87 public void test() throws Exception { 88 JmsComponent component = new JmsComponent(); 90 container.activateComponent(component, "JMSComponent1"); 91 92 ActivationSpec asEcho = new ActivationSpec("echo", new EchoComponent() { 94 public Document getServiceDescription(ServiceEndpoint endpoint) { 95 try { 96 Definition def = WSDLFactory.newInstance().newDefinition(); 97 PortType type = def.createPortType(); 98 type.setUndefined(false); 99 type.setQName(new QName ("http://test", "MyConsumerInterface")); 100 Binding binding = def.createBinding(); 101 binding.setQName(new QName ("http://test", "MyConsumerBinding")); 102 binding.setUndefined(false); 103 binding.setPortType(type); 104 Service svc = def.createService(); 105 svc.setQName(new QName ("http://test", "MyConsumerService")); 106 Port port = def.createPort(); 107 port.setBinding(binding); 108 port.setName("myConsumer"); 109 svc.addPort(port); 110 def.setTargetNamespace("http://test"); 111 def.addNamespace("tns", "http://test"); 112 def.addPortType(type); 113 def.addBinding(binding); 114 def.addService(svc); 115 Document doc = WSDLFactory.newInstance().newWSDLWriter().getDocument(def); 116 return doc; 117 } catch (Exception e) { 118 throw new RuntimeException (e); 119 } 120 } 121 }); 122 asEcho.setEndpoint("myConsumer"); 123 asEcho.setService(new QName ("http://test", "MyConsumerService")); 124 container.activateComponent(asEcho); 125 126 container.start(); 128 129 URL url = getClass().getClassLoader().getResource("xbean/xbean.xml"); 131 File path = new File (new URI (url.toString())); 132 path = path.getParentFile(); 133 component.getServiceUnitManager().deploy("xbean", path.getAbsolutePath()); 134 component.getServiceUnitManager().start("xbean"); 135 136 assertNotNull(container.getRegistry().getEndpointDescriptor( 138 container.getRegistry().getEndpoint( 139 new QName ("http://test", "MyProviderService"), "myProvider"))); 140 assertNotNull(container.getRegistry().getEndpointDescriptor( 141 container.getRegistry().getExternalEndpointsForService( 142 new QName ("http://test", "MyConsumerService"))[0])); 143 144 DefaultServiceMixClient client = new DefaultServiceMixClient(container); 146 InOut me = client.createInOutExchange(); 147 me.setService(new QName ("http://test", "MyProviderService")); 148 me.getInMessage().setContent(new StringSource("<echo xmlns='http://test'><echoin0>world</echoin0></echo>")); 149 client.sendSync(me); 150 if (me.getStatus() == ExchangeStatus.ERROR) { 151 if (me.getFault() != null) { 152 fail("Received fault: " + new SourceTransformer().toString(me.getFault().getContent())); 153 } else if (me.getError() != null) { 154 throw me.getError(); 155 } else { 156 fail("Received ERROR status"); 157 } 158 } else { 159 logger.info(new SourceTransformer().toString(me.getOutMessage().getContent())); 160 } 161 } 162 163 public void testSoap() throws Exception { 164 JmsComponent component = new JmsComponent(); 166 container.activateComponent(component, "JMSComponent2"); 167 168 ActivationSpec asEcho = new ActivationSpec("echo", new EchoComponent() { 170 public Document getServiceDescription(ServiceEndpoint endpoint) { 171 try { 172 Definition def = WSDLFactory.newInstance().newDefinition(); 173 PortType type = def.createPortType(); 174 type.setUndefined(false); 175 type.setQName(new QName ("http://test", "MyConsumerInterface")); 176 Binding binding = def.createBinding(); 177 binding.setUndefined(false); 178 binding.setPortType(type); 179 Service svc = def.createService(); 180 svc.setQName(new QName ("http://test", "MyConsumerService")); 181 Port port = def.createPort(); 182 port.setBinding(binding); 183 port.setName("myConsumer"); 184 svc.addPort(port); 185 def.setTargetNamespace("http://test"); 186 def.addNamespace("tns", "http://test"); 187 def.addPortType(type); 188 def.addBinding(binding); 189 def.addService(svc); 190 Document doc = WSDLFactory.newInstance().newWSDLWriter().getDocument(def); 191 return doc; 192 } catch (Exception e) { 193 throw new RuntimeException (e); 194 } 195 } 196 }); 197 asEcho.setEndpoint("myConsumer"); 198 asEcho.setService(new QName ("http://test", "MyConsumerService")); 199 container.activateComponent(asEcho); 200 201 container.start(); 203 204 URL url = getClass().getClassLoader().getResource("xbean/xbean.xml"); 206 File path = new File (new URI (url.toString())); 207 path = path.getParentFile(); 208 component.getServiceUnitManager().deploy("xbean", path.getAbsolutePath()); 209 component.getServiceUnitManager().start("xbean"); 210 211 assertNotNull(container.getRegistry().getEndpointDescriptor( 213 container.getRegistry().getExternalEndpointsForService( 214 new QName ("http://test", "MyConsumerService"))[0])); 215 216 DefaultServiceMixClient client = new DefaultServiceMixClient(container); 218 InOut me = client.createInOutExchange(); 219 me.setService(new QName ("http://test", "MyProviderService")); 220 me.getInMessage().setContent(new StringSource("<echo xmlns='http://test'><echoin0>world</echoin0></echo>")); 221 client.sendSync(me); 222 if (me.getStatus() == ExchangeStatus.ERROR) { 223 if (me.getFault() != null) { 224 fail("Received fault: " + new SourceTransformer().toString(me.getFault().getContent())); 225 } else if (me.getError() != null) { 226 throw me.getError(); 227 } else { 228 fail("Received ERROR status"); 229 } 230 } else { 231 logger.info(new SourceTransformer().toString(me.getOutMessage().getContent())); 232 } 233 } 234 235 } 236 | Popular Tags |