1 17 package org.apache.servicemix.client; 18 19 import org.apache.commons.logging.Log; 20 import org.apache.commons.logging.LogFactory; 21 import org.apache.servicemix.client.ServiceMixClient; 22 import org.apache.servicemix.jbi.container.SpringJBIContainer; 23 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 24 import org.apache.servicemix.jbi.resolver.EndpointResolver; 25 import org.apache.servicemix.tck.Receiver; 26 import org.springframework.context.support.AbstractXmlApplicationContext; 27 import org.apache.xbean.spring.context.ClassPathXmlApplicationContext; 28 29 import javax.jbi.JBIException; 30 import javax.jbi.messaging.InOnly; 31 import javax.jbi.messaging.InOut; 32 import javax.jbi.messaging.NormalizedMessage; 33 import javax.xml.namespace.QName ; 34 import javax.xml.transform.stream.StreamSource ; 35 36 import java.io.StringReader ; 37 import java.util.Arrays ; 38 import java.util.HashMap ; 39 import java.util.Map ; 40 41 import junit.framework.TestCase; 42 43 46 public class ServiceMixClientTest extends TestCase { 47 private static final transient Log log = LogFactory.getLog(ServiceMixClientTest.class); 48 49 protected AbstractXmlApplicationContext context; 50 protected ServiceMixClient client; 51 protected Receiver receiver; 52 53 protected SourceTransformer transformer = new SourceTransformer(); 54 55 56 public void testSendUsingJbiAPIs() throws Exception { 59 60 InOnly exchange = client.createInOnlyExchange(); 61 62 NormalizedMessage message = exchange.getInMessage(); 63 message.setProperty("name", "James"); 64 message.setContent(new StreamSource (new StringReader ("<hello>world</hello>"))); 65 66 QName service = new QName ("http://servicemix.org/cheese", "receiver"); 67 exchange.setService(service); 68 client.send(exchange); 69 70 receiver.getMessageList().assertMessagesReceived(1); 71 } 72 73 public void testSendUsingMapAndPOJOsByServiceName() throws Exception { 74 75 Map properties = new HashMap (); 76 properties.put("name", "James"); 77 78 QName service = new QName ("http://servicemix.org/cheese", "receiver"); 79 EndpointResolver resolver = client.createResolverForService(service); 80 client.send(resolver, null, properties, "<hello>world</hello>"); 81 82 receiver.getMessageList().assertMessagesReceived(1); 83 } 84 85 public void testSendUsingMapAndPOJOsUsingContainerRouting() throws Exception { 86 87 ServiceMixClient clientNoRouting = (ServiceMixClient) context.getBean("clientWithRouting"); 88 89 Map properties = new HashMap (); 90 properties.put("name", "James"); 91 92 clientNoRouting.send(null, null, properties, "<hello>world</hello>"); 93 94 receiver.getMessageList().assertMessagesReceived(1); 95 } 96 97 public void testSendUsingMapAndPOJOsUsingContainerRoutingWithNoConfiguration() throws Exception { 98 99 100 try { 101 Map properties = new HashMap (); 102 properties.put("name", "James"); 103 104 client.send(null, null, properties, "<hello>world</hello>"); 105 fail("Should have thrown an exception as we have not wired in any container routing information to this client"); 106 } 107 catch (JBIException e) { 108 log.info("Caught expected exception as we have specified no endpoint resolver: " + e); 109 assertNotNull(e); 110 } 111 } 112 113 114 public void testRequestUsingJbiAPIsByServiceName() throws Exception { 117 QName service = new QName ("http://servicemix.org/cheese", "myService"); 118 assertRequestUsingJBIAPIs(service); 119 } 120 121 public void testRequestUsingMapAndPOJOsByServiceName() throws Exception { 122 QName service = new QName ("http://servicemix.org/cheese", "myService"); 123 assertRequestUsingMapAndPOJOByServiceName(service); 124 } 125 126 public void testRequestUsingPOJOWithXStreamMarshaling() throws Exception { 127 QName service = new QName ("http://servicemix.org/cheese", "myService"); 128 129 ServiceMixClient client = (ServiceMixClient) context.getBean("clientWithXStream"); 130 131 Map properties = new HashMap (); 132 properties.put("name", "James"); 133 134 EndpointResolver resolver = client.createResolverForService(service); 135 TestBean bean = new TestBean(); 136 bean.setName("James"); 137 bean.setLength(12); 138 bean.getAddresses().addAll(Arrays.asList(new String [] {"London", "LA"})); 139 140 Object response = client.request(resolver, null, properties, bean); 141 142 assertNotNull("Should have returned a non-null response!", response); 143 144 log.info("Received result: " + response); 145 } 146 147 148 149 protected void assertRequestUsingJBIAPIs(QName service) throws Exception { 152 InOut exchange = client.createInOutExchange(); 153 154 NormalizedMessage inMessage = exchange.getInMessage(); 155 inMessage.setProperty("name", "James"); 156 inMessage.setContent(new StreamSource (new StringReader ("<hello>world</hello>"))); 157 158 exchange.setService(service); 159 boolean answer = client.sendSync(exchange); 160 assertTrue("Should have successed", answer); 161 162 NormalizedMessage outMessage = exchange.getOutMessage(); 163 assertNotNull("outMessage is null!", outMessage); 164 165 assertEquals("foo header", "hello", outMessage.getProperty("foo")); 166 log.info("Received result: " + outMessage.getContent()); 167 log.info("XML is: " + transformer.toString(outMessage.getContent())); 168 } 169 170 protected void assertRequestUsingMapAndPOJOByServiceName(QName service) throws Exception { 171 Map properties = new HashMap (); 172 properties.put("name", "James"); 173 174 EndpointResolver resolver = client.createResolverForService(service); 175 Object response = client.request(resolver, null, properties, "<hello>world</hello>"); 176 177 assertNotNull("Should have returned a non-null response!", response); 178 179 log.info("Received result: " + response); 180 } 181 182 protected void setUp() throws Exception { 183 context = createBeanFactory(); 184 186 client = getClient(); 187 188 191 SpringJBIContainer jbi = (SpringJBIContainer) getBean("jbi"); 192 receiver = (Receiver) jbi.getBean("receiver"); 193 assertNotNull("receiver not found in JBI container", receiver); 194 } 195 196 protected ServiceMixClient getClient() throws Exception { 197 return (ServiceMixClient) getBean("client"); 198 } 199 200 protected void tearDown() throws Exception { 201 super.tearDown(); 202 203 if (context != null) { 204 context.close(); 205 } 206 } 207 208 protected Object getBean(String name) { 209 Object answer = context.getBean(name); 210 assertNotNull("Could not find object in Spring for key: " + name, answer); 211 return answer; 212 } 213 214 protected AbstractXmlApplicationContext createBeanFactory() { 215 return new ClassPathXmlApplicationContext("org/apache/servicemix/client/example.xml"); 216 } 217 } 218 | Popular Tags |