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)
062: throws Exception {
063: sendMessages(service, messageCount);
064: assertMessagesReceived(messageCount);
065: }
066:
067: protected void sendMessages(QName service, int messageCount)
068: throws Exception {
069: sendMessages(service, messageCount, true, null);
070: }
071:
072: protected void sendMessages(QName service, int messageCount,
073: String message) throws Exception {
074: sendMessages(service, messageCount, true, message);
075: }
076:
077: /**
078: * Sends the given number of messages to the given service
079: *
080: * @param service
081: * @throws javax.jbi.messaging.MessagingException
082: */
083: protected void sendMessages(QName service, int messageCount,
084: boolean sync, String msg) throws Exception {
085: for (int i = 1; i <= messageCount; i++) {
086: InOnly exchange = client.createInOnlyExchange();
087:
088: NormalizedMessage message = exchange.getInMessage();
089: message.setProperty("name", "James");
090: message.setProperty("id", new Integer(i));
091: message.setProperty("idText", "" + i);
092: if (msg != null && msg.length() > 0) {
093: message.setContent(new StringSource(msg));
094: } else {
095: message.setContent(new StringSource(
096: createMessageXmlText(i)));
097: }
098:
099: exchange.setService(service);
100: if (sync) {
101: client.sendSync(exchange);
102: } else {
103: client.send(exchange);
104: }
105:
106: // lets assert that we have no failure
107: Exception error = exchange.getError();
108: if (error != null) {
109: throw error;
110: }
111:
112: Fault fault = exchange.getFault();
113: assertEquals("Should have no fault!", null, fault);
114: }
115: }
116:
117: protected String createMessageXmlText(int index) {
118: return "<sample id='" + index + "' sent='" + new Date()
119: + "'>hello world!</sample>";
120: }
121:
122: protected void assertMessagesReceived() throws Exception {
123: assertMessagesReceived(messageCount);
124: }
125:
126: protected void assertMessagesReceived(int messageCount)
127: throws Exception {
128: assertNotNull("receiver not found in JBI container", receiver);
129:
130: MessageList messageList = receiver.getMessageList();
131: assertMessagesReceived(messageList, messageCount);
132: }
133:
134: protected MessageList assertMessagesReceived(String receiverName,
135: int messageCount) throws Exception {
136: Receiver rcv = (Receiver) getBean(receiverName);
137: assertNotNull("receiver: " + receiverName
138: + " not found in JBI container", rcv);
139:
140: MessageList messageList = rcv.getMessageList();
141: assertMessagesReceived(messageList, messageCount);
142: return messageList;
143: }
144:
145: /**
146: * Performs a request using the given file from the classpath as the request
147: * body and return the answer
148: *
149: * @param serviceName
150: * @param fileOnClassPath
151: * @return
152: * @throws JBIException
153: */
154: protected Object requestServiceWithFileRequest(QName serviceName,
155: String fileOnClassPath) throws Exception {
156: Source content = getSourceFromClassPath(fileOnClassPath);
157: ServiceNameEndpointResolver resolver = new ServiceNameEndpointResolver(
158: serviceName);
159: Object answer = client.request(resolver, null, null, content);
160: if (answer instanceof Source) {
161: answer = transformer.toDOMNode((Source) answer);
162: }
163: return answer;
164: }
165:
166: /**
167: * Performs a request using the given file from the classpath as the request
168: * body and return the answer
169: *
170: * @param serviceName
171: * @param fileOnClassPath
172: * @return
173: * @throws JBIException
174: */
175: protected void sendServiceWithFileRequest(QName serviceName,
176: String fileOnClassPath) throws Exception {
177: Source content = getSourceFromClassPath(fileOnClassPath);
178: ServiceNameEndpointResolver resolver = new ServiceNameEndpointResolver(
179: serviceName);
180: client.send(resolver, null, null, content);
181: }
182:
183: protected void assertMessageHeader(MessageList messageList,
184: int index, String propertyName, Object expectedValue) {
185: NormalizedMessage message = (NormalizedMessage) messageList
186: .getMessages().get(index);
187: assertNotNull("Message: " + index + " is null!", message);
188:
189: Object value = message.getProperty(propertyName);
190: assertEquals("property: " + propertyName, expectedValue, value);
191: }
192:
193: protected void assertMessageBody(MessageList messageList,
194: int index, String expectedXml) throws TransformerException {
195: NormalizedMessage message = (NormalizedMessage) messageList
196: .getMessages().get(index);
197: assertNotNull("Message: " + index + " is null!", message);
198:
199: Source content = message.getContent();
200: assertNotNull("Message content: " + index + " is null!",
201: content);
202: String value = transformer.toString(content);
203:
204: assertEquals("message XML for: " + index, expectedXml, value);
205: }
206:
207: protected void assertMessageXPath(MessageList messageList,
208: int index, String xpath, String expectedValue)
209: throws TransformerException, ParserConfigurationException,
210: IOException, SAXException {
211: NormalizedMessage message = (NormalizedMessage) messageList
212: .getMessages().get(index);
213: assertNotNull("Message: " + index + " is null!", message);
214:
215: Source content = message.getContent();
216: assertNotNull("Message content: " + index + " is null!",
217: content);
218: Node node = transformer.toDOMNode(content);
219:
220: String value = textValueOfXPath(node, xpath);
221: String xmlText = transformer.toString(node);
222:
223: if (log.isTraceEnabled()) {
224: log
225: .trace("Message: " + index + " received XML: "
226: + xmlText);
227: }
228:
229: assertEquals("message XML: " + index + " for xpath: " + xpath
230: + " body was: " + xmlText, expectedValue, value);
231: }
232: }
|