001: package org.mactor.framework;
002:
003: import java.util.Calendar;
004: import java.util.Collections;
005: import java.util.Comparator;
006: import java.util.LinkedList;
007: import java.util.List;
008: import java.util.Map;
009: import org.mactor.brokers.Message;
010: import org.mactor.brokers.MessageContextInfo;
011: import org.mactor.framework.spec.SpecNode;
012:
013: public class TestSummary {
014: private MessageInfo[] messageHistory;
015: private Map<String, String> contextValues;
016: private Calendar testStartTime;
017: private Calendar testCompleteTime;
018: private boolean success;
019: private String failedMessage;
020:
021: public Map<String, String> getContextValues() {
022: return contextValues;
023: }
024:
025: public String getFailedMessage() {
026: return failedMessage;
027: }
028:
029: public MessageInfo[] getMessageHistory() {
030: return messageHistory;
031: }
032:
033: public boolean isSuccess() {
034: return success;
035: }
036:
037: public Calendar getTestCompleteTime() {
038: return testCompleteTime;
039: }
040:
041: public Calendar getTestStartTime() {
042: return testStartTime;
043: }
044:
045: public static TestSummary create(TestContext context,
046: LinkedList<TestEvent> events) {
047: TestSummary ts = new TestSummary();
048: ts.contextValues = context.getValues();
049: ts.messageHistory = getSortedMessageHistory(context);
050: if (events != null && events.size() > 0) {
051: ts.testStartTime = events.getFirst().getTime();
052: TestEvent last = events.getLast();
053: ts.testCompleteTime = last.getTime();
054: ts.success = last.isSuccessfulTestCompleteEvent();
055: if (last.isFaultTestCompleteEvent()) {
056: TestEvent errorNode = events.get(events.size() - 2);
057: ts.failedMessage = errorNode.getNode().getName()
058: + " - failed with: "
059: + (errorNode.getCause() == null ? "unknown reason"
060: : errorNode.getCause().getMessage());
061: }
062: }
063: return ts;
064: }
065:
066: private static MessageInfo[] getSortedMessageHistory(
067: TestContext context) {
068: List<MessageInfo> result = new LinkedList<MessageInfo>();
069: List<Message> all = context.getAllMessages();
070: Collections.sort(all, new Comparator<Message>() {
071: public int compare(Message m0, Message m1) {
072: long n0 = m0.getMessageContextInfo()
073: .getMessageSequenceNumber();
074: long n1 = m1.getMessageContextInfo()
075: .getMessageSequenceNumber();
076: if (n0 == n1)
077: return 0;
078: if (n0 > n1)
079: return 1;
080: return -1;
081: }
082: });
083: for (Message m : all) {
084: if (!m.getMessageContextInfo().isResponseMessage()) {
085: result.add(MessageInfo
086: .create(m.getMessageContextInfo()));
087: if (m.getMessageContextInfo().hasResponseMessage())
088: result.add(MessageInfo.create(m
089: .getMessageContextInfo()
090: .getResponseMessage()
091: .getMessageContextInfo()));
092: }
093: }
094: return result.toArray(new MessageInfo[result.size()]);
095: }
096:
097: public static class MessageInfo {
098: private String archivePath;
099: private SpecNode node;
100: private String channel;
101: private Calendar createdTime;
102: private boolean incoming;
103: private boolean response;
104:
105: private static MessageInfo create(MessageContextInfo mci) {
106: MessageInfo mi = new MessageInfo();
107: mi.archivePath = mci.getArchivePath();
108: mi.channel = mci.getChannel();
109: mi.createdTime = mci.getCreatedTime();
110: mi.node = mci.getNode();
111: mi.incoming = mci.isIncoming();
112: mi.response = mci.isResponseMessage();
113: return mi;
114: }
115:
116: public String getArchivePath() {
117: return archivePath;
118: }
119:
120: public String getChannel() {
121: return channel;
122: }
123:
124: public Calendar getCreatedTime() {
125: return createdTime;
126: }
127:
128: public boolean isIncoming() {
129: return incoming;
130: }
131:
132: public SpecNode getNode() {
133: return node;
134: }
135:
136: public boolean isResponse() {
137: return response;
138: }
139: }
140: }
|