KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > servicemix > tck > TestSupport


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

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 JavaDoc;
24 import org.xml.sax.SAXException JavaDoc;
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 JavaDoc;
31 import javax.xml.parsers.ParserConfigurationException JavaDoc;
32 import javax.xml.transform.Source JavaDoc;
33 import javax.xml.transform.TransformerException JavaDoc;
34
35 import java.io.IOException JavaDoc;
36 import java.util.Date JavaDoc;
37
38 /**
39  * @version $Revision: 426415 $
40  */

41 public abstract class TestSupport extends SpringTestSupport {
42     protected ServiceMixClient client;
43     protected Receiver receiver;
44
45     protected void setUp() throws Exception JavaDoc {
46         super.setUp();
47         client = (ServiceMixClient) getBean("client");
48         SpringJBIContainer jbi = (SpringJBIContainer) getBean("jbi");
49         receiver = (Receiver) jbi.getBean("receiver");
50     }
51
52     /**
53      * Sends messages to the given service and asserts that the receiver gets them all
54      *
55      * @param service
56      * @throws javax.jbi.messaging.MessagingException
57      */

58     protected void assertSendAndReceiveMessages(QName JavaDoc service) throws Exception JavaDoc {
59         sendMessages(service, messageCount);
60         assertMessagesReceived(messageCount);
61     }
62
63     protected void sendMessages(QName JavaDoc service, int messageCount) throws Exception JavaDoc {
64         sendMessages(service, messageCount, true, null);
65     }
66
67     protected void sendMessages(QName JavaDoc service, int messageCount, String JavaDoc message) throws Exception JavaDoc {
68         sendMessages(service, messageCount, true, message);
69     }
70
71     /**
72      * Sends the given number of messages to the given service
73      *
74      * @param service
75      * @throws javax.jbi.messaging.MessagingException
76      */

77     protected void sendMessages(QName JavaDoc service, int messageCount, boolean sync, String JavaDoc msg) throws Exception JavaDoc {
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 JavaDoc(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             // lets assert that we have no failure
99
Exception JavaDoc 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 JavaDoc createMessageXmlText(int index) {
110         return "<sample id='" + index + "' sent='" + new Date JavaDoc() + "'>hello world!</sample>";
111     }
112
113     protected void assertMessagesReceived() throws Exception JavaDoc {
114         assertMessagesReceived(messageCount);
115     }
116
117     protected void assertMessagesReceived(int messageCount) throws Exception JavaDoc {
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 JavaDoc receiverName, int messageCount) throws Exception JavaDoc {
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     /**
134      * Performs a request using the given file from the classpath as the request body and return the answer
135      *
136      * @param serviceName
137      * @param fileOnClassPath
138      * @return
139      * @throws JBIException
140      */

141     protected Object JavaDoc requestServiceWithFileRequest(QName JavaDoc serviceName, String JavaDoc fileOnClassPath) throws Exception JavaDoc {
142         Source content = getSourceFromClassPath(fileOnClassPath);
143         ServiceNameEndpointResolver resolver = new ServiceNameEndpointResolver(serviceName);
144         Object JavaDoc 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     /**
152      * Performs a request using the given file from the classpath as the request body and return the answer
153      *
154      * @param serviceName
155      * @param fileOnClassPath
156      * @return
157      * @throws JBIException
158      */

159     protected void sendServiceWithFileRequest(QName JavaDoc serviceName, String JavaDoc fileOnClassPath) throws Exception JavaDoc {
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 JavaDoc propertyName, Object JavaDoc expectedValue) {
166         NormalizedMessage message = (NormalizedMessage) messageList.getMessages().get(index);
167         assertNotNull("Message: " + index + " is null!", message);
168
169         Object JavaDoc value = message.getProperty(propertyName);
170         assertEquals("property: " + propertyName, expectedValue, value);
171     }
172
173     protected void assertMessageBody(MessageList messageList, int index, String JavaDoc expectedXml) throws TransformerException JavaDoc {
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 JavaDoc value = transformer.toString(content);
180
181         assertEquals("message XML for: " + index, expectedXml, value);
182     }
183
184     protected void assertMessageXPath(MessageList messageList, int index, String JavaDoc xpath, String JavaDoc expectedValue) throws TransformerException JavaDoc, ParserConfigurationException JavaDoc, IOException JavaDoc, SAXException JavaDoc {
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 JavaDoc node = transformer.toDOMNode(content);
191
192         String JavaDoc value = textValueOfXPath(node, xpath);
193         String JavaDoc 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