1 17 package org.apache.servicemix.client; 18 19 import java.io.StringReader ; 20 import java.util.Arrays ; 21 22 import javax.jbi.messaging.InOnly; 23 import javax.jbi.messaging.InOut; 24 import javax.jbi.messaging.NormalizedMessage; 25 import javax.xml.transform.stream.StreamSource ; 26 27 import junit.framework.TestCase; 28 29 import org.apache.commons.logging.Log; 30 import org.apache.commons.logging.LogFactory; 31 import org.apache.servicemix.jbi.container.SpringJBIContainer; 32 import org.apache.servicemix.tck.MessageList; 33 import org.apache.servicemix.tck.Receiver; 34 import org.apache.xbean.spring.context.ClassPathXmlApplicationContext; 35 import org.springframework.context.support.AbstractXmlApplicationContext; 36 37 40 public class ClientDestinationTest extends TestCase { 41 private static final transient Log log = LogFactory.getLog(ClientDestinationTest.class); 42 43 protected AbstractXmlApplicationContext context; 44 protected ServiceMixClient client; 45 protected MessageList messageList = new MessageList(); 46 47 public void testInOnlyMessage() throws Exception { 48 Destination destination = client.createDestination("service:http://servicemix.org/cheese/receiver"); 50 Message message = destination.createInOnlyMessage(); 51 message.setProperty("name", "James"); 52 message.setBody("<hello>world</hello>"); 53 54 client.send(message); 55 messageList.assertMessagesReceived(1); 57 } 58 59 public void testInOnlyExchange() throws Exception { 60 Destination destination = client.createDestination("service:http://servicemix.org/cheese/receiver"); 62 InOnly exchange = destination.createInOnlyExchange(); 63 64 NormalizedMessage message = exchange.getInMessage(); 65 message.setProperty("name", "James"); 66 message.setContent(new StreamSource (new StringReader ("<hello>world</hello>"))); 67 68 client.send(exchange); 69 71 messageList.assertMessagesReceived(1); 72 } 73 74 public void testInOutExchange() throws Exception { 75 Destination destination = client.createDestination("service:http://servicemix.org/cheese/myService"); 77 InOut exchange = destination.createInOutExchange(); 78 79 NormalizedMessage request = exchange.getInMessage(); 80 request.setProperty("name", "James"); 81 request.setContent(new StreamSource (new StringReader ("<hello>world</hello>"))); 82 83 client.sendSync(exchange); 84 85 NormalizedMessage response = exchange.getOutMessage(); 86 88 assertNotNull("Should have returned a non-null response!", response); 89 90 log.info("Received result: " + response); 91 } 92 93 94 public void testInOnlyMessageUsingPOJOWithXStreamMarshaling() throws Exception { 95 TestBean bean = new TestBean(); 96 bean.setName("James"); 97 bean.setLength(12); 98 bean.getAddresses().addAll(Arrays.asList(new String [] { "London", "LA" })); 99 100 Destination destination = client.createDestination("service:http://servicemix.org/cheese/receiver"); 101 Message message = destination.createInOnlyMessage(); 102 message.setProperty("name", "James"); 103 message.setBody(bean); 104 105 client.send(message); 106 107 messageList.assertMessagesReceived(1); 108 } 109 110 protected void setUp() throws Exception { 111 context = createBeanFactory(); 112 client = (ServiceMixClient) getBean("client"); 113 messageList = createMessageList(); 114 } 115 116 121 122 protected MessageList createMessageList() throws Exception { 123 SpringJBIContainer jbi = (SpringJBIContainer) getBean("jbi"); 124 Receiver receiver = (Receiver) jbi.getBean("receiver"); 125 assertNotNull("receiver not found in JBI container", receiver); 126 return receiver.getMessageList(); 127 } 128 129 protected void tearDown() throws Exception { 130 super.tearDown(); 131 132 if (context != null) { 133 context.close(); 134 } 135 } 136 137 protected Object getBean(String name) { 138 Object answer = context.getBean(name); 139 assertNotNull("Could not find object in Spring for key: " + name, answer); 140 return answer; 141 } 142 143 protected AbstractXmlApplicationContext createBeanFactory() { 144 return new ClassPathXmlApplicationContext("org/apache/servicemix/client/example.xml"); 145 } 146 } 147 | Popular Tags |