1 17 package org.apache.servicemix.components.groovy; 18 19 import java.io.StringReader ; 20 import java.util.Arrays ; 21 import java.util.HashMap ; 22 import java.util.Map ; 23 24 import javax.jbi.JBIException; 25 import javax.jbi.messaging.InOnly; 26 import javax.jbi.messaging.InOut; 27 import javax.jbi.messaging.NormalizedMessage; 28 import javax.xml.namespace.QName ; 29 import javax.xml.transform.stream.StreamSource ; 30 31 import junit.framework.TestCase; 32 33 import org.apache.commons.logging.Log; 34 import org.apache.commons.logging.LogFactory; 35 import org.apache.servicemix.client.ServiceMixClient; 36 import org.apache.servicemix.jbi.container.SpringJBIContainer; 37 import org.apache.servicemix.jbi.jaxp.SourceTransformer; 38 import org.apache.servicemix.jbi.resolver.EndpointResolver; 39 import org.apache.servicemix.tck.Receiver; 40 import org.springframework.context.support.AbstractXmlApplicationContext; 41 import org.apache.xbean.spring.context.ClassPathXmlApplicationContext; 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 System.out.println("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 System.out.println("Received result: " + outMessage.getContent()); 167 System.out.println("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 System.out.println("Received result: " + response); 180 } 181 182 protected void setUp() throws Exception { 183 context = createBeanFactory(); 184 186 client = (ServiceMixClient) getBean("client"); 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 void tearDown() throws Exception { 197 super.tearDown(); 198 199 if (context != null) { 200 context.close(); 201 } 202 } 203 204 protected Object getBean(String name) { 205 Object answer = context.getBean(name); 206 assertNotNull("Could not find object in Spring for key: " + name, answer); 207 return answer; 208 } 209 210 protected AbstractXmlApplicationContext createBeanFactory() { 211 return new ClassPathXmlApplicationContext("org/apache/servicemix/components/groovy/example.xml"); 212 213 } 214 } 215 | Popular Tags |