001: package org.objectweb.celtix.systest.ws.rm;
002:
003: import java.io.ByteArrayOutputStream;
004: import java.util.Iterator;
005: import java.util.List;
006:
007: import javax.xml.namespace.QName;
008: import javax.xml.soap.Name;
009: import javax.xml.soap.SOAPElement;
010: import javax.xml.soap.SOAPEnvelope;
011: import javax.xml.soap.SOAPHeader;
012: import javax.xml.soap.SOAPHeaderElement;
013: import javax.xml.soap.SOAPMessage;
014: import javax.xml.ws.handler.LogicalMessageContext;
015:
016: import junit.framework.Assert;
017:
018: import org.objectweb.celtix.bus.ws.addressing.ContextUtils;
019: import org.objectweb.celtix.bus.ws.rm.Names;
020: import org.objectweb.celtix.bus.ws.rm.RMContextUtils;
021: import org.objectweb.celtix.ws.addressing.AddressingProperties;
022: import org.objectweb.celtix.ws.rm.RMProperties;
023: import org.objectweb.celtix.ws.rm.SequenceAcknowledgement;
024: import org.objectweb.celtix.ws.rm.SequenceType;
025:
026: public class MessageFlow extends Assert {
027: private List<SOAPMessage> outboundMessages;
028: private List<LogicalMessageContext> inboundContexts;
029:
030: public MessageFlow(List<SOAPMessage> o,
031: List<LogicalMessageContext> i) {
032: outboundMessages = o;
033: inboundContexts = i;
034: }
035:
036: public List<SOAPMessage> getOutboundMessages() {
037: return outboundMessages;
038: }
039:
040: public List<LogicalMessageContext> getInboundContexts() {
041: return inboundContexts;
042: }
043:
044: public void clear() {
045: getOutboundMessages().clear();
046: getInboundContexts().clear();
047: }
048:
049: public void verifyActions(String[] expectedActions, boolean outbound)
050: throws Exception {
051:
052: assertEquals(expectedActions.length,
053: outbound ? outboundMessages.size() : inboundContexts
054: .size());
055:
056: for (int i = 0; i < expectedActions.length; i++) {
057: String action = outbound ? getAction(outboundMessages
058: .get(i)) : getAction(inboundContexts.get(i));
059: if (null == expectedActions[i]) {
060: assertNull((outbound ? "Outbound " : "Inbound")
061: + " message " + i + " has unexpected action: "
062: + action, action);
063: } else {
064: assertEquals((outbound ? "Outbound " : "Inbound")
065: + " message " + i
066: + " does not contain expected action header"
067: + System.getProperty("line.separator"),
068: expectedActions[i], action);
069: }
070: }
071: }
072:
073: public boolean checkActions(String[] expectedActions,
074: boolean outbound) throws Exception {
075:
076: if (expectedActions.length != (outbound ? outboundMessages
077: .size() : inboundContexts.size())) {
078: return false;
079: }
080:
081: for (int i = 0; i < expectedActions.length; i++) {
082: String action = outbound ? getAction(outboundMessages
083: .get(i)) : getAction(inboundContexts.get(i));
084: if (null == expectedActions[i]) {
085: if (action != null) {
086: return false;
087: }
088: } else {
089: if (!expectedActions[i].equals(action)) {
090: return false;
091: }
092: }
093: }
094: return true;
095: }
096:
097: public void verifyMessageNumbers(String[] expectedMessageNumbers,
098: boolean outbound) throws Exception {
099:
100: assertEquals(expectedMessageNumbers.length,
101: outbound ? outboundMessages.size() : inboundContexts
102: .size());
103:
104: for (int i = 0; i < expectedMessageNumbers.length; i++) {
105: if (outbound) {
106: SOAPElement se = getSequence(outboundMessages.get(i));
107: if (null == expectedMessageNumbers[i]) {
108: assertNull("Outbound message " + i
109: + " contains unexpected message number ",
110: se);
111: } else {
112: assertEquals(
113: "Outbound message "
114: + i
115: + " does not contain expected message number "
116: + expectedMessageNumbers[i],
117: expectedMessageNumbers[i],
118: getMessageNumber(se));
119: }
120: } else {
121: SequenceType s = getSequence(inboundContexts.get(i));
122: String messageNumber = null == s ? null : s
123: .getMessageNumber().toString();
124: if (null == expectedMessageNumbers[i]) {
125: assertNull("Inbound message " + i
126: + " contains unexpected message number ",
127: messageNumber);
128: } else {
129: assertEquals(
130: "Inbound message "
131: + i
132: + " does not contain expected message number "
133: + expectedMessageNumbers[i],
134: expectedMessageNumbers[i], messageNumber);
135: }
136: }
137: }
138: }
139:
140: public void verifyLastMessage(boolean[] expectedLastMessages,
141: boolean outbound) throws Exception {
142:
143: assertEquals(expectedLastMessages.length,
144: outbound ? outboundMessages.size() : inboundContexts
145: .size());
146:
147: for (int i = 0; i < expectedLastMessages.length; i++) {
148: boolean lastMessage;
149: if (outbound) {
150: SOAPElement se = getSequence(outboundMessages.get(i));
151: lastMessage = null == se ? false : getLastMessage(se);
152: } else {
153: SequenceType s = getSequence(inboundContexts.get(i));
154: lastMessage = null == s ? false : null != s
155: .getLastMessage();
156: }
157: assertEquals(
158: "Outbound message "
159: + i
160: + (expectedLastMessages[i] ? " does not contain expected last message element."
161: : " contains last message element."),
162: expectedLastMessages[i], lastMessage);
163:
164: }
165: }
166:
167: public void verifyAcknowledgements(boolean[] expectedAcks,
168: boolean outbound) throws Exception {
169: assertEquals(expectedAcks.length, outbound ? outboundMessages
170: .size() : inboundContexts.size());
171:
172: for (int i = 0; i < expectedAcks.length; i++) {
173: boolean ack = outbound ? (null != getAcknowledgment(outboundMessages
174: .get(i)))
175: : (null != getAcknowledgment(inboundContexts.get(i)));
176:
177: if (expectedAcks[i]) {
178: assertTrue((outbound ? "Outbound" : "Inbound")
179: + " message " + i
180: + " does not contain expected acknowledgement",
181: ack);
182: } else {
183: assertFalse((outbound ? "Outbound" : "Inbound")
184: + " message " + i
185: + " contains unexpected acknowledgement", ack);
186: }
187: }
188: }
189:
190: public void verifyAckRequestedOutbound() throws Exception {
191: boolean found = false;
192: for (SOAPMessage m : outboundMessages) {
193: SOAPElement se = getAckRequested(m);
194: if (se != null) {
195: found = true;
196: break;
197: }
198: }
199: assertTrue("expected AckRequested", found);
200: }
201:
202: public void verifyAckRequestedInbound(
203: List<LogicalMessageContext> contexts) throws Exception {
204: boolean found = false;
205: for (LogicalMessageContext context : contexts) {
206: RMProperties rmps = RMContextUtils.retrieveRMProperties(
207: context, false);
208: if (null != rmps && rmps.getAcksRequested() != null
209: && rmps.getAcksRequested().size() > 0) {
210: found = true;
211: break;
212: }
213: }
214: assertTrue("expected AckRequested", found);
215: }
216:
217: protected String getAction(SOAPMessage msg) throws Exception {
218: SOAPEnvelope env = msg.getSOAPPart().getEnvelope();
219: SOAPHeader header = env.getHeader();
220: Iterator headerElements = header.examineAllHeaderElements();
221: while (headerElements.hasNext()) {
222: SOAPHeaderElement headerElement = (SOAPHeaderElement) headerElements
223: .next();
224: Name headerName = headerElement.getElementName();
225: String localName = headerName.getLocalName();
226: if ((headerName
227: .getURI()
228: .equals(
229: org.objectweb.celtix.bus.ws.addressing.Names.WSA_NAMESPACE_NAME) || headerName
230: .getURI()
231: .equals(
232: org.objectweb.celtix.bus.ws.addressing.VersionTransformer.Names200408.WSA_NAMESPACE_NAME))
233: && localName
234: .equals(org.objectweb.celtix.bus.ws.addressing.Names.WSA_ACTION_NAME)) {
235: return headerElement.getTextContent();
236: }
237: }
238: return null;
239: }
240:
241: protected String getAction(LogicalMessageContext context) {
242: AddressingProperties maps = ContextUtils.retrieveMAPs(context,
243: false, false);
244: if (null != maps && null != maps.getAction()) {
245: return maps.getAction().getValue();
246: }
247: return null;
248: }
249:
250: protected SOAPElement getSequence(SOAPMessage msg) throws Exception {
251: SOAPEnvelope env = msg.getSOAPPart().getEnvelope();
252: SOAPHeader header = env.getHeader();
253: Iterator headerElements = header.examineAllHeaderElements();
254: while (headerElements.hasNext()) {
255: SOAPHeaderElement headerElement = (SOAPHeaderElement) headerElements
256: .next();
257: Name headerName = headerElement.getElementName();
258: String localName = headerName.getLocalName();
259: if (headerName.getURI().equals(Names.WSRM_NAMESPACE_NAME)
260: && localName.equals(Names.WSRM_SEQUENCE_NAME)) {
261: return (SOAPElement) header.getChildElements().next();
262: }
263: }
264: return null;
265: }
266:
267: public String getMessageNumber(SOAPElement elem) throws Exception {
268: SOAPElement se = (SOAPElement) elem.getChildElements(
269: new QName(Names.WSRM_NAMESPACE_NAME, "MessageNumber"))
270: .next();
271: return se.getTextContent();
272: }
273:
274: protected SequenceType getSequence(LogicalMessageContext context) {
275: RMProperties rmps = RMContextUtils.retrieveRMProperties(
276: context, false);
277: return rmps == null ? null : rmps.getSequence();
278: }
279:
280: private boolean getLastMessage(SOAPElement elem) throws Exception {
281: return elem.getChildElements(
282: new QName(Names.WSRM_NAMESPACE_NAME, "LastMessage"))
283: .hasNext();
284: }
285:
286: protected SOAPElement getAcknowledgment(SOAPMessage msg)
287: throws Exception {
288: SOAPEnvelope env = msg.getSOAPPart().getEnvelope();
289: SOAPHeader header = env.getHeader();
290: Iterator headerElements = header.examineAllHeaderElements();
291: while (headerElements.hasNext()) {
292: SOAPHeaderElement headerElement = (SOAPHeaderElement) headerElements
293: .next();
294: Name headerName = headerElement.getElementName();
295: String localName = headerName.getLocalName();
296: if (headerName.getURI().equals(Names.WSRM_NAMESPACE_NAME)
297: && localName.equals(Names.WSRM_SEQUENCE_ACK_NAME)) {
298: return (SOAPElement) header.getChildElements().next();
299: }
300: }
301: return null;
302: }
303:
304: protected SequenceAcknowledgement getAcknowledgment(
305: LogicalMessageContext context) {
306: RMProperties rmps = RMContextUtils.retrieveRMProperties(
307: context, false);
308: if (null != rmps && null != rmps.getAcks()
309: && rmps.getAcks().size() > 0) {
310: return rmps.getAcks().iterator().next();
311: }
312: return null;
313: }
314:
315: private SOAPElement getAckRequested(SOAPMessage msg)
316: throws Exception {
317: SOAPEnvelope env = msg.getSOAPPart().getEnvelope();
318: SOAPHeader header = env.getHeader();
319: Iterator headerElements = header.examineAllHeaderElements();
320: while (headerElements.hasNext()) {
321: SOAPHeaderElement headerElement = (SOAPHeaderElement) headerElements
322: .next();
323: Name headerName = headerElement.getElementName();
324: String localName = headerName.getLocalName();
325: if (headerName.getURI().equals(Names.WSRM_NAMESPACE_NAME)
326: && localName.equals(Names.WSRM_ACK_REQUESTED_NAME)) {
327: return (SOAPElement) header.getChildElements().next();
328: }
329: }
330: return null;
331: }
332:
333: public void verifyMessages(int nExpected, boolean outbound) {
334: if (outbound) {
335: assertEquals("Unexpected number of outbound messages"
336: + outboundDump(), nExpected, outboundMessages
337: .size());
338: } else {
339: assertEquals("Unexpected number of inbound messages",
340: nExpected, inboundContexts.size());
341: }
342: }
343:
344: public void verifyMessages(int nExpected, boolean outbound,
345: int interval, int attempts) {
346: for (int i = 0; i < attempts; i++) {
347: if ((outbound && outboundMessages.size() < nExpected)
348: || (!outbound && inboundContexts.size() < nExpected)) {
349: try {
350: Thread.sleep(1000);
351: } catch (InterruptedException ex) {
352: // ignore
353: }
354: } else {
355: break;
356: }
357: }
358: if (outbound) {
359: assertEquals(
360: "Did not send the expected number of outbound messages."
361: + outboundDump(), nExpected,
362: outboundMessages.size());
363: } else {
364: assertEquals(
365: "Did not receive the expected number of inbound messages.",
366: nExpected, inboundContexts.size());
367: }
368: }
369:
370: public void purgePartialResponses() {
371: for (int i = inboundContexts.size() - 1; i >= 0; i--) {
372: if (null == getAction(inboundContexts.get(i))) {
373: inboundContexts.remove(i);
374: }
375: }
376: }
377:
378: private String outboundDump() {
379: StringBuffer buf = new StringBuffer();
380: try {
381: buf.append(System.getProperty("line.separator"));
382: for (int i = 0; i < outboundMessages.size(); i++) {
383: SOAPMessage sm = outboundMessages.get(i);
384: buf.append("[");
385: buf.append(i);
386: buf.append("] : ");
387: ByteArrayOutputStream bos = new ByteArrayOutputStream();
388: sm.writeTo(bos);
389: buf.append(bos.toString());
390: buf.append(System.getProperty("line.separator"));
391: }
392: } catch (Exception ex) {
393: return "";
394: }
395:
396: return buf.toString();
397: }
398: }
|