1 17 package org.apache.servicemix.tck; 18 19 import org.apache.servicemix.client.ServiceMixClient; 20 import org.apache.servicemix.jbi.container.SpringJBIContainer; 21 import org.apache.servicemix.jbi.jaxp.StringSource; 22 import org.apache.servicemix.jbi.resolver.ServiceNameEndpointResolver; 23 import org.w3c.dom.Node ; 24 import org.xml.sax.SAXException ; 25 26 import javax.jbi.JBIException; 27 import javax.jbi.messaging.Fault; 28 import javax.jbi.messaging.InOnly; 29 import javax.jbi.messaging.NormalizedMessage; 30 import javax.xml.namespace.QName ; 31 import javax.xml.parsers.ParserConfigurationException ; 32 import javax.xml.transform.Source ; 33 import javax.xml.transform.TransformerException ; 34 35 import java.io.IOException ; 36 import java.util.Date ; 37 38 41 public abstract class TestSupport extends SpringTestSupport { 42 protected ServiceMixClient client; 43 protected Receiver receiver; 44 45 protected void setUp() throws Exception { 46 super.setUp(); 47 client = (ServiceMixClient) getBean("client"); 48 SpringJBIContainer jbi = (SpringJBIContainer) getBean("jbi"); 49 receiver = (Receiver) jbi.getBean("receiver"); 50 } 51 52 58 protected void assertSendAndReceiveMessages(QName service) throws Exception { 59 sendMessages(service, messageCount); 60 assertMessagesReceived(messageCount); 61 } 62 63 protected void sendMessages(QName service, int messageCount) throws Exception { 64 sendMessages(service, messageCount, true, null); 65 } 66 67 protected void sendMessages(QName service, int messageCount, String message) throws Exception { 68 sendMessages(service, messageCount, true, message); 69 } 70 71 77 protected void sendMessages(QName service, int messageCount, boolean sync, String msg) throws Exception { 78 for (int i = 1; i <= messageCount; i++) { 79 InOnly exchange = client.createInOnlyExchange(); 80 81 NormalizedMessage message = exchange.getInMessage(); 82 message.setProperty("name", "James"); 83 message.setProperty("id", new Integer (i)); 84 message.setProperty("idText", "" + i); 85 if (msg != null && msg.length() > 0) { 86 message.setContent(new StringSource(msg)); 87 } else { 88 message.setContent(new StringSource(createMessageXmlText(i))); 89 } 90 91 exchange.setService(service); 92 if (sync) { 93 client.sendSync(exchange); 94 } else { 95 client.send(exchange); 96 } 97 98 Exception error = exchange.getError(); 100 if (error != null) { 101 throw error; 102 } 103 104 Fault fault = exchange.getFault(); 105 assertEquals("Should have no fault!", null, fault); 106 } 107 } 108 109 protected String createMessageXmlText(int index) { 110 return "<sample id='" + index + "' sent='" + new Date () + "'>hello world!</sample>"; 111 } 112 113 protected void assertMessagesReceived() throws Exception { 114 assertMessagesReceived(messageCount); 115 } 116 117 protected void assertMessagesReceived(int messageCount) throws Exception { 118 assertNotNull("receiver not found in JBI container", receiver); 119 120 MessageList messageList = receiver.getMessageList(); 121 assertMessagesReceived(messageList, messageCount); 122 } 123 124 protected MessageList assertMessagesReceived(String receiverName, int messageCount) throws Exception { 125 Receiver receiver = (Receiver) getBean(receiverName); 126 assertNotNull("receiver: " + receiverName + " not found in JBI container", receiver); 127 128 MessageList messageList = receiver.getMessageList(); 129 assertMessagesReceived(messageList, messageCount); 130 return messageList; 131 } 132 133 141 protected Object requestServiceWithFileRequest(QName serviceName, String fileOnClassPath) throws Exception { 142 Source content = getSourceFromClassPath(fileOnClassPath); 143 ServiceNameEndpointResolver resolver = new ServiceNameEndpointResolver(serviceName); 144 Object answer = client.request(resolver, null, null, content); 145 if (answer instanceof Source) { 146 answer = transformer.toDOMNode((Source) answer); 147 } 148 return answer; 149 } 150 151 159 protected void sendServiceWithFileRequest(QName serviceName, String fileOnClassPath) throws Exception { 160 Source content = getSourceFromClassPath(fileOnClassPath); 161 ServiceNameEndpointResolver resolver = new ServiceNameEndpointResolver(serviceName); 162 client.send(resolver, null, null, content); 163 } 164 165 protected void assertMessageHeader(MessageList messageList, int index, String propertyName, Object expectedValue) { 166 NormalizedMessage message = (NormalizedMessage) messageList.getMessages().get(index); 167 assertNotNull("Message: " + index + " is null!", message); 168 169 Object value = message.getProperty(propertyName); 170 assertEquals("property: " + propertyName, expectedValue, value); 171 } 172 173 protected void assertMessageBody(MessageList messageList, int index, String expectedXml) throws TransformerException { 174 NormalizedMessage message = (NormalizedMessage) messageList.getMessages().get(index); 175 assertNotNull("Message: " + index + " is null!", message); 176 177 Source content = message.getContent(); 178 assertNotNull("Message content: " + index + " is null!", content); 179 String value = transformer.toString(content); 180 181 assertEquals("message XML for: " + index, expectedXml, value); 182 } 183 184 protected void assertMessageXPath(MessageList messageList, int index, String xpath, String expectedValue) throws TransformerException , ParserConfigurationException , IOException , SAXException { 185 NormalizedMessage message = (NormalizedMessage) messageList.getMessages().get(index); 186 assertNotNull("Message: " + index + " is null!", message); 187 188 Source content = message.getContent(); 189 assertNotNull("Message content: " + index + " is null!", content); 190 Node node = transformer.toDOMNode(content); 191 192 String value = textValueOfXPath(node, xpath); 193 String xmlText = transformer.toString(node); 194 195 if (log.isTraceEnabled()) { 196 log.trace("Message: " + index + " received XML: " + xmlText); 197 } 198 199 assertEquals("message XML: " + index + " for xpath: " + xpath + " body was: " + xmlText, expectedValue, value); 200 } 201 } 202 | Popular Tags |