/**
*
* 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.jbi.container.SpringJBIContainer;
import org.servicemix.jbi.jaxp.StringSource;
import org.servicemix.jbi.resolver.ServiceNameEndpointResolver;
import org.xml.sax.SAXException;
/**
* @version $Revision$
*/
public abstract class TestSupport extends SpringTestSupport {
protected ServiceMixClient client;
protected void setUp() throws Exception {
super.setUp();
client = (ServiceMixClient) getBean("client");
// TODO
//receiver = (Receiver) getBean("receiver");
SpringJBIContainer jbi = (SpringJBIContainer) getBean("jbi");
}
/**
* 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);
client.done(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>";
}
/**
* 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;
}
}