/**
*
* Copyright 2005 LogicBlaze, Inc. http://www.logicblaze.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
**/
package org.servicemix;
import java.io.IOException;
import java.util.Date;
import javax.jbi.JBIException;
import javax.jbi.messaging.Fault;
import javax.jbi.messaging.InOnly;
import javax.jbi.messaging.NormalizedMessage;
import javax.xml.namespace.QName;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import org.servicemix.client.ServiceMixClient;
import org.servicemix.examples.MessageList;
import org.servicemix.examples.Receiver;
import org.servicemix.jbi.container.SpringJBIContainer;
import org.servicemix.jbi.jaxp.StringSource;
import org.servicemix.jbi.resolver.ServiceNameEndpointResolver;
import org.w3c.dom.Node;
import org.xml.sax.SAXException;
/**
* @version $Revision: 600 $
*/
public abstract class TestSupport extends SpringTestSupport {
protected ServiceMixClient client;
protected Receiver receiver;
protected void setUp() throws Exception {
super.setUp();
client = (ServiceMixClient) getBean("client");
// TODO
//receiver = (Receiver) getBean("receiver");
SpringJBIContainer jbi = (SpringJBIContainer) getBean("jbi");
receiver = (Receiver) jbi.getBean("receiver");
}
/**
* Sends messages to the given service and asserts that the receiver gets them all
*
* @param service
* @throws javax.jbi.messaging.MessagingException
*/
protected void assertSendAndReceiveMessages(QName service) throws Exception {
sendMessages(service, messageCount);
assertMessagesReceived(messageCount);
}
/**
* Sends the given number of messages to the given service
*
* @param service
* @throws javax.jbi.messaging.MessagingException
*/
protected void sendMessages(QName service, int messageCount) throws Exception {
for (int i = 1; i <= messageCount; i++) {
InOnly exchange = client.createInOnlyExchange();
NormalizedMessage message = exchange.getInMessage();
message.setProperty("name", "James");
message.setProperty("id", new Integer(i));
message.setProperty("idText", "" + i);
message.setContent(new StringSource(createMessageXmlText(i)));
exchange.setService(service);
client.sendSync(exchange);
// lets assert that we have no failure
Exception error = exchange.getError();
if (error != null) {
throw error;
}
Fault fault = exchange.getFault();
assertEquals("Should have no fault!", null, fault);
}
}
protected String createMessageXmlText(int index) {
return "<sample id='" + index + "' sent='" + new Date() + "'>hello world!</sample>";
}
protected void assertMessagesReceived() throws Exception {
assertMessagesReceived(messageCount);
}
protected void assertMessagesReceived(int messageCount) throws Exception {
assertNotNull("receiver not found in JBI container", receiver);
MessageList messageList = receiver.getMessageList();
assertMessagesReceived(messageList, messageCount);
}
protected MessageList assertMessagesReceived(String receiverName, int messageCount) throws Exception {
Receiver receiver = (Receiver) getBean(receiverName);
assertNotNull("receiver: " + receiverName + " not found in JBI container", receiver);
MessageList messageList = receiver.getMessageList();
assertMessagesReceived(messageList, messageCount);
return messageList;
}
/**
* Performs a request using the given file from the classpath as the request body and return the answer
*
* @param serviceName
* @param fileOnClassPath
* @return
* @throws JBIException
* @throws SAXException
* @throws IOException
* @throws ParserConfigurationException
* @throws TransformerException
*/
protected Object requestServiceWithFileRequest(QName serviceName, String fileOnClassPath) throws JBIException, TransformerException, ParserConfigurationException, IOException, SAXException {
Source content = getSourceFromClassPath(fileOnClassPath);
ServiceNameEndpointResolver resolver = new ServiceNameEndpointResolver(serviceName);
Object answer = client.request(resolver, null, null, content);
if (answer instanceof Source) {
answer = transformer.toDOMNode((Source) answer);
}
return answer;
}
protected void assertMessageHeader(MessageList messageList, int index, String propertyName, Object expectedValue) {
NormalizedMessage message = (NormalizedMessage) messageList.getMessages().get(index);
assertNotNull("Message: " + index + " is null!", message);
Object value = message.getProperty(propertyName);
assertEquals("property: " + propertyName, expectedValue, value);
}
protected void assertMessageBody(MessageList messageList, int index, String expectedXml) throws TransformerException {
NormalizedMessage message = (NormalizedMessage) messageList.getMessages().get(index);
assertNotNull("Message: " + index + " is null!", message);
Source content = message.getContent();
assertNotNull("Message content: " + index + " is null!", content);
String value = transformer.toString(content);
assertEquals("message XML for: " + index, expectedXml, value);
}
protected void assertMessageXPath(MessageList messageList, int index, String xpath, String expectedValue) throws TransformerException, ParserConfigurationException, IOException, SAXException {
NormalizedMessage message = (NormalizedMessage) messageList.getMessages().get(index);
assertNotNull("Message: " + index + " is null!", message);
Source content = message.getContent();
assertNotNull("Message content: " + index + " is null!", content);
Node node = transformer.toDOMNode(content);
String value = textValueOfXPath(node, xpath);
String xmlText = transformer.toString(node);
if (log.isTraceEnabled()) {
log.trace("Message: " + index + " received XML: " + xmlText);
}
assertEquals("message XML: " + index + " for xpath: " + xpath + " body was: " + xmlText, expectedValue, value);
}
}