001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.servicemix.tck;
018
019 import java.io.IOException;
020 import java.util.Date;
021
022 import javax.jbi.messaging.Fault;
023 import javax.jbi.messaging.InOnly;
024 import javax.jbi.messaging.NormalizedMessage;
025 import javax.xml.namespace.QName;
026 import javax.xml.parsers.ParserConfigurationException;
027 import javax.xml.transform.Source;
028 import javax.xml.transform.TransformerException;
029
030 import org.w3c.dom.Node;
031
032 import org.xml.sax.SAXException;
033
034 import org.apache.servicemix.client.ServiceMixClient;
035 import org.apache.servicemix.jbi.container.SpringJBIContainer;
036 import org.apache.servicemix.jbi.jaxp.StringSource;
037 import org.apache.servicemix.jbi.resolver.ServiceNameEndpointResolver;
038
039 /**
040 * @version $Revision: 564607 $
041 */
042 public abstract class TestSupport extends SpringTestSupport {
043 protected ServiceMixClient client;
044
045 protected Receiver receiver;
046
047 protected void setUp() throws Exception {
048 super.setUp();
049 client = (ServiceMixClient) getBean("client");
050 SpringJBIContainer jbi = (SpringJBIContainer) getBean("jbi");
051 receiver = (Receiver) jbi.getBean("receiver");
052 }
053
054 /**
055 * Sends messages to the given service and asserts that the receiver gets
056 * them all
057 *
058 * @param service
059 * @throws javax.jbi.messaging.MessagingException
060 */
061 protected void assertSendAndReceiveMessages(QName service) throws Exception {
062 sendMessages(service, messageCount);
063 assertMessagesReceived(messageCount);
064 }
065
066 protected void sendMessages(QName service, int messageCount) throws Exception {
067 sendMessages(service, messageCount, true, null);
068 }
069
070 protected void sendMessages(QName service, int messageCount, String message) throws Exception {
071 sendMessages(service, messageCount, true, message);
072 }
073
074 /**
075 * Sends the given number of messages to the given service
076 *
077 * @param service
078 * @throws javax.jbi.messaging.MessagingException
079 */
080 protected void sendMessages(QName service, int messageCount, boolean sync, String msg) throws Exception {
081 for (int i = 1; i <= messageCount; i++) {
082 InOnly exchange = client.createInOnlyExchange();
083
084 NormalizedMessage message = exchange.getInMessage();
085 message.setProperty("name", "James");
086 message.setProperty("id", new Integer(i));
087 message.setProperty("idText", "" + i);
088 if (msg != null && msg.length() > 0) {
089 message.setContent(new StringSource(msg));
090 } else {
091 message.setContent(new StringSource(createMessageXmlText(i)));
092 }
093
094 exchange.setService(service);
095 if (sync) {
096 client.sendSync(exchange);
097 } else {
098 client.send(exchange);
099 }
100
101 // lets assert that we have no failure
102 Exception error = exchange.getError();
103 if (error != null) {
104 throw error;
105 }
106
107 Fault fault = exchange.getFault();
108 assertEquals("Should have no fault!", null, fault);
109 }
110 }
111
112 protected String createMessageXmlText(int index) {
113 return "<sample id='" + index + "' sent='" + new Date() + "'>hello world!</sample>";
114 }
115
116 protected void assertMessagesReceived() throws Exception {
117 assertMessagesReceived(messageCount);
118 }
119
120 protected void assertMessagesReceived(int messageCount) throws Exception {
121 assertNotNull("receiver not found in JBI container", receiver);
122
123 MessageList messageList = receiver.getMessageList();
124 assertMessagesReceived(messageList, messageCount);
125 }
126
127 protected MessageList assertMessagesReceived(String receiverName, int messageCount) throws Exception {
128 Receiver rcv = (Receiver) getBean(receiverName);
129 assertNotNull("receiver: " + receiverName + " not found in JBI container", rcv);
130
131 MessageList messageList = rcv.getMessageList();
132 assertMessagesReceived(messageList, messageCount);
133 return messageList;
134 }
135
136 /**
137 * Performs a request using the given file from the classpath as the request
138 * body and return the answer
139 *
140 * @param serviceName
141 * @param fileOnClassPath
142 * @return
143 * @throws JBIException
144 */
145 protected Object requestServiceWithFileRequest(QName serviceName, String fileOnClassPath) throws Exception {
146 Source content = getSourceFromClassPath(fileOnClassPath);
147 ServiceNameEndpointResolver resolver = new ServiceNameEndpointResolver(serviceName);
148 Object answer = client.request(resolver, null, null, content);
149 if (answer instanceof Source) {
150 answer = transformer.toDOMNode((Source) answer);
151 }
152 return answer;
153 }
154
155 /**
156 * Performs a request using the given file from the classpath as the request
157 * body and return the answer
158 *
159 * @param serviceName
160 * @param fileOnClassPath
161 * @return
162 * @throws JBIException
163 */
164 protected void sendServiceWithFileRequest(QName serviceName, String fileOnClassPath) throws Exception {
165 Source content = getSourceFromClassPath(fileOnClassPath);
166 ServiceNameEndpointResolver resolver = new ServiceNameEndpointResolver(serviceName);
167 client.send(resolver, null, null, content);
168 }
169
170 protected void assertMessageHeader(MessageList messageList, int index, String propertyName, Object expectedValue) {
171 NormalizedMessage message = (NormalizedMessage) messageList.getMessages().get(index);
172 assertNotNull("Message: " + index + " is null!", message);
173
174 Object value = message.getProperty(propertyName);
175 assertEquals("property: " + propertyName, expectedValue, value);
176 }
177
178 protected void assertMessageBody(MessageList messageList, int index, String expectedXml) throws TransformerException {
179 NormalizedMessage message = (NormalizedMessage) messageList.getMessages().get(index);
180 assertNotNull("Message: " + index + " is null!", message);
181
182 Source content = message.getContent();
183 assertNotNull("Message content: " + index + " is null!", content);
184 String value = transformer.toString(content);
185
186 assertEquals("message XML for: " + index, expectedXml, value);
187 }
188
189 protected void assertMessageXPath(MessageList messageList, int index, String xpath, String expectedValue) throws TransformerException,
190 ParserConfigurationException, IOException, SAXException {
191 NormalizedMessage message = (NormalizedMessage) messageList.getMessages().get(index);
192 assertNotNull("Message: " + index + " is null!", message);
193
194 Source content = message.getContent();
195 assertNotNull("Message content: " + index + " is null!", content);
196 Node node = transformer.toDOMNode(content);
197
198 String value = textValueOfXPath(node, xpath);
199 String xmlText = transformer.toString(node);
200
201 if (log.isTraceEnabled()) {
202 log.trace("Message: " + index + " received XML: " + xmlText);
203 }
204
205 assertEquals("message XML: " + index + " for xpath: " + xpath + " body was: " + xmlText, expectedValue, value);
206 }
207 }